forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firmata_integration.go
48 lines (40 loc) · 1.06 KB
/
firmata_integration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"time"
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/firmata"
"github.com/hybridgroup/gobot/platforms/gpio"
)
func main() {
gbot := gobot.NewGobot()
firmataAdaptor := firmata.NewFirmataAdaptor("arduino", "/dev/ttyACM0")
led1 := gpio.NewLedDriver(firmataAdaptor, "led1", "3")
led2 := gpio.NewLedDriver(firmataAdaptor, "led2", "4")
button := gpio.NewButtonDriver(firmataAdaptor, "button", "2")
sensor := gpio.NewAnalogSensorDriver(firmataAdaptor, "sensor", "0")
work := func() {
gobot.Every(1*time.Second, func() {
led1.Toggle()
})
gobot.Every(2*time.Second, func() {
led2.Toggle()
})
gobot.On(button.Event("push"), func(data interface{}) {
led2.On()
})
gobot.On(button.Event("release"), func(data interface{}) {
led2.Off()
})
gobot.On(sensor.Event("data"), func(data interface{}) {
fmt.Println("sensor", data)
})
}
robot := gobot.NewRobot("bot",
[]gobot.Connection{firmataAdaptor},
[]gobot.Device{led1, led2, button, sensor},
work,
)
gbot.AddRobot(robot)
gbot.Start()
}