gobot.io/x/gobot@v1.16.0/examples/raspi_grove_pi_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/drivers/i2c"
    13  	"gobot.io/x/gobot/platforms/raspi"
    14  )
    15  
    16  func main() {
    17  	r := raspi.NewAdaptor()
    18  	gp := i2c.NewGrovePiDriver(r)
    19  	button := gpio.NewButtonDriver(gp, "D3")
    20  	led := gpio.NewLedDriver(gp, "D2")
    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{gp, button, led},
    37  		work,
    38  	)
    39  
    40  	robot.Start()
    41  }