2014-04-27 17:43:15 -07:00
|
|
|
package leap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-11-07 13:15:45 -08:00
|
|
|
"io"
|
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"
|
|
|
|
)
|
|
|
|
|
2014-11-28 17:52:01 -08:00
|
|
|
var _ gobot.Driver = (*LeapMotionDriver)(nil)
|
2014-11-16 12:25:48 -08:00
|
|
|
|
2014-04-27 17:43:15 -07:00
|
|
|
type LeapMotionDriver struct {
|
2014-11-28 17:52:01 -08:00
|
|
|
name string
|
|
|
|
connection gobot.Connection
|
|
|
|
gobot.Eventer
|
2014-04-27 17:43:15 -07:00
|
|
|
}
|
|
|
|
|
2014-12-18 15:13:53 -08:00
|
|
|
var receive = func(ws io.ReadWriteCloser, msg *[]byte) {
|
|
|
|
websocket.Message.Receive(ws.(*websocket.Conn), msg)
|
2014-11-07 13:15:45 -08:00
|
|
|
}
|
|
|
|
|
2014-10-17 10:43:48 -05:00
|
|
|
// 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{
|
2014-11-28 17:52:01 -08:00
|
|
|
name: name,
|
|
|
|
connection: a,
|
|
|
|
Eventer: gobot.NewEventer(),
|
2014-04-27 17:43:15 -07:00
|
|
|
}
|
2014-07-07 17:19:31 -07:00
|
|
|
|
2014-07-15 11:11:25 -07:00
|
|
|
l.AddEvent("message")
|
2014-07-07 17:19:31 -07:00
|
|
|
return l
|
2014-04-27 17:43:15 -07:00
|
|
|
}
|
2014-11-28 17:52:01 -08:00
|
|
|
func (l *LeapMotionDriver) Name() string { return l.name }
|
|
|
|
func (l *LeapMotionDriver) Connection() gobot.Connection { return l.connection }
|
2014-04-27 17:43:15 -07:00
|
|
|
|
2014-10-17 10:43:48 -05:00
|
|
|
// adaptor returns leap motion adaptor
|
2014-06-15 17:22:50 -07:00
|
|
|
func (l *LeapMotionDriver) adaptor() *LeapMotionAdaptor {
|
2014-11-28 17:52:01 -08:00
|
|
|
return l.Connection().(*LeapMotionAdaptor)
|
2014-06-15 17:22:50 -07:00
|
|
|
}
|
2014-10-17 10:43:48 -05:00
|
|
|
|
|
|
|
// Start inits leap motion driver by enabling gestures
|
|
|
|
// and listening from incoming messages.
|
|
|
|
//
|
|
|
|
// Publishes the following events:
|
2014-11-07 13:15:45 -08:00
|
|
|
// "message" - Emits Frame on new message received from Leap.
|
2014-11-19 23:21:19 -08:00
|
|
|
func (l *LeapMotionDriver) Start() (errs []error) {
|
2014-04-27 17:43:15 -07:00
|
|
|
enableGestures := map[string]bool{"enableGestures": true}
|
2014-11-19 15:11:00 -08:00
|
|
|
b, err := json.Marshal(enableGestures)
|
2014-04-27 17:43:15 -07:00
|
|
|
if err != nil {
|
2014-11-19 23:21:19 -08:00
|
|
|
return []error{err}
|
2014-11-19 15:11:00 -08:00
|
|
|
}
|
|
|
|
_, err = l.adaptor().ws.Write(b)
|
|
|
|
if err != nil {
|
2014-11-19 23:21:19 -08:00
|
|
|
return []error{err}
|
2014-04-27 17:43:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2014-12-18 15:13:53 -08:00
|
|
|
var msg []byte
|
2014-04-27 17:43:15 -07:00
|
|
|
for {
|
2014-12-18 15:13:53 -08:00
|
|
|
receive(l.adaptor().ws, &msg)
|
|
|
|
gobot.Publish(l.Event("message"), l.ParseFrame(msg))
|
2014-04-27 17:43:15 -07:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-11-19 23:21:19 -08:00
|
|
|
return
|
2014-04-27 17:43:15 -07:00
|
|
|
}
|
2014-10-17 10:43:48 -05:00
|
|
|
|
|
|
|
// Halt returns true if driver is halted succesfully
|
2014-11-19 23:21:19 -08:00
|
|
|
func (l *LeapMotionDriver) Halt() (errs []error) { return }
|