Refactor leap to use new driver and adaptor interfaces

This commit is contained in:
Adrian Zankich 2014-11-28 17:52:01 -08:00
parent c1ce92f582
commit b57a26c5cb
3 changed files with 22 additions and 18 deletions

View File

@ -1,16 +1,18 @@
package leap
import (
"code.google.com/p/go.net/websocket"
"fmt"
"github.com/hybridgroup/gobot"
"io"
"code.google.com/p/go.net/websocket"
"github.com/hybridgroup/gobot"
)
var _ gobot.AdaptorInterface = (*LeapMotionAdaptor)(nil)
var _ gobot.Adaptor = (*LeapMotionAdaptor)(nil)
type LeapMotionAdaptor struct {
gobot.Adaptor
name string
port string
ws io.ReadWriteCloser
connect func(*LeapMotionAdaptor) (err error)
}
@ -18,11 +20,8 @@ type LeapMotionAdaptor struct {
// 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,
),
name: name,
port: port,
connect: func(l *LeapMotionAdaptor) (err error) {
ws, err := websocket.Dial(
fmt.Sprintf("ws://%v/v3.json", l.Port()),
@ -37,6 +36,8 @@ func NewLeapMotionAdaptor(name string, port string) *LeapMotionAdaptor {
},
}
}
func (l *LeapMotionAdaptor) Name() string { return l.name }
func (l *LeapMotionAdaptor) Port() string { return l.port }
// Connect returns true if connection to leap motion is established succesfully
func (l *LeapMotionAdaptor) Connect() (errs []error) {

View File

@ -1,8 +1,9 @@
package leap
import (
"github.com/hybridgroup/gobot"
"testing"
"github.com/hybridgroup/gobot"
)
func initTestLeapMotionAdaptor() *LeapMotionAdaptor {

View File

@ -8,10 +8,12 @@ import (
"github.com/hybridgroup/gobot"
)
var _ gobot.DriverInterface = (*LeapMotionDriver)(nil)
var _ gobot.Driver = (*LeapMotionDriver)(nil)
type LeapMotionDriver struct {
gobot.Driver
name string
connection gobot.Connection
gobot.Eventer
}
var receive = func(ws io.ReadWriteCloser) []byte {
@ -26,20 +28,20 @@ var receive = func(ws io.ReadWriteCloser) []byte {
// "message" - Gets triggered when receiving a message from leap motion
func NewLeapMotionDriver(a *LeapMotionAdaptor, name string) *LeapMotionDriver {
l := &LeapMotionDriver{
Driver: *gobot.NewDriver(
name,
"LeapMotionDriver",
a,
),
name: name,
connection: a,
Eventer: gobot.NewEventer(),
}
l.AddEvent("message")
return l
}
func (l *LeapMotionDriver) Name() string { return l.name }
func (l *LeapMotionDriver) Connection() gobot.Connection { return l.connection }
// adaptor returns leap motion adaptor
func (l *LeapMotionDriver) adaptor() *LeapMotionAdaptor {
return l.Adaptor().(*LeapMotionAdaptor)
return l.Connection().(*LeapMotionAdaptor)
}
// Start inits leap motion driver by enabling gestures