kmk_firmware/kmk/utils.py

40 lines
817 B
Python
Raw Normal View History

try:
from typing import Optional
except ImportError:
pass
2022-07-20 15:35:56 +02:00
from supervisor import ticks_ms
2022-10-02 20:49:23 +02:00
def clamp(x: int, bottom: int = 0, top: int = 100) -> int:
2021-12-05 18:25:19 +01:00
return min(max(bottom, x), top)
2022-07-20 15:35:56 +02:00
_debug_enabled = False
class Debug:
'''default usage:
debug = Debug(__name__)
'''
2022-10-02 20:49:23 +02:00
def __init__(self, name: str = __name__):
2022-07-20 15:35:56 +02:00
self.name = name
def __call__(self, *message: str, name: Optional[str] = None) -> None:
if not name:
name = self.name
print(ticks_ms(), end=' ')
print(name, end=': ')
print(*message, sep='')
2022-07-20 15:35:56 +02:00
@property
2022-10-02 20:49:23 +02:00
def enabled(self) -> bool:
2022-07-20 15:35:56 +02:00
global _debug_enabled
return _debug_enabled
@enabled.setter
2022-10-02 20:49:23 +02:00
def enabled(self, enabled: bool):
2022-07-20 15:35:56 +02:00
global _debug_enabled
_debug_enabled = enabled