1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-08 19:29:16 +08:00
hybridgroup.gobot/platforms/leap/leap_motion_adaptor.go

49 lines
1.0 KiB
Go

package leap
import (
"code.google.com/p/go.net/websocket"
"fmt"
"github.com/hybridgroup/gobot"
"io"
)
var _ gobot.AdaptorInterface = (*LeapMotionAdaptor)(nil)
type LeapMotionAdaptor struct {
gobot.Adaptor
ws io.ReadWriteCloser
connect func(*LeapMotionAdaptor)
}
// NewLeapMotionAdaptor creates a new leap motion adaptor using specified name and port
func NewLeapMotionAdaptor(name string, port string) *LeapMotionAdaptor {
return &LeapMotionAdaptor{
Adaptor: *gobot.NewAdaptor(
name,
"LeapMotionAdaptor",
port,
),
connect: func(l *LeapMotionAdaptor) {
ws, err := websocket.Dial(
fmt.Sprintf("ws://%v/v3.json", l.Port()),
"",
fmt.Sprintf("http://%v", l.Port()),
)
if err != nil {
panic(err)
}
l.ws = ws
},
}
}
// Connect returns true if connection to leap motion is established succesfully
func (l *LeapMotionAdaptor) Connect() error {
l.connect(l)
l.SetConnected(true)
return nil
}
// Finalize ends connection to leap motion
func (l *LeapMotionAdaptor) Finalize() error { return nil }