#!/usr/bin/python
import RPi.GPIO as GPIO
import wiringpi as htime
class keypad():
def __init__(self, columnCount=3):
GPIO.setmode(GPIO.BOARD)
# CONSTANTS
if columnCount is 3:
self.KEYPAD = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
["*", 0, "#"]
]
self.ROW = [26, 24, 23, 22]
self.COLUMN = [21, 19, 10]
elif columnCount is 4:
self.KEYPAD = [
[1, 2, 3, "A"],
[4, 5, 6, "B"],
[7, 8, 9, "C"],
["*", 0, "#", "D"]
]
self.COLUMN = [15, 13, 11, 7]
self.ROW = [35, 33, 31, 29]
else:
return
def getKey(self):
# Set all columns as output low
for j in range(len(self.COLUMN)):
GPIO.setup(self.COLUMN[j], GPIO.OUT)
GPIO.output(self.COLUMN[j], GPIO.LOW)
# Set all rows as input 输入引脚必须设置为上拉
for i in range(len(self.ROW)):
GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Scan rows for pushed key/button
# A valid key press should set "rowVal" between 0 and 3.
rowVal = -1
for i in range(len(self.ROW)):
tmpRead = GPIO.input(self.ROW[i])
if tmpRead == 0:
rowVal = i
# if rowVal is not 0 thru 3 then no button was pressed and we can exit
if rowVal < 0 or rowVal > 3:
self.exit()
return None
# Convert columns to input
for j in range(len(self.COLUMN)):
GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# Switch the i-th row found from scan to output
GPIO.setup(self.ROW[rowVal], GPIO.OUT)
GPIO.output(self.ROW[rowVal], GPIO.HIGH)
# Scan columns for still-pushed key/button
# A valid key press should set "colVal" between 0 and 2.
colVal = -1
for j in range(len(self.COLUMN)):
tmpRead = GPIO.input(self.COLUMN[j])
if tmpRead == 1:
colVal = j
# if colVal is not 0 thru 2 then no button was pressed and we can exit
if colVal < 0 or colVal > 3:
self.exit()
return None
# Return the value of the key pressed
self.exit()
return self.KEYPAD[rowVal][colVal]
def exit(self):
# Reinitialize all rows and columns as input at exit
for i in range(len(self.ROW)):
GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
for j in range(len(self.COLUMN)):
GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down=GPIO.PUD_UP)
if __name__ == '__main__':
# Initialize the keypad class
kp = keypad(columnCount=4)
# Loop while waiting for a keypress
digit = None
press=False
while True:
digit = kp.getKey()
if digit is not None:
htime.delay(15)
else:
htime.delay(15)
digit = kp.getKey()
if digit==None:
press=False
if press==True:
continue
digit = kp.getKey()
if digit is not None:
press=True
print(digit)
htime.delay(60)
# Print the result
print(digit)