brettmclean / pad4pi Goto Github PK
View Code? Open in Web Editor NEWInterrupt-based matrix keypad library for Raspberry Pi
License: GNU Lesser General Public License v3.0
Interrupt-based matrix keypad library for Raspberry Pi
License: GNU Lesser General Public License v3.0
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.
The problem arises in this piece of code here:
# Scan columns for pressed key
colVal = None
if rowVal is not None:
for i in range(len(self._col_pins)):
GPIO.output(self._col_pins[i], GPIO.HIGH)
if GPIO.input(self._row_pins[rowVal]) == GPIO.HIGH:
GPIO.output(self._col_pins[i], GPIO.LOW)
colVal = i
break
GPIO.output(self._col_pins[i], GPIO.LOW)
Suppose we have a 2x2 matrix, and someone pushes A1, connecting the first column and first row. All is well.
Now, they later press B1, which doesn't trigger a new row that wasn't already low, no interrupt, all is well.
Keeping those pressed, they press A2 or B2. getKey is triggered, and the first column is driven high for the test, but it is connected to the second column(which is a low output), and you get a short.
Then we do the same for the second column and get a short again. If the repeat timer is doing stuff, this happens over and over again.
Hi
I have found that if i enter numbers quickly (4x4 matrix keypad) it sometimes reports the wrong key pressed, (usually the 1st key in the row, but not always) i.e press key 3 rapidly and it reports a key of '1', sometimes 2, but never 'A'
if i press 'A' in quick succession i can often get 1, 2, or 3
if i press '2' in quick succession i get either 1 or 2, but never 3 or A
def printKey(key):
print(key) # wrong number here
i have tried multiple options with repeats and delays to no avail
any idea how to fix this please. sometimes waiting a few seconds between presses still reports the wrong key
thanks
Bob
The current implementation of Keypad.cleanup()
always calls GPIO.cleanup()
:
Lines 165 to 168 in 0e560a1
However, for some applications it may be necessary to still be able to use RPi.GPIO after keypad cleanup, for example if the keypad should be active only occasionally (i.e. instantiated and destroyed multiple number of times).
When using this keypad library simulatenously with any HD44780 LCD display library based on RPi.GPIO, currently it is necessary to perform keypad cleanup at the very end. Otherwise, the following error will appear when trying to clear the display with lcd.clear()
after keypad cleanup:
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)
According to RPi.GPIO documentation, GPIO.cleanup()
should be called at the end any program. This can be easily achieved by simply placing GPIO.cleanup()
at the end of the script separately or using exit handlers (atexit) manually by the library user.
For convenience and compability purposes, the suggested solution is to call GPIO.cleanup()
conditionally, depending on the value of a boolean parameter (enabled by default). Otherwise, GPIO.remove_event_detect()
should be called to unregister all interrupts. This will allow developers to configure the behaviour depending on the application.
Thanks for your code, I found it used here http://codelectron.com/how-to-make-a-soundboard-using-raspberry-pi-step-by-step-guide-memebox/ (code: https://github.com/codelectron/Soundboard ).
I noticed that sometimes a wrong key is decoded for one of my 2 x 4 keypads, see image. Each micropad with 4 keys has one "row" and four "columns". Each cable is about 70 cm long; perhaps the debouncing can be fine-tuned?
My layout:
# Setup Keypad
KEYPAD = [
["1","2","3","4"],
["5","6","7","8"]
]
COL_PINS = [11,23,24,25] # BCM numbering
ROW_PINS = [17,27] # BCM numbering
I try running the sample code and when I get to the line
keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS)
I get the error
RuntimeError: Conflicting edge detection already enabled for this GPIO channel
Other than changing the row and column pins to match my actual wiring, the code is identical to the sample provided with the library. What am I missing here?
`from pad4pi import rpi_gpio
#import pad4pi
#import RPi.GPIO as rpi_gpio
KEYPAD = [
[1,2,3],
[4,5,6],
[7,8,9],
["*",0,"#"]
]
ROW_PINS = [4,14,15,17] # BCM numbering
COL_PINS = [18,27,22] # BCM numbering
factory = rpi_gpio.KeypadFactory()
keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS)
def printKey(key):
print(key)
keypad.registerKeyPressHandler(printKey)`
o/pad4pi.py", line 1, in <module> from pad4pi import rpi_gpio ImportError: cannot import name 'rpi_gpio'
Keyboard factory also doesnt work
I am trying to get the key that i pressed(the value of the key) , but I get an error: "local variable 'Key' referenced before assignment " I am using from pad4pi import rpi_gpio,
keypadcode.txt
i want to get the Key from the printKey function and use it in the main function , how can i do this
I am trying to add more features and improve the performance of this library but I do not understand the repeat feature of your library. Could you explain a little bit?
I've been using Pad4pi on a 4x5 keypad with no problems for a while. recently I purchased a new 3x4 keypad. I modified my code but some some reason I am missing some of the buttons. I have checked the keypad using a power source and LED so can confirm the all the buttons work and are wired correctly. My code is below:
from pad4pi import rpi_gpio
import time
KEYPAD = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
["*", 0, "#"]
]
COL_PINS = [16, 5, 13] # BCM numbering 5 1 3
ROW_PINS = [19, 20, 21, 6] # BCM numbering 4 6 7 2
factory = rpi_gpio.KeypadFactory()
keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS)
def printKey(key):
print(key)
keypad.registerKeyPressHandler(printKey)
try:
while(True):
time.sleep(0.2)
except:
keypad.cleanup()
I am not getting a response from the keys; 1,5,6,7,*,0,#.
Have you any suggestions please?
I follwed the instructions The Interpreter ceeps giving me the same answer.
Import Error : No module named 'pad4pi'
Kann it be bequase it is no hocked up or wrongli hocked up?
or some other reason.
Client code can currently register handler functions to be called when a button is pressed on the keypad. Ideally, client code should also be able to unregister handler functions when they no longer want a certain action to be performed on key press.
Currently, the only way to do this is to call keypad.cleanup()
and then create a new Keypad
instance, which is inconvenient.
Hi, thanks for writing this nice code, it should help me getting the CPU usage of my pi down!
However, I am running in to an issue which I am finding hard to debug: (almost) every time I press a key it is registered as a double press. Is there anything I can do about that?
Thanks!
I’m not sure whether this is a problem with the library or the installation script or something else, but I have a couple of python programs using pad4pi that I want to run at boot time.
The software works perfectly if run from the command line, whether launched from its own directory or any other. However if I try to run at launch by putting the script in rc.local, nothing happens and I log the following error:
Traceback (most recent call last):
File "/home/pi/sound.py", line 4, in <module>
from pad4pi import rpi_gpio
ImportError: No module named pad4pi
Why would the library be available from the command line but not at launch? Can this be fixed?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.