From 76e95f4bce67fe07d71961ac1ff42c9e1b3e34e6 Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Mon, 17 Nov 2014 16:25:01 -0800 Subject: [PATCH] Update digispark package --- platforms/digispark/digispark_adaptor.go | 63 +++++++++++++------ platforms/digispark/digispark_adaptor_test.go | 24 ++++--- platforms/digispark/littleWire.go | 47 +++++++++----- 3 files changed, 91 insertions(+), 43 deletions(-) diff --git a/platforms/digispark/digispark_adaptor.go b/platforms/digispark/digispark_adaptor.go index 844284d8..ebde8694 100644 --- a/platforms/digispark/digispark_adaptor.go +++ b/platforms/digispark/digispark_adaptor.go @@ -1,6 +1,8 @@ package digispark import ( + "errors" + "fmt" "strconv" "github.com/hybridgroup/gobot" @@ -13,7 +15,7 @@ type DigisparkAdaptor struct { littleWire lw servo bool pwm bool - connect func(*DigisparkAdaptor) + connect func(*DigisparkAdaptor) (err error) } // NewDigisparkAdaptor create a Digispark adaptor with specified name @@ -23,52 +25,77 @@ func NewDigisparkAdaptor(name string) *DigisparkAdaptor { name, "DigisparkAdaptor", ), - connect: func(d *DigisparkAdaptor) { + connect: func(d *DigisparkAdaptor) (err error) { d.littleWire = littleWireConnect() + if d.littleWire.(*littleWire).lwHandle == nil { + return errors.New(fmt.Sprintf("Error connecting to %s", d.Name())) + } + return }, } } // Connect starts connection to digispark, returns true if successful func (d *DigisparkAdaptor) Connect() error { - d.connect(d) - return nil + return d.connect(d) } // Finalize returns true if finalization is successful func (d *DigisparkAdaptor) Finalize() error { return nil } // DigitalWrite writes level to specified pin using littlewire -func (d *DigisparkAdaptor) DigitalWrite(pin string, level byte) { - p, _ := strconv.Atoi(pin) +func (d *DigisparkAdaptor) DigitalWrite(pin string, level byte) (err error) { + p, err := strconv.Atoi(pin) - d.littleWire.pinMode(uint8(p), 0) - d.littleWire.digitalWrite(uint8(p), level) + if err != nil { + return + } + + err = d.littleWire.pinMode(uint8(p), 0) + if err != nil { + return + } + err = d.littleWire.digitalWrite(uint8(p), level) + return } // DigitalRead (not yet implemented) -func (d *DigisparkAdaptor) DigitalRead(pin string) int { - return -1 +func (d *DigisparkAdaptor) DigitalRead(pin string) (val int, err error) { + err = errors.New("DigitalRead is not yet implemented") + return } // PwmWrite updates pwm pin with sent value -func (d *DigisparkAdaptor) PwmWrite(pin string, value byte) { +func (d *DigisparkAdaptor) PwmWrite(pin string, value byte) (err error) { if d.pwm == false { - d.littleWire.pwmInit() - d.littleWire.pwmUpdatePrescaler(1) + err = d.littleWire.pwmInit() + if err != nil { + return err + } + err = d.littleWire.pwmUpdatePrescaler(1) + if err != nil { + return err + } d.pwm = true } - d.littleWire.pwmUpdateCompare(value, value) + err = d.littleWire.pwmUpdateCompare(value, value) + return } // InitServo (not yet implemented) -func (d *DigisparkAdaptor) InitServo() {} +func (d *DigisparkAdaptor) InitServo() (err error) { + return errors.New("InitServo is not yet implemented") +} // ServoWrite updates servo location with specified angle -func (d *DigisparkAdaptor) ServoWrite(pin string, angle uint8) { +func (d *DigisparkAdaptor) ServoWrite(pin string, angle uint8) (err error) { if d.servo == false { - d.littleWire.servoInit() + err = d.littleWire.servoInit() + if err != nil { + return err + } d.servo = true } - d.littleWire.servoUpdateLocation(angle, angle) + err = d.littleWire.servoUpdateLocation(angle, angle) + return } diff --git a/platforms/digispark/digispark_adaptor_test.go b/platforms/digispark/digispark_adaptor_test.go index bb0be753..12174b22 100644 --- a/platforms/digispark/digispark_adaptor_test.go +++ b/platforms/digispark/digispark_adaptor_test.go @@ -17,32 +17,38 @@ type mock struct { state uint8 } -func (l *mock) digitalWrite(pin uint8, state uint8) { +func (l *mock) digitalWrite(pin uint8, state uint8) error { l.pin = pin l.state = state + return l.error() } -func (l *mock) pinMode(pin uint8, mode uint8) { +func (l *mock) pinMode(pin uint8, mode uint8) error { l.pin = pin l.mode = mode + return l.error() } -func (l *mock) pwmInit() {} -func (l *mock) pwmStop() {} -func (l *mock) pwmUpdateCompare(channelA uint8, channelB uint8) { +func (l *mock) pwmInit() error { return l.error() } +func (l *mock) pwmStop() error { return l.error() } +func (l *mock) pwmUpdateCompare(channelA uint8, channelB uint8) error { l.pwmChannelA = channelA l.pwmChannelB = channelB + return l.error() } -func (l *mock) pwmUpdatePrescaler(value uint) { +func (l *mock) pwmUpdatePrescaler(value uint) error { l.pwmPrescalerValue = value + return l.error() } -func (l *mock) servoInit() {} -func (l *mock) servoUpdateLocation(locationA uint8, locationB uint8) { +func (l *mock) servoInit() error { return l.error() } +func (l *mock) servoUpdateLocation(locationA uint8, locationB uint8) error { l.locationA = locationA l.locationB = locationB + return l.error() } +func (l *mock) error() error { return nil } func initTestDigisparkAdaptor() *DigisparkAdaptor { a := NewDigisparkAdaptor("bot") - a.connect = func(a *DigisparkAdaptor) {} + a.connect = func(a *DigisparkAdaptor) (err error) { return nil } a.littleWire = new(mock) return a } diff --git a/platforms/digispark/littleWire.go b/platforms/digispark/littleWire.go index fab3d011..e77c7f5e 100644 --- a/platforms/digispark/littleWire.go +++ b/platforms/digispark/littleWire.go @@ -6,15 +6,18 @@ package digispark //typedef usb_dev_handle littleWire; import "C" +import "errors" + type lw interface { - digitalWrite(uint8, uint8) - pinMode(uint8, uint8) - pwmInit() - pwmStop() - pwmUpdateCompare(uint8, uint8) - pwmUpdatePrescaler(uint) - servoInit() - servoUpdateLocation(uint8, uint8) + digitalWrite(uint8, uint8) error + pinMode(uint8, uint8) error + pwmInit() error + pwmStop() error + pwmUpdateCompare(uint8, uint8) error + pwmUpdatePrescaler(uint) error + servoInit() error + servoUpdateLocation(uint8, uint8) error + error() error } type littleWire struct { @@ -27,34 +30,46 @@ func littleWireConnect() *littleWire { } } -func (l *littleWire) digitalWrite(pin uint8, state uint8) { +func (l *littleWire) digitalWrite(pin uint8, state uint8) error { C.digitalWrite(l.lwHandle, C.uchar(pin), C.uchar(state)) + return l.error() } -func (l *littleWire) pinMode(pin uint8, mode uint8) { +func (l *littleWire) pinMode(pin uint8, mode uint8) error { C.pinMode(l.lwHandle, C.uchar(pin), C.uchar(mode)) + return l.error() } -func (l *littleWire) pwmInit() { +func (l *littleWire) pwmInit() error { C.pwm_init(l.lwHandle) + return l.error() } -func (l *littleWire) pwmStop() { +func (l *littleWire) pwmStop() error { C.pwm_stop(l.lwHandle) + return l.error() } -func (l *littleWire) pwmUpdateCompare(channelA uint8, channelB uint8) { +func (l *littleWire) pwmUpdateCompare(channelA uint8, channelB uint8) error { C.pwm_updateCompare(l.lwHandle, C.uchar(channelA), C.uchar(channelB)) + return l.error() } -func (l *littleWire) pwmUpdatePrescaler(value uint) { +func (l *littleWire) pwmUpdatePrescaler(value uint) error { C.pwm_updatePrescaler(l.lwHandle, C.uint(value)) + return l.error() } -func (l *littleWire) servoInit() { +func (l *littleWire) servoInit() error { C.servo_init(l.lwHandle) + return l.error() } -func (l *littleWire) servoUpdateLocation(locationA uint8, locationB uint8) { +func (l *littleWire) servoUpdateLocation(locationA uint8, locationB uint8) error { C.servo_updateLocation(l.lwHandle, C.uchar(locationA), C.uchar(locationB)) + return l.error() +} + +func (l *littleWire) error() error { + return errors.New(C.GoString(C.littleWire_errorName())) }