2017-03-13 23:01:39 +08:00
|
|
|
// +build example
|
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2017-07-13 08:08:46 +08:00
|
|
|
/*
|
|
|
|
How to run
|
|
|
|
Pass serial port to use as the first param:
|
|
|
|
|
|
|
|
go run examples/firmata_pir_motion.go /dev/ttyACM0
|
|
|
|
*/
|
|
|
|
|
2016-10-22 21:17:49 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-10-31 05:07:01 +08:00
|
|
|
"fmt"
|
2017-07-13 08:08:46 +08:00
|
|
|
"os"
|
2016-10-31 05:07:01 +08:00
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/drivers/gpio"
|
|
|
|
"gobot.io/x/gobot/platforms/firmata"
|
2016-10-22 21:17:49 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2017-07-13 08:08:46 +08:00
|
|
|
firmataAdaptor := firmata.NewAdaptor(os.Args[1])
|
2016-10-22 21:17:49 +08:00
|
|
|
|
|
|
|
sensor := gpio.NewPIRMotionDriver(firmataAdaptor, "5")
|
|
|
|
led := gpio.NewLedDriver(firmataAdaptor, "13")
|
|
|
|
|
|
|
|
work := func() {
|
|
|
|
sensor.On(gpio.MotionDetected, func(data interface{}) {
|
2016-10-31 05:07:01 +08:00
|
|
|
fmt.Println(gpio.MotionDetected)
|
2016-10-22 21:17:49 +08:00
|
|
|
led.On()
|
|
|
|
})
|
|
|
|
sensor.On(gpio.MotionStopped, func(data interface{}) {
|
2016-10-31 05:07:01 +08:00
|
|
|
fmt.Println(gpio.MotionStopped)
|
2016-10-22 21:17:49 +08:00
|
|
|
led.Off()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("motionBot",
|
|
|
|
[]gobot.Connection{firmataAdaptor},
|
|
|
|
[]gobot.Device{sensor, led},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
|
|
|
robot.Start()
|
|
|
|
}
|