# Programa Python para experimentar el uso del JoyStick KN-023 y un led RGB
from machine import Pin, I2C, ADC # , framebuf #Se importa framebuf
import ssd1306
import utime
# Datos del logo (ejemplo)
logo_bytes = bytearray([
0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x10, 0xff, 0xff, 0xff, 0xf0,
0x00, 0x30, 0xff, 0xff, 0xff, 0xc0, 0x00, 0xf0, 0xff, 0xff, 0xff, 0x00, 0x03, 0xf0, 0xff, 0xff,
0xfc, 0x00, 0x0f, 0xf0, 0xff, 0xff, 0xf8, 0x00, 0x1f, 0xf0, 0xff, 0xff, 0xe0, 0x00, 0x7f, 0xf0,
0xff, 0xff, 0x80, 0x01, 0xff, 0xf0, 0xff, 0xfe, 0x00, 0x07, 0xff, 0xf0, 0xff, 0xf8, 0x00, 0x0f,
0xff, 0xf0, 0xff, 0xf0, 0x00, 0x3f, 0xff, 0xf0, 0xff, 0xc0, 0x00, 0xff, 0xff, 0xf0, 0xff, 0x00,
0x03, 0xff, 0xff, 0xf0, 0xfc, 0x00, 0x07, 0xff, 0xff, 0xf0, 0xf0, 0x00, 0x1f, 0xff, 0xff, 0xf0,
0xc0, 0x00, 0x7f, 0xff, 0xff, 0xf0, 0x80, 0x01, 0xff, 0xff, 0xff, 0xf0, 0x80, 0x00, 0x00, 0x00,
0x00, 0x10, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x7f, 0xff,
0xff, 0xff, 0xff, 0xe0, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xe0,
0x7f, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x10, 0xff, 0xff, 0xff, 0xf8,
0x00, 0x10, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x70, 0xff, 0xff, 0xff, 0x80, 0x00, 0xf0, 0xff, 0xff,
0xfe, 0x00, 0x03, 0xf0, 0xff, 0xff, 0xfc, 0x00, 0x0f, 0xf0, 0xff, 0xff, 0xf0, 0x00, 0x3f, 0xf0,
0xff, 0xff, 0xc0, 0x00, 0x7f, 0xf0, 0xff, 0xff, 0x80, 0x01, 0xff, 0xf0, 0xff, 0xfe, 0x00, 0x07,
0xff, 0xf0, 0xff, 0xf8, 0x00, 0x1f, 0xff, 0xf0, 0xff, 0xe0, 0x00, 0x7f, 0xff, 0xf0, 0xff, 0xc0,
0x00, 0xff, 0xff, 0xf0, 0xff, 0x00, 0x03, 0xff, 0xff, 0xf0, 0xfc, 0x00, 0x0f, 0xff, 0xff, 0xf0,
0xf8, 0x00, 0x3f, 0xff, 0xff, 0xf0, 0xe0, 0x00, 0x7f, 0xff, 0xff, 0xf0, 0x80, 0x01, 0xff, 0xff,
0xff, 0xf0, 0x00, 0x07, 0xff, 0xff, 0xff, 0xf0
])
# -------PANTALLA 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)
# Crear un framebuf y dibujar el logo en él
# fb = framebuf.FrameBuffer(logo_bytes, 128, 8, framebuf.MONO_HLSB) #Se crea el framebuffer.
# oled.fill(0)
# oled.blit(fb, 0, 0) #Se carga el framebuffer a la pantalla oled.
# oled.show()
# utime.sleep_ms(3000)
oled.fill(0)
oled.text('Hola, SPick !', 11, 25)
oled.show()
utime.sleep_ms(3000)
oled.fill(0)
oled.show()
# -------JOYSTICK Y LEDS ---------
sw = ADC(Pin(34)) # Pulsador digital
vrx = ADC(Pin(35)) # Señal analógica en x
vry = ADC(Pin(33)) # Señal analógica en y
red = Pin(23, Pin.OUT)
green = Pin(5, Pin.OUT)
blue = Pin(4, Pin.OUT)
# Apaga el led RGB
red.value(1)
green.value(1)
blue.value(1)
# Atenuación y resolución de ADC
vrx.atten(ADC.ATTN_11DB)
vry.atten(ADC.ATTN_11DB)
vrx.width(ADC.WIDTH_10BIT)
vry.width(ADC.WIDTH_10BIT)
def encenderLed(color):
print(color)
if color == "rojo": # Corregido: == para comparación
red.value(0)
green.value(1)
blue.value(1)
elif color == "verde":
red.value(1)
green.value(0)
blue.value(1)
elif color == "amarillo":
red.value(0)
green.value(0)
blue.value(1)
elif color == "azul":
red.value(1)
green.value(1)
blue.value(0)
elif color == "morado":
red.value(0)
green.value(1)
blue.value(0)
elif color == "cian":
red.value(1)
green.value(0)
blue.value(0)
elif color == "blanco":
red.value(0)
green.value(0)
blue.value(0)
elif color == "apagado":
red.value(1)
green.value(1)
blue.value(1)
utime.sleep(1) #Se movio el sleep dentro de la funcion.
red.value(1)
green.value(1)
blue.value(1)
oled.fill(0)
oled.show()
# Ciclo de operación
while True:
valorx = vrx.read()
valory = vry.read()
valorsw = sw.read()
oled.fill(0) #Se movio el fill y show para que se actualice la pantalla en cada iteracion.
# oled.text(str(valorx), 10, 10)
# oled.text(str(valory), 10, 20)
# oled.text(str(valorsw), 10, 30)
# oled.show()
utime.sleep_ms(100)
if valory >= 1000:
print("Disminuye Cantidad: RGB: Amarillo")
oled.fill(0)
oled.text("Disminuye", 35, 15)
oled.text("Cantidad", 31, 35)
oled.show()
encenderLed("amarillo")
elif valory <= 23:
print("Aumenta Cantidad: RGB: Azul")
oled.fill(0)
oled.text("Aumenta", 35, 15)
oled.text("Cantidad", 31, 35)
oled.show()
encenderLed("azul")
elif valorx <= 23:
print("Solicita Reabasto: RGB: Morado")
oled.fill(0)
oled.text("Solicita", 35, 15)
oled.text("Reabasto", 31, 35)
oled.show()
encenderLed("morado")
elif valorx >= 1000:
print("Surtido Nulo: RGB: Rojo")
oled.fill(0)
oled.text("Surtido Nulo", 15, 25)
oled.show()
encenderLed("rojo")
elif valorsw == 0:
print("Surtido Completo: RGB: Verde")
oled.fill(0)
oled.text("Surtido", 35, 15)
oled.text("Completo", 31, 35)
oled.show()
encenderLed("verde")