Skip to content

Commit

Permalink
Fixerrors (#154)
Browse files Browse the repository at this point in the history
* Change app download url

* Fix typo

* Fix bad viper type

* Catch errors in file browser

* Adjust default trackball repeat
  • Loading branch information
echo-lalia authored Sep 22, 2024
1 parent 2135053 commit 94363f1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 41 deletions.
2 changes: 1 addition & 1 deletion devices/CARDPUTER/lib/userinput/_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions devices/TDECK/lib/userinput/_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}


Expand Down Expand Up @@ -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)

Expand Down
93 changes: 57 additions & 36 deletions src/launcher/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/launcher/getapps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/lib/hydra/beeper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 94363f1

Please sign in to comment.