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 package raspi
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -174,7 +175,7 @@ func (r *RaspiAdaptor) Finalize() error {
} }
// digitalPin returns matched digitalPin for specified values // 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 var i int
if val, ok := pins[pin][r.revision]; ok { 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 { } else if val, ok := pins[pin]["*"]; ok {
i = val i = val
} else { } else {
panic("not valid pin") err = errors.New("Not a valid pin")
return
} }
if r.digitalPins[i] == nil { if r.digitalPins[i] == nil {
r.digitalPins[i] = sysfs.NewDigitalPin(i) 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 // DigitalRead reads digital value from pin
func (r *RaspiAdaptor) DigitalRead(pin string) (i int) { func (r *RaspiAdaptor) DigitalRead(pin string) (val int, err error) {
i, _ = r.digitalPin(pin, sysfs.IN).Read() sysfsPin, err := r.digitalPin(pin, sysfs.IN)
return if err != nil {
return
}
return sysfsPin.Read()
} }
// DigitalWrite writes digital value to specified pin // DigitalWrite writes digital value to specified pin
func (r *RaspiAdaptor) DigitalWrite(pin string, val byte) { func (r *RaspiAdaptor) DigitalWrite(pin string, val byte) (err error) {
r.digitalPin(pin, sysfs.OUT).Write(int(val)) sysfsPin, err := r.digitalPin(pin, sysfs.OUT)
if err != nil {
return err
}
return sysfsPin.Write(int(val))
} }
// PwmWrite Not Implemented // PwmWrite Not Implemented
func (r *RaspiAdaptor) PwmWrite(pin string, val byte) { func (r *RaspiAdaptor) PwmWrite(pin string, val byte) (err error) {
fmt.Println("PwmWrite Is Not Implemented") return errors.New("PwmWrite is not yet implemented.")
} }
// I2cStart starts a i2c device in specified address // 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") gobot.Assert(t, fs.Files["/sys/class/gpio/gpio4/value"].Contents, "1")
a.DigitalWrite("13", 1) a.DigitalWrite("13", 1)
i := a.DigitalRead("13") i, _ := a.DigitalRead("13")
gobot.Assert(t, i, 1) gobot.Assert(t, i, 1)
} }