from machine import Pin
import time
# =========================================================================
# 1. CONFIGURACIÓN DEL DIP SWITCH (sw1) - MAPEO SEGÚN TU JSON
# =========================================================================
# Entradas con Pull-Up interno. Al cerrar el switch enciende a GND (da 0).
dip_pins = [
Pin(13, Pin.IN, Pin.PULL_UP), # Bit 3 (MSB) -> Switch 1a
Pin(12, Pin.IN, Pin.PULL_UP), # Bit 2 -> Switch 2a
Pin(14, Pin.IN, Pin.PULL_UP), # Bit 1 -> Switch 3a
Pin(27, Pin.IN, Pin.PULL_UP) # Bit 0 (LSB) -> Switch 4a
]
# =========================================================================
# 2. CONFIGURACIÓN DE SEGMENTOS COMPARTIDOS (BUS PARALELO)
# =========================================================================
# Pines del JSON: [A, B, C, D, E, F, G]
PINS_SEGMENTOS = [23, 22, 21, 19, 18, 5, 17]
segmentos = [Pin(p, Pin.OUT) for p in PINS_SEGMENTOS]
# =========================================================================
# 3. PINES DE CONTROL DE LOS DISPLAYS (SEVSEG3, SEVSEG1, SEVSEG2)
# =========================================================================
# Cátodo Común: 0 = Encendido, 1 = Apagado
# PINS_CONTROL ordenados como: [Display Izquierdo, Display Centro, Display Derecho]
PINS_CONTROL = [16, 4, 2] # sevseg3 (Izq), sevseg1 (Centro), sevseg2 (Der)
control_displays = [Pin(p, Pin.OUT) for p in PINS_CONTROL]
# =========================================================================
# 4. DICCIONARIO DE CARACTERES (Cátodo Común: 1=ON, 0=OFF)
# =========================================================================
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],
'-': [0, 0, 0, 0, 0, 0, 1]
}
# =========================================================================
# 5. MATRIZ DE VENTANAS DESLIZANTES (TUS 16 ESTADOS EXACTOS)
# =========================================================================
# Cada índice mapea directamente: [Display_Izq, Display_Centro, Display_Der]
MATRIZ_ESTADOS = [
[1, 0, 9], # Estado 0 (0000)
[0, 9, 7], # Estado 1 (0001)
[9, 7, 1], # Estado 2 (0010)
[7, 1, 8], # Estado 3 (0011)
[1, 8, 5], # Estado 4 (0100)
[8, 5, 4], # Estado 5 (0101)
[5, 4, 8], # Estado 6 (0110)
[4, 8, 9], # Estado 7 (0111)
[8, 9, '-'], # Estado 8 (1000) -> Guion entra por la derecha
[9, '-', 1], # Estado 9 (1001) -> Guion al centro
['-', 1, 0], # Estado 10 (1010) -> Guion a la izquierda
[1, 0, 9], # Estado 11 (1011)
[0, 9, 7], # Estado 12 (1100)
[9, 7, 1], # Estado 13 (1101)
[7, 1, 0], # Estado 14 (1110)
[1, 0, 9] # Estado 15 (1111) -> Continuidad circular (igual al estado 0)
]
def leer_dip_switch():
"""Lee el valor binario del DIP switch y retorna su entero (0-15)"""
valor = 0
for pin in dip_pins:
# sw1 conecta a GND cuando está en ON, por eso se invierte la lectura
bit = 1 if not pin.value() else 0
valor = (valor << 1) | bit
return valor
def refrescar_pantalla(num_display, caracter):
"""Ejecuta la multiplexación apagando comunes y actualizando el bus de segmentos"""
# 1. Apagar todos los comunes para evitar efecto fantasma (Ghosting)
for ctrl in control_displays:
ctrl.value(1)
# 2. Cargar los bits correspondientes al caracter en el bus paralelo
patron = CARACTERES.get(caracter, CARACTERES['-'])
for pin_seg, estado in zip(segmentos, patron):
pin_seg.value(estado)
# 3. Encender únicamente el display actual (Cátodo se activa con 0)
control_displays[num_display].value(0)
# =========================================================================
# BUCLE PRINCIPAL (Lógica Combinacional Pura con Multiplexación TDM)
# =========================================================================
print("Firmware de Desplazamiento Circular de Cédula inicializado con éxito.")
print("Estudiante: Brayan Sierra")
while True:
# Leer el estado binario síncrono del DIP switch
estado_actual = leer_dip_switch()
# Extraer directamente la ventana de 3 caracteres de la matriz (Sin usar if-else masivos)
ventana = MATRIZ_ESTADOS[estado_actual]
# Ciclo de multiplexación por división de tiempo muy rápido (Persistencia retiniana)
refrescar_pantalla(0, ventana[0]) # sevseg3 (Izquierdo)
time.sleep_ms(4)
refrescar_pantalla(1, ventana[1]) # sevseg1 (Central)
time.sleep_ms(4)
refrescar_pantalla(2, ventana[2]) # sevseg2 (Derecho)
time.sleep_ms(4)