core: WIP on all work happening in separate goroutine
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
da3adc0d85
commit
5eac41a6bc
|
@ -0,0 +1,28 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/drivers/gpio"
|
||||
"github.com/hybridgroup/gobot/platforms/firmata"
|
||||
)
|
||||
|
||||
func main() {
|
||||
firmataAdaptor := firmata.NewAdaptor("/dev/ttyACM0")
|
||||
led := gpio.NewLedDriver(firmataAdaptor, "13")
|
||||
|
||||
work := func() {
|
||||
gobot.Every(1*time.Second, func() {
|
||||
led.Toggle()
|
||||
})
|
||||
}
|
||||
|
||||
robot := gobot.NewRobot("bot",
|
||||
[]gobot.Connection{firmataAdaptor},
|
||||
[]gobot.Device{led},
|
||||
work,
|
||||
)
|
||||
|
||||
robot.Start()
|
||||
}
|
6
robot.go
6
robot.go
|
@ -42,6 +42,7 @@ type Robot struct {
|
|||
Work func()
|
||||
connections *Connections
|
||||
devices *Devices
|
||||
done chan bool
|
||||
Commander
|
||||
Eventer
|
||||
}
|
||||
|
@ -98,6 +99,7 @@ func NewRobot(v ...interface{}) *Robot {
|
|||
Name: fmt.Sprintf("%X", Rand(int(^uint(0)>>1))),
|
||||
connections: &Connections{},
|
||||
devices: &Devices{},
|
||||
done: make(chan bool),
|
||||
Work: nil,
|
||||
Eventer: NewEventer(),
|
||||
Commander: NewCommander(),
|
||||
|
@ -142,7 +144,10 @@ func (r *Robot) Start() (errs []error) {
|
|||
}
|
||||
if r.Work != nil {
|
||||
log.Println("Starting work...")
|
||||
go func() {
|
||||
r.Work()
|
||||
<-r.done
|
||||
}()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -152,6 +157,7 @@ func (r *Robot) Stop() (errs []error) {
|
|||
log.Println("Stopping Robot", r.Name, "...")
|
||||
errs = append(errs, r.Devices().Halt()...)
|
||||
errs = append(errs, r.Connections().Finalize()...)
|
||||
r.done <- true
|
||||
return errs
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue