hybridgroup.gobot/platforms/leap/leap_motion_adaptor.go

44 lines
1.1 KiB
Go
Raw Normal View History

2014-04-28 08:43:15 +08:00
package leap
import (
2014-11-08 05:15:45 +08:00
"io"
"github.com/hybridgroup/gobot"
"golang.org/x/net/websocket"
2014-04-28 08:43:15 +08:00
)
var _ gobot.Adaptor = (*LeapMotionAdaptor)(nil)
2014-04-28 08:43:15 +08:00
type LeapMotionAdaptor 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
}
// NewLeapMotionAdaptor creates a new leap motion adaptor using specified name and port
2014-05-23 11:35:45 +08:00
func NewLeapMotionAdaptor(name string, port string) *LeapMotionAdaptor {
2014-04-28 08:43:15 +08:00
return &LeapMotionAdaptor{
name: name,
port: port,
2014-12-19 07:13:53 +08:00
connect: func(port string) (io.ReadWriteCloser, error) {
return websocket.Dial("ws://"+port+"/v3.json", "", "http://"+port)
2014-04-28 08:43:15 +08:00
},
}
}
func (l *LeapMotionAdaptor) Name() string { return l.name }
func (l *LeapMotionAdaptor) Port() string { return l.port }
2014-04-28 08:43:15 +08:00
// Connect returns true if connection to leap motion is established succesfully
func (l *LeapMotionAdaptor) Connect() (errs []error) {
2014-12-19 07:13:53 +08:00
if ws, err := l.connect(l.Port()); err != nil {
return []error{err}
2014-12-19 07:13:53 +08:00
} else {
l.ws = ws
}
return
2014-04-28 08:43:15 +08:00
}
// Finalize ends connection to leap motion
func (l *LeapMotionAdaptor) Finalize() (errs []error) { return }