brettmclean / pad4pi

Interrupt-based matrix keypad library for Raspberry Pi
GNU Lesser General Public License v3.0
51 stars 19 forks source link

Randomly stops responding to key presses #5

Closed CaptClaude closed 6 years ago

CaptClaude commented 6 years ago

Caveat: This may not have anything to do with your library (for which I owe you a beer), in which case sending me off in the right direction would fantastic. Background: I have an 6 x 5 grid switches (discrete microswitches) that are part of a game I am building. I'm still in the tinkering phase, but the goal is to cause patterns of Neopixels to light up as various switches are pressed. I am using Python 3 on a Raspberry Pi Zero W. Problem: I can press one switch at a time and get the number of the switch, but randomly the Pi0W will stop responding the key presses. The script is still running because control-C breaks out of it and resets the LEDs. Frequency of key pressing is every second or so, not faster. This happens after a random number of presses: sometimes 5, sometimes 65, sometimes 107. I removed all the Neopixel references and the behavior is identical. I have an LED that is triggered and reset (lambda) in the function that's called every time a key press interrupt happens. When the script stops responding, that LED does not trigger so I am thinking somehow an interrupt is not being generated. Why? No idea. Maybe I'm doing something wrong, but I'm using very similar keypad code in other projects without any trouble.
Code without Neopixels (pastebin) Code with Neopixels (pastebin) Edit: I pared the size of the matrix to 3x3 and gave up pressing keys at 450. I upped the matrix to 4x4 and got to 82 before I had the same issue.

brettmclean commented 6 years ago

Hello and thank you for your detailed explanation.

I'll admit I'm relatively novice when it comes to both working with electronics and writing Python. The only hardware I've tested this with is a basic 3x4 membrane matrix keypad on a Raspberry Pi B+. I may not be able to figure out your specific problem, but let's see.

pad4pi version

Which version of pad4pi are you using? 1.1.3 was released in the last month. If you're using 1.1.3, try installing 1.1.2 instead just in case there's a new bug in the latest version.

Infinite loop: use sleep statement instead of pass

This one's a long shot, but in your example code consider replacing

while True:
    pass

with

while True:
    time.sleep(1)

like I do in rpi_gpio_demo.py. I believe that using "pass" will cause the Python script to use 100% CPU, whereas "time.sleep" is a blocking call that gives it a rest. I'm guessing that this is not the actual problem - otherwise none of your button presses would register.

Debugging print statements

On your Pi Zero W, is it possible to modify the installed pad4pi code that's being executed? Try to find rpi_gpio.py on your device and add various print statements to the _onKeyPress function. When it eventually stops responding to key presses, is it at least reaching the beginning of _onKeyPress? If you can figure out which part of _onKeyPress isn't being reached, that would help narrow down what might be happening.

CaptClaude commented 6 years ago

I am long overdue in responding to your suggestions. I substituted a Raspberry Pi 3 for the RPi0W I had been using and the problem went away. For that reason I suspect that this was simply an issue of how many GPIO the processor can poll per unit time. The Pi 3 is faster than the Pi0W. Period, end of discussion. Since your library works well with a Pi0W and "normal" keypads, I can only assume that it's a processor issue and not a library issue. I ran into an issue with the Pi 3 when combining the keyboard code, Neopixel code and the Flask dynamic web framework in one Python script. The problems went away when I split the script into two running as different processes: one for just Flask and one for the keypad & Neopixels. They communicate by writing and reading a file. I think we can close this and chalk it up to limitations of the platform that won't go away until you or someone bare-metal codes a keypad library. Thanks for responding. This has been very educational for me.

brettmclean commented 6 years ago

I'm glad you were able to test this on a Raspberry Pi 3 and get it working. I will close this issue for now. If anyone else has ideas about how to improve pad4pi to work around these limitations, I'd happily accept contributions.

CaptClaude commented 6 years ago

I'm good with that. Thanks.