gobot.io/x/gobot@v1.16.0/examples/firmata_button.go (about) 1 // +build example 2 // 3 // Do not build by default. 4 5 /* 6 How to run 7 Pass serial port to use as the first param: 8 9 go run examples/firmata_button.go /dev/ttyACM0 10 */ 11 12 package main 13 14 import ( 15 "os" 16 17 "gobot.io/x/gobot" 18 "gobot.io/x/gobot/drivers/gpio" 19 "gobot.io/x/gobot/platforms/firmata" 20 ) 21 22 func main() { 23 firmataAdaptor := firmata.NewAdaptor(os.Args[1]) 24 25 button := gpio.NewButtonDriver(firmataAdaptor, "2") 26 led := gpio.NewLedDriver(firmataAdaptor, "3") 27 28 work := func() { 29 button.On(gpio.ButtonPush, func(data interface{}) { 30 led.On() 31 }) 32 button.On(gpio.ButtonRelease, func(data interface{}) { 33 led.Off() 34 }) 35 } 36 37 robot := gobot.NewRobot("buttonBot", 38 []gobot.Connection{firmataAdaptor}, 39 []gobot.Device{button, led}, 40 work, 41 ) 42 43 robot.Start() 44 }