Skip to content
This repository was archived by the owner on Feb 18, 2020. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions gpio_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -122,6 +123,9 @@ func OpenPin(n int, mode Mode) (Pin, error) {
valueFile: value,
initial: true,
}
if exposed {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exposed is never false unless expose() fails

Copy link
Author

@mlgd mlgd Aug 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exposed is set to true if GPIO port is already opened :

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, exposed, err
}

p.currentMode = p.GetMode()
}
if err := p.setMode(mode); err != nil {
p.Close()
return nil, err
Expand Down Expand Up @@ -149,7 +153,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.
Expand All @@ -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
Expand Down Expand Up @@ -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(filepath.Join(gpiobase, "export"), "%d", pin)
err = writeFile(exportPath, "%d", pin)
} else {
exposed = true
}
return pinBase, err
return pinBase, exposed, err
}

func writeFile(path string, format string, args ...interface{}) error {
Expand Down