From 68b34b8f4fe7aaf7c8bb140ba474d6eb0df413b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20GALLARD?= Date: Sat, 6 Aug 2016 19:11:26 +0200 Subject: [PATCH 1/2] Use constant paths --- gpio_linux.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gpio_linux.go b/gpio_linux.go index 7b4dac8..b2277dc 100644 --- a/gpio_linux.go +++ b/gpio_linux.go @@ -149,7 +149,7 @@ func read(path string) ([]byte, error) { // Close destroys the virtual files on the filesystem, unexporting the pin. func (p *pin) Close() error { - return writeFile(filepath.Join(gpiobase, "unexport"), "%d", p.number) + return writeFile(unexportPath, "%d", p.number) } // Mode retrieves the current mode of the pin. @@ -260,7 +260,7 @@ func expose(pin int) (string, error) { pinBase := filepath.Join(gpiobase, fmt.Sprintf("gpio%d", pin)) var err error if _, statErr := os.Stat(pinBase); os.IsNotExist(statErr) { - err = writeFile(filepath.Join(gpiobase, "export"), "%d", pin) + err = writeFile(exportPath, "%d", pin) } return pinBase, err } From fba7efaa348f6532db32fbc45e0d3e54424406eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20GALLARD?= Date: Sat, 6 Aug 2016 19:22:10 +0200 Subject: [PATCH 2/2] Patch for initial direction bug (in/out) --- gpio_linux.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/gpio_linux.go b/gpio_linux.go index b2277dc..4c70cfc 100644 --- a/gpio_linux.go +++ b/gpio_linux.go @@ -98,6 +98,7 @@ type pin struct { modePath string // the path to the /direction FD to avoid string joining each time edgePath string // the path to the /edge FD to avoid string joining each time valueFile *os.File // the file handle for the value file + currentMode Mode // the currentMode (in/out) / empty at start time callback IRQEvent // the callback function to call when an interrupt occurs initial bool // is this the initial epoll trigger? err error //the last error @@ -107,7 +108,7 @@ type pin struct { // It also sets the mode for the pin, making it ready for use. func OpenPin(n int, mode Mode) (Pin, error) { // export this pin to create the virtual files on the system - pinBase, err := expose(n) + pinBase, exposed, err := expose(n) if err != nil { return nil, err } @@ -122,6 +123,9 @@ func OpenPin(n int, mode Mode) (Pin, error) { valueFile: value, initial: true, } + if exposed { + p.currentMode = p.GetMode() + } if err := p.setMode(mode); err != nil { p.Close() return nil, err @@ -171,7 +175,8 @@ func (p *pin) GetMode() Mode { } func (p *pin) setMode(mode Mode) error { - if p.GetMode() != mode { + if p.currentMode != mode { + p.currentMode = mode return write([]byte(mode), p.modePath) } else { return nil @@ -256,13 +261,16 @@ func (p *pin) Err() error { return p.err } -func expose(pin int) (string, error) { +func expose(pin int) (string, bool, error) { + exposed := false pinBase := filepath.Join(gpiobase, fmt.Sprintf("gpio%d", pin)) var err error if _, statErr := os.Stat(pinBase); os.IsNotExist(statErr) { err = writeFile(exportPath, "%d", pin) + } else { + exposed = true } - return pinBase, err + return pinBase, exposed, err } func writeFile(path string, format string, args ...interface{}) error {