mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-05-11 19:29:20 +08:00
Refactor ardrone to use new driver and adaptor interfaces
This commit is contained in:
parent
88db6be3ac
commit
9a0f3b46f7
@ -5,7 +5,7 @@ import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
var _ gobot.AdaptorInterface = (*ArdroneAdaptor)(nil)
|
||||
var _ gobot.Adaptor = (*ArdroneAdaptor)(nil)
|
||||
|
||||
// drone defines expected drone behaviour
|
||||
type drone interface {
|
||||
@ -23,7 +23,7 @@ type drone interface {
|
||||
}
|
||||
|
||||
type ArdroneAdaptor struct {
|
||||
gobot.Adaptor
|
||||
name string
|
||||
drone drone
|
||||
connect func(*ArdroneAdaptor) (err error)
|
||||
}
|
||||
@ -31,10 +31,7 @@ type ArdroneAdaptor struct {
|
||||
// NewArdroneAdaptor creates a new ardrone and connects with default configuration
|
||||
func NewArdroneAdaptor(name string, v ...string) *ArdroneAdaptor {
|
||||
return &ArdroneAdaptor{
|
||||
Adaptor: *gobot.NewAdaptor(
|
||||
name,
|
||||
"ArdroneAdaptor",
|
||||
),
|
||||
name: name,
|
||||
connect: func(a *ArdroneAdaptor) (err error) {
|
||||
config := client.DefaultConfig()
|
||||
if len(v) > 0 {
|
||||
@ -50,6 +47,8 @@ func NewArdroneAdaptor(name string, v ...string) *ArdroneAdaptor {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *ArdroneAdaptor) Name() string { return a.name }
|
||||
|
||||
// Connect returns true when connection to ardrone is established correclty
|
||||
func (a *ArdroneAdaptor) Connect() (errs []error) {
|
||||
if err := a.connect(a); err != nil {
|
||||
@ -59,6 +58,4 @@ func (a *ArdroneAdaptor) Connect() (errs []error) {
|
||||
}
|
||||
|
||||
// Finalize returns true when connection is finalized correctly
|
||||
func (a *ArdroneAdaptor) Finalize() (errs []error) {
|
||||
return
|
||||
}
|
||||
func (a *ArdroneAdaptor) Finalize() (errs []error) { return }
|
||||
|
@ -1,8 +1,9 @@
|
||||
package ardrone
|
||||
|
||||
import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
"testing"
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
func initTestArdroneAdaptor() *ArdroneAdaptor {
|
||||
|
@ -4,31 +4,34 @@ import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
var _ gobot.DriverInterface = (*ArdroneDriver)(nil)
|
||||
var _ gobot.Driver = (*ArdroneDriver)(nil)
|
||||
|
||||
type ArdroneDriver struct {
|
||||
gobot.Driver
|
||||
name string
|
||||
connection gobot.Connection
|
||||
gobot.Eventer
|
||||
}
|
||||
|
||||
// NewArdroneDriver creates an ArdroneDriver with specified name.
|
||||
//
|
||||
// It add the following events:
|
||||
// 'flying' - Sent when the device has taken off.
|
||||
func NewArdroneDriver(adaptor *ArdroneAdaptor, name string) *ArdroneDriver {
|
||||
func NewArdroneDriver(connection *ArdroneAdaptor, name string) *ArdroneDriver {
|
||||
d := &ArdroneDriver{
|
||||
Driver: *gobot.NewDriver(
|
||||
name,
|
||||
"ArdroneDriver",
|
||||
adaptor,
|
||||
),
|
||||
name: name,
|
||||
connection: connection,
|
||||
Eventer: gobot.NewEventer(),
|
||||
}
|
||||
d.AddEvent("flying")
|
||||
return d
|
||||
}
|
||||
|
||||
func (a *ArdroneDriver) Name() string { return a.name }
|
||||
func (a *ArdroneDriver) Connection() gobot.Connection { return a.connection }
|
||||
|
||||
// adaptor returns ardrone adaptor
|
||||
func (a *ArdroneDriver) adaptor() *ArdroneAdaptor {
|
||||
return a.Adaptor().(*ArdroneAdaptor)
|
||||
return a.Connection().(*ArdroneAdaptor)
|
||||
}
|
||||
|
||||
// Start returns true if driver is started succesfully
|
||||
|
@ -1,8 +1,9 @@
|
||||
package ardrone
|
||||
|
||||
import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
"testing"
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
func initTestArdroneDriver() *ArdroneDriver {
|
||||
|
Loading…
x
Reference in New Issue
Block a user