import machine
import utime
import _thread
import network
import socket
import math
from ssd1306 import SSD1306_I2C
# --- 1. INICIALIZACIÓN DE HARDWARE ---
utime.sleep_ms(500) # Espera de estabilización
# Configuración I2C (GP17=SCL, GP16=SDA) con pull-ups internos
i2c = machine.I2C(0,
scl=machine.Pin(17, machine.Pin.OUT, machine.Pin.PULL_UP),
sda=machine.Pin(16, machine.Pin.OUT, machine.Pin.PULL_UP),
freq=400000)
print("Escaneando bus I2C...")
dispositivos = i2c.scan()
# Inicialización segura de la pantalla
oled = None
if dispositivos:
print(f"Dispositivo encontrado en: {[hex(d) for d in dispositivos]}")
try:
oled = SSD1306_I2C(128, 64, i2c)
except Exception as e:
print(f"Error al iniciar OLED: {e}")
else:
print("Error: No se detectó hardware I2C.")
# Parámetros compartidos entre núcleos
config = {
"frecuencia_onda": 0.1,
"amplitud": 15,
"msg": "FLISOL 2026"
}
def core1_task():
""" CORE 1: Renderizado de la interfaz y la onda """
global oled
if oled is None: return
offset = 0
while True:
try:
val_freq = config["frecuencia_onda"]
oled.fill(0)
# Dibujar marco y título
oled.rect(0, 0, 128, 64, 1)
oled.text(config["msg"], 20, 6)
# Dibujar onda senoidal
for x in range(2, 126):
y = int(32 + config["amplitud"] * math.sin(val_freq * (x + offset)))
oled.pixel(x, y, 1)
# Info de estado
oled.hline(2, 48, 124, 1)
oled.text(f"FREQ:{val_freq:.2f}", 4, 52)
oled.text(f"{machine.freq()//1000000}MHz", 75, 52)
oled.show()
offset += 2
utime.sleep_ms(25)
except Exception as e:
print(f"Error en Core 1: {e}")
utime.sleep_ms(500)
def core0_server():
""" CORE 0: Servidor Web de control """
ap = network.WLAN(network.AP_IF)
ap.config(essid="Pico_Web_Control", password="flisol_demo")
ap.active(True)
ip = ap.ifconfig()[0]
print(f"\n--- SISTEMA ACTIVO ---")
print(f"IP del AP: {ip}")
print(f"URL: http://{ip}\n")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 80))
s.listen(5)
while True:
try:
conn, addr = s.accept()
request = str(conn.recv(1024))
# Lógica de botones
if "/mas" in request:
config["frecuencia_onda"] += 0.02
elif "/menos" in request:
config["frecuencia_onda"] = max(0.01, config["frecuencia_onda"] - 0.02)
# HTML Modernizado para el taller
html = f"""<!DOCTYPE html><html><head><meta charset='utf-8'><meta name='viewport' content='width=device-width, initial-scale=1'>
<style>
body {{ font-family: sans-serif; text-align: center; background: #eceff1; color: #263238; padding-top: 50px; }}
.btn {{ padding: 20px; font-size: 22px; margin: 10px; width: 140px; cursor: pointer; border: none; border-radius: 10px; color: white; transition: 0.3s; }}
.btn-up {{ background: #2e7d32; }} .btn-down {{ background: #c62828; }}
.btn:active {{ transform: scale(0.95); }}
.card {{ background: white; padding: 30px; border-radius: 20px; display: inline-block; box-shadow: 0 4px 15px rgba(0,0,0,0.1); }}
</style></head><body>
<div class='card'>
<h1>Pico W | FLISOL 2026</h1>
<p>Frecuencia de la onda: <strong>{config['frecuencia_onda']:.2f}</strong></p>
<button class='btn btn-up' onclick="location.href='/mas'">MÁS +</button>
<button class='btn btn-down' onclick="location.href='/menos'">MENOS -</button>
<p style='font-size: 0.8em; color: #78909c;'>Dual-Core: Render (Core 1) | Web (Core 0)</p>
</div>
</body></html>"""
conn.send('HTTP/1.1 200 OK\nContent-Type: text/html\n\n' + html)
conn.close()
except Exception as e:
print(f"Error en servidor: {e}")
# --- 2. EJECUCIÓN ---
if oled:
print("Iniciando hilos...")
_thread.start_new_thread(core1_task, ())
core0_server()
else:
print("No se puede iniciar: Pantalla no detectada.")