Skip to content

Instantly share code, notes, and snippets.

@1marte1
Created May 21, 2025 20:47
Show Gist options
  • Save 1marte1/a56deccf6f297b60449ad224cd717fae to your computer and use it in GitHub Desktop.
Save 1marte1/a56deccf6f297b60449ad224cd717fae to your computer and use it in GitHub Desktop.
Juego de reacción con botón y pantalla OLED
# =======================================================
# Nombre: Cruz Lafarga Iván
# Fecha: 20/05/2025
# Descripción: Juego de reacción con botón y pantalla OLED
# Plataforma: Raspberry Pi Pico W
# =======================================================
from machine import Pin, I2C
import ssd1306
import time
import urandom # Para esperar un tiempo aleatorio
# Configurar pantalla OLED
i2c = I2C(0, scl=Pin(1), sda=Pin(0))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Configurar botón
btn = Pin(14, Pin.IN, Pin.PULL_UP)
def mostrar_texto(line1="", line2=""):
oled.fill(0)
oled.text(line1, 0, 10)
oled.text(line2, 0, 30)
oled.show()
while True:
mostrar_texto("Prepárate...")
time.sleep(urandom.uniform(2, 5)) # Espera aleatoria entre 2 y 5 segundos
mostrar_texto("¡Presiona ahora!")
t_inicio = time.ticks_ms()
# Espera hasta que presione el botón
while btn.value() == 0:
pass
t_fin = time.ticks_ms()
tiempo_reaccion = time.ticks_diff(t_fin, t_inicio)
mostrar_texto("Reaccionaste en", f"{tiempo_reaccion} ms")
time.sleep(3)
# ssd1306.py - Micropython SSD1306 OLED driver (I2C)
import framebuf
class SSD1306:
def __init__(self, width, height, external_vcc):
self.width = width
self.height = height
self.external_vcc = external_vcc
self.pages = self.height // 8
self.buffer = bytearray(self.width * self.pages)
self.framebuf = framebuf.FrameBuffer(self.buffer, self.width, self.height, framebuf.MONO_VLSB)
self.poweron()
self.init_display()
def init_display(self):
for cmd in (
0xae, 0x20, 0x00, 0x40, 0xa1, 0xc8, 0xda, 0x12, 0x81, 0x7f, 0xa4,
0xa6, 0xd5, 0x80, 0x8d, 0x14, 0xaf):
self.write_cmd(cmd)
def poweron(self):
pass
def poweroff(self):
self.write_cmd(0xae)
def contrast(self, contrast):
self.write_cmd(0x81)
self.write_cmd(contrast)
def invert(self, invert):
self.write_cmd(0xa7 if invert else 0xa6)
def show(self):
for page in range(self.pages):
self.write_cmd(0xb0 | page)
self.write_cmd(0x02)
self.write_cmd(0x10)
self.write_data(self.buffer[self.width * page:self.width * (page + 1)])
def fill(self, col):
self.framebuf.fill(col)
def pixel(self, x, y, col):
self.framebuf.pixel(x, y, col)
def scroll(self, dx, dy):
self.framebuf.scroll(dx, dy)
def text(self, string, x, y, col=1):
self.framebuf.text(string, x, y, col)
def hline(self, x, y, w, col):
self.framebuf.hline(x, y, w, col)
def vline(self, x, y, h, col):
self.framebuf.vline(x, y, h, col)
def line(self, x1, y1, x2, y2, col):
self.framebuf.line(x1, y1, x2, y2, col)
def rect(self, x, y, w, h, col):
self.framebuf.rect(x, y, w, h, col)
def fill_rect(self, x, y, w, h, col):
self.framebuf.fill_rect(x, y, w, h, col)
class SSD1306_I2C(SSD1306):
def __init__(self, width, height, i2c, addr=0x3c, external_vcc=False):
self.i2c = i2c
self.addr = addr
self.temp = bytearray(2)
super().__init__(width, height, external_vcc)
def write_cmd(self, cmd):
self.temp[0] = 0x80
self.temp[1] = cmd
self.i2c.writeto(self.addr, self.temp)
def write_data(self, buf):
self.i2c.writeto(self.addr, b'\x40' + buf)
@1marte1
Copy link
Author

1marte1 commented May 21, 2025

image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment