From a4adaac9b4241b767d00e7ed5460875df504e155 Mon Sep 17 00:00:00 2001 From: BlueFox Date: Sat, 16 Nov 2024 12:14:43 +0100 Subject: [PATCH] Implemented support for setting the timer durations, fixed some minor issues and cleaned up / streamlined the menus --- main.py | 167 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 126 insertions(+), 41 deletions(-) diff --git a/main.py b/main.py index 80b1b54..727390b 100644 --- a/main.py +++ b/main.py @@ -33,20 +33,23 @@ def manual(): return True # exit on press of these buttons; True to disable the Quitting message from lcdMenu def timer(): timer_menu = lcdMenu(config.LCD, btn_mapping, scroll_direction=True, cycle=True, hide_menu_name=False, name="TIMERS") - timer_values = [config.TIMER_1_DURATION, config.TIMER_2_DURATION, config.TIMER_3_DURATION] + timer_values_original = [config.TIMER_1_DURATION, config.TIMER_2_DURATION, config.TIMER_3_DURATION] + timer_values = timer_values_original.copy() # here, the current timers time is stored when interrupting via the interrupt_pin (see below) timer_splits = [lambda: divmod(round(timer_values[0]), 60), lambda: divmod(round(timer_values[1]), 60), lambda: divmod(round(timer_values[2]), 60)] + interrupt_pin = config.PIN_IN_BTN_1 # the interrupt btn stops the timer, saves the current time and goes back to the menu + reset_pin = config.PIN_IN_BTN_2 # the reset btn restores the default value + start_stop_pin = config.PIN_IN_SWITCH # the start_stop switch starts/stops the timer + # timer_number is the number of the timer that will be run, starting at 1 + # the _timer variable is needed because otherwise python will throw crazy errors regarding "variable accessed before assignment"... + # ...just see it as a copy of the timer_number-1'th elemnt of the timer_values list (see above) def run_timer(timer_number: int, _timer: int): - interrupt_pin = config.PIN_IN_BTN_1 - reset_pin = config.PIN_IN_BTN_2 - start_stop_pin = config.PIN_IN_SWITCH - original = _timer config.LCD.clear() config.LCD.putstr(f"Timer {timer_number}".center(16)) last_start_stop_value = start_stop_pin.value() config.PIN_OUT_RELAIS.value(last_start_stop_value) last_time_ns = time_ns() - while True: + while True: # now run the timer (if the switch is high) config.LCD.move_to(0,1) _timer_div = divmod(round(_timer), 60) config.LCD.putstr(f"{_timer_div[0]:02d}:{_timer_div[1]:02d}".center(16)) @@ -56,7 +59,7 @@ def timer(): config.PIN_OUT_RELAIS.value(last_start_stop_value) return None if reset_pin.value() == 1: - _timer = original # reset the timers + _timer = timer_values_original[timer_number-1] # reset the timers if _timer <= 0: config.PIN_OUT_RELAIS.off() return True @@ -78,18 +81,6 @@ def timer(): del timer_menu, timer_programs collect() return True # disable the "Quitting" message from lcdMenu -def uv_on(): - config.PIN_OUT_RELAIS.value(1) - config.LCD.clear() - config.LCD.putstr("------ UV ------ turned on ") - sleep(1) - return True # disable the "Quitting" message from lcdMenu -def uv_off(): - config.PIN_OUT_RELAIS.value(0) - config.LCD.clear() - config.LCD.putstr("------ UV ------ turned off ") - sleep(1) - return True # disable the "Quitting" message from lcdMenu def lcd_big_hello(): import lcd_big_hello as lbh lbh.run(config.LCD) @@ -118,35 +109,131 @@ def settings(): option_down = [" ", "v"][current_cycles>1] config.LCD.putstr(f" Cycles \n{option_down} {str(current_cycles).center(12)} ^") + btn_left = config.PIN_IN_BTN_1 + btn_right = config.PIN_IN_BTN_2 while True: - if config.PIN_IN_BTN_1.value() == 1: - time_ns_when_pressed = time_ns() - while config.PIN_IN_BTN_1.value() == 1: - if (time_ns() - time_ns_when_pressed) > 1000000000: # if the time passed is longer than a second - while config.PIN_IN_BTN_1.value() == 1: - pass # wait till release - config.STARTUP_WELCOME_CYCLES = current_cycles - return True - sleep(0.05) + if btn_left.value() == 1: + sleep(0.1) # this value is a good compromise between being able to press both buttons and a fast up/down speed + if btn_right.value() == 1: + config.STARTUP_WELCOME_CYCLES = current_cycles + config.LCD.move_to(0,1) # move to the second row + config.LCD.putstr(f"Now set to {current_cycles}".center(16)) # show a little info that it is now set + while btn_right.value() == 1 or btn_left.value() == 1: # wait till both btns are released + pass + return True + current_cycles -= 1 if current_cycles < 1: current_cycles = 1 option_down = [" ", "v"][current_cycles>1] config.LCD.putstr(f" Cycles \n{option_down} {str(current_cycles).center(12)} ^") - if config.PIN_IN_BTN_2.value() == 1: - time_ns_when_pressed = time_ns() - while config.PIN_IN_BTN_2.value() == 1: - if (time_ns() - time_ns_when_pressed) > 1000000000: # if the time passed is longer than a second - while config.PIN_IN_BTN_2.value() == 1: - pass # wait till release - config.STARTUP_WELCOME_CYCLES = current_cycles - return True - sleep(0.05) + if btn_right.value() == 1: + sleep(0.1) + if btn_left.value() == 1: + config.STARTUP_WELCOME_CYCLES = current_cycles + config.LCD.move_to(0,1) # move to the second row + config.LCD.putstr(f"Now set to {current_cycles}".center(16)) # show a little info that it is now set + while btn_right.value() == 1 or btn_left.value() == 1: # wait till both btns are released + pass + return True current_cycles += 1 option_down = [" ", "v"][current_cycles>1] config.LCD.putstr(f" Cycles \n{option_down} {str(current_cycles).center(12)} ^") + + # with n being the number of the timer (starting at 0) + def set_n_timer(n: int): + # a small helper function to save the value of the n'th timer... + # as you can't programatically access TIMER_n_DURATION + def set_timer_helper(n, value): + if n == 0: + config.TIMER_1_DURATION = value + elif n == 1: + config.TIMER_2_DURATION = value + elif n == 2: + config.TIMER_3_DURATION = value + else: + utils.log(0, "There are only 3 timers at all. Trying to set the timer number {n} failed.") + + config.LCD.clear() + timer_values = [config.TIMER_1_DURATION, config.TIMER_2_DURATION, config.TIMER_3_DURATION] + current_timer = timer_values[n] # get the n'th timer + current_timer_div = lambda: divmod(current_timer, 60) + + config.LCD.putstr(f"Timer {n+1}".center(16)) + config.LCD.putstr(f"{'v' if current_timer > 1 else ' '} " + f"{current_timer_div()[0]:02d}:{current_timer_div()[1]:02d}".center(12) + " ^") + btn_left = config.PIN_IN_BTN_1 + btn_right = config.PIN_IN_BTN_2 + left_was_released = True + right_was_released = True + while True: + if btn_left.value() == 1: + if left_was_released: + time_press_start = time_ns() + left_was_released = False + sleep(0.1) # this value is a good compromise between being able to press both buttons and a fast up/down speed + if btn_right.value() == 1: # exit if both btns are pressed + set_timer_helper(n, current_timer) + config.LCD.move_to(0,1) # move to the second row + config.LCD.putstr(f"Saved!".center(16)) # show a little info that it is now set + while btn_right.value() == 1 or btn_left.value() == 1: # wait till both btns are released + pass + return True + # define the step width + time_now = time_ns() + if (time_now - time_press_start) <= 1*(10**9): # max. 1 seconds pressed + current_timer -= 1 + elif (time_now - time_press_start) <= 2*(10**9): # max. 2 seconds pressed + current_timer -= 5 + elif (time_now - time_press_start) <= 3*(10**9): # max. 3 seconds pressed + current_timer -= 10 + elif (time_now - time_press_start) <= 4*(10**9): # max. 4 seconds pressed + current_timer -= 30 + elif (time_now - time_press_start) <= 5*(10**9): # max. 5 seconds pressed + current_timer -= 60 + else: # longer than 5s pressed + current_timer -= 300 + if current_timer < 1: current_timer = 5999 + config.LCD.move_to(0,1) + config.LCD.putstr("v " + f"{current_timer_div()[0]:02d}:{current_timer_div()[1]:02d}".center(12) + " ^") + else: + left_was_released = True + if btn_right.value() == 1: + if right_was_released: + time_press_start = time_ns() + right_was_released = False + sleep(0.1) + if btn_left.value() == 1: # exit if both btns are pressed + set_timer_helper(n, current_timer) + config.LCD.move_to(0,1) # move to the second row + config.LCD.putstr(f"Saved!".center(16)) # show a little info that it is now set + while btn_right.value() == 1 or btn_left.value() == 1: # wait till both btns are released + pass + return True + # define the step width + time_now = time_ns() + if (time_now - time_press_start) <= 1*(10**9): # max. 1 seconds pressed + current_timer += 1 + elif (time_now - time_press_start) <= 2*(10**9): # max. 2 seconds pressed + current_timer += 5 + elif (time_now - time_press_start) <= 3*(10**9): # max. 3 seconds pressed + current_timer += 10 + elif (time_now - time_press_start) <= 4*(10**9): # max. 4 seconds pressed + current_timer += 30 + elif (time_now - time_press_start) <= 5*(10**9): # max. 5 seconds pressed + current_timer += 60 + else: # longer than 5s pressed + current_timer += 300 + if current_timer > 5999: current_timer = 1 + config.LCD.move_to(0,1) + config.LCD.putstr("v " + f"{current_timer_div()[0]:02d}:{current_timer_div()[1]:02d}".center(12) + " ^") + else: + right_was_released = True + settings_programs = [("Show welcome", swap_welcome), ("Welcome cycles", welcome_cycles), + ("Timer 1", lambda: set_n_timer(0)), + ("Timer 2", lambda: set_n_timer(1)), + ("Timer 3", lambda: set_n_timer(2)), ("Exit", settings_menu.stop)] settings_menu.setup(settings_programs) # give it the callback list settings_menu.run() # run the menu until it's closed @@ -159,10 +246,10 @@ def run_demo_menu(): ("Input tests", input_tests), ("Exit", demo_menu.stop)] demo_menu.setup(demo_programs) # give it the callback list - ret = demo_menu.run() + demo_menu.run() del demo_menu, demo_programs collect() - return ret + return True if config.STARTUP_WELCOME_SHOW: ws = WelcomeScreen(config.LCD, @@ -179,8 +266,6 @@ if config.STARTUP_WELCOME_SHOW: main_menu = lcdMenu(config.LCD, btn_mapping, scroll_direction=True, cycle=True, hide_menu_name=False, name="PROGRAMS") main_programs = [("Timer", timer), ("Manual", manual), - ("UV off", uv_off), - ("UV on", uv_on), ("Demos", run_demo_menu), ("Settings", settings)] main_menu.setup(main_programs) # give it the callback list