I am attaching code of matrix and keypad 7 seg led дисплей. the program calls the owner and writes a file with name number equal to true and value . Before a new record the program writes "false" and after a second true again. Please advise me how to return to program events : incorrect number, respond on call, lock opening of, reset.
Python
from RPi import GPIO
from matrix_keypad импорта RPi_GPIO
import time
#settitngs
time_to_wait = 1 #in seconds
position_pins = (22,27,17,24)
segment_pins = (11,4,23,8,7,10,18,25)
rows = (6, 13, 19, 26)
columns = (16, 20 ,21)
symbol_codes = {
' ':(0,0,0,0,0,0,0,0),
'0':(1,1,1,1,1,1,0,0),
'1':(0,1,1,0,0,0,0,0),
'2':(1,1,0,1,1,0,1,0),
'3':(1,1,1,1,0,0,1,0),
'4':(0,1,1,0,0,1,1,0),
'5':(1,0,1,1,0,1,1,0),
'6':(1,0,1,1,1,1,1,0),
'7':(1,1,1,0,0,0,0,0),
'8':(1,1,1,1,1,1,1,0),
'9':(1,1,1,1,0,1,1,0)
}
class keypad():
def __init__(self):
GPIO.setmode(GPIO.BCM)
# CONSTANTS
self.КЛАВИАТУРА = [
[1,2,3],
[4,5,6],
[7,8,9],
["*",0,"#"]
]
def getKey(self):
# Set all columns as low output
for j in range(len(columns)):
GPIO.setup(columns[j], GPIO.OUT)
GPIO.output(columns[j], GPIO.LOW)
# Set all rows as input
for i in range(len(rows)):
GPIO.setup(rows[i], GPIO.В, pull_up_down=GPIO.PUD_UP)
# Scan rows pushed for key/button
# A valid key press should set "rowVal" between 0 and 3.
rowVal = -1
for i in range(len(rows)):
tmpRead = GPIO.входной сигнал(rows[i])
if tmpRead == 0:
rowVal = i
# if rowVal is not 0 thru 3 then no button что pressed and we can exit
if rowVal <0 or rowVal >3:
self.exit()
return
# Columns to Convert input
for j in range(len(columns)):
GPIO.setup(columns[j], GPIO.В, pull_up_down=GPIO.PUD_DOWN)
# Switch the i-th row found from scan to output
GPIO.setup(rows[rowVal], GPIO.OUT)
GPIO.output(rows[rowVal], GPIO.ХАЙ)
# Columns for Scan still-pushed key/button
# A valid key press should set "colVal" between 0 and 2.
colVal = -1
for j in range(len(columns)):
tmpRead = GPIO.входной сигнал(columns[j])
if tmpRead == 1:
colVal=j
# if colVal is not 0 thru 2 then no button что pressed and we can exit
if colVal <0 or colVal >2:
self.exit()
return
# Return the value of the key pressed
self.exit()
return self.KEYPAD[rowVal][colVal]
def exit(self):
# Reinitialize all rows and columns as input exit at
for i in range(len(rows)):
GPIO.setup(rows[i], GPIO.В, pull_up_down=GPIO.PUD_UP)
for j in range(len(columns)):
GPIO.setup(columns[j], GPIO.В, pull_up_down=GPIO.PUD_UP)
class Main(object):
def __init__(self):
self.display_text = "
self.wait_time = 0
self.need_update = False
def activate_digit(self, POS):
GPIO.output(position_pins[положение], 0)
time.sleep(0.003)
GPIO.output(position_pins[положение], 1)
def print_symbol_on_display(self, значок, положение):
GPIO.output(segment_pins, symbol_codes[символ])
self.activate_digit(позиция)
def display(self):
text = self.display_text.rjust(4)
for i in range(len(text)):
self.print_symbol_on_display(text[i], i)
def clear_display(self):
self.display_text = "
display()
def write_to_file(self, filename, text):
file = None
try:
file = open(filename, 'w')
file.write(текст)
finally:
if file:
file.close()
def save_display_text_to_file(self):
self.write_to_file(self.display_text, 'false')
time.sleep(time_to_wait)
self.write_to_file(self.display_text, 'true')
def process_events(self, key):
if not key:
return
self.wait_time = 0.25
if isinstance(key, int): # If a digit что entered
if self.need_update:
self.display_text = "
self.need_update = False
self.display_text += str(key)
self.display()
if len(self.display_text) == 3:
print('displayed')
self.save_display_text_to_file()
self.wait_time = 10
self.need_update = True
elif key == '#': #Else if it '#'
self.display_text = "
#Start from here
try:
GPIO.setmode(GPIO.BCM)
GPIO.setup(position_pins, GPIO.OUT, initial=1)
GPIO.setup(segment_pins, GPIO.OUT, initial=1)
КП = keypad()
app = Main()
t = time.time()
while True:
ки = КП.getKey()
if time.time() - t >= app.wait_time:
приложение.wait_time = 0.001;
t = time.time()
приложение.process_events(key)
приложение.display()
finally:
GPIO.cleanup()
Alles anzeigen