Skip to content

Commit

Permalink
The Driver struct now has an Adaptor field
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jun 16, 2014
1 parent 2785875 commit f77689a
Show file tree
Hide file tree
Showing 32 changed files with 223 additions and 158 deletions.
18 changes: 7 additions & 11 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ type JSONConnection struct {
}

type connection struct {
Name string `json:"-"`
Type string `json:"-"`
Adaptor AdaptorInterface `json:"-"`
Port string `json:"-"`
Robot *Robot `json:"-"`
Params map[string]interface{} `json:"-"`
Name string `json:"-"`
Type string `json:"-"`
Adaptor AdaptorInterface `json:"-"`
Robot *Robot `json:"-"`
}

type connections []*connection
Expand Down Expand Up @@ -62,15 +60,13 @@ func NewConnection(adaptor AdaptorInterface, r *Robot) *connection {
return &connection{
Type: t[1:len(t)],
Name: adaptor.name(),
Port: adaptor.port(),
Params: adaptor.params(),
Robot: r,
Adaptor: adaptor,
}
}

func (c *connection) Connect() bool {
log.Println("Connecting to " + c.Name + " on port " + c.Port + "...")
log.Println("Connecting to " + c.Name + " on port " + c.port() + "...")
return c.Adaptor.Connect()
}

