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 # High (inactive)
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']
]
# -----------------------------------------
# Keypad scanning function (MISSING - NOW IMPLEMENTED)
# -----------------------------------------
def scan_keypad():
"""Scan 4x4 keypad and return pressed key or None"""
for r in range(4):
# Activate one row at a time (set to LOW)
row_pins[r].value = False
for c in range(4):
if not col_pins[c].value: # Key pressed (LOW due to pull-up)
# Deactivate row
row_pins[r].value = True
return keys[r][c]
# Reactivate row
row_pins[r].value = True
return None # No key pressed
# -----------------------------------------
# 7-Segment Character Map
# a,b,c,d,e,f,g (fixed '7' pattern)
# -----------------------------------------
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], # Fixed: was [1,1,0,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':[1,0,0,1,1,1,0],
'D':[0,1,1,1,1,0,1],
'-':[0,0,0,1,0,0,0], # Fixed: better dash pattern
'h':[0,0,1,1,1,1,1], # Fixed: better 'h' pattern
' ':[0,0,0,0,0,0,0]
}
# -----------------------------------------
# Segment pins
# -----------------------------------------
seg_pins = [
board.GP16, # a
board.GP17, # b
board.GP18, # c
board.GP19, # d
board.GP20, # e
board.GP21, # f
board.GP22 # g
]
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]
# -----------------------------------------
# Main loop
# -----------------------------------------
last_key = None
print("Keypad 7-Segment Display Ready!")
print("Press keys 0-9, A-D, *, #")
while True:
key = scan_keypad()
if key and key != last_key:
print("Key pressed:", key)
# ---- Numeric ----
if key.isdigit():
display(key)
# ---- A-D ----
elif key in ['A','B','C','D']:
display(key)
# ---- Special ----
elif key == '*':
display('-')
elif key == '#':
display('h')
last_key = key
if not key:
last_key = None # reset when released
time.sleep(0.05)Loading
pi-pico-w
pi-pico-w