2015-10-23 07:56:03 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-09-01 19:32:40 +08:00
|
|
|
"time"
|
2015-10-23 07:56:03 +08:00
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot"
|
2016-10-03 22:58:43 +08:00
|
|
|
"github.com/hybridgroup/gobot/drivers/gpio"
|
2015-10-23 07:56:03 +08:00
|
|
|
"github.com/hybridgroup/gobot/platforms/firmata"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2016-10-16 02:02:54 +08:00
|
|
|
gbot := gobot.NewMaster()
|
2015-10-23 07:56:03 +08:00
|
|
|
|
2016-10-03 22:58:43 +08:00
|
|
|
firmataAdaptor := firmata.NewAdaptor("/dev/ttyACM0")
|
|
|
|
led1 := gpio.NewLedDriver(firmataAdaptor, "3")
|
|
|
|
led2 := gpio.NewLedDriver(firmataAdaptor, "4")
|
|
|
|
button := gpio.NewButtonDriver(firmataAdaptor, "2")
|
|
|
|
sensor := gpio.NewAnalogSensorDriver(firmataAdaptor, "0")
|
2015-10-23 07:56:03 +08:00
|
|
|
|
|
|
|
work := func() {
|
|
|
|
gobot.Every(1*time.Second, func() {
|
|
|
|
led1.Toggle()
|
|
|
|
})
|
|
|
|
gobot.Every(2*time.Second, func() {
|
|
|
|
led2.Toggle()
|
|
|
|
})
|
2016-09-01 18:17:43 +08:00
|
|
|
button.On(gpio.ButtonPush, func(data interface{}) {
|
2015-10-23 07:56:03 +08:00
|
|
|
led2.On()
|
|
|
|
})
|
2016-09-01 18:17:43 +08:00
|
|
|
button.On(gpio.ButtonRelease, func(data interface{}) {
|
2015-10-23 07:56:03 +08:00
|
|
|
led2.Off()
|
|
|
|
})
|
2016-09-01 18:17:43 +08:00
|
|
|
sensor.On(gpio.Data, func(data interface{}) {
|
2015-10-23 07:56:03 +08:00
|
|
|
fmt.Println("sensor", data)
|
2016-08-30 23:53:29 +08:00
|
|
|
})
|
2015-10-23 07:56:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("bot",
|
|
|
|
[]gobot.Connection{firmataAdaptor},
|
|
|
|
[]gobot.Device{led1, led2, button, sensor},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
|
|
|
gbot.AddRobot(robot)
|
|
|
|
|
|
|
|
gbot.Start()
|
|
|
|
}
|