2023-03-23 08:55:34 +01:00
|
|
|
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
|
|
|
|
|
2023-03-23 08:55:34 +01:00
|
|
|
def __call__(self, *message: str, name: Optional[str] = None) -> None:
|
|
|
|
if not name:
|
|
|
|
name = self.name
|
2023-03-15 23:28:36 +01:00
|
|
|
print(ticks_ms(), end=' ')
|
2023-03-23 08:55:34 +01:00
|
|
|
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
|