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])
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := f.Connect(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2016-10-19 00:47:26 +08:00
|
|
|
|
2017-01-30 18:48:15 +08:00
|
|
|
led := gpio.NewLedDriver(f, "2")
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := led.Start(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
if err := led.Off(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2016-10-19 00:47:26 +08:00
|
|
|
|
2017-01-30 18:48:15 +08:00
|
|
|
button := gpio.NewButtonDriver(f, "3")
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := button.Start(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2016-10-19 00:47:26 +08:00
|
|
|
|
|
|
|
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 {
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := led.Toggle(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2016-10-19 00:47:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|