hybridgroup.gobot/examples/raspi_button.go

38 lines
719 B
Go

package main
import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/gpio"
"github.com/hybridgroup/gobot/platforms/raspi"
)
func main() {
gbot := gobot.NewGobot()
r := raspi.NewRaspiAdaptor("raspi")
button := gpio.NewButtonDriver(r, "button", "11")
led := gpio.NewLedDriver(r, "led", "7")
work := func() {
gobot.On(button.Event("push"), func(data interface{}) {
fmt.Println("button pressed")
led.On()
})
gobot.On(button.Event("release"), func(data interface{}) {
fmt.Println("button released")
led.Off()
})
}
robot := gobot.NewRobot("buttonBot",
[]gobot.Connection{r},
[]gobot.Device{button, led},
work,
)
gbot.AddRobot(robot)
gbot.Start()
}