Skip to content

Commit

Permalink
Update Adaptor and Driver interface to use []error
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Nov 20, 2014
1 parent e187096 commit 586507a
Show file tree
Hide file tree
Showing 78 changed files with 380 additions and 293 deletions.
4 changes: 2 additions & 2 deletions adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 15 additions & 5 deletions connection.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gobot

import (
"errors"
"fmt"
"log"
)

Expand All @@ -27,25 +29,33 @@ 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()
if connection.Port() != "" {
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
}
}
return
}

// 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 changes: 15 additions & 9 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,33 @@ 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()
if device.Pin() != "" {
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
}
4 changes: 2 additions & 2 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 22 additions & 8 deletions gobot.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,40 @@ 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
}

// waiting for interrupt coming on the channel
_ = <-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.
Expand Down
11 changes: 7 additions & 4 deletions platforms/ardrone/ardrone_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions platforms/ardrone/ardrone_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 4 additions & 4 deletions platforms/ardrone/ardrone_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions platforms/ardrone/ardrone_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
30 changes: 17 additions & 13 deletions platforms/beaglebone/beaglebone_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,42 +144,46 @@ 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]

return
}

// 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
Expand Down
2 changes: 1 addition & 1 deletion platforms/beaglebone/beaglebone_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand Down
9 changes: 6 additions & 3 deletions platforms/digispark/digispark_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions platforms/digispark/digispark_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
25 changes: 14 additions & 11 deletions platforms/firmata/firmata_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions platforms/firmata/firmata_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 586507a

Please sign in to comment.