from machine import Pin, I2C
import ssd1306
# Configurar conexión I2C y crear objeto de pantalla SSD1306
i2c = I2C(-1, scl=Pin(22), sda=Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Definir lista de opciones del menú
menu_options = ["Opcion 1", "Opcion 2", "Opcion 3"]
selected_option = 0
button_up = Pin(25, Pin.IN)
button_down = Pin(33, Pin.IN)
prev_button_up_state = button_up.value()
prev_button_down_state = button_down.value()
while True:
# Limpiar pantalla y mostrar título del menú
oled.fill(0)
oled.text("Main Menu", 0 ,0 )
# Mostrar las opciones del menú resaltando la opción seleccionada actualmente
for index , option in enumerate(menu_options):
if index == selected_option:
oled.text("* " + option ,0 ,(index+1)*10 )
else:
oled.text(option ,5 ,(index+1)*10 )
oled.show()
current_button_up_state = button_up.value()
if current_button_up_state != prev_button_up_state and current_button_up_state == 0: # Flanco ascendente en el botón Up
selected_option -= 1 # Disminuir valor de selected_option
if selected_option < 0:
selected_option = len(menu_options) - 1
prev_button_up_state = current_button_up_state
current_button_down_state = button_down.value()
if current_button_down_state != prev_button_down_state and current_button_down_state == 0: # Flanco ascendente en el botón Down
selected_option += 1 # Aumentar valor de selected_option
if selected_option >= len(menu_options):
selected_option = 0
prev_button_down_state = current_button_down_state