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

     1  //go:build example
     2  // +build example
     3  
     4  //
     5  // Do not build by default.
     6  
     7  /*
     8   To run this example, pass the device ID as first param,
     9   and the access token as the second param:
    10  
    11  	go run examples/particle_button.go mydevice myaccesstoken
    12  */
    13  
    14  package main
    15  
    16  import (
    17  	"os"
    18  
    19  	"gobot.io/x/gobot/v2"
    20  	"gobot.io/x/gobot/v2/drivers/gpio"
    21  	"gobot.io/x/gobot/v2/platforms/particle"
    22  )
    23  
    24  func main() {
    25  	core := particle.NewAdaptor(os.Args[1], os.Args[2])
    26  	led := gpio.NewLedDriver(core, "D7")
    27  	button := gpio.NewButtonDriver(core, "D5")
    28  
    29  	work := func() {
    30  		button.On(button.Event("push"), func(data interface{}) {
    31  			led.On()
    32  		})
    33  
    34  		button.On(button.Event("release"), func(data interface{}) {
    35  			led.Off()
    36  		})
    37  	}
    38  
    39  	robot := gobot.NewRobot("spark",
    40  		[]gobot.Connection{core},
    41  		[]gobot.Device{button, led},
    42  		work,
    43  	)
    44  
    45  	robot.Start()
    46  }