* Declutter and denoise the debug output in general. * Avoid f-strings. They're nice to look at, but use a lot of ROM, RAM, and time during formatting. * Remove all "debug" information from `KMKKeyboard.__repr__`. It's printed out once at init and the info it gave was useless at that point. Even more free memory. * Add a memory footprint debug info after initialization.
		
			
				
	
	
		
			40 lines
		
	
	
		
			817 B
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			40 lines
		
	
	
		
			817 B
		
	
	
	
		
			Python
		
	
	
	
	
	
| try:
 | |
|     from typing import Optional
 | |
| except ImportError:
 | |
|     pass
 | |
| 
 | |
| from supervisor import ticks_ms
 | |
| 
 | |
| 
 | |
| def clamp(x: int, bottom: int = 0, top: int = 100) -> int:
 | |
|     return min(max(bottom, x), top)
 | |
| 
 | |
| 
 | |
| _debug_enabled = False
 | |
| 
 | |
| 
 | |
| class Debug:
 | |
|     '''default usage:
 | |
|     debug = Debug(__name__)
 | |
|     '''
 | |
| 
 | |
|     def __init__(self, name: str = __name__):
 | |
|         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='')
 | |
| 
 | |
|     @property
 | |
|     def enabled(self) -> bool:
 | |
|         global _debug_enabled
 | |
|         return _debug_enabled
 | |
| 
 | |
|     @enabled.setter
 | |
|     def enabled(self, enabled: bool):
 | |
|         global _debug_enabled
 | |
|         _debug_enabled = enabled
 |