2017-01-30 18:48:15 +08:00
|
|
|
// TO RUN:
|
|
|
|
// firmata_metal_button <PORT>
|
|
|
|
//
|
|
|
|
// EXAMPLE:
|
|
|
|
// go run ./examples/firmata_metal_button /dev/ttyACM0
|
|
|
|
//
|
2016-10-19 00:47:26 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-01-30 18:48:15 +08:00
|
|
|
"os"
|
2016-10-19 00:47:26 +08:00
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot/drivers/gpio"
|
|
|
|
"gobot.io/x/gobot/platforms/firmata"
|
2016-10-19 00:47:26 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2017-01-30 18:48:15 +08:00
|
|
|
f := firmata.NewAdaptor(os.Args[1])
|
2016-10-19 00:47:26 +08:00
|
|
|
f.Connect()
|
|
|
|
|
2017-01-30 18:48:15 +08:00
|
|
|
led := gpio.NewLedDriver(f, "2")
|
2016-10-19 00:47:26 +08:00
|
|
|
led.Start()
|
|
|
|
led.Off()
|
|
|
|
|
2017-01-30 18:48:15 +08:00
|
|
|
button := gpio.NewButtonDriver(f, "3")
|
2016-10-19 00:47:26 +08:00
|
|
|
button.Start()
|
|
|
|
|
|
|
|
buttonEvents := button.Subscribe()
|
2017-01-30 18:48:15 +08:00
|
|
|
for event := range buttonEvents {
|
|
|
|
fmt.Println("Event:", event.Name, event.Data)
|
|
|
|
if event.Name == gpio.ButtonPush {
|
|
|
|
led.Toggle()
|
2016-10-19 00:47:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|