2014-04-27 17:43:15 -07:00
|
|
|
package leap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.google.com/p/go.net/websocket"
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/hybridgroup/gobot"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LeapMotionDriver struct {
|
|
|
|
gobot.Driver
|
|
|
|
}
|
|
|
|
|
2014-05-22 20:35:45 -07:00
|
|
|
func NewLeapMotionDriver(a *LeapMotionAdaptor, name string) *LeapMotionDriver {
|
2014-04-27 17:43:15 -07:00
|
|
|
return &LeapMotionDriver{
|
|
|
|
Driver: gobot.Driver{
|
2014-05-22 20:35:45 -07:00
|
|
|
Name: name,
|
2014-06-11 11:37:20 -07:00
|
|
|
Events: map[string]*gobot.Event{
|
|
|
|
"Message": gobot.NewEvent(),
|
2014-04-27 17:43:15 -07:00
|
|
|
},
|
2014-06-15 17:22:50 -07:00
|
|
|
Adaptor: a,
|
2014-04-27 17:43:15 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-15 17:22:50 -07:00
|
|
|
func (l *LeapMotionDriver) adaptor() *LeapMotionAdaptor {
|
|
|
|
return l.Driver.Adaptor.(*LeapMotionAdaptor)
|
|
|
|
}
|
2014-04-27 17:43:15 -07:00
|
|
|
func (l *LeapMotionDriver) Start() bool {
|
|
|
|
enableGestures := map[string]bool{"enableGestures": true}
|
|
|
|
b, _ := json.Marshal(enableGestures)
|
2014-06-15 17:22:50 -07:00
|
|
|
_, 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
|
2014-06-15 17:22:50 -07:00
|
|
|
websocket.Message.Receive(l.adaptor().ws, &msg)
|
2014-04-27 17:43:15 -07:00
|
|
|
gobot.Publish(l.Events["Message"], l.ParseFrame(msg))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2014-06-10 15:16:11 -07:00
|
|
|
func (l *LeapMotionDriver) Init() bool { return true }
|
|
|
|
func (l *LeapMotionDriver) Halt() bool { return true }
|