1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-24 13:48:49 +08:00

The Driver struct now has an Adaptor field

This commit is contained in:
Adrian Zankich 2014-06-15 17:22:50 -07:00
parent 2785875089
commit f77689abe3
32 changed files with 223 additions and 158 deletions

View File

@ -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
@ -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()
}
@ -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 {

View File

@ -15,6 +15,7 @@ type Device interface {
interval() time.Duration
setName(string)
name() string
adaptor() AdaptorInterface
commands() map[string]func(map[string]interface{}) interface{}
}
@ -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
@ -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)
}
@ -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()

View File

@ -3,16 +3,18 @@ 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)
@ -20,6 +22,10 @@ type DriverInterface interface {
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
}

View File

@ -6,7 +6,6 @@ import (
type ArdroneDriver struct {
gobot.Driver
Adaptor *ArdroneAdaptor
}
func NewArdroneDriver(adaptor *ArdroneAdaptor, name string) *ArdroneDriver {
@ -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
@ -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()
}

View File

@ -6,7 +6,6 @@ import (
type AnalogSensorDriver struct {
gobot.Driver
Adaptor AnalogReader
}
func NewAnalogSensorDriver(a AnalogReader, name string, pin string) *AnalogSensorDriver {
@ -15,8 +14,8 @@ 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()
@ -24,10 +23,14 @@ func NewAnalogSensorDriver(a AnalogReader, name string, pin string) *AnalogSenso
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)
}

View File

@ -6,7 +6,7 @@ import (
)
func initTestAnalogSensorDriver() *AnalogSensorDriver {
return NewAnalogSensorDriver(TestAdaptor{}, "bot", "1")
return NewAnalogSensorDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
}
func TestAnalogSensorDriverStart(t *testing.T) {

View File

@ -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 {
@ -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() {
@ -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) {

View File

@ -6,7 +6,7 @@ import (
)
func initTestButtonDriver() *ButtonDriver {
return NewButtonDriver(TestAdaptor{}, "bot", "1")
return NewButtonDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
}
func TestButtonDriverStart(t *testing.T) {

View File

@ -7,7 +7,6 @@ import (
type DirectPinDriver struct {
gobot.Driver
Adaptor DirectPin
}
func NewDirectPinDriver(a DirectPin, name string, pin string) *DirectPinDriver {
@ -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{} {
@ -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)
}

View File

@ -6,7 +6,7 @@ import (
)
func initTestDirectPinDriver() *DirectPinDriver {
return NewDirectPinDriver(TestAdaptor{}, "bot", "1")
return NewDirectPinDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
}
func TestDirectPinDriverStart(t *testing.T) {

View File

@ -6,8 +6,7 @@ import (
type LedDriver struct {
gobot.Driver
Adaptor PwmDigitalWriter
High bool
High bool
}
func NewLedDriver(a PwmDigitalWriter, name string, pin string) *LedDriver {
@ -16,9 +15,9 @@ func NewLedDriver(a PwmDigitalWriter, name string, pin string) *LedDriver {
Name: name,
Pin: pin,
Commands: make(map[string]func(map[string]interface{}) interface{}),
Adaptor: a.(gobot.AdaptorInterface),
},
High: false,
Adaptor: a,
High: false,
}
l.Driver.AddCommand("Brightness", func(params map[string]interface{}) interface{} {
@ -45,6 +44,10 @@ func NewLedDriver(a PwmDigitalWriter, name string, pin string) *LedDriver {
return l
}
func (l *LedDriver) adaptor() PwmDigitalWriter {
return l.Driver.Adaptor.(PwmDigitalWriter)
}
func (l *LedDriver) Start() bool { return true }
func (l *LedDriver) Halt() bool { return true }
func (l *LedDriver) Init() bool { return true }
@ -78,9 +81,9 @@ func (l *LedDriver) Toggle() {
}
func (l *LedDriver) Brightness(level byte) {
l.Adaptor.PwmWrite(l.Pin, level)
l.adaptor().PwmWrite(l.Pin, level)
}
func (l *LedDriver) changeState(level byte) {
l.Adaptor.DigitalWrite(l.Pin, level)
l.adaptor().DigitalWrite(l.Pin, level)
}

View File

@ -6,7 +6,7 @@ import (
)
func initTestLedDriver() *LedDriver {
return NewLedDriver(TestAdaptor{}, "myLed", "1")
return NewLedDriver(newGpioTestAdaptor("adaptor"), "myLed", "1")
}
func TestLedDriverStart(t *testing.T) {

View File

@ -6,7 +6,6 @@ import (
type MotorDriver struct {
gobot.Driver
Adaptor PwmDigitalWriter
SpeedPin string
SwitchPin string
DirectionPin string
@ -21,17 +20,21 @@ type MotorDriver struct {
func NewMotorDriver(a PwmDigitalWriter, name string, pin string) *MotorDriver {
return &MotorDriver{
Driver: gobot.Driver{
Name: name,
Pin: pin,
Name: name,
Pin: pin,
Adaptor: a.(gobot.AdaptorInterface),
},
CurrentState: 0,
CurrentSpeed: 0,
CurrentMode: "digital",
CurrentDirection: "forward",
Adaptor: a,
}
}
func (m *MotorDriver) adaptor() PwmDigitalWriter {
return m.Driver.Adaptor.(PwmDigitalWriter)
}
func (m *MotorDriver) Start() bool { return true }
func (m *MotorDriver) Halt() bool { return true }
func (m *MotorDriver) Init() bool { return true }
@ -85,7 +88,7 @@ func (m *MotorDriver) Toggle() {
func (m *MotorDriver) Speed(value byte) {
m.CurrentMode = "analog"
m.CurrentSpeed = value
m.Adaptor.PwmWrite(m.SpeedPin, value)
m.adaptor().PwmWrite(m.SpeedPin, value)
}
func (m *MotorDriver) Forward(speed byte) {
@ -107,7 +110,7 @@ func (m *MotorDriver) Direction(direction string) {
} else {
level = 0
}
m.Adaptor.DigitalWrite(m.DirectionPin, level)
m.adaptor().DigitalWrite(m.DirectionPin, level)
} else {
var forwardLevel, backwardLevel byte
switch direction {
@ -121,8 +124,8 @@ func (m *MotorDriver) Direction(direction string) {
forwardLevel = 0
backwardLevel = 0
}
m.Adaptor.DigitalWrite(m.ForwardPin, forwardLevel)
m.Adaptor.DigitalWrite(m.BackwardPin, backwardLevel)
m.adaptor().DigitalWrite(m.ForwardPin, forwardLevel)
m.adaptor().DigitalWrite(m.BackwardPin, backwardLevel)
}
}
@ -150,6 +153,6 @@ func (m *MotorDriver) changeState(state byte) {
m.Direction("none")
}
} else {
m.Adaptor.DigitalWrite(m.SpeedPin, state)
m.adaptor().DigitalWrite(m.SpeedPin, state)
}
}

View File

@ -6,7 +6,7 @@ import (
)
func initTestMotorDriver() *MotorDriver {
return NewMotorDriver(TestAdaptor{}, "bot", "1")
return NewMotorDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
}
func TestMotorDriverStart(t *testing.T) {

View File

@ -6,7 +6,6 @@ import (
type ServoDriver struct {
gobot.Driver
Adaptor Servo
CurrentAngle byte
}
@ -16,9 +15,9 @@ func NewServoDriver(a Servo, name string, pin string) *ServoDriver {
Name: name,
Pin: pin,
Commands: make(map[string]func(map[string]interface{}) interface{}),
Adaptor: a.(gobot.AdaptorInterface),
},
CurrentAngle: 0,
Adaptor: a,
}
s.Driver.AddCommand("Move", func(params map[string]interface{}) interface{} {
@ -43,12 +42,16 @@ func NewServoDriver(a Servo, name string, pin string) *ServoDriver {
}
func (s *ServoDriver) adaptor() Servo {
return s.Driver.Adaptor.(Servo)
}
func (s *ServoDriver) Start() bool { return true }
func (s *ServoDriver) Halt() bool { return true }
func (s *ServoDriver) Init() bool { return true }
func (s *ServoDriver) InitServo() {
s.Adaptor.InitServo()
s.adaptor().InitServo()
}
func (s *ServoDriver) Move(angle uint8) {
@ -56,7 +59,7 @@ func (s *ServoDriver) Move(angle uint8) {
panic("Servo angle must be an integer between 0-180")
}
s.CurrentAngle = angle
s.Adaptor.ServoWrite(s.Pin, s.angleToSpan(angle))
s.adaptor().ServoWrite(s.Pin, s.angleToSpan(angle))
}
func (s *ServoDriver) Min() {

View File

@ -6,7 +6,7 @@ import (
)
func initTestServoDriver() *ServoDriver {
return NewServoDriver(TestAdaptor{}, "bot", "1")
return NewServoDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
}
func TestServoDriverStart(t *testing.T) {

View File

@ -1,15 +1,29 @@
package gpio
type TestAdaptor struct{}
import "github.com/hybridgroup/gobot"
func (t TestAdaptor) AnalogWrite(string, byte) {}
func (t TestAdaptor) DigitalWrite(string, byte) {}
func (t TestAdaptor) ServoWrite(string, byte) {}
func (t TestAdaptor) PwmWrite(string, byte) {}
func (t TestAdaptor) InitServo() {}
func (t TestAdaptor) AnalogRead(string) int {
type gpioTestAdaptor struct {
gobot.Adaptor
}
func (t *gpioTestAdaptor) AnalogWrite(string, byte) {}
func (t *gpioTestAdaptor) DigitalWrite(string, byte) {}
func (t *gpioTestAdaptor) ServoWrite(string, byte) {}
func (t *gpioTestAdaptor) PwmWrite(string, byte) {}
func (t *gpioTestAdaptor) InitServo() {}
func (t *gpioTestAdaptor) AnalogRead(string) int {
return 99
}
func (t TestAdaptor) DigitalRead(string) int {
func (t *gpioTestAdaptor) DigitalRead(string) int {
return 1
}
func (t *gpioTestAdaptor) Connect() bool { return true }
func (t *gpioTestAdaptor) Finalize() bool { return true }
func newGpioTestAdaptor(name string) *gpioTestAdaptor {
return &gpioTestAdaptor{
Adaptor: gobot.Adaptor{
Name: name,
},
}
}

View File

@ -15,8 +15,8 @@ func NewBlinkMDriver(a I2cInterface, name string) *BlinkMDriver {
Driver: gobot.Driver{
Name: name,
Commands: make(map[string]func(map[string]interface{}) interface{}),
Adaptor: a.(gobot.AdaptorInterface),
},
Adaptor: a,
}
b.Driver.AddCommand("FirmwareVersion", func(params map[string]interface{}) interface{} {
@ -43,9 +43,13 @@ func NewBlinkMDriver(a I2cInterface, name string) *BlinkMDriver {
return b
}
func (b *BlinkMDriver) adaptor() I2cInterface {
return b.Driver.Adaptor.(I2cInterface)
}
func (b *BlinkMDriver) Start() bool {
b.Adaptor.I2cStart(0x09)
b.Adaptor.I2cWrite([]byte("o"))
b.adaptor().I2cStart(0x09)
b.adaptor().I2cWrite([]byte("o"))
b.Rgb(0, 0, 0)
return true
}
@ -53,18 +57,18 @@ func (b *BlinkMDriver) Init() bool { return true }
func (b *BlinkMDriver) Halt() bool { return true }
func (b *BlinkMDriver) Rgb(red byte, green byte, blue byte) {
b.Adaptor.I2cWrite([]byte("n"))
b.Adaptor.I2cWrite([]byte{red, green, blue})
b.adaptor().I2cWrite([]byte("n"))
b.adaptor().I2cWrite([]byte{red, green, blue})
}
func (b *BlinkMDriver) Fade(red byte, green byte, blue byte) {
b.Adaptor.I2cWrite([]byte("c"))
b.Adaptor.I2cWrite([]byte{red, green, blue})
b.adaptor().I2cWrite([]byte("c"))
b.adaptor().I2cWrite([]byte{red, green, blue})
}
func (b *BlinkMDriver) FirmwareVersion() string {
b.Adaptor.I2cWrite([]byte("Z"))
data := b.Adaptor.I2cRead(2)
b.adaptor().I2cWrite([]byte("Z"))
data := b.adaptor().I2cRead(2)
if len(data) != 2 {
return ""
}
@ -72,8 +76,8 @@ func (b *BlinkMDriver) FirmwareVersion() string {
}
func (b *BlinkMDriver) Color() []byte {
b.Adaptor.I2cWrite([]byte("g"))
data := b.Adaptor.I2cRead(3)
b.adaptor().I2cWrite([]byte("g"))
data := b.adaptor().I2cRead(3)
if len(data) != 3 {
return make([]byte, 0)
}

View File

@ -6,7 +6,7 @@ import (
)
func initTestBlinkMDriver() *BlinkMDriver {
return NewBlinkMDriver(TestAdaptor{}, "bot")
return NewBlinkMDriver(newI2cTestAdaptor("adaptor"), "bot")
}
func TestBlinkMDriverStart(t *testing.T) {

View File

@ -13,19 +13,23 @@ type HMC6352Driver struct {
func NewHMC6352Driver(a I2cInterface, name string) *HMC6352Driver {
return &HMC6352Driver{
Driver: gobot.Driver{
Name: name,
Name: name,
Adaptor: a.(gobot.AdaptorInterface),
},
Adaptor: a,
}
}
func (h *HMC6352Driver) adaptor() I2cInterface {
return h.Driver.Adaptor.(I2cInterface)
}
func (h *HMC6352Driver) Start() bool {
h.Adaptor.I2cStart(0x21)
h.Adaptor.I2cWrite([]byte("A"))
h.adaptor().I2cStart(0x21)
h.adaptor().I2cWrite([]byte("A"))
gobot.Every(h.Interval, func() {
h.Adaptor.I2cWrite([]byte("A"))
ret := h.Adaptor.I2cRead(2)
h.adaptor().I2cWrite([]byte("A"))
ret := h.adaptor().I2cRead(2)
if len(ret) == 2 {
h.Heading = (uint16(ret[1]) + uint16(ret[0])*256) / 10
}

View File

@ -6,7 +6,7 @@ import (
)
func initTestHMC6352Driver() *HMC6352Driver {
return NewHMC6352Driver(TestAdaptor{}, "bot")
return NewHMC6352Driver(newI2cTestAdaptor("adaptor"), "bot")
}
func TestHMC6352DriverStart(t *testing.T) {

View File

@ -1,16 +1,23 @@
package i2c
type TestAdaptor struct {
import "github.com/hybridgroup/gobot"
type i2cTestAdaptor struct {
gobot.Adaptor
}
func (t TestAdaptor) I2cStart(byte) {
return
}
func (t TestAdaptor) I2cRead(uint) []byte {
func (t *i2cTestAdaptor) I2cStart(byte) {}
func (t *i2cTestAdaptor) I2cRead(uint) []byte {
return []byte{99, 1}
}
func (t *i2cTestAdaptor) I2cWrite([]byte) {}
func (t *i2cTestAdaptor) Connect() bool { return true }
func (t *i2cTestAdaptor) Finalize() bool { return true }
func (t TestAdaptor) I2cWrite([]byte) {
return
func newI2cTestAdaptor(name string) *i2cTestAdaptor {
return &i2cTestAdaptor{
Adaptor: gobot.Adaptor{
Name: name,
},
}
}

View File

@ -7,7 +7,6 @@ import (
type WiichuckDriver struct {
gobot.Driver
Adaptor I2cInterface
joystick map[string]float64
data map[string]float64
}
@ -21,6 +20,7 @@ func NewWiichuckDriver(a I2cInterface, name string) *WiichuckDriver {
"c_button": gobot.NewEvent(),
"joystick": gobot.NewEvent(),
},
Adaptor: a.(gobot.AdaptorInterface),
},
joystick: map[string]float64{
"sy_origin": -1,
@ -32,16 +32,19 @@ func NewWiichuckDriver(a I2cInterface, name string) *WiichuckDriver {
"z": 0,
"c": 0,
},
Adaptor: a,
}
}
func (w *WiichuckDriver) adaptor() I2cInterface {
return w.Driver.Adaptor.(I2cInterface)
}
func (w *WiichuckDriver) Start() bool {
w.Adaptor.I2cStart(0x52)
w.adaptor().I2cStart(0x52)
gobot.Every(w.Interval, func() {
w.Adaptor.I2cWrite([]byte{0x40, 0x00})
w.Adaptor.I2cWrite([]byte{0x00})
newValue := w.Adaptor.I2cRead(6)
w.adaptor().I2cWrite([]byte{0x40, 0x00})
w.adaptor().I2cWrite([]byte{0x00})
newValue := w.adaptor().I2cRead(6)
if len(newValue) == 6 {
w.update(newValue)
}

View File

@ -6,7 +6,7 @@ import (
)
func initTestWiichuckDriver() *WiichuckDriver {
return NewWiichuckDriver(TestAdaptor{}, "bot")
return NewWiichuckDriver(newI2cTestAdaptor("adaptor"), "bot")
}
func TestWiichuckDriverStart(t *testing.T) {

View File

@ -11,8 +11,7 @@ import (
type JoystickDriver struct {
gobot.Driver
Adaptor *JoystickAdaptor
config joystickConfig
config joystickConfig
}
type pair struct {
@ -37,9 +36,9 @@ type joystickConfig struct {
func NewJoystickDriver(a *JoystickAdaptor, name string, config string) *JoystickDriver {
d := &JoystickDriver{
Driver: gobot.Driver{
Events: make(map[string]*gobot.Event),
Events: make(map[string]*gobot.Event),
Adaptor: a,
},
Adaptor: a,
}
file, e := ioutil.ReadFile(config)
@ -62,6 +61,10 @@ func NewJoystickDriver(a *JoystickAdaptor, name string, config string) *Joystick
return d
}
func (j *JoystickDriver) adaptor() *JoystickAdaptor {
return j.Driver.Adaptor.(*JoystickAdaptor)
}
func (j *JoystickDriver) Start() bool {
go func() {
var event sdl.Event
@ -69,7 +72,7 @@ func (j *JoystickDriver) Start() bool {
for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch data := event.(type) {
case *sdl.JoyAxisEvent:
if data.Which == j.Adaptor.joystick.InstanceID() {
if data.Which == j.adaptor().joystick.InstanceID() {
axis := j.findName(data.Axis, j.config.Axis)
if axis == "" {
fmt.Println("Unknown Axis:", data.Axis)
@ -78,7 +81,7 @@ func (j *JoystickDriver) Start() bool {
}
}
case *sdl.JoyButtonEvent:
if data.Which == j.Adaptor.joystick.InstanceID() {
if data.Which == j.adaptor().joystick.InstanceID() {
button := j.findName(data.Button, j.config.Buttons)
if button == "" {
fmt.Println("Unknown Button:", data.Button)
@ -91,7 +94,7 @@ func (j *JoystickDriver) Start() bool {
}
}
case *sdl.JoyHatEvent:
if data.Which == j.Adaptor.joystick.InstanceID() {
if data.Which == j.adaptor().joystick.InstanceID() {
hat := j.findHatName(data.Value, data.Hat, j.config.Hats)
if hat == "" {
fmt.Println("Unknown Hat:", data.Hat, data.Value)

View File

@ -8,7 +8,6 @@ import (
type LeapMotionDriver struct {
gobot.Driver
Adaptor *LeapMotionAdaptor
}
func NewLeapMotionDriver(a *LeapMotionAdaptor, name string) *LeapMotionDriver {
@ -18,15 +17,18 @@ func NewLeapMotionDriver(a *LeapMotionAdaptor, name string) *LeapMotionDriver {
Events: map[string]*gobot.Event{
"Message": gobot.NewEvent(),
},
Adaptor: a,
},
Adaptor: a,
}
}
func (l *LeapMotionDriver) adaptor() *LeapMotionAdaptor {
return l.Driver.Adaptor.(*LeapMotionAdaptor)
}
func (l *LeapMotionDriver) Start() bool {
enableGestures := map[string]bool{"enableGestures": true}
b, _ := json.Marshal(enableGestures)
_, err := l.Adaptor.ws.Write(b)
_, err := l.adaptor().ws.Write(b)
if err != nil {
panic(err)
}
@ -34,7 +36,7 @@ func (l *LeapMotionDriver) Start() bool {
go func() {
for {
var msg []byte
websocket.Message.Receive(l.Adaptor.ws, &msg)
websocket.Message.Receive(l.adaptor().ws, &msg)
gobot.Publish(l.Events["Message"], l.ParseFrame(msg))
}
}()

View File

@ -16,7 +16,6 @@ const CodeAsicEEG byte = 0x83 // ASIC EEG POWER 8 3-byte big-endian intege
type NeuroskyDriver struct {
gobot.Driver
Adaptor *NeuroskyAdaptor
}
type EEG struct {
@ -43,17 +42,19 @@ func NewNeuroskyDriver(a *NeuroskyAdaptor, name string) *NeuroskyDriver {
"Wave": gobot.NewEvent(),
"EEG": gobot.NewEvent(),
},
Adaptor: a,
},
Adaptor: a,
}
}
func (n *NeuroskyDriver) Init() bool { return true }
func (n *NeuroskyDriver) adaptor() *NeuroskyAdaptor {
return n.Driver.Adaptor.(*NeuroskyAdaptor)
}
func (n *NeuroskyDriver) Start() bool {
go func() {
for {
var buff = make([]byte, int(2048))
_, err := n.Adaptor.sp.Read(buff[:])
_, err := n.adaptor().sp.Read(buff[:])
if err != nil {
panic(err)
} else {

View File

@ -20,9 +20,3 @@ func TestNeuroskyDriverHalt(t *testing.T) {
d := initTestNeuroskyDriver()
gobot.Expect(t, d.Halt(), true)
}
func TestNeuroskyDriverInit(t *testing.T) {
t.SkipNow()
d := initTestNeuroskyDriver()
gobot.Expect(t, d.Init(), true)
}

View File

@ -7,7 +7,6 @@ import (
type PebbleDriver struct {
gobot.Driver
Messages []string
Adaptor *PebbleAdaptor
}
type PebbleInterface interface {
@ -23,9 +22,9 @@ func NewPebbleDriver(adaptor *PebbleAdaptor, name string) *PebbleDriver {
"tap": gobot.NewEvent(),
},
Commands: make(map[string]func(map[string]interface{}) interface{}),
Adaptor: adaptor,
},
Messages: []string{},
Adaptor: adaptor,
}
p.Driver.AddCommand("PublishEvent", func(params map[string]interface{}) interface{} {
@ -46,6 +45,9 @@ func NewPebbleDriver(adaptor *PebbleAdaptor, name string) *PebbleDriver {
return p
}
func (d *PebbleDriver) adaptor() *PebbleAdaptor {
return d.Driver.Adaptor.(*PebbleAdaptor)
}
func (d *PebbleDriver) Start() bool { return true }

View File

@ -14,7 +14,6 @@ type packet struct {
type SpheroDriver struct {
gobot.Driver
Adaptor *SpheroAdaptor
seq uint8
asyncResponse [][]uint8
syncResponse [][]uint8
@ -30,8 +29,8 @@ func NewSpheroDriver(a *SpheroAdaptor, name string) *SpheroDriver {
"Collision": gobot.NewEvent(),
},
Commands: make(map[string]func(map[string]interface{}) interface{}),
Adaptor: a,
},
Adaptor: a,
packetChannel: make(chan *packet, 1024),
responseChannel: make(chan []uint8, 1024),
}
@ -79,6 +78,11 @@ func NewSpheroDriver(a *SpheroAdaptor, name string) *SpheroDriver {
return s
}
func (s *SpheroDriver) adaptor() *SpheroAdaptor {
return s.Driver.Adaptor.(*SpheroAdaptor)
}
func (s *SpheroDriver) Init() bool {
return true
}
@ -209,12 +213,12 @@ func (s *SpheroDriver) craftPacket(body []uint8, cid byte) *packet {
func (s *SpheroDriver) write(packet *packet) {
buf := append(packet.header, packet.body...)
buf = append(buf, packet.checksum)
length, err := s.Adaptor.sp.Write(buf)
length, err := s.adaptor().sp.Write(buf)
if err != nil {
fmt.Println(s.Name, err)
s.Adaptor.Disconnect()
s.adaptor().Disconnect()
fmt.Println("Reconnecting to SpheroDriver...")
s.Adaptor.Connect()
s.adaptor().Connect()
return
} else if length != len(buf) {
fmt.Println("Not enough bytes written", s.Name)
@ -251,7 +255,7 @@ func (s *SpheroDriver) readBody(length uint8) []uint8 {
func (s *SpheroDriver) readNextChunk(length uint8) []uint8 {
time.Sleep(1000 * time.Microsecond)
var read = make([]uint8, int(length))
l, err := s.Adaptor.sp.Read(read[:])
l, err := s.adaptor().sp.Read(read[:])
if err != nil || length != uint8(l) {
return nil
}

View File

@ -13,11 +13,11 @@ type JSONRobot struct {
}
type Robot struct {
Name string `json:"-"`
Commands map[string]func(map[string]interface{}) interface{} `json:"-"`
Work func() `json:"-"`
connections connections `json:"-"`
devices devices `json:"-"`
Name string
Commands map[string]func(map[string]interface{}) interface{}
Work func()
connections connections
devices devices
}
type Robots []*Robot

View File

@ -47,7 +47,6 @@ func (NullReadWriteCloser) Close() error {
type testDriver struct {
Driver
Adaptor *testAdaptor
}
func (t *testDriver) Init() bool { return true }
@ -59,8 +58,8 @@ func NewTestDriver(name string, adaptor *testAdaptor) *testDriver {
Driver: Driver{
Commands: make(map[string]func(map[string]interface{}) interface{}),
Name: name,
Adaptor: adaptor,
},
Adaptor: adaptor,
}
t.Driver.AddCommand("TestDriverCommand", func(params map[string]interface{}) interface{} {