mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-27 13:48:56 +08:00
The Driver struct now has an Adaptor field
This commit is contained in:
parent
2785875089
commit
f77689abe3
@ -26,9 +26,7 @@ type connection struct {
|
|||||||
Name string `json:"-"`
|
Name string `json:"-"`
|
||||||
Type string `json:"-"`
|
Type string `json:"-"`
|
||||||
Adaptor AdaptorInterface `json:"-"`
|
Adaptor AdaptorInterface `json:"-"`
|
||||||
Port string `json:"-"`
|
|
||||||
Robot *Robot `json:"-"`
|
Robot *Robot `json:"-"`
|
||||||
Params map[string]interface{} `json:"-"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type connections []*connection
|
type connections []*connection
|
||||||
@ -62,15 +60,13 @@ func NewConnection(adaptor AdaptorInterface, r *Robot) *connection {
|
|||||||
return &connection{
|
return &connection{
|
||||||
Type: t[1:len(t)],
|
Type: t[1:len(t)],
|
||||||
Name: adaptor.name(),
|
Name: adaptor.name(),
|
||||||
Port: adaptor.port(),
|
|
||||||
Params: adaptor.params(),
|
|
||||||
Robot: r,
|
Robot: r,
|
||||||
Adaptor: adaptor,
|
Adaptor: adaptor,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *connection) Connect() bool {
|
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()
|
return c.Adaptor.Connect()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,13 +78,13 @@ func (c *connection) Finalize() bool {
|
|||||||
func (c *connection) ToJSON() *JSONConnection {
|
func (c *connection) ToJSON() *JSONConnection {
|
||||||
return &JSONConnection{
|
return &JSONConnection{
|
||||||
Name: c.Name,
|
Name: c.Name,
|
||||||
Port: c.Port,
|
Port: c.port(),
|
||||||
Adaptor: c.Type,
|
Adaptor: c.Type,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *connection) port() string {
|
func (c *connection) port() string {
|
||||||
return c.Port
|
return c.Adaptor.port()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *connection) name() string {
|
func (c *connection) name() string {
|
||||||
|
21
device.go
21
device.go
@ -15,6 +15,7 @@ type Device interface {
|
|||||||
interval() time.Duration
|
interval() time.Duration
|
||||||
setName(string)
|
setName(string)
|
||||||
name() string
|
name() string
|
||||||
|
adaptor() AdaptorInterface
|
||||||
commands() map[string]func(map[string]interface{}) interface{}
|
commands() map[string]func(map[string]interface{}) interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,10 +27,10 @@ type JSONDevice struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type device struct {
|
type device struct {
|
||||||
Name string `json:"-"`
|
Name string
|
||||||
Type string `json:"-"`
|
Type string
|
||||||
Robot *Robot `json:"-"`
|
Robot *Robot
|
||||||
Driver DriverInterface `json:"-"`
|
Driver DriverInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
type devices []*device
|
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) {
|
func (d *device) setInterval(t time.Duration) {
|
||||||
d.Driver.setInterval(t)
|
d.Driver.setInterval(t)
|
||||||
}
|
}
|
||||||
@ -109,10 +114,12 @@ func (d *device) ToJSON() *JSONDevice {
|
|||||||
jsonDevice := &JSONDevice{
|
jsonDevice := &JSONDevice{
|
||||||
Name: d.Name,
|
Name: d.Name,
|
||||||
Driver: d.Type,
|
Driver: d.Type,
|
||||||
Connection: d.Robot.Connection(FieldByNamePtr(FieldByNamePtr(d.Driver, "Adaptor").
|
|
||||||
Interface().(AdaptorInterface), "Name").
|
|
||||||
Interface().(string)).ToJSON(),
|
|
||||||
Commands: []string{},
|
Commands: []string{},
|
||||||
|
Connection: nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
if d.adaptor() != nil {
|
||||||
|
jsonDevice.Connection = d.Robot.Connection(d.adaptor().name()).ToJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
commands := d.commands()
|
commands := d.commands()
|
||||||
|
16
driver.go
16
driver.go
@ -3,16 +3,18 @@ package gobot
|
|||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type Driver struct {
|
type Driver struct {
|
||||||
Interval time.Duration `json:"interval"`
|
Adaptor AdaptorInterface
|
||||||
Pin string `json:"pin"`
|
Interval time.Duration
|
||||||
Name string `json:"name"`
|
Pin string
|
||||||
Commands map[string]func(map[string]interface{}) interface{} `json:"commands"`
|
Name string
|
||||||
Events map[string]*Event `json:"-"`
|
Commands map[string]func(map[string]interface{}) interface{}
|
||||||
|
Events map[string]*Event
|
||||||
}
|
}
|
||||||
|
|
||||||
type DriverInterface interface {
|
type DriverInterface interface {
|
||||||
Start() bool
|
Start() bool
|
||||||
Halt() bool
|
Halt() bool
|
||||||
|
adaptor() AdaptorInterface
|
||||||
setInterval(time.Duration)
|
setInterval(time.Duration)
|
||||||
interval() time.Duration
|
interval() time.Duration
|
||||||
setName(string)
|
setName(string)
|
||||||
@ -20,6 +22,10 @@ type DriverInterface interface {
|
|||||||
commands() map[string]func(map[string]interface{}) interface{}
|
commands() map[string]func(map[string]interface{}) interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Driver) adaptor() AdaptorInterface {
|
||||||
|
return d.Adaptor
|
||||||
|
}
|
||||||
|
|
||||||
func (d *Driver) setInterval(t time.Duration) {
|
func (d *Driver) setInterval(t time.Duration) {
|
||||||
d.Interval = t
|
d.Interval = t
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
|
|
||||||
type ArdroneDriver struct {
|
type ArdroneDriver struct {
|
||||||
gobot.Driver
|
gobot.Driver
|
||||||
Adaptor *ArdroneAdaptor
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewArdroneDriver(adaptor *ArdroneAdaptor, name string) *ArdroneDriver {
|
func NewArdroneDriver(adaptor *ArdroneAdaptor, name string) *ArdroneDriver {
|
||||||
@ -16,10 +15,13 @@ func NewArdroneDriver(adaptor *ArdroneAdaptor, name string) *ArdroneDriver {
|
|||||||
Events: map[string]*gobot.Event{
|
Events: map[string]*gobot.Event{
|
||||||
"Flying": gobot.NewEvent(),
|
"Flying": gobot.NewEvent(),
|
||||||
},
|
},
|
||||||
},
|
|
||||||
Adaptor: adaptor,
|
Adaptor: adaptor,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func (a *ArdroneDriver) adaptor() *ArdroneAdaptor {
|
||||||
|
return a.Driver.Adaptor.(*ArdroneAdaptor)
|
||||||
|
}
|
||||||
|
|
||||||
func (a *ArdroneDriver) Start() bool {
|
func (a *ArdroneDriver) Start() bool {
|
||||||
return true
|
return true
|
||||||
@ -30,45 +32,45 @@ func (a *ArdroneDriver) Halt() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *ArdroneDriver) TakeOff() {
|
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() {
|
func (a *ArdroneDriver) Land() {
|
||||||
a.Adaptor.drone.Land()
|
a.adaptor().drone.Land()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ArdroneDriver) Up(n float64) {
|
func (a *ArdroneDriver) Up(n float64) {
|
||||||
a.Adaptor.drone.Up(n)
|
a.adaptor().drone.Up(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ArdroneDriver) Down(n float64) {
|
func (a *ArdroneDriver) Down(n float64) {
|
||||||
a.Adaptor.drone.Down(n)
|
a.adaptor().drone.Down(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ArdroneDriver) Left(n float64) {
|
func (a *ArdroneDriver) Left(n float64) {
|
||||||
a.Adaptor.drone.Left(n)
|
a.adaptor().drone.Left(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ArdroneDriver) Right(n float64) {
|
func (a *ArdroneDriver) Right(n float64) {
|
||||||
a.Adaptor.drone.Right(n)
|
a.adaptor().drone.Right(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ArdroneDriver) Forward(n float64) {
|
func (a *ArdroneDriver) Forward(n float64) {
|
||||||
a.Adaptor.drone.Forward(n)
|
a.adaptor().drone.Forward(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ArdroneDriver) Backward(n float64) {
|
func (a *ArdroneDriver) Backward(n float64) {
|
||||||
a.Adaptor.drone.Backward(n)
|
a.adaptor().drone.Backward(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ArdroneDriver) Clockwise(n float64) {
|
func (a *ArdroneDriver) Clockwise(n float64) {
|
||||||
a.Adaptor.drone.Clockwise(n)
|
a.adaptor().drone.Clockwise(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ArdroneDriver) CounterClockwise(n float64) {
|
func (a *ArdroneDriver) CounterClockwise(n float64) {
|
||||||
a.Adaptor.drone.Counterclockwise(n)
|
a.adaptor().drone.Counterclockwise(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ArdroneDriver) Hover() {
|
func (a *ArdroneDriver) Hover() {
|
||||||
a.Adaptor.drone.Hover()
|
a.adaptor().drone.Hover()
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
|
|
||||||
type AnalogSensorDriver struct {
|
type AnalogSensorDriver struct {
|
||||||
gobot.Driver
|
gobot.Driver
|
||||||
Adaptor AnalogReader
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAnalogSensorDriver(a AnalogReader, name string, pin string) *AnalogSensorDriver {
|
func NewAnalogSensorDriver(a AnalogReader, name string, pin string) *AnalogSensorDriver {
|
||||||
@ -15,8 +14,8 @@ func NewAnalogSensorDriver(a AnalogReader, name string, pin string) *AnalogSenso
|
|||||||
Name: name,
|
Name: name,
|
||||||
Pin: pin,
|
Pin: pin,
|
||||||
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
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{} {
|
d.Driver.AddCommand("Read", func(params map[string]interface{}) interface{} {
|
||||||
return d.Read()
|
return d.Read()
|
||||||
@ -24,10 +23,14 @@ func NewAnalogSensorDriver(a AnalogReader, name string, pin string) *AnalogSenso
|
|||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *AnalogSensorDriver) adaptor() AnalogReader {
|
||||||
|
return a.Driver.Adaptor.(AnalogReader)
|
||||||
|
}
|
||||||
|
|
||||||
func (a *AnalogSensorDriver) Start() bool { return true }
|
func (a *AnalogSensorDriver) Start() bool { return true }
|
||||||
func (a *AnalogSensorDriver) Init() bool { return true }
|
func (a *AnalogSensorDriver) Init() bool { return true }
|
||||||
func (a *AnalogSensorDriver) Halt() bool { return true }
|
func (a *AnalogSensorDriver) Halt() bool { return true }
|
||||||
|
|
||||||
func (a *AnalogSensorDriver) Read() int {
|
func (a *AnalogSensorDriver) Read() int {
|
||||||
return a.Adaptor.AnalogRead(a.Pin)
|
return a.adaptor().AnalogRead(a.Pin)
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func initTestAnalogSensorDriver() *AnalogSensorDriver {
|
func initTestAnalogSensorDriver() *AnalogSensorDriver {
|
||||||
return NewAnalogSensorDriver(TestAdaptor{}, "bot", "1")
|
return NewAnalogSensorDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAnalogSensorDriverStart(t *testing.T) {
|
func TestAnalogSensorDriverStart(t *testing.T) {
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
|
|
||||||
type ButtonDriver struct {
|
type ButtonDriver struct {
|
||||||
gobot.Driver
|
gobot.Driver
|
||||||
Adaptor DigitalReader
|
|
||||||
Active bool
|
Active bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -19,12 +18,16 @@ func NewButtonDriver(a DigitalReader, name string, pin string) *ButtonDriver {
|
|||||||
"push": gobot.NewEvent(),
|
"push": gobot.NewEvent(),
|
||||||
"release": gobot.NewEvent(),
|
"release": gobot.NewEvent(),
|
||||||
},
|
},
|
||||||
|
Adaptor: a.(gobot.AdaptorInterface),
|
||||||
},
|
},
|
||||||
Active: false,
|
Active: false,
|
||||||
Adaptor: a,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *ButtonDriver) adaptor() DigitalReader {
|
||||||
|
return b.Driver.Adaptor.(DigitalReader)
|
||||||
|
}
|
||||||
|
|
||||||
func (b *ButtonDriver) Start() bool {
|
func (b *ButtonDriver) Start() bool {
|
||||||
state := 0
|
state := 0
|
||||||
gobot.Every(b.Interval, func() {
|
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) Init() bool { return true }
|
||||||
|
|
||||||
func (b *ButtonDriver) readState() int {
|
func (b *ButtonDriver) readState() int {
|
||||||
return b.Adaptor.DigitalRead(b.Pin)
|
return b.adaptor().DigitalRead(b.Pin)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *ButtonDriver) update(newVal int) {
|
func (b *ButtonDriver) update(newVal int) {
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func initTestButtonDriver() *ButtonDriver {
|
func initTestButtonDriver() *ButtonDriver {
|
||||||
return NewButtonDriver(TestAdaptor{}, "bot", "1")
|
return NewButtonDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestButtonDriverStart(t *testing.T) {
|
func TestButtonDriverStart(t *testing.T) {
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
|
|
||||||
type DirectPinDriver struct {
|
type DirectPinDriver struct {
|
||||||
gobot.Driver
|
gobot.Driver
|
||||||
Adaptor DirectPin
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDirectPinDriver(a DirectPin, name string, pin string) *DirectPinDriver {
|
func NewDirectPinDriver(a DirectPin, name string, pin string) *DirectPinDriver {
|
||||||
@ -16,8 +15,8 @@ func NewDirectPinDriver(a DirectPin, name string, pin string) *DirectPinDriver {
|
|||||||
Name: name,
|
Name: name,
|
||||||
Pin: pin,
|
Pin: pin,
|
||||||
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
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{} {
|
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
|
return d
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *DirectPinDriver) adaptor() DirectPin {
|
||||||
|
return d.Driver.Adaptor.(DirectPin)
|
||||||
|
}
|
||||||
func (d *DirectPinDriver) Start() bool { return true }
|
func (d *DirectPinDriver) Start() bool { return true }
|
||||||
func (d *DirectPinDriver) Halt() bool { return true }
|
func (d *DirectPinDriver) Halt() bool { return true }
|
||||||
func (d *DirectPinDriver) Init() bool { return true }
|
func (d *DirectPinDriver) Init() bool { return true }
|
||||||
|
|
||||||
func (d *DirectPinDriver) DigitalRead() int {
|
func (d *DirectPinDriver) DigitalRead() int {
|
||||||
return d.Adaptor.DigitalRead(d.Pin)
|
return d.adaptor().DigitalRead(d.Pin)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DirectPinDriver) DigitalWrite(level byte) {
|
func (d *DirectPinDriver) DigitalWrite(level byte) {
|
||||||
d.Adaptor.DigitalWrite(d.Pin, level)
|
d.adaptor().DigitalWrite(d.Pin, level)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DirectPinDriver) AnalogRead() int {
|
func (d *DirectPinDriver) AnalogRead() int {
|
||||||
return d.Adaptor.AnalogRead(d.Pin)
|
return d.adaptor().AnalogRead(d.Pin)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DirectPinDriver) AnalogWrite(level byte) {
|
func (d *DirectPinDriver) AnalogWrite(level byte) {
|
||||||
d.Adaptor.AnalogWrite(d.Pin, level)
|
d.adaptor().AnalogWrite(d.Pin, level)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DirectPinDriver) PwmWrite(level byte) {
|
func (d *DirectPinDriver) PwmWrite(level byte) {
|
||||||
d.Adaptor.PwmWrite(d.Pin, level)
|
d.adaptor().PwmWrite(d.Pin, level)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DirectPinDriver) ServoWrite(level byte) {
|
func (d *DirectPinDriver) ServoWrite(level byte) {
|
||||||
d.Adaptor.ServoWrite(d.Pin, level)
|
d.adaptor().ServoWrite(d.Pin, level)
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func initTestDirectPinDriver() *DirectPinDriver {
|
func initTestDirectPinDriver() *DirectPinDriver {
|
||||||
return NewDirectPinDriver(TestAdaptor{}, "bot", "1")
|
return NewDirectPinDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDirectPinDriverStart(t *testing.T) {
|
func TestDirectPinDriverStart(t *testing.T) {
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
|
|
||||||
type LedDriver struct {
|
type LedDriver struct {
|
||||||
gobot.Driver
|
gobot.Driver
|
||||||
Adaptor PwmDigitalWriter
|
|
||||||
High bool
|
High bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -16,9 +15,9 @@ func NewLedDriver(a PwmDigitalWriter, name string, pin string) *LedDriver {
|
|||||||
Name: name,
|
Name: name,
|
||||||
Pin: pin,
|
Pin: pin,
|
||||||
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
||||||
|
Adaptor: a.(gobot.AdaptorInterface),
|
||||||
},
|
},
|
||||||
High: false,
|
High: false,
|
||||||
Adaptor: a,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
l.Driver.AddCommand("Brightness", func(params map[string]interface{}) interface{} {
|
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
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *LedDriver) adaptor() PwmDigitalWriter {
|
||||||
|
return l.Driver.Adaptor.(PwmDigitalWriter)
|
||||||
|
}
|
||||||
|
|
||||||
func (l *LedDriver) Start() bool { return true }
|
func (l *LedDriver) Start() bool { return true }
|
||||||
func (l *LedDriver) Halt() bool { return true }
|
func (l *LedDriver) Halt() bool { return true }
|
||||||
func (l *LedDriver) Init() bool { return true }
|
func (l *LedDriver) Init() bool { return true }
|
||||||
@ -78,9 +81,9 @@ func (l *LedDriver) Toggle() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *LedDriver) Brightness(level byte) {
|
func (l *LedDriver) Brightness(level byte) {
|
||||||
l.Adaptor.PwmWrite(l.Pin, level)
|
l.adaptor().PwmWrite(l.Pin, level)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LedDriver) changeState(level byte) {
|
func (l *LedDriver) changeState(level byte) {
|
||||||
l.Adaptor.DigitalWrite(l.Pin, level)
|
l.adaptor().DigitalWrite(l.Pin, level)
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func initTestLedDriver() *LedDriver {
|
func initTestLedDriver() *LedDriver {
|
||||||
return NewLedDriver(TestAdaptor{}, "myLed", "1")
|
return NewLedDriver(newGpioTestAdaptor("adaptor"), "myLed", "1")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLedDriverStart(t *testing.T) {
|
func TestLedDriverStart(t *testing.T) {
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
|
|
||||||
type MotorDriver struct {
|
type MotorDriver struct {
|
||||||
gobot.Driver
|
gobot.Driver
|
||||||
Adaptor PwmDigitalWriter
|
|
||||||
SpeedPin string
|
SpeedPin string
|
||||||
SwitchPin string
|
SwitchPin string
|
||||||
DirectionPin string
|
DirectionPin string
|
||||||
@ -23,15 +22,19 @@ func NewMotorDriver(a PwmDigitalWriter, name string, pin string) *MotorDriver {
|
|||||||
Driver: gobot.Driver{
|
Driver: gobot.Driver{
|
||||||
Name: name,
|
Name: name,
|
||||||
Pin: pin,
|
Pin: pin,
|
||||||
|
Adaptor: a.(gobot.AdaptorInterface),
|
||||||
},
|
},
|
||||||
CurrentState: 0,
|
CurrentState: 0,
|
||||||
CurrentSpeed: 0,
|
CurrentSpeed: 0,
|
||||||
CurrentMode: "digital",
|
CurrentMode: "digital",
|
||||||
CurrentDirection: "forward",
|
CurrentDirection: "forward",
|
||||||
Adaptor: a,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *MotorDriver) adaptor() PwmDigitalWriter {
|
||||||
|
return m.Driver.Adaptor.(PwmDigitalWriter)
|
||||||
|
}
|
||||||
|
|
||||||
func (m *MotorDriver) Start() bool { return true }
|
func (m *MotorDriver) Start() bool { return true }
|
||||||
func (m *MotorDriver) Halt() bool { return true }
|
func (m *MotorDriver) Halt() bool { return true }
|
||||||
func (m *MotorDriver) Init() bool { return true }
|
func (m *MotorDriver) Init() bool { return true }
|
||||||
@ -85,7 +88,7 @@ func (m *MotorDriver) Toggle() {
|
|||||||
func (m *MotorDriver) Speed(value byte) {
|
func (m *MotorDriver) Speed(value byte) {
|
||||||
m.CurrentMode = "analog"
|
m.CurrentMode = "analog"
|
||||||
m.CurrentSpeed = value
|
m.CurrentSpeed = value
|
||||||
m.Adaptor.PwmWrite(m.SpeedPin, value)
|
m.adaptor().PwmWrite(m.SpeedPin, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MotorDriver) Forward(speed byte) {
|
func (m *MotorDriver) Forward(speed byte) {
|
||||||
@ -107,7 +110,7 @@ func (m *MotorDriver) Direction(direction string) {
|
|||||||
} else {
|
} else {
|
||||||
level = 0
|
level = 0
|
||||||
}
|
}
|
||||||
m.Adaptor.DigitalWrite(m.DirectionPin, level)
|
m.adaptor().DigitalWrite(m.DirectionPin, level)
|
||||||
} else {
|
} else {
|
||||||
var forwardLevel, backwardLevel byte
|
var forwardLevel, backwardLevel byte
|
||||||
switch direction {
|
switch direction {
|
||||||
@ -121,8 +124,8 @@ func (m *MotorDriver) Direction(direction string) {
|
|||||||
forwardLevel = 0
|
forwardLevel = 0
|
||||||
backwardLevel = 0
|
backwardLevel = 0
|
||||||
}
|
}
|
||||||
m.Adaptor.DigitalWrite(m.ForwardPin, forwardLevel)
|
m.adaptor().DigitalWrite(m.ForwardPin, forwardLevel)
|
||||||
m.Adaptor.DigitalWrite(m.BackwardPin, backwardLevel)
|
m.adaptor().DigitalWrite(m.BackwardPin, backwardLevel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,6 +153,6 @@ func (m *MotorDriver) changeState(state byte) {
|
|||||||
m.Direction("none")
|
m.Direction("none")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
m.Adaptor.DigitalWrite(m.SpeedPin, state)
|
m.adaptor().DigitalWrite(m.SpeedPin, state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func initTestMotorDriver() *MotorDriver {
|
func initTestMotorDriver() *MotorDriver {
|
||||||
return NewMotorDriver(TestAdaptor{}, "bot", "1")
|
return NewMotorDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMotorDriverStart(t *testing.T) {
|
func TestMotorDriverStart(t *testing.T) {
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
|
|
||||||
type ServoDriver struct {
|
type ServoDriver struct {
|
||||||
gobot.Driver
|
gobot.Driver
|
||||||
Adaptor Servo
|
|
||||||
CurrentAngle byte
|
CurrentAngle byte
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -16,9 +15,9 @@ func NewServoDriver(a Servo, name string, pin string) *ServoDriver {
|
|||||||
Name: name,
|
Name: name,
|
||||||
Pin: pin,
|
Pin: pin,
|
||||||
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
||||||
|
Adaptor: a.(gobot.AdaptorInterface),
|
||||||
},
|
},
|
||||||
CurrentAngle: 0,
|
CurrentAngle: 0,
|
||||||
Adaptor: a,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Driver.AddCommand("Move", func(params map[string]interface{}) interface{} {
|
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) Start() bool { return true }
|
||||||
func (s *ServoDriver) Halt() bool { return true }
|
func (s *ServoDriver) Halt() bool { return true }
|
||||||
func (s *ServoDriver) Init() bool { return true }
|
func (s *ServoDriver) Init() bool { return true }
|
||||||
|
|
||||||
func (s *ServoDriver) InitServo() {
|
func (s *ServoDriver) InitServo() {
|
||||||
s.Adaptor.InitServo()
|
s.adaptor().InitServo()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServoDriver) Move(angle uint8) {
|
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")
|
panic("Servo angle must be an integer between 0-180")
|
||||||
}
|
}
|
||||||
s.CurrentAngle = angle
|
s.CurrentAngle = angle
|
||||||
s.Adaptor.ServoWrite(s.Pin, s.angleToSpan(angle))
|
s.adaptor().ServoWrite(s.Pin, s.angleToSpan(angle))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServoDriver) Min() {
|
func (s *ServoDriver) Min() {
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func initTestServoDriver() *ServoDriver {
|
func initTestServoDriver() *ServoDriver {
|
||||||
return NewServoDriver(TestAdaptor{}, "bot", "1")
|
return NewServoDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServoDriverStart(t *testing.T) {
|
func TestServoDriverStart(t *testing.T) {
|
||||||
|
@ -1,15 +1,29 @@
|
|||||||
package gpio
|
package gpio
|
||||||
|
|
||||||
type TestAdaptor struct{}
|
import "github.com/hybridgroup/gobot"
|
||||||
|
|
||||||
func (t TestAdaptor) AnalogWrite(string, byte) {}
|
type gpioTestAdaptor struct {
|
||||||
func (t TestAdaptor) DigitalWrite(string, byte) {}
|
gobot.Adaptor
|
||||||
func (t TestAdaptor) ServoWrite(string, byte) {}
|
}
|
||||||
func (t TestAdaptor) PwmWrite(string, byte) {}
|
|
||||||
func (t TestAdaptor) InitServo() {}
|
func (t *gpioTestAdaptor) AnalogWrite(string, byte) {}
|
||||||
func (t TestAdaptor) AnalogRead(string) int {
|
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
|
return 99
|
||||||
}
|
}
|
||||||
func (t TestAdaptor) DigitalRead(string) int {
|
func (t *gpioTestAdaptor) DigitalRead(string) int {
|
||||||
return 1
|
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,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -15,8 +15,8 @@ func NewBlinkMDriver(a I2cInterface, name string) *BlinkMDriver {
|
|||||||
Driver: gobot.Driver{
|
Driver: gobot.Driver{
|
||||||
Name: name,
|
Name: name,
|
||||||
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
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{} {
|
b.Driver.AddCommand("FirmwareVersion", func(params map[string]interface{}) interface{} {
|
||||||
@ -43,9 +43,13 @@ func NewBlinkMDriver(a I2cInterface, name string) *BlinkMDriver {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *BlinkMDriver) adaptor() I2cInterface {
|
||||||
|
return b.Driver.Adaptor.(I2cInterface)
|
||||||
|
}
|
||||||
|
|
||||||
func (b *BlinkMDriver) Start() bool {
|
func (b *BlinkMDriver) Start() bool {
|
||||||
b.Adaptor.I2cStart(0x09)
|
b.adaptor().I2cStart(0x09)
|
||||||
b.Adaptor.I2cWrite([]byte("o"))
|
b.adaptor().I2cWrite([]byte("o"))
|
||||||
b.Rgb(0, 0, 0)
|
b.Rgb(0, 0, 0)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -53,18 +57,18 @@ func (b *BlinkMDriver) Init() bool { return true }
|
|||||||
func (b *BlinkMDriver) Halt() bool { return true }
|
func (b *BlinkMDriver) Halt() bool { return true }
|
||||||
|
|
||||||
func (b *BlinkMDriver) Rgb(red byte, green byte, blue byte) {
|
func (b *BlinkMDriver) Rgb(red byte, green byte, blue byte) {
|
||||||
b.Adaptor.I2cWrite([]byte("n"))
|
b.adaptor().I2cWrite([]byte("n"))
|
||||||
b.Adaptor.I2cWrite([]byte{red, green, blue})
|
b.adaptor().I2cWrite([]byte{red, green, blue})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlinkMDriver) Fade(red byte, green byte, blue byte) {
|
func (b *BlinkMDriver) Fade(red byte, green byte, blue byte) {
|
||||||
b.Adaptor.I2cWrite([]byte("c"))
|
b.adaptor().I2cWrite([]byte("c"))
|
||||||
b.Adaptor.I2cWrite([]byte{red, green, blue})
|
b.adaptor().I2cWrite([]byte{red, green, blue})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlinkMDriver) FirmwareVersion() string {
|
func (b *BlinkMDriver) FirmwareVersion() string {
|
||||||
b.Adaptor.I2cWrite([]byte("Z"))
|
b.adaptor().I2cWrite([]byte("Z"))
|
||||||
data := b.Adaptor.I2cRead(2)
|
data := b.adaptor().I2cRead(2)
|
||||||
if len(data) != 2 {
|
if len(data) != 2 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@ -72,8 +76,8 @@ func (b *BlinkMDriver) FirmwareVersion() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlinkMDriver) Color() []byte {
|
func (b *BlinkMDriver) Color() []byte {
|
||||||
b.Adaptor.I2cWrite([]byte("g"))
|
b.adaptor().I2cWrite([]byte("g"))
|
||||||
data := b.Adaptor.I2cRead(3)
|
data := b.adaptor().I2cRead(3)
|
||||||
if len(data) != 3 {
|
if len(data) != 3 {
|
||||||
return make([]byte, 0)
|
return make([]byte, 0)
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func initTestBlinkMDriver() *BlinkMDriver {
|
func initTestBlinkMDriver() *BlinkMDriver {
|
||||||
return NewBlinkMDriver(TestAdaptor{}, "bot")
|
return NewBlinkMDriver(newI2cTestAdaptor("adaptor"), "bot")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBlinkMDriverStart(t *testing.T) {
|
func TestBlinkMDriverStart(t *testing.T) {
|
||||||
|
@ -14,18 +14,22 @@ func NewHMC6352Driver(a I2cInterface, name string) *HMC6352Driver {
|
|||||||
return &HMC6352Driver{
|
return &HMC6352Driver{
|
||||||
Driver: gobot.Driver{
|
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 {
|
func (h *HMC6352Driver) Start() bool {
|
||||||
h.Adaptor.I2cStart(0x21)
|
h.adaptor().I2cStart(0x21)
|
||||||
h.Adaptor.I2cWrite([]byte("A"))
|
h.adaptor().I2cWrite([]byte("A"))
|
||||||
|
|
||||||
gobot.Every(h.Interval, func() {
|
gobot.Every(h.Interval, func() {
|
||||||
h.Adaptor.I2cWrite([]byte("A"))
|
h.adaptor().I2cWrite([]byte("A"))
|
||||||
ret := h.Adaptor.I2cRead(2)
|
ret := h.adaptor().I2cRead(2)
|
||||||
if len(ret) == 2 {
|
if len(ret) == 2 {
|
||||||
h.Heading = (uint16(ret[1]) + uint16(ret[0])*256) / 10
|
h.Heading = (uint16(ret[1]) + uint16(ret[0])*256) / 10
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func initTestHMC6352Driver() *HMC6352Driver {
|
func initTestHMC6352Driver() *HMC6352Driver {
|
||||||
return NewHMC6352Driver(TestAdaptor{}, "bot")
|
return NewHMC6352Driver(newI2cTestAdaptor("adaptor"), "bot")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHMC6352DriverStart(t *testing.T) {
|
func TestHMC6352DriverStart(t *testing.T) {
|
||||||
|
@ -1,16 +1,23 @@
|
|||||||
package i2c
|
package i2c
|
||||||
|
|
||||||
type TestAdaptor struct {
|
import "github.com/hybridgroup/gobot"
|
||||||
|
|
||||||
|
type i2cTestAdaptor struct {
|
||||||
|
gobot.Adaptor
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TestAdaptor) I2cStart(byte) {
|
func (t *i2cTestAdaptor) I2cStart(byte) {}
|
||||||
return
|
func (t *i2cTestAdaptor) I2cRead(uint) []byte {
|
||||||
}
|
|
||||||
|
|
||||||
func (t TestAdaptor) I2cRead(uint) []byte {
|
|
||||||
return []byte{99, 1}
|
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) {
|
func newI2cTestAdaptor(name string) *i2cTestAdaptor {
|
||||||
return
|
return &i2cTestAdaptor{
|
||||||
|
Adaptor: gobot.Adaptor{
|
||||||
|
Name: name,
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
|
|
||||||
type WiichuckDriver struct {
|
type WiichuckDriver struct {
|
||||||
gobot.Driver
|
gobot.Driver
|
||||||
Adaptor I2cInterface
|
|
||||||
joystick map[string]float64
|
joystick map[string]float64
|
||||||
data map[string]float64
|
data map[string]float64
|
||||||
}
|
}
|
||||||
@ -21,6 +20,7 @@ func NewWiichuckDriver(a I2cInterface, name string) *WiichuckDriver {
|
|||||||
"c_button": gobot.NewEvent(),
|
"c_button": gobot.NewEvent(),
|
||||||
"joystick": gobot.NewEvent(),
|
"joystick": gobot.NewEvent(),
|
||||||
},
|
},
|
||||||
|
Adaptor: a.(gobot.AdaptorInterface),
|
||||||
},
|
},
|
||||||
joystick: map[string]float64{
|
joystick: map[string]float64{
|
||||||
"sy_origin": -1,
|
"sy_origin": -1,
|
||||||
@ -32,16 +32,19 @@ func NewWiichuckDriver(a I2cInterface, name string) *WiichuckDriver {
|
|||||||
"z": 0,
|
"z": 0,
|
||||||
"c": 0,
|
"c": 0,
|
||||||
},
|
},
|
||||||
Adaptor: a,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *WiichuckDriver) adaptor() I2cInterface {
|
||||||
|
return w.Driver.Adaptor.(I2cInterface)
|
||||||
|
}
|
||||||
|
|
||||||
func (w *WiichuckDriver) Start() bool {
|
func (w *WiichuckDriver) Start() bool {
|
||||||
w.Adaptor.I2cStart(0x52)
|
w.adaptor().I2cStart(0x52)
|
||||||
gobot.Every(w.Interval, func() {
|
gobot.Every(w.Interval, func() {
|
||||||
w.Adaptor.I2cWrite([]byte{0x40, 0x00})
|
w.adaptor().I2cWrite([]byte{0x40, 0x00})
|
||||||
w.Adaptor.I2cWrite([]byte{0x00})
|
w.adaptor().I2cWrite([]byte{0x00})
|
||||||
newValue := w.Adaptor.I2cRead(6)
|
newValue := w.adaptor().I2cRead(6)
|
||||||
if len(newValue) == 6 {
|
if len(newValue) == 6 {
|
||||||
w.update(newValue)
|
w.update(newValue)
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func initTestWiichuckDriver() *WiichuckDriver {
|
func initTestWiichuckDriver() *WiichuckDriver {
|
||||||
return NewWiichuckDriver(TestAdaptor{}, "bot")
|
return NewWiichuckDriver(newI2cTestAdaptor("adaptor"), "bot")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWiichuckDriverStart(t *testing.T) {
|
func TestWiichuckDriverStart(t *testing.T) {
|
||||||
|
@ -11,7 +11,6 @@ import (
|
|||||||
|
|
||||||
type JoystickDriver struct {
|
type JoystickDriver struct {
|
||||||
gobot.Driver
|
gobot.Driver
|
||||||
Adaptor *JoystickAdaptor
|
|
||||||
config joystickConfig
|
config joystickConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,8 +37,8 @@ func NewJoystickDriver(a *JoystickAdaptor, name string, config string) *Joystick
|
|||||||
d := &JoystickDriver{
|
d := &JoystickDriver{
|
||||||
Driver: gobot.Driver{
|
Driver: gobot.Driver{
|
||||||
Events: make(map[string]*gobot.Event),
|
Events: make(map[string]*gobot.Event),
|
||||||
},
|
|
||||||
Adaptor: a,
|
Adaptor: a,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
file, e := ioutil.ReadFile(config)
|
file, e := ioutil.ReadFile(config)
|
||||||
@ -62,6 +61,10 @@ func NewJoystickDriver(a *JoystickAdaptor, name string, config string) *Joystick
|
|||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (j *JoystickDriver) adaptor() *JoystickAdaptor {
|
||||||
|
return j.Driver.Adaptor.(*JoystickAdaptor)
|
||||||
|
}
|
||||||
|
|
||||||
func (j *JoystickDriver) Start() bool {
|
func (j *JoystickDriver) Start() bool {
|
||||||
go func() {
|
go func() {
|
||||||
var event sdl.Event
|
var event sdl.Event
|
||||||
@ -69,7 +72,7 @@ func (j *JoystickDriver) Start() bool {
|
|||||||
for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||||
switch data := event.(type) {
|
switch data := event.(type) {
|
||||||
case *sdl.JoyAxisEvent:
|
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)
|
axis := j.findName(data.Axis, j.config.Axis)
|
||||||
if axis == "" {
|
if axis == "" {
|
||||||
fmt.Println("Unknown Axis:", data.Axis)
|
fmt.Println("Unknown Axis:", data.Axis)
|
||||||
@ -78,7 +81,7 @@ func (j *JoystickDriver) Start() bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
case *sdl.JoyButtonEvent:
|
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)
|
button := j.findName(data.Button, j.config.Buttons)
|
||||||
if button == "" {
|
if button == "" {
|
||||||
fmt.Println("Unknown Button:", data.Button)
|
fmt.Println("Unknown Button:", data.Button)
|
||||||
@ -91,7 +94,7 @@ func (j *JoystickDriver) Start() bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
case *sdl.JoyHatEvent:
|
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)
|
hat := j.findHatName(data.Value, data.Hat, j.config.Hats)
|
||||||
if hat == "" {
|
if hat == "" {
|
||||||
fmt.Println("Unknown Hat:", data.Hat, data.Value)
|
fmt.Println("Unknown Hat:", data.Hat, data.Value)
|
||||||
|
@ -8,7 +8,6 @@ import (
|
|||||||
|
|
||||||
type LeapMotionDriver struct {
|
type LeapMotionDriver struct {
|
||||||
gobot.Driver
|
gobot.Driver
|
||||||
Adaptor *LeapMotionAdaptor
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLeapMotionDriver(a *LeapMotionAdaptor, name string) *LeapMotionDriver {
|
func NewLeapMotionDriver(a *LeapMotionAdaptor, name string) *LeapMotionDriver {
|
||||||
@ -18,15 +17,18 @@ func NewLeapMotionDriver(a *LeapMotionAdaptor, name string) *LeapMotionDriver {
|
|||||||
Events: map[string]*gobot.Event{
|
Events: map[string]*gobot.Event{
|
||||||
"Message": gobot.NewEvent(),
|
"Message": gobot.NewEvent(),
|
||||||
},
|
},
|
||||||
},
|
|
||||||
Adaptor: a,
|
Adaptor: a,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *LeapMotionDriver) adaptor() *LeapMotionAdaptor {
|
||||||
|
return l.Driver.Adaptor.(*LeapMotionAdaptor)
|
||||||
|
}
|
||||||
func (l *LeapMotionDriver) Start() bool {
|
func (l *LeapMotionDriver) Start() bool {
|
||||||
enableGestures := map[string]bool{"enableGestures": true}
|
enableGestures := map[string]bool{"enableGestures": true}
|
||||||
b, _ := json.Marshal(enableGestures)
|
b, _ := json.Marshal(enableGestures)
|
||||||
_, err := l.Adaptor.ws.Write(b)
|
_, err := l.adaptor().ws.Write(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -34,7 +36,7 @@ func (l *LeapMotionDriver) Start() bool {
|
|||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
var msg []byte
|
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))
|
gobot.Publish(l.Events["Message"], l.ParseFrame(msg))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -16,7 +16,6 @@ const CodeAsicEEG byte = 0x83 // ASIC EEG POWER 8 3-byte big-endian intege
|
|||||||
|
|
||||||
type NeuroskyDriver struct {
|
type NeuroskyDriver struct {
|
||||||
gobot.Driver
|
gobot.Driver
|
||||||
Adaptor *NeuroskyAdaptor
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type EEG struct {
|
type EEG struct {
|
||||||
@ -43,17 +42,19 @@ func NewNeuroskyDriver(a *NeuroskyAdaptor, name string) *NeuroskyDriver {
|
|||||||
"Wave": gobot.NewEvent(),
|
"Wave": gobot.NewEvent(),
|
||||||
"EEG": 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 {
|
func (n *NeuroskyDriver) Start() bool {
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
var buff = make([]byte, int(2048))
|
var buff = make([]byte, int(2048))
|
||||||
_, err := n.Adaptor.sp.Read(buff[:])
|
_, err := n.adaptor().sp.Read(buff[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
|
@ -20,9 +20,3 @@ func TestNeuroskyDriverHalt(t *testing.T) {
|
|||||||
d := initTestNeuroskyDriver()
|
d := initTestNeuroskyDriver()
|
||||||
gobot.Expect(t, d.Halt(), true)
|
gobot.Expect(t, d.Halt(), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNeuroskyDriverInit(t *testing.T) {
|
|
||||||
t.SkipNow()
|
|
||||||
d := initTestNeuroskyDriver()
|
|
||||||
gobot.Expect(t, d.Init(), true)
|
|
||||||
}
|
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
type PebbleDriver struct {
|
type PebbleDriver struct {
|
||||||
gobot.Driver
|
gobot.Driver
|
||||||
Messages []string
|
Messages []string
|
||||||
Adaptor *PebbleAdaptor
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type PebbleInterface interface {
|
type PebbleInterface interface {
|
||||||
@ -23,9 +22,9 @@ func NewPebbleDriver(adaptor *PebbleAdaptor, name string) *PebbleDriver {
|
|||||||
"tap": gobot.NewEvent(),
|
"tap": gobot.NewEvent(),
|
||||||
},
|
},
|
||||||
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
||||||
|
Adaptor: adaptor,
|
||||||
},
|
},
|
||||||
Messages: []string{},
|
Messages: []string{},
|
||||||
Adaptor: adaptor,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Driver.AddCommand("PublishEvent", func(params map[string]interface{}) interface{} {
|
p.Driver.AddCommand("PublishEvent", func(params map[string]interface{}) interface{} {
|
||||||
@ -46,6 +45,9 @@ func NewPebbleDriver(adaptor *PebbleAdaptor, name string) *PebbleDriver {
|
|||||||
|
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
func (d *PebbleDriver) adaptor() *PebbleAdaptor {
|
||||||
|
return d.Driver.Adaptor.(*PebbleAdaptor)
|
||||||
|
}
|
||||||
|
|
||||||
func (d *PebbleDriver) Start() bool { return true }
|
func (d *PebbleDriver) Start() bool { return true }
|
||||||
|
|
||||||
|
@ -14,7 +14,6 @@ type packet struct {
|
|||||||
|
|
||||||
type SpheroDriver struct {
|
type SpheroDriver struct {
|
||||||
gobot.Driver
|
gobot.Driver
|
||||||
Adaptor *SpheroAdaptor
|
|
||||||
seq uint8
|
seq uint8
|
||||||
asyncResponse [][]uint8
|
asyncResponse [][]uint8
|
||||||
syncResponse [][]uint8
|
syncResponse [][]uint8
|
||||||
@ -30,8 +29,8 @@ func NewSpheroDriver(a *SpheroAdaptor, name string) *SpheroDriver {
|
|||||||
"Collision": gobot.NewEvent(),
|
"Collision": gobot.NewEvent(),
|
||||||
},
|
},
|
||||||
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
||||||
},
|
|
||||||
Adaptor: a,
|
Adaptor: a,
|
||||||
|
},
|
||||||
packetChannel: make(chan *packet, 1024),
|
packetChannel: make(chan *packet, 1024),
|
||||||
responseChannel: make(chan []uint8, 1024),
|
responseChannel: make(chan []uint8, 1024),
|
||||||
}
|
}
|
||||||
@ -79,6 +78,11 @@ func NewSpheroDriver(a *SpheroAdaptor, name string) *SpheroDriver {
|
|||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SpheroDriver) adaptor() *SpheroAdaptor {
|
||||||
|
return s.Driver.Adaptor.(*SpheroAdaptor)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *SpheroDriver) Init() bool {
|
func (s *SpheroDriver) Init() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -209,12 +213,12 @@ func (s *SpheroDriver) craftPacket(body []uint8, cid byte) *packet {
|
|||||||
func (s *SpheroDriver) write(packet *packet) {
|
func (s *SpheroDriver) write(packet *packet) {
|
||||||
buf := append(packet.header, packet.body...)
|
buf := append(packet.header, packet.body...)
|
||||||
buf = append(buf, packet.checksum)
|
buf = append(buf, packet.checksum)
|
||||||
length, err := s.Adaptor.sp.Write(buf)
|
length, err := s.adaptor().sp.Write(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(s.Name, err)
|
fmt.Println(s.Name, err)
|
||||||
s.Adaptor.Disconnect()
|
s.adaptor().Disconnect()
|
||||||
fmt.Println("Reconnecting to SpheroDriver...")
|
fmt.Println("Reconnecting to SpheroDriver...")
|
||||||
s.Adaptor.Connect()
|
s.adaptor().Connect()
|
||||||
return
|
return
|
||||||
} else if length != len(buf) {
|
} else if length != len(buf) {
|
||||||
fmt.Println("Not enough bytes written", s.Name)
|
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 {
|
func (s *SpheroDriver) readNextChunk(length uint8) []uint8 {
|
||||||
time.Sleep(1000 * time.Microsecond)
|
time.Sleep(1000 * time.Microsecond)
|
||||||
var read = make([]uint8, int(length))
|
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) {
|
if err != nil || length != uint8(l) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
10
robot.go
10
robot.go
@ -13,11 +13,11 @@ type JSONRobot struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Robot struct {
|
type Robot struct {
|
||||||
Name string `json:"-"`
|
Name string
|
||||||
Commands map[string]func(map[string]interface{}) interface{} `json:"-"`
|
Commands map[string]func(map[string]interface{}) interface{}
|
||||||
Work func() `json:"-"`
|
Work func()
|
||||||
connections connections `json:"-"`
|
connections connections
|
||||||
devices devices `json:"-"`
|
devices devices
|
||||||
}
|
}
|
||||||
|
|
||||||
type Robots []*Robot
|
type Robots []*Robot
|
||||||
|
@ -47,7 +47,6 @@ func (NullReadWriteCloser) Close() error {
|
|||||||
|
|
||||||
type testDriver struct {
|
type testDriver struct {
|
||||||
Driver
|
Driver
|
||||||
Adaptor *testAdaptor
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *testDriver) Init() bool { return true }
|
func (t *testDriver) Init() bool { return true }
|
||||||
@ -59,8 +58,8 @@ func NewTestDriver(name string, adaptor *testAdaptor) *testDriver {
|
|||||||
Driver: Driver{
|
Driver: Driver{
|
||||||
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
||||||
Name: name,
|
Name: name,
|
||||||
},
|
|
||||||
Adaptor: adaptor,
|
Adaptor: adaptor,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Driver.AddCommand("TestDriverCommand", func(params map[string]interface{}) interface{} {
|
t.Driver.AddCommand("TestDriverCommand", func(params map[string]interface{}) interface{} {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user