State machine for LCD menu
Posted: Thu May 21, 2020 9:58 pm
I am trying to implement a state machine to display a menu with an 2x16 LCD and joystick to control it. After 15-20 moves I get an error "RuntimeError: maximum recursion depth exceeded". How should I approach that problem? Should I give up using a function for each menu? There will be also submenus.
Code: Select all
from time import sleep_ms
from machine import I2C, Pin, ADC
from esp8266_i2c_lcd import I2cLcd
DEFAULT_I2C_ADDR = 0x27
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=400000)
lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16)
x = ADC(Pin(33))
x.atten(ADC.ATTN_11DB)
x.width(ADC.WIDTH_9BIT)
def menu1():
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("menu1")
sleep_ms(500)
while(1):
if(x.read()>500):
num_to_func(2)
def menu2():
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("menu2")
sleep_ms(500)
while(1):
if(x.read()>500):
num_to_func(3)
def menu3():
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("menu3")
sleep_ms(500)
while(1):
if(x.read()>500):
num_to_func(1)
'''while(1):
lcd.clear()
val = "X val: {}".format(x.read())
lcd.putstr(val)
lcd.move_to(0, 0)
sleep_ms(200)'''
def num_to_func(argument):
switcher = {
1: menu1,
2: menu2,
3: menu3,
}
func = switcher.get(argument)
return func()
menu1()