From 78ced9e7eafc47effdc0320acf0dfa752d285fc2 Mon Sep 17 00:00:00 2001 From: BlueFox Date: Fri, 15 Nov 2024 23:13:03 +0100 Subject: [PATCH] Updated submodules, implemented timer functionality, fixed configuration issue --- lib/lcdMenu | 2 +- main.py | 49 +++++++++++++++++++++++++++++++++++++++++++++---- utils.py | 8 +++++--- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/lib/lcdMenu b/lib/lcdMenu index 50e9b52..4b7e572 160000 --- a/lib/lcdMenu +++ b/lib/lcdMenu @@ -1 +1 @@ -Subproject commit 50e9b5211b7383f1b02f3e54db9eb76d42f11eb2 +Subproject commit 4b7e5723ca1c2d4b18e452561b2c5a025e489eb2 diff --git a/main.py b/main.py index 3be545b..80b1b54 100644 --- a/main.py +++ b/main.py @@ -32,10 +32,51 @@ def manual(): if config.PIN_IN_BTN_1.value() == 1 or config.PIN_IN_BTN_2.value() == 1: return True # exit on press of these buttons; True to disable the Quitting message from lcdMenu def timer(): - # display WIP - config.LCD.clear() - config.LCD.putstr(" Still work-in-progress") - sleep(3) + 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_splits = [lambda: divmod(round(timer_values[0]), 60), lambda: divmod(round(timer_values[1]), 60), lambda: divmod(round(timer_values[2]), 60)] + + 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: + 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)) + if interrupt_pin.value() == 1: + timer_values[timer_number-1] = _timer # save the current state + last_start_stop_value = 0 # turn the LEDs off! + config.PIN_OUT_RELAIS.value(last_start_stop_value) + return None + if reset_pin.value() == 1: + _timer = original # reset the timers + if _timer <= 0: + config.PIN_OUT_RELAIS.off() + return True + sleep(0.05) + if last_start_stop_value != (new_value := start_stop_pin.value()): + last_start_stop_value = new_value + config.PIN_OUT_RELAIS.value(new_value) + last_time_ns = time_ns() + if start_stop_pin.value() == 1: + _timer -= (time_ns() - last_time_ns) / 1000**3 + last_time_ns = time_ns() + + timer_programs = [(f"{timer_splits[0]()[0]:02d}:{timer_splits[0]()[1]:02d}", lambda: run_timer(1, timer_values[0])), + (f"{timer_splits[1]()[0]:02d}:{timer_splits[1]()[1]:02d}", lambda: run_timer(2, timer_values[1])), + (f"{timer_splits[2]()[0]:02d}:{timer_splits[2]()[1]:02d}", lambda: run_timer(3, timer_values[2])), + ("Exit", timer_menu.stop)] + timer_menu.setup(timer_programs) # give it the callback list + timer_menu.run() + del timer_menu, timer_programs + collect() return True # disable the "Quitting" message from lcdMenu def uv_on(): config.PIN_OUT_RELAIS.value(1) diff --git a/utils.py b/utils.py index 9985331..09a9f92 100644 --- a/utils.py +++ b/utils.py @@ -39,7 +39,10 @@ class Config: "LCD_I2C_ADDR", # the i2c adress of the display "LCD_I2C_NUM_ROWS", # how many rows for character display has the display? "LCD_I2C_NUM_COLS", # and how many characters can it display per row? - "LCD"] # the actual lcd object (of the PCF8574T I2C_LCD class, see libraries) + "LCD", # the actual lcd object (of the PCF8574T I2C_LCD class, see libraries) + "TIMER_1_DURATION", # the duration of the first timer in seconds + "TIMER_2_DURATION", # the duration of the second first timer in seconds + "TIMER_3_DURATION"] # the duration of the third timer in seconds self._config_file = config_file self.load_config() @@ -129,5 +132,4 @@ def log(log_level: int, message: str): print(f"[LOGGER] Got a message of unknown log level ({log_level}). Original message is printed below.") print(f"{message}") elif cfg.LOG_LEVEL >= log_level: # if log level is valid - print(f"[{log_mapping[log_level]}] {message}") - + print(f"[{log_mapping[log_level]}] {message}") \ No newline at end of file