From b57a26c5cb8ce283d73eb989393f829e9430c7ba Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Fri, 28 Nov 2014 17:52:01 -0800 Subject: [PATCH] Refactor leap to use new driver and adaptor interfaces --- platforms/leap/leap_motion_adaptor.go | 19 ++++++++++--------- platforms/leap/leap_motion_adaptor_test.go | 3 ++- platforms/leap/leap_motion_driver.go | 18 ++++++++++-------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/platforms/leap/leap_motion_adaptor.go b/platforms/leap/leap_motion_adaptor.go index 1c70fc54..b96fd423 100644 --- a/platforms/leap/leap_motion_adaptor.go +++ b/platforms/leap/leap_motion_adaptor.go @@ -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) { diff --git a/platforms/leap/leap_motion_adaptor_test.go b/platforms/leap/leap_motion_adaptor_test.go index 4c1934b6..8eec3038 100644 --- a/platforms/leap/leap_motion_adaptor_test.go +++ b/platforms/leap/leap_motion_adaptor_test.go @@ -1,8 +1,9 @@ package leap import ( - "github.com/hybridgroup/gobot" "testing" + + "github.com/hybridgroup/gobot" ) func initTestLeapMotionAdaptor() *LeapMotionAdaptor { diff --git a/platforms/leap/leap_motion_driver.go b/platforms/leap/leap_motion_driver.go index 34c99587..28fa5761 100644 --- a/platforms/leap/leap_motion_driver.go +++ b/platforms/leap/leap_motion_driver.go @@ -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