import time
import board
import digitalio
# --------------------------
# Keypad setup (4x4)
# --------------------------
rows = [board.GP13, board.GP12, board.GP11, board.GP10]
cols = [board.GP9, board.GP8, board.GP7, board.GP6]
row_pins = []
col_pins = []
for r in rows:
pin = digitalio.DigitalInOut(r)
pin.direction = digitalio.Direction.OUTPUT
pin.value = True
row_pins.append(pin)
for c in cols:
pin = digitalio.DigitalInOut(c)
pin.direction = digitalio.Direction.INPUT
pin.pull = digitalio.Pull.UP
col_pins.append(pin)
# Key map
keys = [
['1','2','3','A'],
['4','5','6','B'],
['7','8','9','C'],
['*','0','#','D']
]
# --------------------------
# Segment patterns (a,b,c,d,e,f,g)
# --------------------------
segments = {
'0':[1,1,1,1,1,1,0],
'1':[0,1,1,0,0,0,0],
'2':[1,1,0,1,1,0,1],
'3':[1,1,1,1,0,0,1],
'4':[0,1,1,0,0,1,1],
'5':[1,0,1,1,0,1,1],
'6':[1,0,1,1,1,1,1],
'7':[1,1,1,0,0,0,0],
'8':[1,1,1,1,1,1,1],
'9':[1,1,1,1,0,1,1],
'A':[1,1,1,0,1,1,1],
'b':[0,0,1,1,1,1,1],
'c':[0,0,0,1,1,0,1],
'-':[0,0,0,0,0,0,1],
'H':[0,1,1,0,1,1,1],
' ':[0,0,0,0,0,0,0]
}
# --------------------------
# 7-segment pins
# --------------------------
seg_pins = [
board.GP16,
board.GP17,
board.GP18,
board.GP19,
board.GP20,
board.GP21,
board.GP22
]
seg_outputs = []
for p in seg_pins:
pin = digitalio.DigitalInOut(p)
pin.direction = digitalio.Direction.OUTPUT
seg_outputs.append(pin)
# --------------------------
# Display function
# --------------------------
def display(char):
pattern = segments.get(char, segments[' '])
for i in range(7):
seg_outputs[i].value = pattern[i]
# --------------------------
# Keypad scan function
# --------------------------
def scan_keypad():
for i, row in enumerate(row_pins):
row.value = False
for j, col in enumerate(col_pins):
if not col.value:
row.value = True
return keys[i][j]
row.value = True
return None
# --------------------------
# Main loop
# --------------------------
last_key = None
while True:
key = scan_keypad()
if key and key != last_key:
print("Key:", key)
if key.isdigit():
display(key)
elif key == 'A':
display('A')
elif key == 'B':
display('b')
elif key == 'C':
display('c') #jadi c kecik
elif key == '*':
display('-')
elif key == '#':
display('H') #jadi H besar
last_key = key
if not key:
last_key = None
time.sleep(0.05)