from time import sleep, sleep_ms
import ssd1306
import framebuf
import logo
from mpu6050 import MPU6050
import time
from machine import Pin, SoftSPI, SoftI2C
spi = SoftSPI(baudrate=500000, polarity=1, phase=0, sck=Pin(27), mosi=Pin(14), miso=Pin(34))
dc = Pin(25) # data/command
rst = Pin(26) # reset
cs = Pin(33) # chip select, some modules do not have a pin for this
#display = ssd1306.SSD1306_SPI(128, 64, spi, dc, rst, cs)
pin = Pin(32, Pin.OUT)
pin.value(0) #Configura GPIO16 en bajo para resetear el OLED
pin.value(1) #Mientras que el OLED esté ejecutándose, GPIO16 debe estar en 1
# Dimensiones de la pantalla OLED
WIDTH = 128
HEIGHT = 64
# Configuración de I2C para ESP32
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
# Inicializar la pantalla OLED
oled = ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c)
# Mostrar nombres iniciales
oled.fill(0)
oled.text("VIEYRA PEREZ", 10, 0)
oled.text("LEON RODRIGUEZ", 10, 20)
oled.show()
sleep(3) # Mostrar por 3 segundos
# Mostrar objetivo de la práctica
oled.fill(0)
oled.text("Objetivo:", 0, 0) # Título
oled.text("Desarrollar un", 0, 10) # Primera línea
oled.text("sistema con ESP32", 0, 20) # Segunda línea
oled.text("para detectar", 0, 30) # Tercera línea
oled.text("movimiento y medir", 0, 40) # Cuarta línea
oled.text("distancias.", 0, 50) # Quinta línea
oled.show()
sleep(5) # Mostrar por 5 segundos
# Mostrar imagen/logo inicial
oled.fill(0)
imu = MPU6050(i2c)
# Cargar logo
buffer = bytearray(logo.LOGO)
fb = framebuf.FrameBuffer(buffer, WIDTH, HEIGHT ,framebuf.MONO_HLSB)
while True:
Ax = imu.accel.x
Ay = imu.accel.y
Az = imu.accel.z
Gx = imu.gyro.x
Gy = imu.gyro.y
Gz = imu.gyro.z
print("Ax=",str(Ax)," ","Ay=",str(Ay)," ","Az=",str(Az),"\t",
"Gx=",str(Gx)," ","Gy=", str(Gy)," ","Gz=", str(Gz),"\t",
"Temp=",imu.temperature," ",end="\r")
oled.fill(0)
oled.blit(fb,0,0)
oled.show()
if Gy > 90.0:
oled.fill(0)
oled.rotate(90)
oled.blit(fb,0,0)
oled.show()
time.sleep(3)
oled.fill(0)
oled.rotate(1)
oled.show()
if Gy < -90.0:
oled.fill(0)
oled.rotate(90)
oled.blit(fb,0,0)
oled.show()
time.sleep(3)
oled.fill(0)
oled.rotate(1)
oled.show()
time.sleep(1)