|
14 | 14 | import sys |
15 | 15 | sys.path.append(lib_path) |
16 | 16 |
|
| 17 | +import atexit |
17 | 18 | import displayio |
18 | 19 | import gc |
19 | 20 | import math |
|
36 | 37 | import adafruit_imageload |
37 | 38 | from adafruit_portalbase.network import HttpError |
38 | 39 | import adafruit_usb_host_mouse |
39 | | -import asyncio |
40 | 40 |
|
41 | 41 | from zipfile import ZipFile |
42 | 42 |
|
@@ -836,69 +836,59 @@ def toggle_application(full_name: str = None) -> bool: |
836 | 836 | return result |
837 | 837 |
|
838 | 838 | # mouse control |
839 | | -mouse_group = displayio.Group(scale=SCALE) |
840 | | -root_group.append(mouse_group) |
841 | | -async def mouse_task() -> None: |
842 | | - global selected_category, selected_application |
| 839 | +mouse = None |
| 840 | +if config is not None and config.use_mouse and (mouse := adafruit_usb_host_mouse.find_and_init_boot_mouse()) is not None: |
| 841 | + mouse.scale = SCALE |
| 842 | + mouse.x = DISPLAY_WIDTH // 2 |
| 843 | + mouse.y = DISPLAY_HEIGHT // 2 |
| 844 | + |
| 845 | + mouse_group = displayio.Group(scale=SCALE) |
| 846 | + mouse_group.append(mouse.tilegrid) |
| 847 | + root_group.append(mouse_group) |
| 848 | + |
| 849 | +def atexit_callback() -> None: |
| 850 | + if mouse and mouse.was_attached and not mouse.device.is_kernel_driver_active(0): |
| 851 | + mouse.device.attach_kernel_driver(0) |
| 852 | +atexit.register(atexit_callback) |
| 853 | + |
| 854 | +# flush input buffer |
| 855 | +while supervisor.runtime.serial_bytes_available: |
| 856 | + sys.stdin.read(1) |
| 857 | + |
| 858 | +# control loop |
| 859 | +try: |
| 860 | + previous_mouse_state = False |
843 | 861 | while True: |
844 | | - if (mouse := adafruit_usb_host_mouse.find_and_init_boot_mouse()) is not None: |
845 | | - mouse.scale = SCALE |
846 | | - mouse.x = DISPLAY_WIDTH // 2 |
847 | | - mouse.y = DISPLAY_HEIGHT // 2 |
848 | | - mouse_group.append(mouse.tilegrid) |
849 | | - |
850 | | - timeouts = 0 |
851 | | - previous_mouse_state = False |
852 | | - while timeouts < 99: |
853 | | - if mouse.update() is not None: |
854 | | - timeouts = 0 |
855 | | - mouse_state = "left" in mouse.pressed_btns |
856 | | - if mouse_state and not previous_mouse_state: |
857 | | - if dialog_buttons.hidden: |
858 | | - if (clicked_cell := item_grid.which_cell_contains((mouse.x * SCALE, mouse.y * SCALE))) is not None: |
859 | | - select_application(clicked_cell[1] * PAGE_COLUMNS + clicked_cell[0]) |
860 | | - elif not right_arrow.hidden and right_arrow.contains((mouse.x, mouse.y, 0)): |
861 | | - next_page() |
862 | | - elif not left_arrow.hidden and left_arrow.contains((mouse.x, mouse.y, 0)): |
863 | | - previous_page() |
864 | | - elif exit_tg.contains((mouse.x, mouse.y, 0)): |
865 | | - reset() |
866 | | - else: |
867 | | - for button in category_group: |
868 | | - if button.contains((mouse.x, mouse.y)): |
869 | | - select_category(button.label) |
870 | | - break |
871 | | - else: |
872 | | - for button in dialog_buttons: |
873 | | - if button.contains((mouse.x, mouse.y, 0)): |
874 | | - button.click() |
875 | | - previous_mouse_state = mouse_state |
876 | | - else: |
877 | | - timeouts += 1 |
878 | | - await asyncio.sleep(1/30) |
879 | | - |
880 | | - mouse_group.remove(mouse.tilegrid) |
881 | | - await asyncio.sleep(1) |
882 | | - |
883 | | -async def keyboard_task() -> None: |
884 | | - # flush input buffer |
885 | | - while supervisor.runtime.serial_bytes_available: |
886 | | - sys.stdin.read(1) |
887 | 862 |
|
888 | | - while True: |
889 | | - while (c := supervisor.runtime.serial_bytes_available) > 0: |
890 | | - key = sys.stdin.read(c) |
| 863 | + # keyboard input |
| 864 | + if (available := supervisor.runtime.serial_bytes_available) > 0: |
| 865 | + key = sys.stdin.read(available) |
891 | 866 | if key == "\x1b": # escape |
892 | 867 | reset() |
893 | | - await asyncio.sleep(1/30) |
894 | 868 |
|
895 | | -async def main() -> None: |
896 | | - await asyncio.gather( |
897 | | - asyncio.create_task(mouse_task()), |
898 | | - asyncio.create_task(keyboard_task()), |
899 | | - ) |
| 869 | + # mouse input |
| 870 | + if mouse is not None and mouse.update() is not None: |
| 871 | + mouse_state = "left" in mouse.pressed_btns |
| 872 | + if mouse_state and not previous_mouse_state: |
| 873 | + if dialog_buttons.hidden: |
| 874 | + if (clicked_cell := item_grid.which_cell_contains((mouse.x * SCALE, mouse.y * SCALE))) is not None: |
| 875 | + select_application(clicked_cell[1] * PAGE_COLUMNS + clicked_cell[0]) |
| 876 | + elif not right_arrow.hidden and right_arrow.contains((mouse.x, mouse.y, 0)): |
| 877 | + next_page() |
| 878 | + elif not left_arrow.hidden and left_arrow.contains((mouse.x, mouse.y, 0)): |
| 879 | + previous_page() |
| 880 | + elif exit_tg.contains((mouse.x, mouse.y, 0)): |
| 881 | + reset() |
| 882 | + else: |
| 883 | + for button in category_group: |
| 884 | + if button.contains((mouse.x, mouse.y)): |
| 885 | + select_category(button.label) |
| 886 | + break |
| 887 | + else: |
| 888 | + for button in dialog_buttons: |
| 889 | + if button.contains((mouse.x, mouse.y, 0)): |
| 890 | + button.click() |
| 891 | + previous_mouse_state = mouse_state |
900 | 892 |
|
901 | | -try: |
902 | | - asyncio.run(main()) |
903 | 893 | except KeyboardInterrupt: |
904 | 894 | reset() |
0 commit comments