import machine
from machine import Pin,SoftI2C
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
from time import sleep
I2C_ADDR=0x27
totalRows=2
totalColumns=16
i2c = SoftI2C (scl=Pin (22), sda= Pin(21), freq=10000)
lcd =I2cLcd (i2c, I2C_ADDR, totalRows, totalColumns)
import machine
# Define the menu structure
menu = [
{
"Option 1": ["Suboption 1: Off", "Suboption 2: Off", "Suboption 3: Off"]
},
{
"Option 2": ["motor A: Off", "motor B: Off"]
},
{
"Option 3": ["Suboption X: Off", "Suboption Y: Off", "Suboption Z: Off"]
}
]
current_option = 0
current_submenu = None
sub_option_index = 0
# Define button pins
button_up = machine.Pin(12, machine.Pin.IN, machine.Pin.PULL_UP)
button_down = machine.Pin(13, machine.Pin.IN, machine.Pin.PULL_UP)
button_ok = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_UP) # OK button pin
led1 = machine.Pin(2, machine.Pin.OUT)
led2 = Pin(4, Pin.OUT)
def display_option(option):
lcd.clear()
lcd.putstr(list(option.keys())[0])
def display_sub_option(sub_option):
lcd.clear()
lcd.putstr(sub_option)
def navigate_options():
global current_option, current_submenu, sub_option_index
display_option(menu[current_option])
while True:
if not button_up.value(): # Check if button_up is pressed (Back)
if current_submenu: # If in a submenu, go back to the main menu
current_submenu = None
display_option(menu[current_option])
else: # If in the main menu, move to the previous option
current_option = (current_option - 1) % len(menu)
display_option(menu[current_option])
while not button_up.value(): # Wait for button release
pass
if not button_down.value(): # Check if button_down is pressed (Move Down)
if not current_submenu: # If not in a submenu, move to the next option
current_option = (current_option + 1) % len(menu)
display_option(menu[current_option])
else: # If in a submenu, move to the next sub-option
sub_option_index = (sub_option_index + 1) % len(current_submenu)
display_sub_option(current_submenu[sub_option_index])
while not button_down.value(): # Wait for button release
pass
if not button_ok.value(): # Check if OK button is pressed (Open Sub-menu or Toggle On/Off)
if not current_submenu: # If not in a submenu, open the sub-options
current_submenu = list(menu[current_option].values())[0]
sub_option_index = 0
display_sub_option(current_submenu[sub_option_index])
else: # If in a submenu, toggle On/Off
submenu_item = current_submenu[sub_option_index]
if "Off" in submenu_item:
submenu_item = submenu_item.replace("Off", "On")
else:
submenu_item = submenu_item.replace("On", "Off")
current_submenu[sub_option_index] = submenu_item
display_sub_option(submenu_item)
while not button_ok.value(): # Wait for button release
pass
navigate_options()
'''while True:
lcd.putstr('' hiatul'')
lcd.move_to(2,0)
lcd.putstr('' hl'')'''
'''for i in range(16,0,-1):
lcd.move_to(i,0)
lcd.putstr('*')
sleep(0.5)
lcd.clear()'''