from machine import Pin
from esp32_gpio_lcd import GpioLcd
from time import sleep
lcd = GpioLcd(rs_pin=Pin(4),
enable_pin=Pin(17),
d4_pin=Pin(5),
d5_pin=Pin(18),
d6_pin=Pin(21),
d7_pin=Pin(22),
num_lines=2, num_columns=20)
Key_up = const(0)
Key_down = const(1)
key = [['1', '2', '3', 'A'], ['4', '5', '6', 'B'], ['7', '8', '9', 'C'], ['*', '0', '#', 'D']]
# Dinh nghia chan
rows = [2,4,5,19]
colums = [12,27,26,25]
# Dinh nghia chan hang
pin_rows = [Pin(pin_name, mode=Pin.OUT) for pin_name in rows]
# dinh nghia chan cot
pin_colums = [Pin(pin_name, mode=Pin.IN, pull=Pin.PULL_DOWN) for pin_name in colums]
#khoi tao keypad
def initialization():
"""khoi tao"""
for row in range(0,4):
for col in range(0,4):
pin_rows[row].value(0)
def scan(row, column):
""" quet phim """
# dung cac phim len muc 1
pin_rows[row].value(1)
key = None
# xac thuc khi nhan phim
if pin_colums [column].value() == Key_down:
key = Key_down
if pin_colums [column].value() == Key_up:
key = Key_up
pin_rows [row].value(0)
return key
# dua cac phim ve muc 0
initialization()
lcd.putstr("An phim: ")
while True:
for row in range(4):
for column in range(4):
keypad = scan(row, column)
if keypad == Key_down:
lcd.putstr(str(key[row][column]))
last_key_press = key[row][column]
sleep(0.5)