Created
July 10, 2015 22:38
-
-
Save gautamrege/be0e007333943f18fd45 to your computer and use it in GitHub Desktop.
Gophercon 2015 #hackday - demo with Sphere - make it move to keyboard arrow keys.
This file contains 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 ( | |
"fmt" | |
"github.com/hybridgroup/gobot" | |
"github.com/hybridgroup/gobot/platforms/keyboard" | |
"github.com/hybridgroup/gobot/platforms/sphero" | |
) | |
func main() { | |
gbot := gobot.NewGobot() | |
adaptor := sphero.NewSpheroAdaptor("sphero", "/dev/tty.Sphero-OOP-AMP-SPP") | |
driver := sphero.NewSpheroDriver(adaptor, "sphero") | |
keys := keyboard.NewKeyboardDriver("keyboard") | |
var oldKey int // old key | |
var speed uint8 = 30 | |
work := func() { | |
gobot.On(keys.Event("key"), func(data interface{}) { | |
key := data.(keyboard.KeyEvent) | |
direction := uint16(0) | |
switch key.Key { | |
case keyboard.ArrowRight: | |
direction = 0 | |
case keyboard.ArrowLeft: | |
direction = 180 | |
case keyboard.ArrowUp: | |
direction = 270 | |
case keyboard.ArrowDown: | |
direction = 90 | |
} | |
if oldKey == key.Key { | |
speed = speed + 20 | |
fmt.Println("speed++", speed) | |
} else { | |
// reset speed | |
speed = 30 | |
} | |
driver.Roll(speed, direction) | |
// update the key | |
oldKey = key.Key | |
}) | |
} | |
robot := gobot.NewRobot("sphero", | |
[]gobot.Connection{adaptor}, | |
[]gobot.Device{keys, driver}, | |
work, | |
) | |
gbot.AddRobot(robot) | |
gbot.Start() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment