1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00
This commit is contained in:
Adrian Zankich 2014-10-30 16:22:25 -07:00
parent a7cee22918
commit e78257c201
4 changed files with 12 additions and 8 deletions

View File

@ -21,7 +21,9 @@ type DigitalPin struct {
direction string direction string
} }
// NewDigitalPin returns a DigitalPin given the pin number and sysfs pin label // NewDigitalPin returns a DigitalPin given the pin number and an optional sysfs pin label.
// If no label is supplied the default label will prepend "gpio" to the pin number,
// eg. a pin number of 10 will have a label of "gpio10"
func NewDigitalPin(pin int, v ...string) *DigitalPin { func NewDigitalPin(pin int, v ...string) *DigitalPin {
d := &DigitalPin{pin: strconv.Itoa(pin)} d := &DigitalPin{pin: strconv.Itoa(pin)}
if len(v) > 0 { if len(v) > 0 {
@ -38,14 +40,14 @@ func (d *DigitalPin) Direction() string {
return d.direction return d.direction
} }
// SetDirection sets the current direction for specified pin // SetDirection sets the current direction for the pin
func (d *DigitalPin) SetDirection(dir string) error { func (d *DigitalPin) SetDirection(dir string) error {
d.direction = dir d.direction = dir
_, err := writeFile(fmt.Sprintf("%v/%v/direction", GPIOPATH, d.label), []byte(d.direction)) _, err := writeFile(fmt.Sprintf("%v/%v/direction", GPIOPATH, d.label), []byte(d.direction))
return err return err
} }
// Write writes specified value to the pin // Write writes to the pin
func (d *DigitalPin) Write(b int) error { func (d *DigitalPin) Write(b int) error {
_, err := writeFile(fmt.Sprintf("%v/%v/value", GPIOPATH, d.label), []byte(strconv.Itoa(b))) _, err := writeFile(fmt.Sprintf("%v/%v/value", GPIOPATH, d.label), []byte(strconv.Itoa(b)))
return err return err

View File

@ -40,10 +40,6 @@ func TestDigitalPin(t *testing.T) {
gobot.Assert(t, lastPath, "/sys/class/gpio/gpio10/value") gobot.Assert(t, lastPath, "/sys/class/gpio/gpio10/value")
gobot.Assert(t, string(lastData), "1") gobot.Assert(t, string(lastData), "1")
pin.Write(1)
gobot.Assert(t, lastPath, "/sys/class/gpio/gpio10/value")
gobot.Assert(t, string(lastData), "1")
pin.SetDirection(IN) pin.SetDirection(IN)
gobot.Assert(t, lastPath, "/sys/class/gpio/gpio10/direction") gobot.Assert(t, lastPath, "/sys/class/gpio/gpio10/direction")
gobot.Assert(t, string(lastData), "in") gobot.Assert(t, string(lastData), "in")

6
sysfs/doc.go Normal file
View File

@ -0,0 +1,6 @@
/*
Package sysfs provides generic access to linux gpio.
It is intended to be used while implementing support for a single board linux computer
*/
package sysfs

View File

@ -10,7 +10,7 @@ import (
const I2CSlave = 0x0703 const I2CSlave = 0x0703
/// NewI2cDevice creates a new i2c device given a device location and address // NewI2cDevice creates a new io.ReadWriteCloser with the proper ioctrl given an i2c bus location and device address
func NewI2cDevice(location string, address byte) (io.ReadWriteCloser, error) { func NewI2cDevice(location string, address byte) (io.ReadWriteCloser, error) {
file, err := os.OpenFile(location, os.O_RDWR, os.ModeExclusive) file, err := os.OpenFile(location, os.O_RDWR, os.ModeExclusive)