Update raspi package for new gpio interface

This commit is contained in:
Adrian Zankich 2014-11-19 12:19:43 -08:00
parent cd1a016b2b
commit 036541476b
2 changed files with 26 additions and 13 deletions

View File

@ -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

View File

@ -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)
}