mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-24 13:48:49 +08:00
Update Adaptor and Driver interface to use []error
This commit is contained in:
parent
e187096a5a
commit
586507a489
@ -11,8 +11,8 @@ type Adaptor struct {
|
||||
|
||||
// AdaptorInterface defines behaviour expected for a Gobot Adaptor
|
||||
type AdaptorInterface interface {
|
||||
Finalize() error
|
||||
Connect() error
|
||||
Finalize() []error
|
||||
Connect() []error
|
||||
Port() string
|
||||
Name() string
|
||||
Type() string
|
||||
|
@ -1,6 +1,8 @@
|
||||
package gobot
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
@ -27,7 +29,7 @@ func (c *connections) Each(f func(Connection)) {
|
||||
}
|
||||
|
||||
// Start initializes all the connections.
|
||||
func (c *connections) Start() (err error) {
|
||||
func (c *connections) Start() (errs []error) {
|
||||
log.Println("Starting connections...")
|
||||
for _, connection := range *c {
|
||||
info := "Starting connection " + connection.Name()
|
||||
@ -35,8 +37,10 @@ func (c *connections) Start() (err error) {
|
||||
info = info + " on port " + connection.Port()
|
||||
}
|
||||
log.Println(info + "...")
|
||||
err = connection.Connect()
|
||||
if err != nil {
|
||||
if errs = connection.Connect(); len(errs) > 0 {
|
||||
for i, err := range errs {
|
||||
errs[i] = errors.New(fmt.Sprintf("Connection %q: %v", connection.Name(), err))
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -44,8 +48,14 @@ func (c *connections) Start() (err error) {
|
||||
}
|
||||
|
||||
// Finalize finishes all the connections.
|
||||
func (c *connections) Finalize() {
|
||||
func (c *connections) Finalize() (errs []error) {
|
||||
for _, connection := range *c {
|
||||
connection.Finalize()
|
||||
if cerrs := connection.Finalize(); cerrs != nil {
|
||||
for i, err := range cerrs {
|
||||
cerrs[i] = errors.New(fmt.Sprintf("Connection %q: %v", connection.Name(), err))
|
||||
}
|
||||
errs = append(errs, cerrs...)
|
||||
}
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
24
device.go
24
device.go
@ -31,8 +31,7 @@ func (d *devices) Each(f func(Device)) {
|
||||
}
|
||||
|
||||
// Start starts all the devices.
|
||||
func (d *devices) Start() error {
|
||||
var err error
|
||||
func (d *devices) Start() (errs []error) {
|
||||
log.Println("Starting devices...")
|
||||
for _, device := range *d {
|
||||
info := "Starting device " + device.Name()
|
||||
@ -40,18 +39,25 @@ func (d *devices) Start() error {
|
||||
info = info + " on pin " + device.Pin()
|
||||
}
|
||||
log.Println(info + "...")
|
||||
err = device.Start()
|
||||
if err != nil {
|
||||
err = errors.New(fmt.Sprintf("Could not start device: %v", err))
|
||||
break
|
||||
if errs = device.Start(); len(errs) > 0 {
|
||||
for i, err := range errs {
|
||||
errs[i] = errors.New(fmt.Sprintf("Device %q: %v", device.Name(), err))
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
return err
|
||||
return
|
||||
}
|
||||
|
||||
// Halt stop all the devices.
|
||||
func (d *devices) Halt() {
|
||||
func (d *devices) Halt() (errs []error) {
|
||||
for _, device := range *d {
|
||||
device.Halt()
|
||||
if derrs := device.Halt(); len(derrs) > 0 {
|
||||
for i, err := range derrs {
|
||||
derrs[i] = errors.New(fmt.Sprintf("Device %q: %v", device.Name(), err))
|
||||
}
|
||||
errs = append(errs, derrs...)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -7,8 +7,8 @@ import (
|
||||
|
||||
// DriverInterface defines Driver expected behaviour
|
||||
type DriverInterface interface {
|
||||
Start() error
|
||||
Halt() error
|
||||
Start() []error
|
||||
Halt() []error
|
||||
Adaptor() AdaptorInterface
|
||||
SetInterval(time.Duration)
|
||||
Interval() time.Duration
|
||||
|
30
gobot.go
30
gobot.go
@ -56,15 +56,19 @@ func (g *Gobot) Command(name string) func(map[string]interface{}) interface{} {
|
||||
}
|
||||
|
||||
// Start runs the main Gobot event loop
|
||||
func (g *Gobot) Start() (err error) {
|
||||
err = g.robots.Start()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
func (g *Gobot) Start() (errs []error) {
|
||||
if rerrs := g.robots.Start(); len(rerrs) > 0 {
|
||||
for _, err := range rerrs {
|
||||
log.Println("Error:", err)
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
c := make(chan os.Signal, 1)
|
||||
g.trap(c)
|
||||
if err != nil {
|
||||
if len(errs) > 0 {
|
||||
// there was an error during start, so we immediatly pass the interrupt
|
||||
// in order to disconnect the initialized robots, connections and devices
|
||||
c <- os.Interrupt
|
||||
}
|
||||
|
||||
@ -72,10 +76,20 @@ func (g *Gobot) Start() (err error) {
|
||||
_ = <-c
|
||||
g.robots.Each(func(r *Robot) {
|
||||
log.Println("Stopping Robot", r.Name, "...")
|
||||
r.Devices().Halt()
|
||||
r.Connections().Finalize()
|
||||
if herrs := r.Devices().Halt(); len(herrs) > 0 {
|
||||
for _, err := range herrs {
|
||||
log.Println("Error:", err)
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
if cerrs := r.Connections().Finalize(); len(cerrs) > 0 {
|
||||
for _, err := range cerrs {
|
||||
log.Println("Error:", err)
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
return err
|
||||
return errs
|
||||
}
|
||||
|
||||
// Robots fetch all robots associated with this Gobot instance.
|
||||
|
@ -51,11 +51,14 @@ func NewArdroneAdaptor(name string, v ...string) *ArdroneAdaptor {
|
||||
}
|
||||
|
||||
// Connect returns true when connection to ardrone is established correclty
|
||||
func (a *ArdroneAdaptor) Connect() error {
|
||||
return a.connect(a)
|
||||
func (a *ArdroneAdaptor) Connect() (errs []error) {
|
||||
if err := a.connect(a); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Finalize returns true when connection is finalized correctly
|
||||
func (a *ArdroneAdaptor) Finalize() error {
|
||||
return nil
|
||||
func (a *ArdroneAdaptor) Finalize() (errs []error) {
|
||||
return
|
||||
}
|
||||
|
@ -16,10 +16,10 @@ func initTestArdroneAdaptor() *ArdroneAdaptor {
|
||||
|
||||
func TestConnect(t *testing.T) {
|
||||
a := initTestArdroneAdaptor()
|
||||
gobot.Assert(t, a.Connect(), nil)
|
||||
gobot.Assert(t, len(a.Connect()), 0)
|
||||
}
|
||||
|
||||
func TestFinalize(t *testing.T) {
|
||||
a := initTestArdroneAdaptor()
|
||||
gobot.Assert(t, a.Finalize(), nil)
|
||||
gobot.Assert(t, len(a.Finalize()), 0)
|
||||
}
|
||||
|
@ -32,13 +32,13 @@ func (a *ArdroneDriver) adaptor() *ArdroneAdaptor {
|
||||
}
|
||||
|
||||
// Start returns true if driver is started succesfully
|
||||
func (a *ArdroneDriver) Start() error {
|
||||
return nil
|
||||
func (a *ArdroneDriver) Start() (errs []error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Halt returns true if driver is halted succesfully
|
||||
func (a *ArdroneDriver) Halt() error {
|
||||
return nil
|
||||
func (a *ArdroneDriver) Halt() (errs []error) {
|
||||
return
|
||||
}
|
||||
|
||||
// TakeOff makes the drone start flying
|
||||
|
@ -18,12 +18,12 @@ func initTestArdroneDriver() *ArdroneDriver {
|
||||
|
||||
func TestArdroneDriverStart(t *testing.T) {
|
||||
d := initTestArdroneDriver()
|
||||
gobot.Assert(t, d.Start(), nil)
|
||||
gobot.Assert(t, len(d.Start()), 0)
|
||||
}
|
||||
|
||||
func TestArdroneDriverHalt(t *testing.T) {
|
||||
d := initTestArdroneDriver()
|
||||
gobot.Assert(t, d.Halt(), nil)
|
||||
gobot.Assert(t, len(d.Halt()), 0)
|
||||
}
|
||||
func TestArdroneDriverTakeOff(t *testing.T) {
|
||||
d := initTestArdroneDriver()
|
||||
|
@ -144,20 +144,18 @@ func NewBeagleboneAdaptor(name string) *BeagleboneAdaptor {
|
||||
|
||||
// Connect returns true on a succesful connection to beaglebone board.
|
||||
// It initializes digital, pwm and analog pins
|
||||
func (b *BeagleboneAdaptor) Connect() (err error) {
|
||||
err = ensureSlot(b.slots, "cape-bone-iio")
|
||||
if err != nil {
|
||||
return
|
||||
func (b *BeagleboneAdaptor) Connect() (errs []error) {
|
||||
if err := ensureSlot(b.slots, "cape-bone-iio"); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
|
||||
err = ensureSlot(b.slots, "am33xx_pwm")
|
||||
if err != nil {
|
||||
return
|
||||
if err := ensureSlot(b.slots, "am33xx_pwm"); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
|
||||
g, err := glob(fmt.Sprintf("%v/helper.*", b.ocp))
|
||||
if err != nil {
|
||||
return
|
||||
return []error{err}
|
||||
}
|
||||
b.helper = g[0]
|
||||
|
||||
@ -165,21 +163,27 @@ func (b *BeagleboneAdaptor) Connect() (err error) {
|
||||
}
|
||||
|
||||
// Finalize returns true when board connection is finalized correctly.
|
||||
func (b *BeagleboneAdaptor) Finalize() (err error) {
|
||||
func (b *BeagleboneAdaptor) Finalize() (errs []error) {
|
||||
for _, pin := range b.pwmPins {
|
||||
if pin != nil {
|
||||
pin.release()
|
||||
if err := pin.release(); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, pin := range b.digitalPins {
|
||||
if pin != nil {
|
||||
pin.Unexport()
|
||||
if err := pin.Unexport(); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if b.i2cDevice != nil {
|
||||
b.i2cDevice.Close()
|
||||
if err := b.i2cDevice.Close(); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// PwmWrite writes value in specified pin
|
||||
|
@ -116,7 +116,7 @@ func TestBeagleboneAdaptor(t *testing.T) {
|
||||
data, _ := a.I2cRead(2)
|
||||
gobot.Assert(t, data, []byte{0x00, 0x01})
|
||||
|
||||
gobot.Assert(t, a.Finalize(), nil)
|
||||
gobot.Assert(t, len(a.Finalize()), 0)
|
||||
|
||||
gobot.Assert(t, a.InitServo(), errors.New("InitServo is not yet implemented"))
|
||||
|
||||
|
@ -36,12 +36,15 @@ func NewDigisparkAdaptor(name string) *DigisparkAdaptor {
|
||||
}
|
||||
|
||||
// Connect starts connection to digispark, returns true if successful
|
||||
func (d *DigisparkAdaptor) Connect() error {
|
||||
return d.connect(d)
|
||||
func (d *DigisparkAdaptor) Connect() (errs []error) {
|
||||
if err := d.connect(d); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Finalize returns true if finalization is successful
|
||||
func (d *DigisparkAdaptor) Finalize() error { return nil }
|
||||
func (d *DigisparkAdaptor) Finalize() (errs []error) { return }
|
||||
|
||||
// DigitalWrite writes level to specified pin using littlewire
|
||||
func (d *DigisparkAdaptor) DigitalWrite(pin string, level byte) (err error) {
|
||||
|
@ -56,12 +56,12 @@ func initTestDigisparkAdaptor() *DigisparkAdaptor {
|
||||
|
||||
func TestDigisparkAdaptorConnect(t *testing.T) {
|
||||
a := NewDigisparkAdaptor("bot")
|
||||
gobot.Assert(t, a.Connect(), errors.New("Error connecting to bot"))
|
||||
gobot.Assert(t, a.Connect()[0], errors.New("Error connecting to bot"))
|
||||
}
|
||||
|
||||
func TestDigisparkAdaptorFinalize(t *testing.T) {
|
||||
a := initTestDigisparkAdaptor()
|
||||
gobot.Assert(t, a.Finalize(), nil)
|
||||
gobot.Assert(t, len(a.Finalize()), 0)
|
||||
}
|
||||
|
||||
func TestDigisparkAdaptorIO(t *testing.T) {
|
||||
|
@ -62,27 +62,30 @@ func NewFirmataAdaptor(name string, args ...interface{}) *FirmataAdaptor {
|
||||
}
|
||||
|
||||
// Connect returns true if connection to board is succesfull
|
||||
func (f *FirmataAdaptor) Connect() (err error) {
|
||||
err = f.connect(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = f.board.connect()
|
||||
if err != nil {
|
||||
return err
|
||||
func (f *FirmataAdaptor) Connect() (errs []error) {
|
||||
if err := f.connect(f); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
f.SetConnected(true)
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// close finishes connection to serial port
|
||||
// Prints error message on error
|
||||
func (f *FirmataAdaptor) Disconnect() (err error) {
|
||||
return f.board.serial.Close()
|
||||
if f.board != nil {
|
||||
return f.board.serial.Close()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Finalize disconnects firmata adaptor
|
||||
func (f *FirmataAdaptor) Finalize() error { return f.Disconnect() }
|
||||
func (f *FirmataAdaptor) Finalize() (errs []error) {
|
||||
if err := f.Disconnect(); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// InitServo (not yet implemented)
|
||||
func (f *FirmataAdaptor) InitServo() (err error) {
|
||||
|
@ -37,12 +37,12 @@ func initTestFirmataAdaptor() *FirmataAdaptor {
|
||||
|
||||
func TestFirmataAdaptorFinalize(t *testing.T) {
|
||||
a := initTestFirmataAdaptor()
|
||||
gobot.Assert(t, a.Finalize(), nil)
|
||||
gobot.Assert(t, len(a.Finalize()), 0)
|
||||
}
|
||||
|
||||
func TestFirmataAdaptorConnect(t *testing.T) {
|
||||
a := initTestFirmataAdaptor()
|
||||
gobot.Assert(t, a.Connect(), nil)
|
||||
gobot.Assert(t, len(a.Connect()), 0)
|
||||
|
||||
a = NewFirmataAdaptor("board", gobot.NullReadWriteCloser{})
|
||||
gobot.Assert(t, a.connect(a), nil)
|
||||
|
@ -44,7 +44,7 @@ func (a *AnalogSensorDriver) adaptor() AnalogReader {
|
||||
// Returns true on successful start of the driver.
|
||||
// Emits the Events:
|
||||
// "data" int - Event is emitted on change and represents the current reading from the sensor.
|
||||
func (a *AnalogSensorDriver) Start() error {
|
||||
func (a *AnalogSensorDriver) Start() (errs []error) {
|
||||
value := 0
|
||||
go func() {
|
||||
for {
|
||||
@ -58,11 +58,11 @@ func (a *AnalogSensorDriver) Start() error {
|
||||
<-time.After(a.Interval())
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Halt returns true on a successful halt of the driver
|
||||
func (a *AnalogSensorDriver) Halt() error { return nil }
|
||||
func (a *AnalogSensorDriver) Halt() (errs []error) { return }
|
||||
|
||||
// Read returns the current reading from the Analog Sensor
|
||||
func (a *AnalogSensorDriver) Read() (val int, err error) {
|
||||
|
@ -12,12 +12,12 @@ func initTestAnalogSensorDriver() *AnalogSensorDriver {
|
||||
|
||||
func TestAnalogSensorDriverStart(t *testing.T) {
|
||||
d := initTestAnalogSensorDriver()
|
||||
gobot.Assert(t, d.Start(), nil)
|
||||
gobot.Assert(t, len(d.Start()), 0)
|
||||
}
|
||||
|
||||
func TestAnalogSensorDriverHalt(t *testing.T) {
|
||||
d := initTestAnalogSensorDriver()
|
||||
gobot.Assert(t, d.Halt(), nil)
|
||||
gobot.Assert(t, len(d.Halt()), 0)
|
||||
}
|
||||
|
||||
func TestAnalogSensorDriverRead(t *testing.T) {
|
||||
|
@ -43,7 +43,7 @@ func (b *ButtonDriver) adaptor() DigitalReader {
|
||||
// "push" int - On button push
|
||||
// "release" int - On button release
|
||||
// "error" error - On button error
|
||||
func (b *ButtonDriver) Start() error {
|
||||
func (b *ButtonDriver) Start() (errs []error) {
|
||||
state := 0
|
||||
go func() {
|
||||
for {
|
||||
@ -57,11 +57,11 @@ func (b *ButtonDriver) Start() error {
|
||||
<-time.After(b.Interval())
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Halt returns true on a successful halt of the driver
|
||||
func (b *ButtonDriver) Halt() error { return nil }
|
||||
func (b *ButtonDriver) Halt() (errs []error) { return }
|
||||
|
||||
func (b *ButtonDriver) readState() (val int, err error) {
|
||||
return b.adaptor().DigitalRead(b.Pin())
|
||||
|
@ -12,12 +12,12 @@ func initTestButtonDriver() *ButtonDriver {
|
||||
|
||||
func TestButtonDriverStart(t *testing.T) {
|
||||
d := initTestButtonDriver()
|
||||
gobot.Assert(t, d.Start(), nil)
|
||||
gobot.Assert(t, len(d.Start()), 0)
|
||||
}
|
||||
|
||||
func TestButtonDriverHalt(t *testing.T) {
|
||||
d := initTestButtonDriver()
|
||||
gobot.Assert(t, d.Halt(), nil)
|
||||
gobot.Assert(t, len(d.Halt()), 0)
|
||||
}
|
||||
|
||||
func TestButtonDriverReadState(t *testing.T) {
|
||||
|
@ -65,10 +65,10 @@ func (d *DirectPinDriver) adaptor() DirectPin {
|
||||
}
|
||||
|
||||
// Starts the DirectPinDriver. Returns true on successful start of the driver
|
||||
func (d *DirectPinDriver) Start() error { return nil }
|
||||
func (d *DirectPinDriver) Start() (errs []error) { return }
|
||||
|
||||
// Halts the DirectPinDriver. Returns true on successful halt of the driver
|
||||
func (d *DirectPinDriver) Halt() error { return nil }
|
||||
func (d *DirectPinDriver) Halt() (errs []error) { return }
|
||||
|
||||
// DigitalRead returns the current digital state of the pin
|
||||
func (d *DirectPinDriver) DigitalRead() (val int, err error) {
|
||||
|
@ -12,12 +12,12 @@ func initTestDirectPinDriver() *DirectPinDriver {
|
||||
|
||||
func TestDirectPinDriverStart(t *testing.T) {
|
||||
d := initTestDirectPinDriver()
|
||||
gobot.Assert(t, d.Start(), nil)
|
||||
gobot.Assert(t, len(d.Start()), 0)
|
||||
}
|
||||
|
||||
func TestDirectPinDriverHalt(t *testing.T) {
|
||||
d := initTestDirectPinDriver()
|
||||
gobot.Assert(t, d.Halt(), nil)
|
||||
gobot.Assert(t, len(d.Halt()), 0)
|
||||
}
|
||||
|
||||
func TestDirectPinDriverDigitalRead(t *testing.T) {
|
||||
|
@ -1,8 +1,6 @@
|
||||
package gpio
|
||||
|
||||
import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
import "github.com/hybridgroup/gobot"
|
||||
|
||||
var _ gobot.DriverInterface = (*LedDriver)(nil)
|
||||
|
||||
@ -55,10 +53,10 @@ func (l *LedDriver) adaptor() PwmDigitalWriter {
|
||||
}
|
||||
|
||||
// Start starts the LedDriver. Returns true on successful start of the driver
|
||||
func (l *LedDriver) Start() error { return nil }
|
||||
func (l *LedDriver) Start() (errs []error) { return }
|
||||
|
||||
// Halt halts the LedDriver. Returns true on successful halt of the driver
|
||||
func (l *LedDriver) Halt() error { return nil }
|
||||
func (l *LedDriver) Halt() (errs []error) { return }
|
||||
|
||||
// State return true if the led is On and false if the led is Off
|
||||
func (l *LedDriver) State() bool {
|
||||
|
@ -12,12 +12,12 @@ func initTestLedDriver() *LedDriver {
|
||||
|
||||
func TestLedDriverStart(t *testing.T) {
|
||||
d := initTestLedDriver()
|
||||
gobot.Assert(t, d.Start(), nil)
|
||||
gobot.Assert(t, len(d.Start()), 0)
|
||||
}
|
||||
|
||||
func TestLedDriverHalt(t *testing.T) {
|
||||
d := initTestLedDriver()
|
||||
gobot.Assert(t, d.Halt(), nil)
|
||||
gobot.Assert(t, len(d.Halt()), 0)
|
||||
}
|
||||
|
||||
func TestLedDriverOn(t *testing.T) {
|
||||
|
@ -43,7 +43,7 @@ func (b *MakeyButtonDriver) adaptor() DigitalReader {
|
||||
// Emits the Events:
|
||||
// "push" int - On button push
|
||||
// "release" int - On button release
|
||||
func (m *MakeyButtonDriver) Start() error {
|
||||
func (m *MakeyButtonDriver) Start() (errs []error) {
|
||||
state := 0
|
||||
go func() {
|
||||
for {
|
||||
@ -63,11 +63,11 @@ func (m *MakeyButtonDriver) Start() error {
|
||||
}
|
||||
<-time.After(m.Interval())
|
||||
}()
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Halt returns true on a successful halt of the driver
|
||||
func (m *MakeyButtonDriver) Halt() error { return nil }
|
||||
func (m *MakeyButtonDriver) Halt() (errs []error) { return }
|
||||
|
||||
func (m *MakeyButtonDriver) readState() (val int, err error) {
|
||||
return m.adaptor().DigitalRead(m.Pin())
|
||||
|
@ -40,10 +40,10 @@ func (m *MotorDriver) adaptor() PwmDigitalWriter {
|
||||
}
|
||||
|
||||
// Start starts the MotorDriver. Returns true on successful start of the driver
|
||||
func (m *MotorDriver) Start() error { return nil }
|
||||
func (m *MotorDriver) Start() (errs []error) { return }
|
||||
|
||||
// Halt halts the MotorDriver. Returns true on successful halt of the driver
|
||||
func (m *MotorDriver) Halt() error { return nil }
|
||||
func (m *MotorDriver) Halt() (errs []error) { return }
|
||||
|
||||
// Off turns the motor off or sets the motor to a 0 speed
|
||||
func (m *MotorDriver) Off() (err error) {
|
||||
|
@ -12,12 +12,12 @@ func initTestMotorDriver() *MotorDriver {
|
||||
|
||||
func TestMotorDriverStart(t *testing.T) {
|
||||
d := initTestMotorDriver()
|
||||
gobot.Assert(t, d.Start(), nil)
|
||||
gobot.Assert(t, len(d.Start()), 0)
|
||||
}
|
||||
|
||||
func TestMotorDriverHalt(t *testing.T) {
|
||||
d := initTestMotorDriver()
|
||||
gobot.Assert(t, d.Halt(), nil)
|
||||
gobot.Assert(t, len(d.Halt()), 0)
|
||||
}
|
||||
|
||||
func TestMotorDriverIsOn(t *testing.T) {
|
||||
|
@ -55,10 +55,10 @@ func (s *ServoDriver) adaptor() Servo {
|
||||
}
|
||||
|
||||
// Start starts the ServoDriver. Returns true on successful start of the driver.
|
||||
func (s *ServoDriver) Start() error { return nil }
|
||||
func (s *ServoDriver) Start() (errs []error) { return }
|
||||
|
||||
// Halt halts the ServoDriver. Returns true on successful halt of the driver.
|
||||
func (s *ServoDriver) Halt() error { return nil }
|
||||
func (s *ServoDriver) Halt() (errs []error) { return }
|
||||
|
||||
// InitServo initializes the ServoDriver on platforms which require an explicit initialization.
|
||||
func (s *ServoDriver) InitServo() (err error) {
|
||||
|
@ -12,12 +12,12 @@ func initTestServoDriver() *ServoDriver {
|
||||
|
||||
func TestServoDriverStart(t *testing.T) {
|
||||
d := initTestServoDriver()
|
||||
gobot.Assert(t, d.Start(), nil)
|
||||
gobot.Assert(t, len(d.Start()), 0)
|
||||
}
|
||||
|
||||
func TestServoDriverHalt(t *testing.T) {
|
||||
d := initTestServoDriver()
|
||||
gobot.Assert(t, d.Halt(), nil)
|
||||
gobot.Assert(t, len(d.Halt()), 0)
|
||||
}
|
||||
|
||||
func TestServoDriverMove(t *testing.T) {
|
||||
|
@ -17,8 +17,8 @@ func (t *gpioTestAdaptor) AnalogRead(string) (val int, err error) {
|
||||
func (t *gpioTestAdaptor) DigitalRead(string) (val int, err error) {
|
||||
return 1, nil
|
||||
}
|
||||
func (t *gpioTestAdaptor) Connect() error { return nil }
|
||||
func (t *gpioTestAdaptor) Finalize() error { return nil }
|
||||
func (t *gpioTestAdaptor) Connect() (errs []error) { return }
|
||||
func (t *gpioTestAdaptor) Finalize() (errs []error) { return }
|
||||
|
||||
func newGpioTestAdaptor(name string) *gpioTestAdaptor {
|
||||
return &gpioTestAdaptor{
|
||||
|
@ -58,18 +58,18 @@ func (b *BlinkMDriver) adaptor() I2cInterface {
|
||||
}
|
||||
|
||||
// Start writes start bytes
|
||||
func (b *BlinkMDriver) Start() (err error) {
|
||||
if err = b.adaptor().I2cStart(0x09); err != nil {
|
||||
return
|
||||
func (b *BlinkMDriver) Start() (errs []error) {
|
||||
if err := b.adaptor().I2cStart(0x09); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
if err = b.adaptor().I2cWrite([]byte("o")); err != nil {
|
||||
return
|
||||
if err := b.adaptor().I2cWrite([]byte("o")); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Halt returns true if device is halted successfully
|
||||
func (b *BlinkMDriver) Halt() error { return nil }
|
||||
func (b *BlinkMDriver) Halt() (errs []error) { return }
|
||||
|
||||
// Rgb sets color using r,g,b params
|
||||
func (b *BlinkMDriver) Rgb(red byte, green byte, blue byte) (err error) {
|
||||
|
@ -91,12 +91,12 @@ func TestNewBlinkMDriverCommands_Color(t *testing.T) {
|
||||
func TestBlinkMDriverStart(t *testing.T) {
|
||||
blinkM := initTestBlinkMDriver()
|
||||
|
||||
gobot.Assert(t, blinkM.Start(), nil)
|
||||
gobot.Assert(t, len(blinkM.Start()), 0)
|
||||
}
|
||||
|
||||
func TestBlinkMDriverHalt(t *testing.T) {
|
||||
blinkM := initTestBlinkMDriver()
|
||||
gobot.Assert(t, blinkM.Halt(), nil)
|
||||
gobot.Assert(t, len(blinkM.Halt()), 0)
|
||||
}
|
||||
|
||||
func TestBlinkMDriverFirmwareVersion(t *testing.T) {
|
||||
|
@ -30,13 +30,19 @@ func (h *HMC6352Driver) adaptor() I2cInterface {
|
||||
|
||||
// Start writes initialization bytes and reads from adaptor
|
||||
// using specified interval to update Heading
|
||||
func (h *HMC6352Driver) Start() (err error) {
|
||||
if err = h.adaptor().I2cStart(0x21); err != nil {
|
||||
return
|
||||
func (h *HMC6352Driver) Start() (errs []error) {
|
||||
if err := h.adaptor().I2cStart(0x21); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
return h.adaptor().I2cWrite([]byte("A"))
|
||||
if err := h.adaptor().I2cWrite([]byte("A")); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Halt returns true if devices is halted successfully
|
||||
func (h *HMC6352Driver) Halt() (errs []error) { return }
|
||||
|
||||
// Heading returns the current heading
|
||||
func (h *HMC6352Driver) Heading() (heading uint16, err error) {
|
||||
if err = h.adaptor().I2cWrite([]byte("A")); err != nil {
|
||||
@ -54,6 +60,3 @@ func (h *HMC6352Driver) Heading() (heading uint16, err error) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Halt returns true if devices is halted successfully
|
||||
func (h *HMC6352Driver) Halt() error { return nil }
|
||||
|
@ -51,7 +51,7 @@ func TestHMC6352DriverStart(t *testing.T) {
|
||||
numberOfCyclesForEvery := 3
|
||||
|
||||
hmc.SetInterval(1 * time.Millisecond)
|
||||
gobot.Assert(t, hmc.Start(), nil)
|
||||
gobot.Assert(t, len(hmc.Start()), 0)
|
||||
go func() {
|
||||
for {
|
||||
<-time.After(time.Duration(numberOfCyclesForEvery) * time.Millisecond)
|
||||
@ -76,7 +76,7 @@ func TestHMC6352DriverStart(t *testing.T) {
|
||||
}
|
||||
|
||||
hmc.SetInterval(1 * time.Millisecond)
|
||||
gobot.Assert(t, hmc.Start(), nil)
|
||||
gobot.Assert(t, len(hmc.Start()), 0)
|
||||
go func() {
|
||||
for {
|
||||
<-time.After(time.Duration(numberOfCyclesForEvery) * time.Millisecond)
|
||||
@ -97,5 +97,5 @@ func TestHMC6352DriverStart(t *testing.T) {
|
||||
func TestHMC6352DriverHalt(t *testing.T) {
|
||||
hmc := initTestHMC6352Driver()
|
||||
|
||||
gobot.Assert(t, hmc.Halt(), nil)
|
||||
gobot.Assert(t, len(hmc.Halt()), 0)
|
||||
}
|
||||
|
@ -54,12 +54,12 @@ func (h *MPL115A2Driver) adaptor() I2cInterface {
|
||||
|
||||
// Start writes initialization bytes and reads from adaptor
|
||||
// using specified interval to accelerometer andtemperature data
|
||||
func (h *MPL115A2Driver) Start() (err error) {
|
||||
func (h *MPL115A2Driver) Start() (errs []error) {
|
||||
var temperature uint16
|
||||
var pressure uint16
|
||||
var pressureComp float32
|
||||
|
||||
if err = h.initialization(); err != nil {
|
||||
if err := h.initialization(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@ -70,7 +70,7 @@ func (h *MPL115A2Driver) Start() (err error) {
|
||||
}
|
||||
<-time.After(5 * time.Millisecond)
|
||||
|
||||
if err = h.adaptor().I2cWrite([]byte{MPL115A2_REGISTER_PRESSURE_MSB}); err != nil {
|
||||
if err := h.adaptor().I2cWrite([]byte{MPL115A2_REGISTER_PRESSURE_MSB}); err != nil {
|
||||
gobot.Publish(h.Event("error"), err)
|
||||
return
|
||||
}
|
||||
@ -93,11 +93,11 @@ func (h *MPL115A2Driver) Start() (err error) {
|
||||
h.Temperature = ((float32(temperature) - 498.0) / -5.35) + 25.0
|
||||
}
|
||||
})
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Halt returns true if devices is halted successfully
|
||||
func (h *MPL115A2Driver) Halt() error { return nil }
|
||||
func (h *MPL115A2Driver) Halt() (err []error) { return }
|
||||
|
||||
func (h *MPL115A2Driver) initialization() (err error) {
|
||||
var coA0 int16
|
||||
|
@ -41,11 +41,11 @@ func TestNewMPL115A2Driver(t *testing.T) {
|
||||
func TestMPL115A2DriverStart(t *testing.T) {
|
||||
mpl := initTestMPL115A2Driver()
|
||||
|
||||
gobot.Assert(t, mpl.Start(), nil)
|
||||
gobot.Assert(t, len(mpl.Start()), 0)
|
||||
}
|
||||
|
||||
func TestMPL115A2DriverHalt(t *testing.T) {
|
||||
mpl := initTestMPL115A2Driver()
|
||||
|
||||
gobot.Assert(t, mpl.Halt(), nil)
|
||||
gobot.Assert(t, len(mpl.Halt()), 0)
|
||||
}
|
||||
|
@ -56,9 +56,9 @@ func (h *MPU6050Driver) adaptor() I2cInterface {
|
||||
|
||||
// Start writes initialization bytes and reads from adaptor
|
||||
// using specified interval to accelerometer andtemperature data
|
||||
func (h *MPU6050Driver) Start() (err error) {
|
||||
if err = h.initialize(); err != nil {
|
||||
return
|
||||
func (h *MPU6050Driver) Start() (errs []error) {
|
||||
if err := h.initialize(); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
|
||||
gobot.Every(h.Interval(), func() {
|
||||
@ -77,11 +77,11 @@ func (h *MPU6050Driver) Start() (err error) {
|
||||
binary.Read(buf, binary.BigEndian, &h.Gyroscope)
|
||||
binary.Read(buf, binary.BigEndian, &h.Temperature)
|
||||
})
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Halt returns true if devices is halted successfully
|
||||
func (h *MPU6050Driver) Halt() error { return nil }
|
||||
func (h *MPU6050Driver) Halt() (errs []error) { return }
|
||||
|
||||
func (h *MPU6050Driver) initialize() (err error) {
|
||||
if err = h.adaptor().I2cStart(0x68); err != nil {
|
||||
|
@ -41,11 +41,11 @@ func TestNewMPU6050Driver(t *testing.T) {
|
||||
func TestMPU6050DriverStart(t *testing.T) {
|
||||
mpu := initTestMPU6050Driver()
|
||||
|
||||
gobot.Assert(t, mpu.Start(), nil)
|
||||
gobot.Assert(t, len(mpu.Start()), 0)
|
||||
}
|
||||
|
||||
func TestMPU6050DriverHalt(t *testing.T) {
|
||||
mpu := initTestMPU6050Driver()
|
||||
|
||||
gobot.Assert(t, mpu.Halt(), nil)
|
||||
gobot.Assert(t, len(mpu.Halt()), 0)
|
||||
}
|
||||
|
@ -28,8 +28,8 @@ func (t *i2cTestAdaptor) I2cRead(uint) (data []byte, err error) {
|
||||
return t.i2cReadImpl(), nil
|
||||
}
|
||||
func (t *i2cTestAdaptor) I2cWrite([]byte) (err error) { return nil }
|
||||
func (t *i2cTestAdaptor) Connect() error { return nil }
|
||||
func (t *i2cTestAdaptor) Finalize() error { return nil }
|
||||
func (t *i2cTestAdaptor) Connect() (errs []error) { return }
|
||||
func (t *i2cTestAdaptor) Finalize() (errs []error) { return }
|
||||
|
||||
func newI2cTestAdaptor(name string) *i2cTestAdaptor {
|
||||
return &i2cTestAdaptor{
|
||||
|
@ -52,16 +52,16 @@ func (w *WiichuckDriver) adaptor() I2cInterface {
|
||||
|
||||
// Start initilizes i2c and reads from adaptor
|
||||
// using specified interval to update with new value
|
||||
func (w *WiichuckDriver) Start() (err error) {
|
||||
if err = w.adaptor().I2cStart(0x52); err != nil {
|
||||
return
|
||||
func (w *WiichuckDriver) Start() (errs []error) {
|
||||
if err := w.adaptor().I2cStart(0x52); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
gobot.Every(w.Interval(), func() {
|
||||
if err := w.adaptor().I2cWrite([]byte{0x40, 0x00}); err != nil {
|
||||
gobot.Publish(w.Event("error"), err)
|
||||
return
|
||||
}
|
||||
if err = w.adaptor().I2cWrite([]byte{0x00}); err != nil {
|
||||
if err := w.adaptor().I2cWrite([]byte{0x00}); err != nil {
|
||||
gobot.Publish(w.Event("error"), err)
|
||||
return
|
||||
}
|
||||
@ -77,11 +77,11 @@ func (w *WiichuckDriver) Start() (err error) {
|
||||
}
|
||||
}
|
||||
})
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Halt returns true if driver is halted successfully
|
||||
func (w *WiichuckDriver) Halt() error { return nil }
|
||||
func (w *WiichuckDriver) Halt() (errs []error) { return }
|
||||
|
||||
// update parses value to update buttons and joystick.
|
||||
// If value is encrypted, warning message is printed
|
||||
|
@ -48,7 +48,7 @@ func TestWiichuckDriverStart(t *testing.T) {
|
||||
numberOfCyclesForEvery := 3
|
||||
|
||||
wii.SetInterval(1 * time.Millisecond)
|
||||
gobot.Assert(t, wii.Start(), nil)
|
||||
gobot.Assert(t, len(wii.Start()), 0)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
@ -71,7 +71,7 @@ func TestWiichuckDriverStart(t *testing.T) {
|
||||
func TestWiichuckDriverHalt(t *testing.T) {
|
||||
wii := initTestWiichuckDriver()
|
||||
|
||||
gobot.Assert(t, wii.Halt(), nil)
|
||||
gobot.Assert(t, len(wii.Halt()), 0)
|
||||
}
|
||||
|
||||
func TestWiichuckDriverUpdate(t *testing.T) {
|
||||
|
@ -253,30 +253,43 @@ func NewEdisonAdaptor(name string) *EdisonAdaptor {
|
||||
|
||||
// Connect starts conection with board and creates
|
||||
// digitalPins and pwmPins adaptor maps
|
||||
func (e *EdisonAdaptor) Connect() error {
|
||||
func (e *EdisonAdaptor) Connect() (errs []error) {
|
||||
e.digitalPins = make(map[int]sysfs.DigitalPin)
|
||||
e.pwmPins = make(map[int]*pwmPin)
|
||||
return e.connect(e)
|
||||
if err := e.connect(e); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Finalize closes connection to board and pins
|
||||
func (e *EdisonAdaptor) Finalize() error {
|
||||
e.tristate.Unexport()
|
||||
func (e *EdisonAdaptor) Finalize() (errs []error) {
|
||||
if err := e.tristate.Unexport(); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
for _, pin := range e.digitalPins {
|
||||
if pin != nil {
|
||||
pin.Unexport()
|
||||
if err := pin.Unexport(); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, pin := range e.pwmPins {
|
||||
if pin != nil {
|
||||
pin.enable("0")
|
||||
pin.unexport()
|
||||
if err := pin.enable("0"); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
if err := pin.unexport(); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if e.i2cDevice != nil {
|
||||
e.i2cDevice.Close()
|
||||
if err := e.i2cDevice.Close(); errs != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return errs
|
||||
}
|
||||
|
||||
// digitalPin returns matched digitalPin for specified values
|
||||
|
@ -86,7 +86,7 @@ func initTestEdisonAdaptor() (*EdisonAdaptor, *sysfs.MockFilesystem) {
|
||||
|
||||
func TestEdisonAdaptorConnect(t *testing.T) {
|
||||
a, _ := initTestEdisonAdaptor()
|
||||
gobot.Assert(t, a.Connect(), nil)
|
||||
gobot.Assert(t, len(a.Connect()), 0)
|
||||
}
|
||||
|
||||
func TestEdisonAdaptorFinalize(t *testing.T) {
|
||||
@ -94,7 +94,7 @@ func TestEdisonAdaptorFinalize(t *testing.T) {
|
||||
a.DigitalWrite("3", 1)
|
||||
a.PwmWrite("5", 100)
|
||||
a.i2cDevice = new(gobot.NullReadWriteCloser)
|
||||
gobot.Assert(t, a.Finalize(), nil)
|
||||
gobot.Assert(t, len(a.Finalize()), 0)
|
||||
}
|
||||
|
||||
func TestEdisonAdaptorDigitalIO(t *testing.T) {
|
||||
|
@ -40,12 +40,15 @@ func NewJoystickAdaptor(name string) *JoystickAdaptor {
|
||||
}
|
||||
|
||||
// Connect returns true if connection to device is succesfull
|
||||
func (j *JoystickAdaptor) Connect() error {
|
||||
return j.connect(j)
|
||||
func (j *JoystickAdaptor) Connect() (errs []error) {
|
||||
if err := j.connect(j); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Finalize closes connection to device
|
||||
func (j *JoystickAdaptor) Finalize() error {
|
||||
func (j *JoystickAdaptor) Finalize() (errs []error) {
|
||||
j.joystick.Close()
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
@ -18,14 +18,14 @@ func initTestJoystickAdaptor() *JoystickAdaptor {
|
||||
|
||||
func TestJoystickAdaptorConnect(t *testing.T) {
|
||||
a := initTestJoystickAdaptor()
|
||||
gobot.Assert(t, a.Connect(), nil)
|
||||
gobot.Assert(t, len(a.Connect()), 0)
|
||||
|
||||
a = NewJoystickAdaptor("bot")
|
||||
gobot.Assert(t, a.Connect(), errors.New("No joystick available"))
|
||||
gobot.Assert(t, a.Connect()[0], errors.New("No joystick available"))
|
||||
}
|
||||
|
||||
func TestJoystickAdaptorFinalize(t *testing.T) {
|
||||
a := initTestJoystickAdaptor()
|
||||
a.Connect()
|
||||
gobot.Assert(t, a.Finalize(), nil)
|
||||
gobot.Assert(t, len(a.Finalize()), 0)
|
||||
}
|
||||
|
@ -70,10 +70,10 @@ func (j *JoystickDriver) adaptor() *JoystickAdaptor {
|
||||
}
|
||||
|
||||
// Start initiallizes event polling with defined interval
|
||||
func (j *JoystickDriver) Start() (err error) {
|
||||
func (j *JoystickDriver) Start() (errs []error) {
|
||||
file, err := ioutil.ReadFile(j.configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
return []error{err}
|
||||
}
|
||||
|
||||
var jsontype joystickConfig
|
||||
@ -102,9 +102,12 @@ func (j *JoystickDriver) Start() (err error) {
|
||||
<-time.After(j.Interval())
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Halt stops joystick driver
|
||||
func (j *JoystickDriver) Halt() (errs []error) { return }
|
||||
|
||||
// HandleEvent publishes an specific event according to data received
|
||||
func (j *JoystickDriver) handleEvent(event sdl.Event) error {
|
||||
switch data := event.(type) {
|
||||
@ -143,9 +146,6 @@ func (j *JoystickDriver) handleEvent(event sdl.Event) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Halt stops joystick driver
|
||||
func (j *JoystickDriver) Halt() error { return nil }
|
||||
|
||||
func (j *JoystickDriver) findName(id uint8, list []pair) string {
|
||||
for _, value := range list {
|
||||
if int(id) == value.ID {
|
||||
|
@ -25,13 +25,13 @@ func initTestJoystickDriver() *JoystickDriver {
|
||||
func TestJoystickDriverStart(t *testing.T) {
|
||||
d := initTestJoystickDriver()
|
||||
d.SetInterval(1 * time.Millisecond)
|
||||
gobot.Assert(t, d.Start(), nil)
|
||||
gobot.Assert(t, len(d.Start()), 0)
|
||||
<-time.After(2 * time.Millisecond)
|
||||
}
|
||||
|
||||
func TestJoystickDriverHalt(t *testing.T) {
|
||||
d := initTestJoystickDriver()
|
||||
gobot.Assert(t, d.Halt(), nil)
|
||||
gobot.Assert(t, len(d.Halt()), 0)
|
||||
}
|
||||
|
||||
func TestJoystickDriverHandleEvent(t *testing.T) {
|
||||
|
@ -39,9 +39,12 @@ func NewLeapMotionAdaptor(name string, port string) *LeapMotionAdaptor {
|
||||
}
|
||||
|
||||
// Connect returns true if connection to leap motion is established succesfully
|
||||
func (l *LeapMotionAdaptor) Connect() error {
|
||||
return l.connect(l)
|
||||
func (l *LeapMotionAdaptor) Connect() (errs []error) {
|
||||
if err := l.connect(l); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Finalize ends connection to leap motion
|
||||
func (l *LeapMotionAdaptor) Finalize() error { return nil }
|
||||
func (l *LeapMotionAdaptor) Finalize() (errs []error) { return }
|
||||
|
@ -13,10 +13,10 @@ func initTestLeapMotionAdaptor() *LeapMotionAdaptor {
|
||||
|
||||
func TestLeapMotionAdaptorConnect(t *testing.T) {
|
||||
a := initTestLeapMotionAdaptor()
|
||||
gobot.Assert(t, a.Connect(), nil)
|
||||
gobot.Assert(t, len(a.Connect()), 0)
|
||||
}
|
||||
|
||||
func TestLeapMotionAdaptorFinalize(t *testing.T) {
|
||||
a := initTestLeapMotionAdaptor()
|
||||
gobot.Assert(t, a.Finalize(), nil)
|
||||
gobot.Assert(t, len(a.Finalize()), 0)
|
||||
}
|
||||
|
@ -47,15 +47,15 @@ func (l *LeapMotionDriver) adaptor() *LeapMotionAdaptor {
|
||||
//
|
||||
// Publishes the following events:
|
||||
// "message" - Emits Frame on new message received from Leap.
|
||||
func (l *LeapMotionDriver) Start() error {
|
||||
func (l *LeapMotionDriver) Start() (errs []error) {
|
||||
enableGestures := map[string]bool{"enableGestures": true}
|
||||
b, err := json.Marshal(enableGestures)
|
||||
if err != nil {
|
||||
return err
|
||||
return []error{err}
|
||||
}
|
||||
_, err = l.adaptor().ws.Write(b)
|
||||
if err != nil {
|
||||
return err
|
||||
return []error{err}
|
||||
}
|
||||
|
||||
go func() {
|
||||
@ -64,8 +64,8 @@ func (l *LeapMotionDriver) Start() error {
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Halt returns true if driver is halted succesfully
|
||||
func (l *LeapMotionDriver) Halt() error { return nil }
|
||||
func (l *LeapMotionDriver) Halt() (errs []error) { return }
|
||||
|
@ -23,14 +23,13 @@ func initTestLeapMotionDriver() *LeapMotionDriver {
|
||||
}
|
||||
|
||||
func TestLeapMotionDriverStart(t *testing.T) {
|
||||
//t.SkipNow()
|
||||
d := initTestLeapMotionDriver()
|
||||
gobot.Assert(t, d.Start(), nil)
|
||||
gobot.Assert(t, len(d.Start()), 0)
|
||||
}
|
||||
|
||||
func TestLeapMotionDriverHalt(t *testing.T) {
|
||||
d := initTestLeapMotionDriver()
|
||||
gobot.Assert(t, d.Halt(), nil)
|
||||
gobot.Assert(t, len(d.Halt()), 0)
|
||||
}
|
||||
|
||||
func TestLeapMotionDriverParser(t *testing.T) {
|
||||
|
@ -35,11 +35,17 @@ func NewMavlinkAdaptor(name string, port string) *MavlinkAdaptor {
|
||||
}
|
||||
|
||||
// Connect returns true if connection to device is successful
|
||||
func (m *MavlinkAdaptor) Connect() error {
|
||||
return m.connect(m)
|
||||
func (m *MavlinkAdaptor) Connect() (errs []error) {
|
||||
if err := m.connect(m); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Finalize returns true if connection to devices is closed successfully
|
||||
func (m *MavlinkAdaptor) Finalize() error {
|
||||
return m.sp.Close()
|
||||
func (m *MavlinkAdaptor) Finalize() (errs []error) {
|
||||
if err := m.sp.Close(); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -15,10 +15,10 @@ func initTestMavlinkAdaptor() *MavlinkAdaptor {
|
||||
|
||||
func TestMavlinkAdaptorConnect(t *testing.T) {
|
||||
a := initTestMavlinkAdaptor()
|
||||
gobot.Assert(t, a.Connect(), nil)
|
||||
gobot.Assert(t, len(a.Connect()), 0)
|
||||
}
|
||||
|
||||
func TestMavlinkAdaptorFinalize(t *testing.T) {
|
||||
a := initTestMavlinkAdaptor()
|
||||
gobot.Assert(t, a.Finalize(), nil)
|
||||
gobot.Assert(t, len(a.Finalize()), 0)
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ func (m *MavlinkDriver) adaptor() *MavlinkAdaptor {
|
||||
|
||||
// Start begins process to read mavlink packets every m.Interval
|
||||
// and process them
|
||||
func (m *MavlinkDriver) Start() error {
|
||||
func (m *MavlinkDriver) Start() (errs []error) {
|
||||
go func() {
|
||||
for {
|
||||
packet, err := common.ReadMAVLinkPacket(m.adaptor().sp)
|
||||
@ -62,14 +62,14 @@ func (m *MavlinkDriver) Start() error {
|
||||
<-time.After(m.Interval())
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Halt returns true if device is halted successfully
|
||||
func (m *MavlinkDriver) Halt() (errs []error) { return }
|
||||
|
||||
// SendPacket sends a packet to mavlink device
|
||||
func (m *MavlinkDriver) SendPacket(packet *common.MAVLinkPacket) (err error) {
|
||||
_, err = m.adaptor().sp.Write(packet.Pack())
|
||||
return err
|
||||
}
|
||||
|
||||
// Halt returns true if device is halted successfully
|
||||
func (m *MavlinkDriver) Halt() error { return nil }
|
||||
|
@ -15,10 +15,10 @@ func initTestMavlinkDriver() *MavlinkDriver {
|
||||
|
||||
func TestMavlinkDriverStart(t *testing.T) {
|
||||
d := initTestMavlinkDriver()
|
||||
gobot.Assert(t, d.Start(), nil)
|
||||
gobot.Assert(t, len(d.Start()), 0)
|
||||
}
|
||||
|
||||
func TestMavlinkDriverHalt(t *testing.T) {
|
||||
d := initTestMavlinkDriver()
|
||||
gobot.Assert(t, d.Halt(), nil)
|
||||
gobot.Assert(t, len(d.Halt()), 0)
|
||||
}
|
||||
|
@ -27,25 +27,25 @@ func NewMqttAdaptor(name string, host string, clientID string) *MqttAdaptor {
|
||||
}
|
||||
|
||||
// Connect returns true if connection to mqtt is established
|
||||
func (a *MqttAdaptor) Connect() error {
|
||||
func (a *MqttAdaptor) Connect() (errs []error) {
|
||||
opts := createClientOptions(a.clientID, a.Host)
|
||||
a.client = mqtt.NewClient(opts)
|
||||
a.client.Start()
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Disconnect returns true if connection to mqtt is closed
|
||||
func (a *MqttAdaptor) Disconnect() error {
|
||||
func (a *MqttAdaptor) Disconnect() (err error) {
|
||||
if a.client != nil {
|
||||
a.client.Disconnect(500)
|
||||
}
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Finalize returns true if connection to mqtt is finalized succesfully
|
||||
func (a *MqttAdaptor) Finalize() error {
|
||||
func (a *MqttAdaptor) Finalize() (errs []error) {
|
||||
a.Disconnect()
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Publish a message under a specific topic
|
||||
|
@ -13,12 +13,12 @@ func initTestMqttAdaptor() *MqttAdaptor {
|
||||
|
||||
func TestMqttAdaptorConnect(t *testing.T) {
|
||||
a := initTestMqttAdaptor()
|
||||
gobot.Assert(t, a.Connect(), nil)
|
||||
gobot.Assert(t, len(a.Connect()), 0)
|
||||
}
|
||||
|
||||
func TestMqttAdaptorFinalize(t *testing.T) {
|
||||
a := initTestMqttAdaptor()
|
||||
gobot.Assert(t, a.Finalize(), nil)
|
||||
gobot.Assert(t, len(a.Finalize()), 0)
|
||||
}
|
||||
|
||||
func TestMqttAdaptorCannotPublishUnlessConnected(t *testing.T) {
|
||||
|
@ -35,11 +35,17 @@ func NewNeuroskyAdaptor(name string, port string) *NeuroskyAdaptor {
|
||||
}
|
||||
|
||||
// Connect returns true if connection to device is successful
|
||||
func (n *NeuroskyAdaptor) Connect() error {
|
||||
return n.connect(n)
|
||||
func (n *NeuroskyAdaptor) Connect() (errs []error) {
|
||||
if err := n.connect(n); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Finalize returns true if device finalization is successful
|
||||
func (n *NeuroskyAdaptor) Finalize() error {
|
||||
return n.sp.Close()
|
||||
func (n *NeuroskyAdaptor) Finalize() (errs []error) {
|
||||
if err := n.sp.Close(); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -17,11 +17,11 @@ func initTestNeuroskyAdaptor() *NeuroskyAdaptor {
|
||||
|
||||
func TestNeuroskyAdaptorConnect(t *testing.T) {
|
||||
a := initTestNeuroskyAdaptor()
|
||||
gobot.Assert(t, a.Connect(), nil)
|
||||
gobot.Assert(t, len(a.Connect()), 0)
|
||||
}
|
||||
|
||||
func TestNeuroskyAdaptorFinalize(t *testing.T) {
|
||||
a := initTestNeuroskyAdaptor()
|
||||
a.Connect()
|
||||
gobot.Assert(t, a.Finalize(), nil)
|
||||
gobot.Assert(t, len(a.Finalize()), 0)
|
||||
}
|
||||
|
@ -84,7 +84,7 @@ func (n *NeuroskyDriver) adaptor() *NeuroskyAdaptor {
|
||||
|
||||
// Start creates a go routine to listen from serial port
|
||||
// and parse buffer readings
|
||||
func (n *NeuroskyDriver) Start() (err error) {
|
||||
func (n *NeuroskyDriver) Start() (errs []error) {
|
||||
go func() {
|
||||
for {
|
||||
buff := make([]byte, 1024)
|
||||
@ -96,11 +96,11 @@ func (n *NeuroskyDriver) Start() (err error) {
|
||||
}
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Halt stops neurosky driver (void)
|
||||
func (n *NeuroskyDriver) Halt() error { return nil }
|
||||
func (n *NeuroskyDriver) Halt() (errs []error) { return }
|
||||
|
||||
// parse converts bytes buffer into packets until no more data is present
|
||||
func (n *NeuroskyDriver) parse(buf *bytes.Buffer) {
|
||||
|
@ -20,12 +20,12 @@ func initTestNeuroskyDriver() *NeuroskyDriver {
|
||||
|
||||
func TestNeuroskyDriverStart(t *testing.T) {
|
||||
d := initTestNeuroskyDriver()
|
||||
gobot.Assert(t, d.Start(), nil)
|
||||
gobot.Assert(t, len(d.Start()), 0)
|
||||
}
|
||||
|
||||
func TestNeuroskyDriverHalt(t *testing.T) {
|
||||
d := initTestNeuroskyDriver()
|
||||
gobot.Assert(t, d.Halt(), nil)
|
||||
gobot.Assert(t, len(d.Halt()), 0)
|
||||
}
|
||||
|
||||
func TestNeuroskyDriverParse(t *testing.T) {
|
||||
|
@ -45,9 +45,9 @@ func NewCameraDriver(name string, source interface{}) *CameraDriver {
|
||||
|
||||
// Start initializes camera by grabbing a frame
|
||||
// every `interval` and publishing an frame event
|
||||
func (c *CameraDriver) Start() (err error) {
|
||||
if err = c.start(c); err != nil {
|
||||
return err
|
||||
func (c *CameraDriver) Start() (errs []error) {
|
||||
if err := c.start(c); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
gobot.Every(c.Interval(), func() {
|
||||
if c.camera.GrabFrame() {
|
||||
@ -57,8 +57,8 @@ func (c *CameraDriver) Start() (err error) {
|
||||
}
|
||||
}
|
||||
})
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Halt stops camera driver
|
||||
func (c *CameraDriver) Halt() error { return nil }
|
||||
func (c *CameraDriver) Halt() (errs []error) { return }
|
||||
|
@ -19,7 +19,7 @@ func initTestCameraDriver() *CameraDriver {
|
||||
func TestCameraDriverStart(t *testing.T) {
|
||||
sem := make(chan bool)
|
||||
d := initTestCameraDriver()
|
||||
gobot.Assert(t, d.Start(), nil)
|
||||
gobot.Assert(t, len(d.Start()), 0)
|
||||
gobot.On(d.Event("frame"), func(data interface{}) {
|
||||
sem <- true
|
||||
})
|
||||
@ -32,15 +32,13 @@ func TestCameraDriverStart(t *testing.T) {
|
||||
}
|
||||
func TestCameraDriver(t *testing.T) {
|
||||
d := NewCameraDriver("bot", "")
|
||||
err := d.Start()
|
||||
gobot.Assert(t, err, nil)
|
||||
gobot.Assert(t, len(d.Start()), 0)
|
||||
|
||||
d = NewCameraDriver("bot", true)
|
||||
err = d.Start()
|
||||
gobot.Refute(t, err, nil)
|
||||
gobot.Refute(t, len(d.Start()), 0)
|
||||
}
|
||||
|
||||
func TestCameraDriverHalt(t *testing.T) {
|
||||
d := initTestCameraDriver()
|
||||
gobot.Assert(t, d.Halt(), nil)
|
||||
gobot.Assert(t, len(d.Halt()), 0)
|
||||
}
|
||||
|
@ -28,14 +28,14 @@ func NewWindowDriver(name string) *WindowDriver {
|
||||
}
|
||||
|
||||
// Start starts window thread and driver
|
||||
func (w *WindowDriver) Start() error {
|
||||
func (w *WindowDriver) Start() (errs []error) {
|
||||
cv.StartWindowThread()
|
||||
w.start(w)
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Halt returns true if camera is halted successfully
|
||||
func (w *WindowDriver) Halt() error { return nil }
|
||||
func (w *WindowDriver) Halt() (errs []error) { return }
|
||||
|
||||
// ShowImage displays image in window
|
||||
func (w *WindowDriver) ShowImage(image *cv.IplImage) {
|
||||
|
@ -19,12 +19,12 @@ func initTestWindowDriver() *WindowDriver {
|
||||
|
||||
func TestWindowDriverStart(t *testing.T) {
|
||||
d := initTestWindowDriver()
|
||||
gobot.Assert(t, d.Start(), nil)
|
||||
gobot.Assert(t, len(d.Start()), 0)
|
||||
}
|
||||
|
||||
func TestWindowDriverHalt(t *testing.T) {
|
||||
d := initTestWindowDriver()
|
||||
gobot.Assert(t, d.Halt(), nil)
|
||||
gobot.Assert(t, len(d.Halt()), 0)
|
||||
}
|
||||
|
||||
func TestWindowDriverShowImage(t *testing.T) {
|
||||
|
@ -21,11 +21,11 @@ func NewPebbleAdaptor(name string) *PebbleAdaptor {
|
||||
}
|
||||
|
||||
// Connect returns true if connection to pebble is established succesfully
|
||||
func (a *PebbleAdaptor) Connect() error {
|
||||
return nil
|
||||
func (a *PebbleAdaptor) Connect() (errs []error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Finalize returns true if connection to pebble is finalized succesfully
|
||||
func (a *PebbleAdaptor) Finalize() error {
|
||||
return nil
|
||||
func (a *PebbleAdaptor) Finalize() (errs []error) {
|
||||
return
|
||||
}
|
||||
|
@ -11,10 +11,10 @@ func initTestPebbleAdaptor() *PebbleAdaptor {
|
||||
|
||||
func TestPebbleAdaptorConnect(t *testing.T) {
|
||||
a := initTestPebbleAdaptor()
|
||||
gobot.Assert(t, a.Connect(), nil)
|
||||
gobot.Assert(t, len(a.Connect()), 0)
|
||||
}
|
||||
|
||||
func TestPebbleAdaptorFinalize(t *testing.T) {
|
||||
a := initTestPebbleAdaptor()
|
||||
gobot.Assert(t, a.Finalize(), nil)
|
||||
gobot.Assert(t, len(a.Finalize()), 0)
|
||||
}
|
||||
|
@ -52,10 +52,10 @@ func NewPebbleDriver(adaptor *PebbleAdaptor, name string) *PebbleDriver {
|
||||
}
|
||||
|
||||
// Start returns true if driver is initialized correctly
|
||||
func (d *PebbleDriver) Start() error { return nil }
|
||||
func (d *PebbleDriver) Start() (errs []error) { return }
|
||||
|
||||
// Halt returns true if driver is halted succesfully
|
||||
func (d *PebbleDriver) Halt() error { return nil }
|
||||
func (d *PebbleDriver) Halt() (errs []error) { return }
|
||||
|
||||
// PublishEvent publishes event with specified name and data in gobot
|
||||
func (d *PebbleDriver) PublishEvent(name string, data string) {
|
||||
|
@ -13,12 +13,12 @@ func initTestPebbleDriver() *PebbleDriver {
|
||||
|
||||
func TestPebbleDriverStart(t *testing.T) {
|
||||
d := initTestPebbleDriver()
|
||||
gobot.Assert(t, d.Start(), nil)
|
||||
gobot.Assert(t, len(d.Start()), 0)
|
||||
}
|
||||
|
||||
func TestPebbleDriverHalt(t *testing.T) {
|
||||
d := initTestPebbleDriver()
|
||||
gobot.Assert(t, d.Halt(), nil)
|
||||
gobot.Assert(t, len(d.Halt()), 0)
|
||||
}
|
||||
|
||||
func TestPebbleDriver(t *testing.T) {
|
||||
|
@ -157,21 +157,25 @@ func NewRaspiAdaptor(name string) *RaspiAdaptor {
|
||||
|
||||
// Connect starts conection with board and creates
|
||||
// digitalPins and pwmPins adaptor maps
|
||||
func (r *RaspiAdaptor) Connect() error {
|
||||
return nil
|
||||
func (r *RaspiAdaptor) Connect() (errs []error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Finalize closes connection to board and pins
|
||||
func (r *RaspiAdaptor) Finalize() error {
|
||||
func (r *RaspiAdaptor) Finalize() (errs []error) {
|
||||
for _, pin := range r.digitalPins {
|
||||
if pin != nil {
|
||||
pin.Unexport()
|
||||
if err := pin.Unexport(); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if r.i2cDevice != nil {
|
||||
r.i2cDevice.Close()
|
||||
if err := r.i2cDevice.Close(); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return errs
|
||||
}
|
||||
|
||||
// digitalPin returns matched digitalPin for specified values
|
||||
|
@ -18,9 +18,15 @@ func initTestRaspiAdaptor() *RaspiAdaptor {
|
||||
|
||||
func TestRaspiAdaptorFinalize(t *testing.T) {
|
||||
a := initTestRaspiAdaptor()
|
||||
fs := sysfs.NewMockFilesystem([]string{
|
||||
"/sys/class/gpio/export",
|
||||
"/sys/class/gpio/unexport",
|
||||
})
|
||||
|
||||
sysfs.SetFilesystem(fs)
|
||||
a.DigitalWrite("3", 1)
|
||||
a.i2cDevice = new(gobot.NullReadWriteCloser)
|
||||
gobot.Assert(t, a.Finalize(), nil)
|
||||
gobot.Assert(t, len(a.Finalize()), 0)
|
||||
}
|
||||
|
||||
func TestRaspiAdaptorDigitalIO(t *testing.T) {
|
||||
|
@ -35,15 +35,15 @@ func NewSparkCoreAdaptor(name string, deviceID string, accessToken string) *Spar
|
||||
}
|
||||
|
||||
// Connect returns true if connection to spark core is succesfull
|
||||
func (s *SparkCoreAdaptor) Connect() error {
|
||||
func (s *SparkCoreAdaptor) Connect() (errs []error) {
|
||||
s.SetConnected(true)
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Finalize returns true if connection to spark core is finalized successfully
|
||||
func (s *SparkCoreAdaptor) Finalize() error {
|
||||
func (s *SparkCoreAdaptor) Finalize() (errs []error) {
|
||||
s.SetConnected(false)
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// AnalogRead reads analog ping value using spark cloud api
|
||||
|
@ -78,11 +78,11 @@ func TestNewSparkCoreAdaptor(t *testing.T) {
|
||||
|
||||
func TestSparkCoreAdaptorConnect(t *testing.T) {
|
||||
a := initTestSparkCoreAdaptor()
|
||||
gobot.Assert(t, a.Connect(), nil)
|
||||
gobot.Assert(t, len(a.Connect()), 0)
|
||||
|
||||
a.SetConnected(false)
|
||||
|
||||
gobot.Assert(t, a.Connect(), nil)
|
||||
gobot.Assert(t, len(a.Connect()), 0)
|
||||
gobot.Assert(t, a.Connected(), true)
|
||||
}
|
||||
|
||||
@ -91,7 +91,7 @@ func TestSparkCoreAdaptorFinalize(t *testing.T) {
|
||||
|
||||
a.Connect()
|
||||
|
||||
gobot.Assert(t, a.Finalize(), nil)
|
||||
gobot.Assert(t, len(a.Finalize()), 0)
|
||||
gobot.Assert(t, a.Connected(), false)
|
||||
}
|
||||
|
||||
|
@ -37,9 +37,9 @@ func NewSpheroAdaptor(name string, port string) *SpheroAdaptor {
|
||||
}
|
||||
|
||||
// Connect initiates a connection to the Sphero. Returns true on successful connection.
|
||||
func (a *SpheroAdaptor) Connect() (err error) {
|
||||
if err = a.connect(a); err != nil {
|
||||
return
|
||||
func (a *SpheroAdaptor) Connect() (errs []error) {
|
||||
if err := a.connect(a); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
a.SetConnected(true)
|
||||
return
|
||||
@ -48,7 +48,7 @@ func (a *SpheroAdaptor) Connect() (err error) {
|
||||
// Reconnect attempts to reconnect to the Sphero. If the Sphero has an active connection
|
||||
// it will first close that connection and then establish a new connection.
|
||||
// Returns true on Successful reconnection
|
||||
func (a *SpheroAdaptor) Reconnect() (err error) {
|
||||
func (a *SpheroAdaptor) Reconnect() (errs []error) {
|
||||
if a.Connected() == true {
|
||||
a.Disconnect()
|
||||
}
|
||||
@ -56,15 +56,15 @@ func (a *SpheroAdaptor) Reconnect() (err error) {
|
||||
}
|
||||
|
||||
// Disconnect terminates the connection to the Sphero. Returns true on successful disconnect.
|
||||
func (a *SpheroAdaptor) Disconnect() (err error) {
|
||||
if err = a.sp.Close(); err != nil {
|
||||
return
|
||||
func (a *SpheroAdaptor) Disconnect() (errs []error) {
|
||||
if err := a.sp.Close(); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
a.SetConnected(false)
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Finalize finalizes the SpheroAdaptor
|
||||
func (a *SpheroAdaptor) Finalize() error {
|
||||
return nil
|
||||
func (a *SpheroAdaptor) Finalize() (errs []error) {
|
||||
return
|
||||
}
|
||||
|
@ -14,9 +14,9 @@ func initTestSpheroAdaptor() *SpheroAdaptor {
|
||||
|
||||
func TestSpheroAdaptorFinalize(t *testing.T) {
|
||||
a := initTestSpheroAdaptor()
|
||||
gobot.Assert(t, a.Finalize(), nil)
|
||||
gobot.Assert(t, len(a.Finalize()), 0)
|
||||
}
|
||||
func TestSpheroAdaptorConnect(t *testing.T) {
|
||||
a := initTestSpheroAdaptor()
|
||||
gobot.Assert(t, a.Connect(), nil)
|
||||
gobot.Assert(t, len(a.Connect()), 0)
|
||||
}
|
||||
|
@ -115,11 +115,11 @@ func (s *SpheroDriver) adaptor() *SpheroAdaptor {
|
||||
//
|
||||
// Emits the Events:
|
||||
// "collision" SpheroDriver.Collision - On Collision Detected
|
||||
func (s *SpheroDriver) Start() (err error) {
|
||||
func (s *SpheroDriver) Start() (errs []error) {
|
||||
go func() {
|
||||
for {
|
||||
packet := <-s.packetChannel
|
||||
err = s.write(packet)
|
||||
err := s.write(packet)
|
||||
if err != nil {
|
||||
gobot.Publish(s.Event("error"), err)
|
||||
}
|
||||
@ -169,17 +169,17 @@ func (s *SpheroDriver) Start() (err error) {
|
||||
s.configureCollisionDetection()
|
||||
s.enableStopOnDisconnect()
|
||||
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Halt halts the SpheroDriver and sends a SpheroDriver.Stop command to the Sphero.
|
||||
// Returns true on successful halt.
|
||||
func (s *SpheroDriver) Halt() error {
|
||||
func (s *SpheroDriver) Halt() (errs []error) {
|
||||
gobot.Every(10*time.Millisecond, func() {
|
||||
s.Stop()
|
||||
})
|
||||
time.Sleep(1 * time.Second)
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// SetRGB sets the Sphero to the given r, g, and b values
|
||||
|
@ -14,12 +14,12 @@ func initTestSpheroDriver() *SpheroDriver {
|
||||
|
||||
func TestSpheroDriverStart(t *testing.T) {
|
||||
d := initTestSpheroDriver()
|
||||
gobot.Assert(t, d.Start(), nil)
|
||||
gobot.Assert(t, len(d.Start()), 0)
|
||||
}
|
||||
|
||||
func TestSpheroDriverHalt(t *testing.T) {
|
||||
d := initTestSpheroDriver()
|
||||
gobot.Assert(t, d.Halt(), nil)
|
||||
gobot.Assert(t, len(d.Halt()), 0)
|
||||
}
|
||||
|
||||
func TestCalculateChecksum(t *testing.T) {
|
||||
|
21
robot.go
21
robot.go
@ -1,6 +1,7 @@
|
||||
package gobot
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
@ -33,10 +34,12 @@ func (r *robots) Len() int {
|
||||
|
||||
// Start initialises the event loop. All robots that were added will
|
||||
// be automtically started as a result of this call.
|
||||
func (r *robots) Start() (err error) {
|
||||
func (r *robots) Start() (errs []error) {
|
||||
for _, robot := range *r {
|
||||
err = robot.Start()
|
||||
if err != nil {
|
||||
if errs = robot.Start(); len(errs) > 0 {
|
||||
for i, err := range errs {
|
||||
errs[i] = errors.New(fmt.Sprintf("Robot %q: %v", robot.Name, err))
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -109,19 +112,21 @@ func (r *Robot) Command(name string) func(map[string]interface{}) interface{} {
|
||||
// Start a robot instance and runs it's work function if any. You should not
|
||||
// need to manually start a robot if already part of a Gobot application as the
|
||||
// robot will be automatically started for you.
|
||||
func (r *Robot) Start() (err error) {
|
||||
func (r *Robot) Start() (errs []error) {
|
||||
log.Println("Starting Robot", r.Name, "...")
|
||||
if err = r.Connections().Start(); err != nil {
|
||||
if cerrs := r.Connections().Start(); len(cerrs) > 0 {
|
||||
errs = append(errs, cerrs...)
|
||||
return
|
||||
}
|
||||
if err = r.Devices().Start(); err != nil {
|
||||
return err
|
||||
if derrs := r.Devices().Start(); len(derrs) > 0 {
|
||||
errs = append(errs, derrs...)
|
||||
return
|
||||
}
|
||||
if r.Work != nil {
|
||||
log.Println("Starting work...")
|
||||
r.Work()
|
||||
}
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// Devices retrieves all devices associated with this robot.
|
||||
|
@ -62,8 +62,8 @@ type testDriver struct {
|
||||
Driver
|
||||
}
|
||||
|
||||
func (t *testDriver) Start() error { return nil }
|
||||
func (t *testDriver) Halt() error { return nil }
|
||||
func (t *testDriver) Start() (errs []error) { return }
|
||||
func (t *testDriver) Halt() (errs []error) { return }
|
||||
|
||||
func NewTestDriver(name string, adaptor *testAdaptor) *testDriver {
|
||||
t := &testDriver{
|
||||
@ -93,8 +93,8 @@ type testAdaptor struct {
|
||||
Adaptor
|
||||
}
|
||||
|
||||
func (t *testAdaptor) Finalize() error { return nil }
|
||||
func (t *testAdaptor) Connect() error { return nil }
|
||||
func (t *testAdaptor) Finalize() (errs []error) { return }
|
||||
func (t *testAdaptor) Connect() (errs []error) { return }
|
||||
|
||||
func NewTestAdaptor(name string) *testAdaptor {
|
||||
return &testAdaptor{
|
||||
@ -131,8 +131,8 @@ type loopbackAdaptor struct {
|
||||
Adaptor
|
||||
}
|
||||
|
||||
func (t *loopbackAdaptor) Finalize() error { return nil }
|
||||
func (t *loopbackAdaptor) Connect() error { return nil }
|
||||
func (t *loopbackAdaptor) Finalize() (errs []error) { return }
|
||||
func (t *loopbackAdaptor) Connect() (errs []error) { return }
|
||||
|
||||
func NewLoopbackAdaptor(name string) *loopbackAdaptor {
|
||||
return &loopbackAdaptor{
|
||||
@ -147,8 +147,8 @@ type pingDriver struct {
|
||||
Driver
|
||||
}
|
||||
|
||||
func (t *pingDriver) Start() error { return nil }
|
||||
func (t *pingDriver) Halt() error { return nil }
|
||||
func (t *pingDriver) Start() (errs []error) { return }
|
||||
func (t *pingDriver) Halt() (errs []error) { return }
|
||||
|
||||
func NewPingDriver(adaptor *loopbackAdaptor, name string) *pingDriver {
|
||||
t := &pingDriver{
|
||||
|
Loading…
x
Reference in New Issue
Block a user