Skip to content

Commit 2e9f5e9

Browse files
committed
FruitJam PyPaint: Add support for Composite HID devices
1 parent 76842db commit 2e9f5e9

1 file changed

Lines changed: 43 additions & 5 deletions

File tree

Fruit_Jam/Fruit_Jam_PyPaint/code.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717
"""
1818

1919
import gc
20+
import sys
2021
import time
2122
import supervisor
23+
import atexit
2224
import board
2325
import displayio
26+
import terminalio
27+
from adafruit_display_text import label
2428

2529
try:
26-
from adafruit_usb_host_mouse import find_and_init_boot_mouse
30+
from adafruit_usb_host_mouse import find_and_init_boot_mouse, find_and_init_report_mouse
2731
usb_available = True
2832
except ImportError:
2933
usb_available = False
@@ -208,13 +212,16 @@ def __init__(self, splash, cursor_bmp, screen_width, screen_height, sensitivity=
208212
def find_mouse(self): # pylint: disable=too-many-statements, too-many-locals
209213
"""Find and initialize the USB mouse."""
210214
self.mouse = find_and_init_boot_mouse()
215+
if self.mouse is None:
216+
self.mouse = find_and_init_report_mouse()
217+
self.sensitivity = 1
211218
if self.mouse is None:
212219
print("No mouse found.")
213220
return False
214221

215222
# Change the mouse resolution so it's not too sensitive
216223
self.mouse.display_size = (supervisor.runtime.display.width*self.sensitivity,
217-
supervisor.runtime.display.height*self.sensitivity)
224+
(supervisor.runtime.display.height - terminalio.FONT.get_bounding_box()[1])*self.sensitivity)
218225
return True
219226

220227
def poll(self):
@@ -324,12 +331,24 @@ def _cursor_bitmap_3():
324331

325332
self._display = display
326333
self._w = self._display.width
327-
self._h = self._display.height
334+
self._h = self._display.height - terminalio.FONT.get_bounding_box()[1]
328335
self._x = self._w // 2
329336
self._y = self._h // 2
330337

331338
self._splash = displayio.Group()
332339

340+
self._info_label = label.Label(
341+
terminalio.FONT,
342+
text = "Right Click->Palette:Exit Right Click->Canvas:Fill"[:self._w],
343+
color = 0xFFFFFF,
344+
x = 0,
345+
y = self._h
346+
)
347+
self._info_label.anchor_point = (0.0, 1.0)
348+
self._info_label.anchored_position = (2, display.height - 2)
349+
350+
self._splash.append(self._info_label)
351+
333352
self._bg_bitmap = displayio.Bitmap(self._w, self._h, 1)
334353
self._bg_palette = displayio.Palette(1)
335354
self._bg_palette[0] = Color.BLACK
@@ -386,7 +405,7 @@ def _cursor_bitmap_3():
386405
if not self._poller.mouse:
387406
raise RuntimeError("No mouse found. Please connect a USB mouse.")
388407
else:
389-
raise AttributeError("PyPaint requires a touchscreen or cursor.")
408+
raise AttributeError("PyPaint requires a mouse, touchscreen or cursor.")
390409

391410
self._a_pressed = False
392411
self._last_a_pressed = False
@@ -603,6 +622,8 @@ def _handle_b_release(self, location):
603622
if location[0] >= self._w // 10: # not in color picker
604623
self._fill(location[0], location[1], self._pencolor)
605624
self._poller.poke()
625+
else:
626+
supervisor.reload()
606627

607628
@property
608629
def _was_a_just_pressed(self):
@@ -646,6 +667,23 @@ def run(self):
646667
self._handle_motion(self._last_location, self._location)
647668
time.sleep(0.1)
648669

649-
650670
painter = Paint()
671+
672+
def atexit_callback():
673+
"""
674+
re-attach USB devices to kernel if needed.
675+
:return:
676+
"""
677+
print("inside atexit callback")
678+
if type(painter._poller) == MousePoller:
679+
mouse = painter._poller.mouse
680+
if mouse.was_attached and not mouse.device.is_kernel_driver_active(0):
681+
mouse.device.attach_kernel_driver(0)
682+
# The keyboard buffer seems to have data left over from when it was detached
683+
# This clears it before the next process starts
684+
while supervisor.runtime.serial_bytes_available:
685+
sys.stdin.read(1)
686+
687+
atexit.register(atexit_callback)
688+
651689
painter.run()

0 commit comments

Comments
 (0)