from machine import Pin, SPI, I2C
import time
import random
import framebuf
from ssd1306 import SSD1306_I2C
from eyes import peyes, eye0
# (La importación de sdcard y os se mantiene si las necesitas para el resto de tu sistema)
import os
#import sdcard
# -----------------------------
# HW (según tu hardware)
# -----------------------------
SPI_ID = 1
PIN_SCK = 14
PIN_MOSI = 15
PIN_MISO = 12
PIN_SD_CS = 13
I2C_ID = 0
PIN_SCL = 17
PIN_SDA = 16
# -----------------------------
# OLED
# -----------------------------
i2c = I2C(I2C_ID, scl=Pin(PIN_SCL), sda=Pin(PIN_SDA), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
# -----------------------------
# Entradas (Botones)
# -----------------------------
# Asumiendo los mismos pines lógicos 8, 9, 10 y 11 del Pico
pin_izq = Pin(6, Pin.IN, Pin.PULL_UP)
pin_arr = Pin(8, Pin.IN, Pin.PULL_UP)
pin_aba = Pin(7, Pin.IN, Pin.PULL_UP)
pin_fue = Pin(9, Pin.IN, Pin.PULL_UP)
def readkey():
ret = 0
if pin_izq.value() == 0: ret += 1 # izquierda
if pin_arr.value() == 0: ret += 2 # arriba
if pin_aba.value() == 0: ret += 4 # abajo
if pin_fue.value() == 0: ret += 8 # fuego
return ret
# -----------------------------
# Bucle Principal (Loop)
# -----------------------------
xp = 16
mood = 1
xd = 0
espera = 0
step = 0
print("Booting...")
oled.fill(0)
oled.show()
while True:
if espera > 0:
espera -= 1
time.sleep_ms(1)
else:
print("0")
# Cálculo de las posiciones X para cada ojo (ternarios equivalentes a los de C)
x1 = xd + (16 + 2 * (xp - 16) if xp > 16 else xp)
x2 = 64 + xd + (-16 + (xp * 2) if xp < 16 else xp)
if step == 0:
oled.fill(0)
if xp < 6:
oled.blit(peyes[mood][2][0], x1, 8)
oled.blit(peyes[mood][1][1], x2, 8)
elif xp < 26:
oled.blit(peyes[mood][0][0], x1, 8)
oled.blit(peyes[mood][0][1], x2, 8)
else:
oled.blit(peyes[mood][1][0], x1, 8)
oled.blit(peyes[mood][2][1], x2, 8)
oled.show()
# random en C: random(min, max) es excluyente del max.
# random.randint en Python es incluyente de ambos.
espera = random.randint(250, 999)
n_rand = random.randint(0, 6) # Equivale a random(0, 7) en C
if n_rand == 6:
step = 1
else:
step = 2
elif step == 1:
oled.fill(0)
oled.blit(eye0, x1, 8)
oled.blit(eye0, x2, 8)
oled.show()
espera = 100
step = 0
elif step == 2:
n_rand = random.randint(0, 9) # Equivale a random(0, 10) en C
if n_rand < 5: xd -= 1
if n_rand > 5: xd += 1
if xd < -4: xd = -3
if xd > 4: xd = 3
espera = 0
step = 0
# Lectura de entradas
n = readkey()
if n == 2:
xp = 0 if xp <= 0 else xp - 1
if n == 4:
xp = 32 if xp >= 32 else xp + 1
#print("XP=",xp)
if n == 1:
mood = 0 if mood >= 5 else mood + 1
# Bucle equivalente al do-while de C para esperar a que se suelte el botón
while readkey() != 0:
time.sleep_ms(10) # Pequeña demora para evitar saturar el CPU (Debounce)
if n != 0:
espera = 0
step = 0