hybridgroup.gobot/platforms/leap/leap_motion_adaptor.go

51 lines
1.2 KiB
Go
Raw Permalink Normal View History

2014-04-28 08:43:15 +08:00
package leap
import (
2014-11-08 05:15:45 +08:00
"io"
"golang.org/x/net/websocket"
2014-04-28 08:43:15 +08:00
)
// Adaptor is the Gobot Adaptor connection to the Leap Motion
type Adaptor struct {
name string
port string
2014-11-08 05:15:45 +08:00
ws io.ReadWriteCloser
2014-12-19 07:13:53 +08:00
connect func(string) (io.ReadWriteCloser, error)
2014-04-28 08:43:15 +08: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
func NewAdaptor(port string) *Adaptor {
return &Adaptor{
name: "LeapMotion",
port: port,
connect: func(host string) (io.ReadWriteCloser, error) {
return websocket.Dial("ws://"+host+"/v3.json", "", "http://"+host)
2014-04-28 08:43:15 +08:00
},
}
}
// Name returns the Adaptor Name
func (l *Adaptor) Name() string { return l.name }
// SetName sets the Adaptor Name
func (l *Adaptor) SetName(n string) { l.name = n }
// 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-28 08:43:15 +08:00
2016-07-14 00:44:47 +08:00
// Connect returns true if connection to leap motion is established successfully
func (l *Adaptor) Connect() (err error) {
ws, e := l.connect(l.Port())
if e != nil {
return e
}
l.ws = ws
return
2014-04-28 08:43:15 +08:00
}
// Finalize ends connection to leap motion
func (l *Adaptor) Finalize() (err error) { return }