From 9ef192cf5b7163170583a0c106c540cac1b616c7 Mon Sep 17 00:00:00 2001 From: xs5871 Date: Thu, 9 Nov 2023 11:00:32 +0000 Subject: [PATCH] Add automatic debug logs --- docs/en/debugging.md | 14 ++++++++------ kmk/utils.py | 13 +++++++++++-- tests/mocks.py | 1 + util/aspell.en.pws | 3 ++- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/docs/en/debugging.md b/docs/en/debugging.md index 61145ee3e..b026f9790 100644 --- a/docs/en/debugging.md +++ b/docs/en/debugging.md @@ -1,11 +1,13 @@ # Debugging -Debug will output most of the useful state to the console. This can be enable in your firmware -by setting this in your keymap. NOTE that it will be slower, so only enable this when you -need debugging. +KMK's debug output is written to CircuitPython's serial console -- the one that's +used for the REPL -- and is automatically enabled if it detects a connection to +that console. +It can also be enabled manually, though that shouldn't be necessary in +general: ```python keyboard.debug_enabled = True ``` -The output can be viewed by connecting to the serial port of the keybord. Please refer to [THIS](https://learn.adafruit.com/welcome-to-circuitpython/kattni-connecting-to-the-serial-console) for -more information when connecting to serial console. For Linux users, we recommend [picocom](https://github.com/npat-efault/picocom) or -[screen](https://www.gnu.org/software/screen/manual/screen.html) +Follow for example Adafruit's beginners guide on [how to connect to the serial console](https://learn.adafruit.com/welcome-to-circuitpython/kattni-connecting-to-the-serial-console). +For Linux users, we recommend [picocom](https://github.com/npat-efault/picocom) +or [screen](https://www.gnu.org/software/screen/manual/screen.html) diff --git a/kmk/utils.py b/kmk/utils.py index b27a04ba6..f6e6494ea 100644 --- a/kmk/utils.py +++ b/kmk/utils.py @@ -5,12 +5,14 @@ from supervisor import ticks_ms +from usb_cdc import console + def clamp(x: int, bottom: int = 0, top: int = 100) -> int: return min(max(bottom, x), top) -_debug_enabled = False +_debug_enabled = None class Debug: @@ -31,7 +33,14 @@ def __call__(self, *message: str, name: Optional[str] = None) -> None: @property def enabled(self) -> bool: global _debug_enabled - return _debug_enabled + if ( + _debug_enabled is None + and console + and console.connected + and console.out_waiting == 0 + ): + return True + return bool(_debug_enabled) @enabled.setter def enabled(self, enabled: bool): diff --git a/tests/mocks.py b/tests/mocks.py index 991ca83b9..923f47819 100644 --- a/tests/mocks.py +++ b/tests/mocks.py @@ -31,6 +31,7 @@ def init_circuit_python_modules_mocks(): sys.modules['supervisor'] = Mock() sys.modules['supervisor'].ticks_ms = ticks_ms + sys.modules['usb_cdc'] = Mock() from . import task diff --git a/util/aspell.en.pws b/util/aspell.en.pws index 4a76dcdd1..952c8a05a 100644 --- a/util/aspell.en.pws +++ b/util/aspell.en.pws @@ -1,10 +1,11 @@ -personal_ws-1.1 en 345 +personal_ws-1.1 en 347 ADNS AMS ANAVI APA AVR Adafruit +Adafruit's Affero BT BYO