Skip to content

Commit

Permalink
sysfs: export DigitalPin to make it more like PWMPin
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Apr 28, 2017
1 parent e3f1a55 commit 7429a15
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
32 changes: 16 additions & 16 deletions sysfs/digital_pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,23 @@ const (
GPIOPATH = "/sys/class/gpio"
)

// DigitalPin is the interface for sysfs gpio interactions
type DigitalPin interface {
// Unexport unexports the pin and releases the pin from the operating system
Unexport() error
var errNotExported = errors.New("pin has not been exported")

// DigitalPinner is the interface for sysfs gpio interactions
type DigitalPinner interface {
// Export exports the pin for use by the operating system
Export() error
// Read reads the current value of the pin
Read() (int, error)
// Unexport unexports the pin and releases the pin from the operating system
Unexport() error
// Direction sets the direction for the pin
Direction(string) error
// Read reads the current value of the pin
Read() (int, error)
// Write writes to the pin
Write(int) error
}

type digitalPin struct {
type DigitalPin struct {
pin string
label string

Expand All @@ -47,8 +49,8 @@ type digitalPin struct {
// 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 {
d := &digitalPin{pin: strconv.Itoa(pin)}
func NewDigitalPin(pin int, v ...string) *DigitalPin {
d := &DigitalPin{pin: strconv.Itoa(pin)}
if len(v) > 0 {
d.label = v[0]
} else {
Expand All @@ -58,27 +60,25 @@ func NewDigitalPin(pin int, v ...string) DigitalPin {
return d
}

var errNotExported = errors.New("pin has not been exported")

func (d *digitalPin) Direction(dir string) error {
func (d *DigitalPin) Direction(dir string) error {
_, err := writeFile(d.direction, []byte(dir))
return err
}

func (d *digitalPin) Write(b int) error {
func (d *DigitalPin) Write(b int) error {
_, err := writeFile(d.value, []byte(strconv.Itoa(b)))
return err
}

func (d *digitalPin) Read() (n int, err error) {
func (d *DigitalPin) Read() (n int, err error) {
buf, err := readFile(d.value)
if err != nil {
return 0, err
}
return strconv.Atoi(string(buf[0]))
}

func (d *digitalPin) Export() error {
func (d *DigitalPin) Export() error {
export, err := fs.OpenFile(GPIOPATH+"/export", os.O_WRONLY, 0644)
if err != nil {
return err
Expand Down Expand Up @@ -127,7 +127,7 @@ func (d *digitalPin) Export() error {
return err
}

func (d *digitalPin) Unexport() error {
func (d *DigitalPin) Unexport() error {
unexport, err := fs.OpenFile(GPIOPATH+"/unexport", os.O_WRONLY, 0644)
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions sysfs/digital_pin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func TestDigitalPin(t *testing.T) {

SetFilesystem(fs)

pin := NewDigitalPin(10, "custom").(*digitalPin)
pin := NewDigitalPin(10, "custom")
gobottest.Assert(t, pin.pin, "10")
gobottest.Assert(t, pin.label, "custom")

pin = NewDigitalPin(10).(*digitalPin)
pin = NewDigitalPin(10)
gobottest.Assert(t, pin.pin, "10")
gobottest.Assert(t, pin.label, "gpio10")
gobottest.Assert(t, pin.value, nil)
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestDigitalPinExportError(t *testing.T) {

SetFilesystem(fs)

pin := NewDigitalPin(10, "custom").(*digitalPin)
pin := NewDigitalPin(10, "custom")
writeFile = func(File, []byte) (int, error) {
return 0, &os.PathError{Err: syscall.EBUSY}
}
Expand All @@ -114,7 +114,7 @@ func TestDigitalPinUnexportError(t *testing.T) {

SetFilesystem(fs)

pin := NewDigitalPin(10, "custom").(*digitalPin)
pin := NewDigitalPin(10, "custom")
writeFile = func(File, []byte) (int, error) {
return 0, &os.PathError{Err: syscall.EBUSY}
}
Expand Down

0 comments on commit 7429a15

Please sign in to comment.