From 91aa54d37b879a5a47c2415276d4c1a5d124a8e5 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Tue, 18 Oct 2016 18:47:26 +0200 Subject: [PATCH] examples: add Firmata button metal example Signed-off-by: deadprogram --- examples/firmata_button.go | 2 +- examples/firmata_metal_button.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 examples/firmata_metal_button.go diff --git a/examples/firmata_button.go b/examples/firmata_button.go index dce50e9b..88cd2fb1 100644 --- a/examples/firmata_button.go +++ b/examples/firmata_button.go @@ -11,7 +11,7 @@ func main() { firmataAdaptor := firmata.NewAdaptor("/dev/ttyACM0") - button := gpio.NewButtonDriver(firmataAdaptor, "2") + button := gpio.NewButtonDriver(firmataAdaptor, "5") led := gpio.NewLedDriver(firmataAdaptor, "13") work := func() { diff --git a/examples/firmata_metal_button.go b/examples/firmata_metal_button.go new file mode 100644 index 00000000..c011628e --- /dev/null +++ b/examples/firmata_metal_button.go @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + + "github.com/hybridgroup/gobot/drivers/gpio" + "github.com/hybridgroup/gobot/platforms/firmata" +) + +func main() { + f := firmata.NewAdaptor("/dev/ttyACM0") + f.Connect() + + led := gpio.NewLedDriver(f, "13") + led.Start() + led.Off() + + button := gpio.NewButtonDriver(f, "5") + button.Start() + + buttonEvents := button.Subscribe() + for { + select { + case event := <-buttonEvents: + fmt.Println("Event:", event.Name, event.Data) + if event.Name == gpio.ButtonPush { + led.Toggle() + } + } + } +}