gobot.io/x/gobot/v2@v2.1.0/examples/metal_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/drivers/gpio"
    13  	"gobot.io/x/gobot/v2/platforms/intel-iot/edison"
    14  )
    15  
    16  func main() {
    17  	e := edison.NewAdaptor()
    18  	e.Connect()
    19  
    20  	led := gpio.NewLedDriver(e, "13")
    21  	led.Start()
    22  	led.Off()
    23  
    24  	button := gpio.NewButtonDriver(e, "5")
    25  	button.Start()
    26  
    27  	buttonEvents := button.Subscribe()
    28  	for {
    29  		select {
    30  		case event := <-buttonEvents:
    31  			fmt.Println("Event:", event.Name, event.Data)
    32  			if event.Name == gpio.ButtonPush {
    33  				led.Toggle()
    34  			}
    35  		}
    36  	}
    37  }