import machine
import utime
keypad_rows = [machine.Pin(13), machine.Pin(12), machine.Pin(11), machine.Pin(10)]
keypad_cols = [machine.Pin(9), machine.Pin(8), machine.Pin(7), machine.Pin(6)]
segments = [28, 27, 26, 22, 21, 20, 19, 0]
keypad_layout = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
segment_patterns = {
'0': [0, 0, 0, 0, 0, 0, 1, 1],
'1': [1, 0, 0, 1, 1, 1, 1, 1],
'2': [0, 0, 1, 0, 0, 1, 0, 1],
'3': [0, 0, 0, 0, 1, 1, 0, 1],
'4': [1, 0, 0, 1, 1, 0, 0, 1],
'5': [0, 1, 0, 0, 1, 0, 0, 1],
'6': [0, 1, 0, 0, 0, 0, 0, 1],
'7': [0, 0, 0, 1, 1, 1, 1, 1],
'8': [0, 0, 0, 0, 0, 0, 0, 1],
'9': [0, 0, 0, 1, 1, 0, 0, 1],
'A': [0, 0, 0, 1, 0, 0, 0, 1],
'B': [1, 1, 0, 0, 0, 0, 0, 1],
'C': [0, 1, 1, 0, 0, 0, 1, 1],
'D': [1, 0, 0, 0, 0, 1, 0, 1],
'*': [0, 0, 1, 1, 1, 0, 0, 1],
'#': [1, 1, 0, 0, 0, 1, 0, 1]
}
for row in keypad_rows:
row.init(machine.Pin.IN, pull=machine.Pin.PULL_UP)
for col in keypad_cols:
col.init(machine.Pin.OUT)
for pin in segments:
machine.Pin(pin, machine.Pin.OUT)
def display_character(char):
if char in segment_patterns:
pattern = segment_patterns[char]
for i, segment_pin in enumerate(segments):
machine.Pin(segment_pin, machine.Pin.OUT).value(pattern[i])
def scan_keypad():
key = None
for i in range(len(keypad_cols)):
keypad_cols[i].low()
for j in range(len(keypad_rows)):
if not keypad_rows[j].value():
key = keypad_layout[j][i]
keypad_cols[i].high()
return key
while True:
key_pressed = scan_keypad()
display_character(key_pressed)
utime.sleep(0.1)