kidoman / embd

Embedded Programming Framework in Go
http://embd.kidoman.io
MIT License
1.28k stars 156 forks source link

Unexport digital pins on Close(). #44

Open wujiang opened 8 years ago

wujiang commented 8 years ago

Exported digital pins can be uninitialized.

How to reproduce the problem

package main

import (
        "fmt"

        "github.com/kidoman/embd"
        _ "github.com/wujiang/embd/host/rpi"
)

func main() {
        embd.InitGPIO()
        defer embd.CloseGPIO()
        fmt.Println(embd.SetDirection(12, embd.Out))
}
/tmp $ go build
/tmp $ ./tmp
open /sys/class/gpio/gpio12/direction: permission denied
/tmp $ ./tmp
write /sys/class/gpio/export: device or resource busy
/tmp $ ./tmp
write /sys/class/gpio/export: device or resource busy
/tmp $ ls /sys/class/gpio/
export  gpio12  gpiochip0  unexport

After the change:

/tmp $ ./tmp
open /sys/class/gpio/gpio12/direction: permission denied
/tmp $ ./tmp
open /sys/class/gpio/gpio12/direction: permission denied
/tmp $ ./tmp
open /sys/class/gpio/gpio12/direction: permission denied
jpillora commented 8 years ago

:+1: Just ran into this issue

tve commented 7 years ago

While I understand the problem, it seems to me that the unexporting should happen last after closing the exported files. So it seems that the Close() calls should be skipped on !initialized but the order of events shouldn't be changed.