gobot.io/x/gobot/v2@v2.1.0/examples/raspi_button.go (about)

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