2014-04-28 08:43:15 +08:00
|
|
|
package leap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.google.com/p/go.net/websocket"
|
|
|
|
"fmt"
|
|
|
|
"github.com/hybridgroup/gobot"
|
2014-11-08 05:15:45 +08:00
|
|
|
"io"
|
2014-04-28 08:43:15 +08:00
|
|
|
)
|
|
|
|
|
2014-11-17 04:25:48 +08:00
|
|
|
var _ gobot.AdaptorInterface = (*LeapMotionAdaptor)(nil)
|
|
|
|
|
2014-04-28 08:43:15 +08:00
|
|
|
type LeapMotionAdaptor struct {
|
|
|
|
gobot.Adaptor
|
2014-11-08 05:15:45 +08:00
|
|
|
ws io.ReadWriteCloser
|
2014-11-20 07:11:00 +08:00
|
|
|
connect func(*LeapMotionAdaptor) (err error)
|
2014-04-28 08:43:15 +08:00
|
|
|
}
|
|
|
|
|
2014-10-17 23:43:48 +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{
|
2014-07-08 08:19:31 +08:00
|
|
|
Adaptor: *gobot.NewAdaptor(
|
|
|
|
name,
|
|
|
|
"LeapMotionAdaptor",
|
|
|
|
port,
|
|
|
|
),
|
2014-11-20 07:11:00 +08:00
|
|
|
connect: func(l *LeapMotionAdaptor) (err error) {
|
2014-11-08 05:15:45 +08:00
|
|
|
ws, err := websocket.Dial(
|
|
|
|
fmt.Sprintf("ws://%v/v3.json", l.Port()),
|
|
|
|
"",
|
|
|
|
fmt.Sprintf("http://%v", l.Port()),
|
|
|
|
)
|
2014-04-28 08:43:15 +08:00
|
|
|
if err != nil {
|
2014-11-20 07:11:00 +08:00
|
|
|
return err
|
2014-04-28 08:43:15 +08:00
|
|
|
}
|
|
|
|
l.ws = ws
|
2014-11-20 07:11:00 +08:00
|
|
|
return
|
2014-04-28 08:43:15 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-17 23:43:48 +08:00
|
|
|
// Connect returns true if connection to leap motion is established succesfully
|
2014-11-17 04:25:48 +08:00
|
|
|
func (l *LeapMotionAdaptor) Connect() error {
|
2014-11-20 07:11:00 +08:00
|
|
|
return l.connect(l)
|
2014-04-28 08:43:15 +08:00
|
|
|
}
|
2014-10-17 23:43:48 +08:00
|
|
|
|
|
|
|
// Finalize ends connection to leap motion
|
2014-11-17 04:25:48 +08:00
|
|
|
func (l *LeapMotionAdaptor) Finalize() error { return nil }
|