2014-04-28 08:43:15 +08:00
|
|
|
package leap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-11-08 05:15:45 +08:00
|
|
|
"io"
|
2014-07-10 09:32:27 +08:00
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
2015-04-16 17:08:50 +08:00
|
|
|
"golang.org/x/net/websocket"
|
2014-04-28 08:43:15 +08:00
|
|
|
)
|
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
const (
|
2016-11-08 02:38:18 +08:00
|
|
|
// MessageEvent event
|
2016-09-01 18:17:43 +08:00
|
|
|
MessageEvent = "message"
|
2016-11-08 02:38:18 +08:00
|
|
|
// HandEvent event
|
2016-09-01 18:17:43 +08:00
|
|
|
HandEvent = "hand"
|
2016-11-08 02:38:18 +08:00
|
|
|
// GestureEvent event
|
2016-09-01 18:17:43 +08:00
|
|
|
GestureEvent = "gesture"
|
|
|
|
)
|
|
|
|
|
2016-12-01 18:28:22 +08:00
|
|
|
// Driver the Gobot software device to the Leap Motion
|
2016-09-26 03:36:01 +08:00
|
|
|
type Driver struct {
|
2014-11-29 09:52:01 +08:00
|
|
|
name string
|
|
|
|
connection gobot.Connection
|
|
|
|
gobot.Eventer
|
2014-04-28 08:43:15 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 07:13:53 +08:00
|
|
|
var receive = func(ws io.ReadWriteCloser, msg *[]byte) {
|
|
|
|
websocket.Message.Receive(ws.(*websocket.Conn), msg)
|
2014-11-08 05:15:45 +08:00
|
|
|
}
|
|
|
|
|
2016-12-01 18:28:22 +08:00
|
|
|
// NewDriver creates a new leap motion driver
|
2014-10-17 23:43:48 +08:00
|
|
|
//
|
|
|
|
// Adds the following events:
|
|
|
|
// "message" - Gets triggered when receiving a message from leap motion
|
2015-07-03 05:40:13 +08:00
|
|
|
// "hand" - Gets triggered per-message when leap motion detects a hand
|
|
|
|
// "gesture" - Gets triggered per-message when leap motion detects a hand
|
2016-09-26 03:36:01 +08:00
|
|
|
func NewDriver(a *Adaptor) *Driver {
|
|
|
|
l := &Driver{
|
2016-10-04 16:25:14 +08:00
|
|
|
name: "LeapMotion",
|
2014-11-29 09:52:01 +08:00
|
|
|
connection: a,
|
|
|
|
Eventer: gobot.NewEventer(),
|
2014-04-28 08:43:15 +08:00
|
|
|
}
|
2014-07-08 08:19:31 +08:00
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
l.AddEvent(MessageEvent)
|
|
|
|
l.AddEvent(HandEvent)
|
|
|
|
l.AddEvent(GestureEvent)
|
2014-07-08 08:19:31 +08:00
|
|
|
return l
|
2014-04-28 08:43:15 +08:00
|
|
|
}
|
2016-12-01 18:28:22 +08:00
|
|
|
|
|
|
|
// Name returns the Driver Name
|
|
|
|
func (l *Driver) Name() string { return l.name }
|
|
|
|
|
|
|
|
// SetName sets the Driver Name
|
|
|
|
func (l *Driver) SetName(n string) { l.name = n }
|
|
|
|
|
|
|
|
// Connection returns the Driver's Connection
|
2016-09-26 03:36:01 +08:00
|
|
|
func (l *Driver) Connection() gobot.Connection { return l.connection }
|
2014-04-28 08:43:15 +08:00
|
|
|
|
2014-10-17 23:43:48 +08:00
|
|
|
// adaptor returns leap motion adaptor
|
2016-09-26 03:36:01 +08:00
|
|
|
func (l *Driver) adaptor() *Adaptor {
|
|
|
|
return l.Connection().(*Adaptor)
|
2014-06-16 08:22:50 +08:00
|
|
|
}
|
2014-10-17 23:43:48 +08:00
|
|
|
|
|
|
|
// Start inits leap motion driver by enabling gestures
|
|
|
|
// and listening from incoming messages.
|
|
|
|
//
|
|
|
|
// Publishes the following events:
|
2014-11-08 05:15:45 +08:00
|
|
|
// "message" - Emits Frame on new message received from Leap.
|
2015-07-03 05:40:13 +08:00
|
|
|
// "hand" - Emits Hand when detected in message from Leap.
|
|
|
|
// "gesture" - Emits Gesture when detected in message from Leap.
|
2016-11-08 02:38:18 +08:00
|
|
|
func (l *Driver) Start() (err error) {
|
2014-04-28 08:43:15 +08:00
|
|
|
enableGestures := map[string]bool{"enableGestures": true}
|
2016-11-08 02:38:18 +08:00
|
|
|
b, e := json.Marshal(enableGestures)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
2014-11-20 07:11:00 +08:00
|
|
|
}
|
2016-11-08 02:38:18 +08:00
|
|
|
_, e = l.adaptor().ws.Write(b)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
2014-04-28 08:43:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2014-12-19 07:13:53 +08:00
|
|
|
var msg []byte
|
2015-07-03 05:40:13 +08:00
|
|
|
var frame Frame
|
2014-04-28 08:43:15 +08:00
|
|
|
for {
|
2014-12-19 07:13:53 +08:00
|
|
|
receive(l.adaptor().ws, &msg)
|
2015-07-03 05:40:13 +08:00
|
|
|
frame = l.ParseFrame(msg)
|
2016-09-01 18:17:43 +08:00
|
|
|
l.Publish(MessageEvent, frame)
|
2015-07-03 05:40:13 +08:00
|
|
|
|
|
|
|
for _, hand := range frame.Hands {
|
2016-09-01 18:17:43 +08:00
|
|
|
l.Publish(HandEvent, hand)
|
2015-07-03 05:40:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, gesture := range frame.Gestures {
|
2016-09-01 18:17:43 +08:00
|
|
|
l.Publish(GestureEvent, gesture)
|
2015-07-03 05:40:13 +08:00
|
|
|
}
|
2014-04-28 08:43:15 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-11-20 15:21:19 +08:00
|
|
|
return
|
2014-04-28 08:43:15 +08:00
|
|
|
}
|
2014-10-17 23:43:48 +08:00
|
|
|
|
2016-11-08 02:38:18 +08:00
|
|
|
// Halt returns nil if driver is halted successfully
|
|
|
|
func (l *Driver) Halt() (errs error) { return }
|