1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-29 13:49:14 +08:00
hybridgroup.gobot/platforms/leap/leap_motion_driver.go

65 lines
1.5 KiB
Go
Raw Normal View History

2014-04-27 17:43:15 -07:00
package leap
import (
"encoding/json"
2014-07-09 18:32:27 -07:00
"code.google.com/p/go.net/websocket"
2014-04-27 17:43:15 -07:00
"github.com/hybridgroup/gobot"
)
type LeapMotionDriver struct {
gobot.Driver
}
// NewLeapMotionDriver creates a new leap motion driver with specified name
//
// Adds the following events:
// "message" - Gets triggered when receiving a message from leap motion
2014-05-22 20:35:45 -07:00
func NewLeapMotionDriver(a *LeapMotionAdaptor, name string) *LeapMotionDriver {
2014-07-07 17:19:31 -07:00
l := &LeapMotionDriver{
Driver: *gobot.NewDriver(
name,
"LeapMotionDriver",
a,
),
2014-04-27 17:43:15 -07:00
}
2014-07-07 17:19:31 -07:00
l.AddEvent("message")
2014-07-07 17:19:31 -07:00
return l
2014-04-27 17:43:15 -07:00
}
// adaptor returns leap motion adaptor
func (l *LeapMotionDriver) adaptor() *LeapMotionAdaptor {
2014-07-09 18:32:27 -07:00
return l.Adaptor().(*LeapMotionAdaptor)
}
// Start inits leap motion driver by enabling gestures
// and listening from incoming messages.
//
// Publishes the following events:
// "message" - Sends parsed data of received frames.
2014-04-27 17:43:15 -07:00
func (l *LeapMotionDriver) Start() bool {
enableGestures := map[string]bool{"enableGestures": true}
b, _ := json.Marshal(enableGestures)
_, err := l.adaptor().ws.Write(b)
2014-04-27 17:43:15 -07:00
if err != nil {
panic(err)
}
go func() {
for {
var msg []byte
websocket.Message.Receive(l.adaptor().ws, &msg)
gobot.Publish(l.Event("message"), l.ParseFrame(msg))
2014-04-27 17:43:15 -07:00
}
}()
return true
}
// Init returns true if driver is initialized correctly
2014-06-10 15:16:11 -07:00
func (l *LeapMotionDriver) Init() bool { return true }
// Halt returns true if driver is halted succesfully
2014-06-10 15:16:11 -07:00
func (l *LeapMotionDriver) Halt() bool { return true }