Skip to content

Commit 21ce0d8

Browse files
committed
change usb mouse debug prints to adafruit_logging msgs
1 parent a5a62a1 commit 21ce0d8

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

CircuitPython_PyPaint/code.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,10 @@ class MousePoller(object):
179179
"""Get 'pressed' and location updates from a USB mouse."""
180180

181181
def __init__(self, splash, cursor_bmp, screen_width, screen_height):
182-
logger = logging.getLogger("Paint")
183-
if not logger.hasHandlers():
184-
logger.addHandler(logging.StreamHandler())
185-
logger.debug("Creating a MousePoller")
182+
self._logger = logging.getLogger("Paint")
183+
if not self._logger.hasHandlers():
184+
self._logger.addHandler(logging.StreamHandler())
185+
self._logger.debug("Creating a MousePoller")
186186
self._display_grp = splash
187187
self._cursor_grp = displayio.Group()
188188
self._cur_palette = displayio.Palette(3)
@@ -217,8 +217,8 @@ def __init__(self, splash, cursor_bmp, screen_width, screen_height):
217217
if self.find_mouse():
218218
mouse_found = True
219219
else:
220-
print("WARNING: Mouse not found after multiple attempts.")
221-
print("The application will run, but mouse control may not work.")
220+
self._logger.debug("WARNING: Mouse not found after multiple attempts.")
221+
self._logger.debug("The application will run, but mouse control may not work.")
222222

223223

224224
def find_mouse(self):
@@ -227,12 +227,12 @@ def find_mouse(self):
227227
RETRY_DELAY = 1 # seconds
228228

229229
if not usb_available:
230-
print("USB library not available; cannot find mouse.")
230+
self._logger.debug("USB library not available; cannot find mouse.")
231231
return False
232232

233233
for attempt in range(MAX_ATTEMPTS):
234234
try:
235-
print(f"Mouse detection attempt {attempt+1}/{MAX_ATTEMPTS}")
235+
self._logger.debug(f"Mouse detection attempt {attempt+1}/{MAX_ATTEMPTS}")
236236

237237
# Constants for USB control transfers
238238
DIR_OUT = 0
@@ -246,7 +246,7 @@ def find_mouse(self):
246246

247247
for device in usb.core.find(find_all=True):
248248
devices_found = True
249-
print(f"Found device: {device.idVendor:04x}:{device.idProduct:04x}")
249+
self._logger.debug(f"Found device: {device.idVendor:04x}:{device.idProduct:04x}")
250250

251251
try:
252252
# Try to get device info
@@ -266,18 +266,18 @@ def find_mouse(self):
266266
if has_kernel_driver and device.is_kernel_driver_active(0):
267267
device.detach_kernel_driver(0)
268268
except Exception as e: # pylint: disable=broad-except
269-
print(f"Error detaching kernel driver: {e}")
269+
self._logger.debug(f"Error detaching kernel driver: {e}")
270270

271271
# Set configuration
272272
try:
273273
device.set_configuration()
274274
except Exception as e: # pylint: disable=broad-except
275-
print(f"Error setting configuration: {e}")
275+
self._logger.debug(f"Error setting configuration: {e}")
276276
continue # Try next device
277277

278278
# Just assume endpoint 0x81 (common for mice)
279279
self.in_endpoint = 0x81
280-
print(f"Using mouse: {manufacturer}, {product}")
280+
self._logger.debug(f"Using mouse: {manufacturer}, {product}")
281281

282282
# Set to report protocol mode
283283
try:
@@ -288,49 +288,49 @@ def find_mouse(self):
288288

289289
buf = bytearray(1)
290290
device.ctrl_transfer(bmRequestType, bRequest, wValue, wIndex, buf)
291-
print("Set to report protocol mode")
291+
self._logger.debug("Set to report protocol mode")
292292
except Exception as e: # pylint: disable=broad-except
293-
print(f"Could not set protocol: {e}")
293+
self._logger.debug(f"Could not set protocol: {e}")
294294

295295
# Buffer for reading data
296296
self.buf = array.array("B", [0] * 4)
297-
print("Created 4-byte buffer for mouse data")
297+
self._logger.debug("Created 4-byte buffer for mouse data")
298298

299299
# Verify mouse works by reading from it
300300
try:
301301
# Try to read some data with a short timeout
302302
data = device.read(self.in_endpoint, self.buf, timeout=100)
303-
print(f"Mouse test read successful: {data} bytes")
303+
self._logger.debug(f"Mouse test read successful: {data} bytes")
304304
return True
305305
except usb.core.USBTimeoutError:
306306
# Timeout is normal if mouse isn't moving
307-
print("Mouse connected but not sending data (normal)")
307+
self._logger.debug("Mouse connected but not sending data (normal)")
308308
return True
309309
except Exception as e: # pylint: disable=broad-except
310-
print(f"Mouse test read failed: {e}")
310+
self._logger.debug(f"Mouse test read failed: {e}")
311311
# Continue to try next device or retry
312312
self.mouse = None
313313
self.in_endpoint = None
314314
continue
315315

316316
except Exception as e: # pylint: disable=broad-except
317-
print(f"Error initializing device: {e}")
317+
self._logger.debug(f"Error initializing device: {e}")
318318
continue
319319

320320
if not devices_found:
321-
print("No USB devices found")
321+
self._logger.debug("No USB devices found")
322322

323323
# If we get here without returning, no suitable mouse was found
324-
print(f"No working mouse found on attempt {attempt+1}, retrying...")
324+
self._logger.debug(f"No working mouse found on attempt {attempt+1}, retrying...")
325325
gc.collect()
326326
time.sleep(RETRY_DELAY)
327327

328328
except Exception as e: # pylint: disable=broad-except
329-
print(f"Error during mouse detection: {e}")
329+
self._logger.debug(f"Error during mouse detection: {e}")
330330
gc.collect()
331331
time.sleep(RETRY_DELAY)
332332

333-
print("Failed to find a working mouse after multiple attempts")
333+
self._logger.debug("Failed to find a working mouse after multiple attempts")
334334
return False
335335

336336

@@ -381,14 +381,14 @@ def _process_mouse_input(self):
381381

382382
# Handle disconnections
383383
if e.errno == 19: # No such device
384-
print("Mouse disconnected")
384+
self._logger.debug("Mouse disconnected")
385385
self.mouse = None
386386
self.in_endpoint = None
387387
gc.collect()
388388

389389
return False
390390
except Exception as e: # pylint: disable=broad-except
391-
print(f"Error reading mouse: {type(e).__name__}")
391+
self._logger.debug(f"Error reading mouse: {type(e).__name__}")
392392
return False
393393

394394
if count >= 3: # We need at least buttons, X and Y

0 commit comments

Comments
 (0)