hybridgroup.gobot/examples/raspi_grove_pi_button.go

42 lines
717 B
Go

// +build example
//
// Do not build by default.
package main
import (
"fmt"
"gobot.io/x/gobot"
"gobot.io/x/gobot/drivers/gpio"
"gobot.io/x/gobot/drivers/i2c"
"gobot.io/x/gobot/platforms/raspi"
)
func main() {
r := raspi.NewAdaptor()
gp := i2c.NewGrovePiDriver(r)
button := gpio.NewButtonDriver(gp, "D3")
led := gpio.NewLedDriver(gp, "D2")
work := func() {
button.On(gpio.ButtonPush, func(data interface{}) {
fmt.Println("button pressed")
led.On()
})
button.On(gpio.ButtonRelease, func(data interface{}) {
fmt.Println("button released")
led.Off()
})
}
robot := gobot.NewRobot("buttonBot",
[]gobot.Connection{r},
[]gobot.Device{gp, button, led},
work,
)
robot.Start()
}