From 036541476b8cd297c3a193f65e4d14cfc5a26b3e Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Wed, 19 Nov 2014 12:19:43 -0800 Subject: [PATCH] Update raspi package for new gpio interface --- platforms/raspi/raspi_adaptor.go | 37 ++++++++++++++++++--------- platforms/raspi/raspi_adaptor_test.go | 2 +- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/platforms/raspi/raspi_adaptor.go b/platforms/raspi/raspi_adaptor.go index 83f54900..c56723fe 100644 --- a/platforms/raspi/raspi_adaptor.go +++ b/platforms/raspi/raspi_adaptor.go @@ -1,6 +1,7 @@ package raspi import ( + "errors" "fmt" "io" "io/ioutil" @@ -174,7 +175,7 @@ func (r *RaspiAdaptor) Finalize() error { } // digitalPin returns matched digitalPin for specified values -func (r *RaspiAdaptor) digitalPin(pin string, dir string) sysfs.DigitalPin { +func (r *RaspiAdaptor) digitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPin, err error) { var i int if val, ok := pins[pin][r.revision]; ok { @@ -182,33 +183,45 @@ func (r *RaspiAdaptor) digitalPin(pin string, dir string) sysfs.DigitalPin { } else if val, ok := pins[pin]["*"]; ok { i = val } else { - panic("not valid pin") + err = errors.New("Not a valid pin") + return } if r.digitalPins[i] == nil { r.digitalPins[i] = sysfs.NewDigitalPin(i) - r.digitalPins[i].Export() + if err = r.digitalPins[i].Export(); err != nil { + return + } } - r.digitalPins[i].Direction(dir) + if err = r.digitalPins[i].Direction(dir); err != nil { + return + } - return r.digitalPins[i] + return r.digitalPins[i], nil } // DigitalRead reads digital value from pin -func (r *RaspiAdaptor) DigitalRead(pin string) (i int) { - i, _ = r.digitalPin(pin, sysfs.IN).Read() - return +func (r *RaspiAdaptor) DigitalRead(pin string) (val int, err error) { + sysfsPin, err := r.digitalPin(pin, sysfs.IN) + if err != nil { + return + } + return sysfsPin.Read() } // DigitalWrite writes digital value to specified pin -func (r *RaspiAdaptor) DigitalWrite(pin string, val byte) { - r.digitalPin(pin, sysfs.OUT).Write(int(val)) +func (r *RaspiAdaptor) DigitalWrite(pin string, val byte) (err error) { + sysfsPin, err := r.digitalPin(pin, sysfs.OUT) + if err != nil { + return err + } + return sysfsPin.Write(int(val)) } // PwmWrite Not Implemented -func (r *RaspiAdaptor) PwmWrite(pin string, val byte) { - fmt.Println("PwmWrite Is Not Implemented") +func (r *RaspiAdaptor) PwmWrite(pin string, val byte) (err error) { + return errors.New("PwmWrite is not yet implemented.") } // I2cStart starts a i2c device in specified address diff --git a/platforms/raspi/raspi_adaptor_test.go b/platforms/raspi/raspi_adaptor_test.go index fbcaad7f..1a409c55 100644 --- a/platforms/raspi/raspi_adaptor_test.go +++ b/platforms/raspi/raspi_adaptor_test.go @@ -40,7 +40,7 @@ func TestRaspiAdaptorDigitalIO(t *testing.T) { gobot.Assert(t, fs.Files["/sys/class/gpio/gpio4/value"].Contents, "1") a.DigitalWrite("13", 1) - i := a.DigitalRead("13") + i, _ := a.DigitalRead("13") gobot.Assert(t, i, 1) }