Add support for 'hand' and 'gesture' Leap Motion events

This commit is contained in:
Andrew Stewart 2015-07-02 14:40:13 -07:00 committed by deadprogram
parent f8f6dfeb0b
commit 111a6df996
3 changed files with 56 additions and 7 deletions

View File

@ -0,0 +1,35 @@
package main
import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/leap"
)
func main() {
gbot := gobot.NewGobot()
leapMotionAdaptor := leap.NewLeapMotionAdaptor("leap", "127.0.0.1:6437")
l := leap.NewLeapMotionDriver(leapMotionAdaptor, "leap")
work := func() {
gobot.On(l.Event("gesture"), func(data interface{}) {
printGesture(data.(leap.Gesture))
})
}
robot := gobot.NewRobot("leapBot",
[]gobot.Connection{leapMotionAdaptor},
[]gobot.Device{l},
work,
)
gbot.AddRobot(robot)
gbot.Start()
}
func printGesture(gesture leap.Gesture) {
fmt.Println("Gesture", gesture)
}

View File

@ -14,8 +14,8 @@ func main() {
l := leap.NewLeapMotionDriver(leapMotionAdaptor, "leap")
work := func() {
gobot.On(l.Event("message"), func(data interface{}) {
printHands(data.(leap.Frame))
gobot.On(l.Event("hand"), func(data interface{}) {
printHand(data.(leap.Hand))
})
}
@ -30,8 +30,6 @@ func main() {
gbot.Start()
}
func printHands(frame leap.Frame) {
for key, hand := range frame.Hands {
fmt.Println("Hand", key, hand)
}
func printHand(hand leap.Hand) {
fmt.Println("Hand", hand)
}

View File

@ -24,6 +24,8 @@ var receive = func(ws io.ReadWriteCloser, msg *[]byte) {
//
// Adds the following events:
// "message" - Gets triggered when receiving a message from leap motion
// "hand" - Gets triggered per-message when leap motion detects a hand
// "gesture" - Gets triggered per-message when leap motion detects a hand
func NewLeapMotionDriver(a *LeapMotionAdaptor, name string) *LeapMotionDriver {
l := &LeapMotionDriver{
name: name,
@ -32,6 +34,8 @@ func NewLeapMotionDriver(a *LeapMotionAdaptor, name string) *LeapMotionDriver {
}
l.AddEvent("message")
l.AddEvent("hand")
l.AddEvent("gesture")
return l
}
func (l *LeapMotionDriver) Name() string { return l.name }
@ -47,6 +51,8 @@ func (l *LeapMotionDriver) adaptor() *LeapMotionAdaptor {
//
// Publishes the following events:
// "message" - Emits Frame on new message received from Leap.
// "hand" - Emits Hand when detected in message from Leap.
// "gesture" - Emits Gesture when detected in message from Leap.
func (l *LeapMotionDriver) Start() (errs []error) {
enableGestures := map[string]bool{"enableGestures": true}
b, err := json.Marshal(enableGestures)
@ -60,9 +66,19 @@ func (l *LeapMotionDriver) Start() (errs []error) {
go func() {
var msg []byte
var frame Frame
for {
receive(l.adaptor().ws, &msg)
gobot.Publish(l.Event("message"), l.ParseFrame(msg))
frame = l.ParseFrame(msg)
gobot.Publish(l.Event("message"), frame)
for _, hand := range frame.Hands {
gobot.Publish(l.Event("hand"), hand)
}
for _, gesture := range frame.Gestures {
gobot.Publish(l.Event("gesture"), gesture)
}
}
}()