gobot.io/x/gobot@v1.16.0/examples/raspi_button.go (about)

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"gobot.io/x/gobot"
    11  	"gobot.io/x/gobot/drivers/gpio"
    12  	"gobot.io/x/gobot/platforms/raspi"
    13  )
    14  
    15  func main() {
    16  	r := raspi.NewAdaptor()
    17  	button := gpio.NewButtonDriver(r, "11")
    18  	led := gpio.NewLedDriver(r, "7")
    19  
    20  	work := func() {
    21  		button.On(gpio.ButtonPush, func(data interface{}) {
    22  			fmt.Println("button pressed")
    23  			led.On()
    24  		})
    25  
    26  		button.On(gpio.ButtonRelease, func(data interface{}) {
    27  			fmt.Println("button released")
    28  			led.Off()
    29  		})
    30  	}
    31  
    32  	robot := gobot.NewRobot("buttonBot",
    33  		[]gobot.Connection{r},
    34  		[]gobot.Device{button, led},
    35  		work,
    36  	)
    37  
    38  	robot.Start()
    39  }