1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-02 22:17:12 +08:00
hybridgroup.gobot/platforms/leap/leap_motion_adaptor.go

52 lines
1.2 KiB
Go
Raw Normal View History

2014-04-27 17:43:15 -07:00
package leap
import (
"fmt"
2014-11-07 13:15:45 -08:00
"io"
"code.google.com/p/go.net/websocket"
"github.com/hybridgroup/gobot"
2014-04-27 17:43:15 -07:00
)
var _ gobot.Adaptor = (*LeapMotionAdaptor)(nil)
2014-04-27 17:43:15 -07:00
type LeapMotionAdaptor struct {
name string
port string
2014-11-07 13:15:45 -08:00
ws io.ReadWriteCloser
2014-11-19 15:11:00 -08:00
connect func(*LeapMotionAdaptor) (err error)
2014-04-27 17:43:15 -07:00
}
// NewLeapMotionAdaptor creates a new leap motion adaptor using specified name and port
2014-05-22 20:35:45 -07:00
func NewLeapMotionAdaptor(name string, port string) *LeapMotionAdaptor {
2014-04-27 17:43:15 -07:00
return &LeapMotionAdaptor{
name: name,
port: port,
2014-11-19 15:11:00 -08:00
connect: func(l *LeapMotionAdaptor) (err error) {
2014-11-07 13:15:45 -08:00
ws, err := websocket.Dial(
fmt.Sprintf("ws://%v/v3.json", l.Port()),
"",
fmt.Sprintf("http://%v", l.Port()),
)
2014-04-27 17:43:15 -07:00
if err != nil {
2014-11-19 15:11:00 -08:00
return err
2014-04-27 17:43:15 -07:00
}
l.ws = ws
2014-11-19 15:11:00 -08:00
return
2014-04-27 17:43:15 -07:00
},
}
}
func (l *LeapMotionAdaptor) Name() string { return l.name }
func (l *LeapMotionAdaptor) Port() string { return l.port }
2014-04-27 17:43:15 -07:00
// Connect returns true if connection to leap motion is established succesfully
func (l *LeapMotionAdaptor) Connect() (errs []error) {
if err := l.connect(l); err != nil {
return []error{err}
}
return
2014-04-27 17:43:15 -07:00
}
// Finalize ends connection to leap motion
func (l *LeapMotionAdaptor) Finalize() (errs []error) { return }