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
|
|
|
|
2014-04-28 08:43:15 +08:00
|
|
|
"github.com/hybridgroup/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 (
|
|
|
|
// Message event
|
|
|
|
MessageEvent = "message"
|
|
|
|
// Hand event
|
|
|
|
HandEvent = "hand"
|
|
|
|
// Gesture event
|
|
|
|
GestureEvent = "gesture"
|
|
|
|
)
|
|
|
|
|
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-09-26 03:36:01 +08:00
|
|
|
// NewLDriver 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-09-26 03:36:01 +08:00
|
|
|
func (l *Driver) Name() string { return l.name }
|
|
|
|
func (l *Driver) SetName(n string) { l.name = n }
|
|
|
|
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-09-26 03:36:01 +08:00
|
|
|
func (l *Driver) Start() (errs []error) {
|
2014-04-28 08:43:15 +08:00
|
|
|
enableGestures := map[string]bool{"enableGestures": true}
|
2014-11-20 07:11:00 +08:00
|
|
|
b, err := json.Marshal(enableGestures)
|
2014-04-28 08:43:15 +08:00
|
|
|
if err != nil {
|
2014-11-20 15:21:19 +08:00
|
|
|
return []error{err}
|
2014-11-20 07:11:00 +08:00
|
|
|
}
|
|
|
|
_, err = l.adaptor().ws.Write(b)
|
|
|
|
if err != nil {
|
2014-11-20 15:21:19 +08:00
|
|
|
return []error{err}
|
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-07-14 00:44:47 +08:00
|
|
|
// Halt returns true if driver is halted successfully
|
2016-09-26 03:36:01 +08:00
|
|
|
func (l *Driver) Halt() (errs []error) { return }
|