gobot.io/x/gobot@v1.16.0/examples/particle_button.go (about)

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