Created
April 13, 2015 21:28
-
-
Save agocs/5291b95913241b914e8b to your computer and use it in GitHub Desktop.
Web to Firmata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "net/http" | |
| "time" | |
| "github.com/hybridgroup/gobot" | |
| "github.com/hybridgroup/gobot/platforms/firmata" | |
| "github.com/hybridgroup/gobot/platforms/gpio" | |
| ) | |
| func main() { | |
| //initialize command channels | |
| colors := make(chan int, 500) | |
| go blink(colors) | |
| //initialize web server | |
| http.HandleFunc("/green/", func(w http.ResponseWriter, req *http.Request) { | |
| colors <- 0 | |
| http.Redirect(w, req, "/", 302) | |
| return | |
| }) | |
| http.HandleFunc("/blue/", func(w http.ResponseWriter, req *http.Request) { | |
| colors <- 1 | |
| http.Redirect(w, req, "/", 302) | |
| return | |
| }) | |
| http.HandleFunc("/red/", func(w http.ResponseWriter, req *http.Request) { | |
| colors <- 2 | |
| http.Redirect(w, req, "/", 302) | |
| return | |
| }) | |
| http.HandleFunc("/rainbow/", func(w http.ResponseWriter, req *http.Request) { | |
| colors <- 3 | |
| http.Redirect(w, req, "/", 302) | |
| return | |
| }) | |
| clientfs := http.FileServer(http.Dir("client")) | |
| http.Handle("/", clientfs) | |
| err := http.ListenAndServe(":9000", nil) | |
| if err != nil { | |
| close(colors) | |
| panic(err.Error()) | |
| } | |
| } | |
| func blink(colors chan int) { | |
| //initialize robot friend | |
| gbot := gobot.NewGobot() | |
| firmataAdaptor := firmata.NewFirmataAdaptor("arduino", "/dev/cu.usbmodem1421") | |
| led_g := gpio.NewLedDriver(firmataAdaptor, "led", "13") | |
| led_r := gpio.NewLedDriver(firmataAdaptor, "led", "12") | |
| led_b := gpio.NewLedDriver(firmataAdaptor, "led", "11") | |
| led_rainbow := gpio.NewLedDriver(firmataAdaptor, "led", "10") | |
| work := func() { | |
| gobot.Every(1*time.Millisecond, func() { | |
| for { | |
| color, alive := <-colors | |
| if !alive { | |
| break | |
| } | |
| switch color { | |
| case 0: | |
| led_g.On() | |
| case 1: | |
| led_b.On() | |
| case 2: | |
| led_r.On() | |
| case 3: | |
| led_g.Off() | |
| led_b.Off() | |
| led_r.Off() | |
| led_rainbow.On() | |
| } | |
| } | |
| }) | |
| } | |
| robot := gobot.NewRobot("bot", | |
| []gobot.Connection{firmataAdaptor}, | |
| []gobot.Device{led_g}, | |
| []gobot.Device{led_r}, | |
| []gobot.Device{led_b}, | |
| []gobot.Device{led_rainbow}, | |
| work, | |
| ) | |
| gbot.AddRobot(robot) | |
| gbot.Start() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment