8000 Merge pull request #10128 from FoamyGuy/mouse_examples · cmarxmeier/circuitpython@130284a · GitHub
[go: up one dir, main page]

Skip to content

Commit 130284a

Browse files
authored
Merge pull request adafruit#10128 from FoamyGuy/mouse_examples
fix basic_mouse test and add displayio_mouse test
2 parents 6f9fd36 + aae26fb commit 130284a

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

tests/circuitpython-manual/usb/basic_mouse.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
if device.is_kernel_driver_active(0):
1818
device.detach_kernel_driver(0)
1919

20+
device.set_configuration()
21+
2022
# Boot mice have 4 byte reports
2123
buf = array.array("b", [0] * 4)
2224
report_count = 0
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
import terminalio
4+
import array
5+
import usb.core
6+
import supervisor
7+
from displayio import Group, OnDiskBitmap, TileGrid
8+
from adafruit_display_text.bitmap_label import Label
9+
10+
display = supervisor.runtime.display
11+
12+
main_group = Group()
13+
display.root_group = main_group
14+
15+
mouse_bmp = OnDiskBitmap("mouse_cursor.bmp")
16+
mouse_bmp.pixel_shader.make_transparent(0)
17+
mouse_tg = TileGrid(mouse_bmp, pixel_shader=mouse_bmp.pixel_shader)
18+
mouse_tg.x = display.width // 2
19+
mouse_tg.y = display.height // 2
20+
21+
output_lbl = Label(terminalio.FONT, text=f"{mouse_tg.x},{mouse_tg.y}", color=0xFFFFFF, scale=1)
22+
output_lbl.anchor_point = (0, 0)
23+
output_lbl.anchored_position = (1, 1)
24+
main_group.append(output_lbl)
25+
26+
main_group.append(mouse_tg)
27+
28+
# This is a basic Microsoft optical mouse with two buttons and a wheel that can
29+
# also be pressed.
30+
USB_VID = 0x046D
31+
USB_PID = 0xC52F
32+
# This is ordered by bit position.
33+
BUTTONS = ["left", "right", "middle"]
34+
35+
for device in usb.core.find(find_all=True):
36+
print(f"{device.idVendor:04x}:{device.idProduct:04x}&q 8000 uot;)
37+
print(device.manufacturer, device.product)
38+
print(device.serial_number)
39+
if device.idVendor == USB_VID and device.idProduct == USB_PID:
40+
mouse = device
41+
#
42+
print(mouse.manufacturer, mouse.product)
43+
44+
if mouse.is_kernel_driver_active(0):
45+
mouse.detach_kernel_driver(0)
46+
47+
mouse.set_configuration()
48+
49+
# Boot mice have 4 byte reports
50+
buf = array.array("b", [0] * 4)
51+
report_count = 0
52+
53+
# try:
54+
while True:
55+
try:
56+
count = mouse.read(0x81, buf, timeout=10)
57+
except usb.core.USBTimeoutError:
58+
continue
59+
60+
mouse_tg.x = max(0, min(display.width - 1, mouse_tg.x + buf[1]))
61+
mouse_tg.y = max(0, min(display.height - 1, mouse_tg.y + buf[2]))
62+
out_str = f"{mouse_tg.x},{mouse_tg.y}"
63+
for i, button in enumerate(BUTTONS):
64+
if buf[0] & (1 << i) != 0:
65+
out_str += f" {button}"
66+
67+
output_lbl.text = out_str
198 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)
0