Expand All @@ -82,13 +78,13 @@ func (c *connection) Finalize() bool {
func (c *connection) ToJSON() *JSONConnection {
return &JSONConnection{
Name: c.Name,
Port: c.Port,
Port: c.port(),
Adaptor: c.Type,
}
}

func (c *connection) port() string {
return c.Port
return c.Adaptor.port()
}

func (c *connection) name() string {
Expand Down
27 changes: 17 additions & 10 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Device interface {
interval() time.Duration
setName(string)
name() string
adaptor() AdaptorInterface
commands() map[string]func(map[string]interface{}) interface{}
}

Expand All @@ -26,10 +27,10 @@ type JSONDevice struct {
}

type device struct {
Name string `json:"-"`
Type string `json:"-"`
Robot *Robot `json:"-"`
Driver DriverInterface `json:"-"`
Name string
Type string
Robot *Robot
Driver DriverInterface
}

type devices []*device
Expand Down Expand Up @@ -71,6 +72,10 @@ func NewDevice(driver DriverInterface, r *Robot) *device {
}
}

func (d *device) adaptor() AdaptorInterface {
return d.Driver.adaptor()
}

func (d *device) setInterval(t time.Duration) {
d.Driver.setInterval(t)
}
Expand Down Expand Up @@ -107,12 +112,14 @@ func (d *device) Halt() bool {

func (d *device) ToJSON() *JSONDevice {
jsonDevice := &JSONDevice{
Name: d.Name,
Driver: d.Type,
Connection: d.Robot.Connection(FieldByNamePtr(FieldByNamePtr(d.Driver, "Adaptor").
Interface().(AdaptorInterface), "Name").
Interface().(string)).ToJSON(),
Commands: []string{},
Name: d.Name,
Driver: d.Type,
Commands: []string{},
Connection: nil,
}

if d.adaptor() != nil {
jsonDevice.Connection = d.Robot.Connection(d.adaptor().name()).ToJSON()
}

commands := d.commands()
Expand Down
16 changes: 11 additions & 5 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,29 @@ package gobot
import "time"

type Driver struct {
Interval time.Duration `json:"interval"`
Pin string `json:"pin"`
Name string `json:"name"`
Commands map[string]func(map[string]interface{}) interface{} `json:"commands"`
Events map[string]*Event `json:"-"`
Adaptor AdaptorInterface
Interval time.Duration
Pin string
Name string
Commands map[string]func(map[string]interface{}) interface{}
Events map[string]*Event
}

type DriverInterface interface {
Start() bool
Halt() bool
adaptor() AdaptorInterface
setInterval(time.Duration)
interval() time.Duration
setName(string)
name() string
commands() map[string]func(map[string]interface{}) interface{}
}

func (d *Driver) adaptor() AdaptorInterface {
return d.Adaptor
}

func (d *Driver) setInterval(t time.Duration) {
d.Interval = t
}
Expand Down
28 changes: 15 additions & 13 deletions platforms/ardrone/ardrone_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

type ArdroneDriver struct {
gobot.Driver
Adaptor *ArdroneAdaptor
}

func NewArdroneDriver(adaptor *ArdroneAdaptor, name string) *ArdroneDriver {
Expand All @@ -16,10 +15,13 @@ func NewArdroneDriver(adaptor *ArdroneAdaptor, name string) *ArdroneDriver {
Events: map[string]*gobot.Event{
"Flying": gobot.NewEvent(),
},
Adaptor: adaptor,
},
Adaptor: adaptor,
}
}
func (a *ArdroneDriver) adaptor() *ArdroneAdaptor {
return a.Driver.Adaptor.(*ArdroneAdaptor)
}

func (a *ArdroneDriver) Start() bool {
return true
Expand All @@ -30,45 +32,45 @@ func (a *ArdroneDriver) Halt() bool {
}

func (a *ArdroneDriver) TakeOff() {
gobot.Publish(a.Events["Flying"], a.Adaptor.drone.Takeoff())
gobot.Publish(a.Events["Flying"], a.adaptor().drone.Takeoff())
}

func (a *ArdroneDriver) Land() {
a.Adaptor.drone.Land()
a.adaptor().drone.Land()
}

func (a *ArdroneDriver) Up(n float64) {
a.Adaptor.drone.Up(n)
a.adaptor().drone.Up(n)
}

func (a *ArdroneDriver) Down(n float64) {
a.Adaptor.drone.Down(n)
a.adaptor().drone.Down(n)
}

func (a *ArdroneDriver) Left(n float64) {
a.Adaptor.drone.Left(n)
a.adaptor().drone.Left(n)
}

func (a *ArdroneDriver) Right(n float64) {
a.Adaptor.drone.Right(n)
a.adaptor().drone.Right(n)
}

func (a *ArdroneDriver) Forward(n float64) {
a.Adaptor.drone.Forward(n)
a.adaptor().drone.Forward(n)
}

func (a *ArdroneDriver) Backward(n float64) {
a.Adaptor.drone.Backward(n)
a.adaptor().drone.Backward(n)
}

func (a *ArdroneDriver) Clockwise(n float64) {
a.Adaptor.drone.Clockwise(n)
a.adaptor().drone.Clockwise(n)
}

func (a *ArdroneDriver) CounterClockwise(n float64) {
a.Adaptor.drone.Counterclockwise(n)
a.adaptor().drone.Counterclockwise(n)
}

func (a *ArdroneDriver) Hover() {
a.Adaptor.drone.Hover()
a.adaptor().drone.Hover()
}
9 changes: 6 additions & 3 deletions platforms/gpio/analog_sensor_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

type AnalogSensorDriver struct {
gobot.Driver
Adaptor AnalogReader
}

func NewAnalogSensorDriver(a AnalogReader, name string, pin string) *AnalogSensorDriver {
Expand All @@ -15,19 +14,23 @@ func NewAnalogSensorDriver(a AnalogReader, name string, pin string) *AnalogSenso
Name: name,
Pin: pin,
Commands: make(map[string]func(map[string]interface{}) interface{}),
Adaptor: a.(gobot.AdaptorInterface),
},
Adaptor: a,
}
d.Driver.AddCommand("Read", func(params map[string]interface{}) interface{} {
return d.Read()
})
return d
}

func (a *AnalogSensorDriver) adaptor() AnalogReader {
return a.Driver.Adaptor.(AnalogReader)
}

func (a *AnalogSensorDriver) Start() bool { return true }
func (a *AnalogSensorDriver) Init() bool { return true }
func (a *AnalogSensorDriver) Halt() bool { return true }

func (a *AnalogSensorDriver) Read() int {
return a.Adaptor.AnalogRead(a.Pin)
return a.adaptor().AnalogRead(a.Pin)
}
2 changes: 1 addition & 1 deletion platforms/gpio/analog_sensor_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func initTestAnalogSensorDriver() *AnalogSensorDriver {
return NewAnalogSensorDriver(TestAdaptor{}, "bot", "1")
return NewAnalogSensorDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
}

func TestAnalogSensorDriverStart(t *testing.T) {
Expand Down
13 changes: 8 additions & 5 deletions platforms/gpio/button_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (

type ButtonDriver struct {
gobot.Driver
Adaptor DigitalReader
Active bool
Active bool
}

func NewButtonDriver(a DigitalReader, name string, pin string) *ButtonDriver {
Expand All @@ -19,12 +18,16 @@ func NewButtonDriver(a DigitalReader, name string, pin string) *ButtonDriver {
"push": gobot.NewEvent(),
"release": gobot.NewEvent(),
},
Adaptor: a.(gobot.AdaptorInterface),
},
Active: false,
Adaptor: a,
Active: false,
}
}

func (b *ButtonDriver) adaptor() DigitalReader {
return b.Driver.Adaptor.(DigitalReader)
}

func (b *ButtonDriver) Start() bool {
state := 0
gobot.Every(b.Interval, func() {
Expand All @@ -40,7 +43,7 @@ func (b *ButtonDriver) Halt() bool { return true }
func (b *ButtonDriver) Init() bool { return true }

func (b *ButtonDriver) readState() int {
return b.Adaptor.DigitalRead(b.Pin)
return b.adaptor().DigitalRead(b.Pin)
}

func (b *ButtonDriver) update(newVal int) {
Expand Down
2 changes: 1 addition & 1 deletion platforms/gpio/button_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func initTestButtonDriver() *ButtonDriver {
return NewButtonDriver(TestAdaptor{}, "bot", "1")
return NewButtonDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
}

func TestButtonDriverStart(t *testing.T) {
Expand Down
18 changes: 10 additions & 8 deletions platforms/gpio/direct_pin_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

type DirectPinDriver struct {
gobot.Driver
Adaptor DirectPin
}

func NewDirectPinDriver(a DirectPin, name string, pin string) *DirectPinDriver {
Expand All @@ -16,8 +15,8 @@ func NewDirectPinDriver(a DirectPin, name string, pin string) *DirectPinDriver {
Name: name,
Pin: pin,
Commands: make(map[string]func(map[string]interface{}) interface{}),
Adaptor: a.(gobot.AdaptorInterface),
},
Adaptor: a,
}

d.Driver.AddCommand("DigitalRead", func(params map[string]interface{}) interface{} {
Expand Down Expand Up @@ -50,30 +49,33 @@ func NewDirectPinDriver(a DirectPin, name string, pin string) *DirectPinDriver {
return d
}

func (d *DirectPinDriver) adaptor() DirectPin {
return d.Driver.Adaptor.(DirectPin)
}
func (d *DirectPinDriver) Start() bool { return true }
func (d *DirectPinDriver) Halt() bool { return true }
func (d *DirectPinDriver) Init() bool { return true }

func (d *DirectPinDriver) DigitalRead() int {
return d.Adaptor.DigitalRead(d.Pin)
return d.adaptor().DigitalRead(d.Pin)
}

func (d *DirectPinDriver) DigitalWrite(level byte) {
d.Adaptor.DigitalWrite(d.Pin, level)
d.adaptor().DigitalWrite(d.Pin, level)
}

func (d *DirectPinDriver) AnalogRead() int {
return d.Adaptor.AnalogRead(d.Pin)
return d.adaptor().AnalogRead(d.Pin)
}

func (d *DirectPinDriver) AnalogWrite(level byte) {
d.Adaptor.AnalogWrite(d.Pin, level)
d.adaptor().AnalogWrite(d.Pin, level)
}

func (d *DirectPinDriver) PwmWrite(level byte) {
d.Adaptor.PwmWrite(d.Pin, level)
d.adaptor().PwmWrite(d.Pin, level)
}

func (d *DirectPinDriver) ServoWrite(level byte) {
d.Adaptor.ServoWrite(d.Pin, level)
d.adaptor().ServoWrite(d.Pin, level)
}
2 changes: 1 addition & 1 deletion platforms/gpio/direct_pin_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func initTestDirectPinDriver() *DirectPinDriver {
return NewDirectPinDriver(TestAdaptor{}, "bot", "1")
return NewDirectPinDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
}

func TestDirectPinDriverStart(t *testing.T) {
Expand Down
Loading

0 comments on commit f77689a

Please sign in to comment.