2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2017-03-13 23:01:39 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2017-03-13 23:01:39 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2017-01-30 18:48:15 +08:00
|
|
|
// TO RUN:
|
2023-05-20 20:25:21 +08:00
|
|
|
//
|
2017-01-30 18:48:15 +08:00
|
|
|
// firmata_metal_button <PORT>
|
|
|
|
//
|
|
|
|
// EXAMPLE:
|
|
|
|
//
|
2023-05-20 20:25:21 +08:00
|
|
|
// 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
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2/drivers/gpio"
|
|
|
|
"gobot.io/x/gobot/v2/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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|