from machine import Pin
import time
linhas = [
Pin(19, Pin.OUT),
Pin(21, Pin.OUT),
Pin(22, Pin.OUT),
Pin(23, Pin.OUT)
]
colunas = [
Pin(18, Pin.IN, Pin.PULL_DOWN),
Pin(5, Pin.IN, Pin.PULL_DOWN),
Pin(17, Pin.IN, Pin.PULL_DOWN),
Pin(16, Pin.IN, Pin.PULL_DOWN)
]
teclas = [
['E', '0', 'F', 'D'],
['7', '8', '9', 'C'],
['4', '5', '6', 'B'],
['1', '2', '3', 'A']
]
segmentos = [
Pin(25, Pin.OUT), # a
Pin(26, Pin.OUT), # b
Pin(27, Pin.OUT), # c
Pin(14, Pin.OUT), # d
Pin(12, Pin.OUT), # e
Pin(33, Pin.OUT), # f
Pin(32, Pin.OUT) # g
]
# Mapeamento dos números para os segmentos (comum cátodo)
caracteres = {
'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': [1, 0, 0, 1, 1, 1, 0],
'D': [0, 1, 1, 1, 1, 0, 1],
'E': [1, 0, 0, 1, 1, 1, 1],
'F': [1, 0, 0, 0, 1, 1, 1]
}
def mostrar_caractere(caractere):
if caractere in caracteres:
for i in range(7):
segmentos[i].value(caracteres[caractere][i])
else:
for seg in segmentos:
seg.value(0)
def ler_teclado():
for i in range(4):
for linha in linhas:
linha.value(0)
linhas[i].value(1)
for j in range(4):
if colunas[j].value() == 1:
time.sleep_ms(20)
if colunas[j].value() == 1:
linhas[i].value(0)
return teclas[i][j]
linhas[i].value(0)
return None
def apagar_display():
for seg in segmentos:
seg.value(0)
ultima_tecla = None
while True:
tecla = ler_teclado()
if tecla and tecla != ultima_tecla:
print("Tecla pressionada:", tecla)
if tecla in caracteres:
mostrar_caractere(tecla)
ultima_tecla = tecla
time.sleep_ms(20)