gobot.io/x/gobot/v2@v2.1.0/examples/beaglebone_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/beaglebone"
    15  )
    16  
    17  func main() {
    18  	beagleboneAdaptor := beaglebone.NewAdaptor()
    19  	button := gpio.NewButtonDriver(beagleboneAdaptor, "P8_09")
    20  
    21  	work := func() {
    22  		button.On(gpio.ButtonPush, func(data interface{}) {
    23  			fmt.Println("button pressed")
    24  		})
    25  
    26  		button.On(gpio.ButtonRelease, func(data interface{}) {
    27  			fmt.Println("button released")
    28  		})
    29  	}
    30  
    31  	robot := gobot.NewRobot("buttonBot",
    32  		[]gobot.Connection{beagleboneAdaptor},
    33  		[]gobot.Device{button},
    34  		work,
    35  	)
    36  
    37  	robot.Start()
    38  }