uv-belichter-software/lib/ProgramChooserAdapted.py

94 lines
3.9 KiB
Python

"""
ProgramChooser - a program launcher for microPython (adapted to the UV Belichter needs)
Copyright (C) 2024 Benjamin Burkhardt <bluefox@privacynerd.de>
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import utils, config
from PCF8574T import I2C_LCD
import time
class ProgramChooser:
def __init__(self, programs, debug=False, run_directly=False):
self.lcd = config.LCD
self.ok_btn = config.BTN_1
self.next_btn = config.BTN_2
self.lcd.move_to(0,0)
self.lcd.putstr("[ PROGRAMS ]< >")
self.current_selection = None # no selection
self.programs = programs # a dictionary of programs and it's callbacks e.g. {"lora_test": some_callback}
self.show_selection()
if run_directly: self.run()
def log(self, msg, is_debug=False):
if is_debug:
utils.log(2, f"[ProgramChooser] {msg}")
else:
utils.log(1, f"[ProgramChooser] {msg}")
def show_selection(self):
self.lcd.move_to(1,1)
if len(self.programs) == 0:
self.lcd.putstr(" No programs!")
return True
if self.current_selection == None: # set it initially
self.current_selection = 0
# the actual displaying process
to_show = list(self.programs.keys())[self.current_selection]
if len(to_show) > 14:
to_show = to_show[:13] + chr(0)
else:
to_show = to_show[:14]
to_show = to_show.center(14)
self.lcd.putstr(to_show)
return True
def run(self):
while True:
if self.next_btn.value() == 1:
former_program_name = list(self.programs.keys())[self.current_selection]
self.current_selection = (self.current_selection+1)%len(list(self.programs.keys()))
self.show_selection()
now_program_name = list(self.programs.keys())[self.current_selection]
self.log(f"Selected next program (\"{former_program_name}\" -> \"{now_program_name}\")")
while self.next_btn.value() == 1: time.sleep(0.01) # wait till release
if self.ok_btn.value() == 1:
program_name = list(self.programs.keys())[self.current_selection]
self.log(f"Running selected program! (\"{program_name}\")")
# shorten the name for displaying (if too long)
if len(program_name) > 14:
program_name = program_name[:13] + chr(0)
else:
program_name = program_name[:14]
program_name = program_name.center(14)
self.lcd.move_to(0,0)
self.lcd.putstr(f" {program_name} Executing... ")
self.execute_selection()
while self.ok_btn.value() == 1: time.sleep(0.01) # wait till release (e.g. if the "program" is a simple send action)
self.lcd.putstr(f" {program_name} Closing... ")
time.sleep(1)
self.lcd.move_to(0,0)
self.lcd.putstr("[ PROGRAMS ]< >")
self.show_selection()
time.sleep(0.01)
def execute_selection(self): # execute the current selected program's callback
self.programs[list(self.programs.keys())[self.current_selection]]()