From 94363f1ebe6c497fb064e73b34270ec5f14154d7 Mon Sep 17 00:00:00 2001 From: Ethan <108598670+echo-lalia@users.noreply.github.com> Date: Sun, 22 Sep 2024 00:42:17 -0700 Subject: [PATCH] Fixerrors (#154) * Change app download url * Fix typo * Fix bad viper type * Catch errors in file browser * Adjust default trackball repeat --- devices/CARDPUTER/lib/userinput/_keys.py | 2 +- devices/TDECK/lib/userinput/_keys.py | 4 +- src/launcher/files.py | 93 +++++++++++++++--------- src/launcher/getapps.py | 2 +- src/lib/hydra/beeper.py | 2 +- 5 files changed, 62 insertions(+), 41 deletions(-) diff --git a/devices/CARDPUTER/lib/userinput/_keys.py b/devices/CARDPUTER/lib/userinput/_keys.py index 51f3aef..6624d91 100644 --- a/devices/CARDPUTER/lib/userinput/_keys.py +++ b/devices/CARDPUTER/lib/userinput/_keys.py @@ -80,7 +80,7 @@ def __init__(self, **kwargs): # noqa: ARG002 self.key_state = [] @micropython.viper - def scan(self) -> list: + def scan(self): # noqa: ANN202 """Scan through the matrix to see what keys are pressed.""" key_list_buffer = [] self._key_list_buffer = key_list_buffer diff --git a/devices/TDECK/lib/userinput/_keys.py b/devices/TDECK/lib/userinput/_keys.py index 5ade5b1..b01faac 100644 --- a/devices/TDECK/lib/userinput/_keys.py +++ b/devices/TDECK/lib/userinput/_keys.py @@ -41,7 +41,7 @@ 1:'#', 2:'1', 17:'2', 33:'3', 35:'(', 51:')', 49:'_', 67:'-', 65:'+', 20:'@', 4:'*', 18:'4', 19:'5', 39:'6', 34:'/', 50:':', 55:';', 71:"'", 66:'"', 68:'DEL', 5:'ALT',22:'7', 21:'8', 38:'9', 37:'?', 53:'!', 54:',', 70:'.', 69:'SPEAK', - 23:'SHIFT', 7:'0', 6:'SPC', 3:'FN', 36:'SHIFT', + 23:'SHIFT', 7:'0', 6:'TAB', 3:'FN', 36:'SHIFT', } @@ -93,7 +93,7 @@ class Keys: ext_dir_dict = {'i':'UP', 'j':'LEFT', 'k':'DOWN', 'l':'RIGHT', 'q':'ESC'} - def __init__(self, tb_repeat_ms=50, **kwargs): # noqa: ARG002 + def __init__(self, tb_repeat_ms=60, **kwargs): # noqa: ARG002 # turn on keyboard KBD_PWR.value(1) diff --git a/src/launcher/files.py b/src/launcher/files.py index b216591..c1c4d07 100644 --- a/src/launcher/files.py +++ b/src/launcher/files.py @@ -415,6 +415,55 @@ def refresh_files(view: ListView) -> tuple[list, dict]: return file_list, dir_dict +def panic_recover() -> tuple[ListView, list, dict]: + """When an error would cause a crash, try recovering instead.""" + os.chdir('/') + file_list, dir_dict = parse_files() + view = ListView(tft, config, file_list, dir_dict) + return view, file_list, dir_dict + + +def handle_input(key, view, file_list, dir_dict) -> tuple[list, dict]: + """React to user inputs.""" + if key == "UP": + view.up() + beep.play(("G3", "B3"), 30) + elif key == "DOWN": + view.down() + beep.play(("D3", "B3"), 30) + + elif key in {kb.main_action, kb.secondary_action}: + beep.play(("G3", "B3", "D3"), 30) + selection_name = file_list[view.cursor_index] + if selection_name == "/.../": # new file + ext_options(overlay) + file_list, dir_dict = refresh_files(view) + + elif dir_dict[selection_name]: + # this is a directory, open it + os.chdir(selection_name) + file_list, dir_dict = refresh_files(view) + else: + # this is a file, give file options + file_options(file_list[view.cursor_index], overlay) + file_list, dir_dict = refresh_files(view) + + elif key == "BSPC": + beep.play(("D3", "B3", "G3"), 30) + # previous directory + if os.getcwd() == "/sd": + os.chdir("/") + else: + os.chdir("..") + file_list, dir_dict = refresh_files(view) + + elif key == kb.aux_action: + ext_options(overlay) + file_list, dir_dict = refresh_files(view) + + return file_list, dir_dict + + def main_loop(tft, kb, config, overlay): """Run the main loop.""" @@ -428,42 +477,14 @@ def main_loop(tft, kb, config, overlay): new_keys = kb.get_new_keys() kb.ext_dir_keys(new_keys) - for key in new_keys: - if key == "UP": - view.up() - beep.play(("G3", "B3"), 30) - elif key == "DOWN": - view.down() - beep.play(("D3", "B3"), 30) - - elif key in {kb.main_action, kb.secondary_action}: - beep.play(("G3", "B3", "D3"), 30) - selection_name = file_list[view.cursor_index] - if selection_name == "/.../": # new file - ext_options(overlay) - file_list, dir_dict = refresh_files(view) - - elif dir_dict[selection_name]: - # this is a directory, open it - os.chdir(selection_name) - file_list, dir_dict = refresh_files(view) - else: - # this is a file, give file options - file_options(file_list[view.cursor_index], overlay) - file_list, dir_dict = refresh_files(view) - - elif key == "BSPC": - beep.play(("D3", "B3", "G3"), 30) - # previous directory - if os.getcwd() == "/sd": - os.chdir("/") - else: - os.chdir("..") - file_list, dir_dict = refresh_files(view) - - elif key == kb.aux_action: - ext_options(overlay) - file_list, dir_dict = refresh_files(view) + try: + for key in new_keys: + file_list, dir_dict = handle_input(key, view, file_list, dir_dict) + + except (OSError, UnicodeError) as e: + # File operations can sometimes have unexpected results + overlay.error(repr(e)) + view, file_list, dir_dict = panic_recover() view.draw() tft.show() diff --git a/src/launcher/getapps.py b/src/launcher/getapps.py index 0c29d09..ed327b1 100644 --- a/src/launcher/getapps.py +++ b/src/launcher/getapps.py @@ -111,7 +111,7 @@ def request_file(file_path: str) -> requests.Response: """Get the specific app file from GitHub""" TERM.print('Making request...') response = requests.get( # noqa: S113 # no point using a timeout here - f'https://api.github.com/repos/echo-lalia/MicroHydra-Apps/contents/catalog-output/{file_path}', + f'https://raw.githubusercontent.com/echo-lalia/MicroHydra-Apps/main/catalog-output/{file_path}', headers={ "accept": "application/vnd.github.v3.raw", "User-Agent": f"{Device.name} - MicroHydra", diff --git a/src/lib/hydra/beeper.py b/src/lib/hydra/beeper.py index 1619f9b..c0505c2 100644 --- a/src/lib/hydra/beeper.py +++ b/src/lib/hydra/beeper.py @@ -112,7 +112,7 @@ def play(self, notes, time_ms=100, volume=None): """Play the given note. This is the main outward-facing method of Beeper. - Use this to play a simple square wave over the SPI speaker. + Use this to play a simple square wave over the I2C speaker. "notes" should be: - a string containing a note's name