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

     1  //go:build example
     2  // +build example
     3  
     4  //
     5  // Do not build by default.
     6  
     7  // TO RUN:
     8  //
     9  //	firmata_metal_button <PORT>
    10  //
    11  // EXAMPLE:
    12  //
    13  //	go run ./examples/firmata_metal_button /dev/ttyACM0
    14  package main
    15  
    16  import (
    17  	"fmt"
    18  	"os"
    19  
    20  	"gobot.io/x/gobot/v2/drivers/gpio"
    21  	"gobot.io/x/gobot/v2/platforms/firmata"
    22  )
    23  
    24  func main() {
    25  	f := firmata.NewAdaptor(os.Args[1])
    26  	f.Connect()
    27  
    28  	led := gpio.NewLedDriver(f, "2")
    29  	led.Start()
    30  	led.Off()
    31  
    32  	button := gpio.NewButtonDriver(f, "3")
    33  	button.Start()
    34  
    35  	buttonEvents := button.Subscribe()
    36  	for event := range buttonEvents {
    37  		fmt.Println("Event:", event.Name, event.Data)
    38  		if event.Name == gpio.ButtonPush {
    39  			led.Toggle()
    40  		}
    41  	}
    42  }