brian-armstrong / gpio

Go library to do GPIO on systems with /sys/class/gpio (sysfs)
BSD 3-Clause "New" or "Revised" License
139 stars 50 forks source link

Issue with watcher on Pi Zero #1

Open curiositry opened 7 years ago

curiositry commented 7 years ago

Hello Brian,

I’m using the latest Raspbian image on Raspberry Pi Zero + Zero Ws, I can read a pin manually without issue, but I haven’t gotten any action out of the watcher. I have two questions:

  1. Has the library been tested on the Zero / Zero W before? If not, is there any reason to think it might not be compatible?

  2. I’m using BCM pin 7 (physical pin 26) as an input. As far as I understood, the kernel uses BCM numbering; pin := gpio.NewInput(7) works, so is there any reason why watcher.AddPin(7) wouldn’t?

Thanks for your help!

brian-armstrong commented 7 years ago

Hi @curiositry,

Sorry to hear it didn't work. I don't know if the Zero has been tried but can't think of any specific reason it wouldn't work.

Just to confirm, do you have a /sys/class/gpio directory?

curiositry commented 7 years ago

Thanks for responding @brian-armstrong — yes, /sys/class/gpio does exist.

brian-armstrong commented 7 years ago

@curiositry I can think of one thing, after rereading some of my code. It looks like the Watcher will not fail gracefuly if you Close it before calling Watch. If you did that, Watch would just hang forever.

Just to be sure, could you share the code that uses the Watcher, or make sure that it isn't somehow getting Closed prematurely?

curiositry commented 7 years ago

@brian-armstrong Thanks for your help. Here are the relevant portions (I was basically just testing it with what’s in the README):

pin := gpio.NewInput(7)
fmt.Println(pin.Read())
watcher := gpio.NewWatcher()
watcher.AddPin(7)
defer watcher.Close()

for {
    fmt.Println("Starting watcher.")
    pin, value := watcher.Watch()
    fmt.Print("Watcher value: ")
    fmt.Println(pin)
    fmt.Print(value)
}

It would hang at pin, value := watcher.Watch() without printing anything.

olb17 commented 6 years ago

Hi Brian

I had similar experience with go 1.9 and a raspberry pi model 1. I tracked the issue down to the way the fdset is built in the watcher : fdset.Bits[val/64] |= 1 << uint(val) % 64 is not working on my set-up.

and should be fixed as fdset.Bits[val/64] |= 1 << (uint(val) % 64)

https://play.golang.org/p/HyV6wX-aihG

I propose you a PR to fix this.

lsdev75 commented 5 years ago

Hello, I've go 1.9.7 and the code is correctly fixed as per last olb17 message, but after some acquisitions the Watcher stops to detect pin changes even if using 'cat' I can see the gpio change. I want to detect the falling edge so I put Falling instead of Both inside the watcher.