2014-04-27 17:43:15 -07:00
|
|
|
package leap
|
|
|
|
|
|
|
|
import (
|
2014-11-07 13:15:45 -08:00
|
|
|
"io"
|
2014-11-28 17:52:01 -08:00
|
|
|
|
2017-02-02 16:28:12 +01:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
|
2015-04-16 11:08:50 +02:00
|
|
|
"golang.org/x/net/websocket"
|
2014-04-27 17:43:15 -07:00
|
|
|
)
|
|
|
|
|
2016-12-01 11:28:22 +01:00
|
|
|
// Adaptor is the Gobot Adaptor connection to the Leap Motion
|
2016-09-25 21:36:01 +02:00
|
|
|
type Adaptor struct {
|
2014-11-28 17:52:01 -08:00
|
|
|
name string
|
|
|
|
port string
|
2014-11-07 13:15:45 -08:00
|
|
|
ws io.ReadWriteCloser
|
2014-12-18 15:13:53 -08:00
|
|
|
connect func(string) (io.ReadWriteCloser, error)
|
2014-04-27 17:43:15 -07:00
|
|
|
}
|
|
|
|
|
2016-12-01 11:28:22 +01:00
|
|
|
// NewAdaptor creates a new leap motion adaptor using specified port,
|
|
|
|
// which is this case is the host IP or name of the Leap Motion daemon
|
2016-09-25 21:36:01 +02:00
|
|
|
func NewAdaptor(port string) *Adaptor {
|
|
|
|
return &Adaptor{
|
2017-02-02 16:28:12 +01:00
|
|
|
name: gobot.DefaultName("LeapMotion"),
|
2014-11-28 17:52:01 -08:00
|
|
|
port: port,
|
2016-12-01 11:28:22 +01:00
|
|
|
connect: func(host string) (io.ReadWriteCloser, error) {
|
|
|
|
return websocket.Dial("ws://"+host+"/v3.json", "", "http://"+host)
|
2014-04-27 17:43:15 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2016-12-01 11:28:22 +01:00
|
|
|
|
|
|
|
// Name returns the Adaptor Name
|
|
|
|
func (l *Adaptor) Name() string { return l.name }
|
|
|
|
|
|
|
|
// SetName sets the Adaptor Name
|
2016-09-25 21:36:01 +02:00
|
|
|
func (l *Adaptor) SetName(n string) { l.name = n }
|
2016-12-01 11:28:22 +01:00
|
|
|
|
|
|
|
// Port returns the Adaptor Port which is this case is the host IP or name
|
|
|
|
func (l *Adaptor) Port() string { return l.port }
|
2014-04-27 17:43:15 -07:00
|
|
|
|
2016-07-13 10:44:47 -06:00
|
|
|
// Connect returns true if connection to leap motion is established successfully
|
2016-11-07 19:38:18 +01:00
|
|
|
func (l *Adaptor) Connect() (err error) {
|
2016-12-01 11:28:22 +01:00
|
|
|
ws, e := l.connect(l.Port())
|
|
|
|
if e != nil {
|
2016-11-07 19:38:18 +01:00
|
|
|
return e
|
2014-11-19 23:21:19 -08:00
|
|
|
}
|
2016-12-01 11:28:22 +01:00
|
|
|
|
|
|
|
l.ws = ws
|
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
|
|
|
|
|
|
|
// Finalize ends connection to leap motion
|
2016-11-07 19:38:18 +01:00
|
|
|
func (l *Adaptor) Finalize() (err error) { return }
|