Skip to content

Commit

Permalink
Add automatic debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
xs5871 committed Nov 18, 2023
1 parent 689ef82 commit 108c59c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
14 changes: 8 additions & 6 deletions docs/en/debugging.md
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 11 additions & 2 deletions kmk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down
1 change: 1 addition & 0 deletions tests/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion util/aspell.en.pws
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 108c59c

Please sign in to comment.