2014-04-27 17:43:15 -07:00
|
|
|
package leap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.google.com/p/go.net/websocket"
|
|
|
|
"fmt"
|
|
|
|
"github.com/hybridgroup/gobot"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LeapMotionAdaptor struct {
|
|
|
|
gobot.Adaptor
|
|
|
|
ws *websocket.Conn
|
|
|
|
connect func(*LeapMotionAdaptor)
|
|
|
|
}
|
|
|
|
|
2014-10-17 10:43:48 -05: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{
|
2014-07-07 17:19:31 -07:00
|
|
|
Adaptor: *gobot.NewAdaptor(
|
|
|
|
name,
|
|
|
|
"LeapMotionAdaptor",
|
|
|
|
port,
|
|
|
|
),
|
2014-04-27 17:43:15 -07:00
|
|
|
connect: func(l *LeapMotionAdaptor) {
|
2014-07-14 13:13:07 -07:00
|
|
|
origin := fmt.Sprintf("http://%v", l.Port())
|
|
|
|
url := fmt.Sprintf("ws://%v/v3.json", l.Port())
|
2014-04-27 17:43:15 -07:00
|
|
|
ws, err := websocket.Dial(url, "", origin)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
l.ws = ws
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-17 10:43:48 -05:00
|
|
|
// Connect returns true if connection to leap motion is established succesfully
|
2014-04-27 17:43:15 -07:00
|
|
|
func (l *LeapMotionAdaptor) Connect() bool {
|
|
|
|
l.connect(l)
|
2014-07-07 17:19:31 -07:00
|
|
|
l.SetConnected(true)
|
2014-04-27 17:43:15 -07:00
|
|
|
return true
|
|
|
|
}
|
2014-10-17 10:43:48 -05:00
|
|
|
|
|
|
|
// Finalize ends connection to leap motion
|
2014-06-10 15:16:11 -07:00
|
|
|
func (l *LeapMotionAdaptor) Finalize() bool { return true }
|