Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/boot_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
i2c.unlock()

if tlv320_present:
fjPeriphs = adafruit_fruitjam.peripherals.Peripherals()
fjPeriphs = adafruit_fruitjam.peripherals.Peripherals(
audio_output=launcher_config.audio_output,
safe_volume_limit=launcher_config.audio_volume_override_danger
)

if launcher_config.audio_output_speaker:
# use speaker
fjPeriphs.audio_output = "speaker"
fjPeriphs.volume = launcher_config.audio_volume

wave_file = "/boot_animation_assets/ada_fruitjam_boot_jingle.wav"
Expand Down
126 changes: 41 additions & 85 deletions src/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@
import supervisor
import sys
import terminalio
import usb

from adafruit_usb_host_mouse import find_and_init_boot_mouse
import adafruit_pathlib as pathlib
from adafruit_bitmap_font import bitmap_font
from adafruit_display_text.text_box import TextBox
from adafruit_display_text.bitmap_label import Label
from adafruit_displayio_layout.layouts.grid_layout import GridLayout
from adafruit_anchored_tilegrid import AnchoredTileGrid
import adafruit_imageload
import adafruit_usb_host_descriptors
from adafruit_anchored_group import AnchoredGroup
from adafruit_fruitjam.peripherals import request_display_config, VALID_DISPLAY_SIZES
from adafruit_argv_file import read_argv, write_argv
Expand Down Expand Up @@ -87,64 +86,19 @@
bg_tg = displayio.TileGrid(bitmap=background_bmp, pixel_shader=bg_palette)
scaled_group.append(bg_tg)

# load the mouse cursor bitmap
mouse_bmp = displayio.OnDiskBitmap("launcher_assets/mouse_cursor.bmp")

# make the background pink pixels transparent
mouse_bmp.pixel_shader.make_transparent(0)

# create a TileGrid for the mouse, using its bitmap and pixel_shader
mouse_tg = displayio.TileGrid(mouse_bmp, pixel_shader=mouse_bmp.pixel_shader)

# move it to the center of the display
mouse_tg.x = display.width // (2 * scale)
mouse_tg.y = display.height // (2 * scale)
# 046d:c52f

# mouse = usb.core.find(idVendor=0x046d, idProduct=0xc52f)
WIDTH = int(298 / 360 * display.width // scale)
HEIGHT = int(182 / 200 * display.height // scale)

DIR_IN = 0x80
mouse_interface_index, mouse_endpoint_address = None, None
mouse = None
mouse_was_attached = None

last_left_button_state = 0
left_button_pressed = False
if launcher_config.use_mouse:

# scan for connected USB device and loop over any found
print("scanning usb")
for device in usb.core.find(find_all=True):
# print device info
print(f"{device.idVendor:04x}:{device.idProduct:04x}")
print(device.manufacturer, device.product)
print()
config_descriptor = adafruit_usb_host_descriptors.get_configuration_descriptor(
device, 0
)
print(config_descriptor)

_possible_interface_index, _possible_endpoint_address = adafruit_usb_host_descriptors.find_boot_mouse_endpoint(device)
if _possible_interface_index is not None and _possible_endpoint_address is not None:
mouse = device
mouse_interface_index = _possible_interface_index
mouse_endpoint_address = _possible_endpoint_address
print(f"mouse interface: {mouse_interface_index} endpoint_address: {hex(mouse_endpoint_address)}")

mouse_was_attached = None
if mouse is not None:
# detach the kernel driver if needed
if mouse.is_kernel_driver_active(0):
mouse_was_attached = True
mouse.detach_kernel_driver(0)
else:
mouse_was_attached = False

# set configuration on the mouse so we can use it
mouse.set_configuration()

mouse_buf = array.array("b", [0] * 8)

WIDTH = int(298 / 360 * display.width // scale)
HEIGHT = int(182 / 200 * display.height // scale)
mouse = find_and_init_boot_mouse()
if mouse:
mouse.scale = scale
mouse_tg = mouse.tilegrid
mouse_tg.x = display.width // (2 * scale)
mouse_tg.y = display.height // (2 * scale)

config = {
"menu_title": "Launcher Menu",
Expand Down Expand Up @@ -376,8 +330,8 @@ def atexit_callback():
:return:
"""
print("inside atexit callback")
if mouse_was_attached and not mouse.is_kernel_driver_active(0):
mouse.attach_kernel_driver(0)
if mouse and mouse.was_attached and not mouse.device.is_kernel_driver_active(0):
mouse.device.attach_kernel_driver(0)


atexit.register(atexit_callback)
Expand Down Expand Up @@ -503,7 +457,6 @@ def handle_key_press(key):


print(f"apps: {apps}")
print(mouse_interface_index, mouse_endpoint_address)
while True:
index = None
editor_index = None
Expand All @@ -519,31 +472,34 @@ def handle_key_press(key):
# app_titles[selected].background_color = launcher_config.palette_accent

if mouse:
try:
# attempt to read data from the mouse
# 10ms timeout, so we don't block long if there
# is no data
count = mouse.read(mouse_endpoint_address, mouse_buf, timeout=20)
except usb.core.USBTimeoutError:
# skip the rest of the loop if there is no data
count = 0

# update the mouse tilegrid x and y coordinates
# based on the delta values read from the mouse
if count > 0:
mouse_tg.x = max(0, min((display.width // scale) - 1, mouse_tg.x + mouse_buf[1]))
mouse_tg.y = max(0, min((display.height // scale) - 1, mouse_tg.y + mouse_buf[2]))

if mouse_buf[0] & (1 << 0) != 0:
print("left click")
clicked_cell = menu_grid.which_cell_contains((mouse_tg.x, mouse_tg.y))
if clicked_cell is not None:
index = (clicked_cell[1] * config["width"] + clicked_cell[0]) + (cur_page * page_size)

if right_tg.contains((mouse_tg.x, mouse_tg.y, 0)):
page_right()
if left_tg.contains((mouse_tg.x, mouse_tg.y, 0)):
page_left()

buttons = mouse.update()

# Extract button states
if buttons is None:
current_left_button_state = 0
else:
current_left_button_state = 1 if 'left' in buttons else 0

# Detect button presses
if current_left_button_state == 1 and last_left_button_state == 0:
left_button_pressed = True
elif current_left_button_state == 0 and last_left_button_state == 1:
left_button_pressed = False

# Update button states
last_left_button_state = current_left_button_state

if left_button_pressed:
print("left click")
clicked_cell = menu_grid.which_cell_contains((mouse_tg.x, mouse_tg.y))
if clicked_cell is not None:
index = (clicked_cell[1] * config["width"] + clicked_cell[0]) + (cur_page * page_size)

if right_tg.contains((mouse_tg.x, mouse_tg.y, 0)):
page_right()
if left_tg.contains((mouse_tg.x, mouse_tg.y, 0)):
page_left()


if index is not None:
Expand Down
8 changes: 8 additions & 0 deletions src/launcher_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ def audio_volume(self) -> int:
def audio_volume(self, value: int) -> None:
self._data["audio"]["volume"] = min(max(value, 1), 20)

@property
def audio_volume_override_danger(self) -> int:
return int(self._data["audio"].get("volume_override_danger", 12))

@audio_volume_override_danger.setter
def audio_volume_override_danger(self, value: int) -> None:
self._data["audio"]["volume_override_danger"] = min(max(value, 1), 20)

@property
def boot_animation(self) -> str:
value = self._data["boot_animation"] if "boot_animation" in self._data else ""
Expand Down