from machine import Pin, SPI, ADC, PWM, I2C
import machine
import ssd1306
import time
#import String
#OLED
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
#Teclado Matricial 4x4
ROWS = [19, 18, 5, 17]
COLS = [32, 33, 25, 26]
keys = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
row_pins = [Pin(pin, Pin.OUT) for pin in ROWS]
col_pins = [Pin(pin, Pin.IN, Pin.PULL_DOWN) for pin in COLS]
#Led RGB
red_pin = machine.Pin(12, machine.Pin.OUT)
green_pin = machine.Pin(13, machine.Pin.OUT)
blue_pin = machine.Pin(27, machine.Pin.OUT)
#PushButton
buttonPin = Pin(34, Pin.IN, Pin.PULL_UP) # Pin del botón
def oledEspera():
oled.text("Sistema listo!", 10, 10)
oled.text("Presione #", 10, 30)
oled.text("Para iniciar...", 10, 40)
oled.show()
oled.fill(0)
def oledCaptura(digitosCasa):
oled.text("Captura iniciada!", 0, 10)
oled.text("Ingrese 3", 10, 30)
oled.text(f"digitos: {digitosCasa}", 10, 40)
oled.show()
oled.fill(0)
def oledLlamadaActiva(digitosCasa):
oled.text("Llamando a casa:", 0, 10)
oled.text(digitosCasa, 10, 20)
oled.show()
oled.fill(0)
def oledLlamadaCancelada(digitosCasa):
oled.text("Llamada a casa:", 10, 10)
oled.text(digitosCasa, 10, 30)
oled.text("CANCELADA", 10, 40)
oled.show()
oled.fill(0)
def oledRespuestaCasa(digitosCasa):
oled.text("La casa:" + digitosCasa, 10, 10)
oled.text("Ha respondido", 10, 30)
oled.text("Sistema listo.", 10, 40)
oled.show()
oled.fill(0)
def set_color(red, green, blue):
red_pin.value(red)
green_pin.value(green)
blue_pin.value(blue)
def setColorLedRgb(EstadoDelProceso):
if(EstadoDelProceso == 0):
set_color(0, 0, 1) # Azul - Espera
elif(EstadoDelProceso == 1):
set_color(1, 1, 0) # Amarillo - Captura
elif(EstadoDelProceso == 2):
set_color(0, 1, 0) # Verde - Llamada
elif(EstadoDelProceso == 3):
set_color(1, 0, 0) # rojo - Cancelar
def read_keypad():
for i, row in enumerate(row_pins):
row.value(1)
for j, col in enumerate(col_pins):
if col.value() == 1:
TeclaPresionada = keys[i][j]
while col.value() == 1:
pass
row.value(0)
return keys[i][j]
row.value(0)
return None
def leerBoton():
print(buttonPin.value())
return buttonPin.value()
"""State list
0 Estado inicial
1 Captura
2 Llamada activa
3 Cancelacion
4 Respuesta de la casa
"""
"""led list
0 Estado inicial - Azul
1 Captura - Amarillo
2 Llamada activa - Verde
3 Cancelacion - Rojo
4 Respuesta de la casa - Verde luego Azul
"""
#Variables globales
digitos = ['0', '1', '2', '3', '4', '5', '6',
'7', '8', '9']
botonPresionado = False
estado = 0
colorLed = 0
stringCasa = ""
#Inicial
setColorLedRgb(colorLed)
oledEspera()
while True:
key = read_keypad()
if estado == 0: #Inicial - Azul
oledEspera()
setColorLedRgb(colorLed)
if key != None: #Hemos leido algo
print("Tecla presionada:", key)
if key == "#":
estado = 1
colorLed = 1
if estado == 1: #Captura - Amarillo
if len(stringCasa) >= 3:
estado = 2
colorLed = 2
else:
oledCaptura(stringCasa)
setColorLedRgb(colorLed)
if key in digitos and len(stringCasa) < 3:
stringCasa += key
elif key == "D":
stringCasa = stringCasa[:-1]
elif key == "C":
stringCasa = ""
if estado == 2: #Llamada activa
oledLlamadaActiva(stringCasa)
setColorLedRgb(colorLed)
if key == "A": #Cancelacion
estado = 3
colorLed = 3
if leerBoton() == 0: #ya se presiono
estado = 4
if estado == 3:
oledLlamadaCancelada(stringCasa)
setColorLedRgb(colorLed)
if key == "C" or key == "D":
estado = 0
colorLed = 0
stringCasa = ""
if estado == 4:
oledRespuestaCasa(stringCasa)
time.sleep(5)
estado = 0
colorLed = 0
stringCasa = ""