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
|
|
|
|
2015-04-16 11:08:50 +02:00
|
|
|
"golang.org/x/net/websocket"
|
2014-04-27 17:43:15 -07:00
|
|
|
)
|
|
|
|
|
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-09-25 21:36:01 +02:00
|
|
|
// NewAdaptor creates a new leap motion adaptor using specified port
|
|
|
|
func NewAdaptor(port string) *Adaptor {
|
|
|
|
return &Adaptor{
|
2016-10-03 19:06:37 +02:00
|
|
|
name: "Leap Motion",
|
2014-11-28 17:52:01 -08:00
|
|
|
port: port,
|
2014-12-18 15:13:53 -08:00
|
|
|
connect: func(port string) (io.ReadWriteCloser, error) {
|
|
|
|
return websocket.Dial("ws://"+port+"/v3.json", "", "http://"+port)
|
2014-04-27 17:43:15 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2016-09-25 21:36:01 +02:00
|
|
|
func (l *Adaptor) Name() string { return l.name }
|
|
|
|
func (l *Adaptor) SetName(n string) { l.name = n }
|
|
|
|
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-09-25 21:36:01 +02:00
|
|
|
func (l *Adaptor) Connect() (errs []error) {
|
2014-12-18 15:13:53 -08:00
|
|
|
if ws, err := l.connect(l.Port()); err != nil {
|
2014-11-19 23:21:19 -08:00
|
|
|
return []error{err}
|
2014-12-18 15:13:53 -08:00
|
|
|
} else {
|
|
|
|
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-09-25 21:36:01 +02:00
|
|
|
func (l *Adaptor) Finalize() (errs []error) { return }
|