diff --git a/kmk/extensions/led.py b/kmk/extensions/led.py index 1cdcbc508..cd155efb7 100644 --- a/kmk/extensions/led.py +++ b/kmk/extensions/led.py @@ -3,7 +3,9 @@ from kmk.extensions import Extension, InvalidExtensionEnvironment from kmk.keys import Key, make_argumented_key, make_key -from kmk.utils import clamp +from kmk.utils import Debug, clamp + +debug = Debug(__name__) class LEDKey(Key): @@ -46,7 +48,8 @@ def __init__( try: self._leds = [pwmio.PWMOut(pin) for pin in pins_iter] except Exception as e: - print(e) + if debug.enabled: + debug(e) raise InvalidExtensionEnvironment( 'Unable to create pwmio.PWMOut() instance with provided led_pin' ) diff --git a/kmk/extensions/peg_rgb_matrix.py b/kmk/extensions/peg_rgb_matrix.py index 1e81d635e..816dc5783 100644 --- a/kmk/extensions/peg_rgb_matrix.py +++ b/kmk/extensions/peg_rgb_matrix.py @@ -4,6 +4,9 @@ from kmk.extensions import Extension from kmk.keys import make_key +from kmk.utils import Debug + +debug = Debug(__name__) class Color: @@ -26,10 +29,12 @@ class Color: class Rgb_matrix_data: def __init__(self, keys=[], underglow=[]): if len(keys) == 0: - print('No colors passed for your keys') + if debug.enabled: + debug('No colors passed for your keys') return if len(underglow) == 0: - print('No colors passed for your underglow') + if debug.enabled: + debug('No colors passed for your underglow') return self.data = keys + underglow @@ -39,7 +44,8 @@ def generate_led_map( ): keys = [key_color] * number_of_keys underglow = [underglow_color] * number_of_underglow - print(f'Rgb_matrix_data(keys={keys},\nunderglow={underglow})') + if debug.enabled: + debug('Rgb_matrix_data(keys=', keys, ', nunderglow=', underglow, ')') class Rgb_matrix(Extension): diff --git a/kmk/extensions/statusled.py b/kmk/extensions/statusled.py index 3e56f5aef..7610ca121 100644 --- a/kmk/extensions/statusled.py +++ b/kmk/extensions/statusled.py @@ -5,6 +5,9 @@ from kmk.extensions import Extension, InvalidExtensionEnvironment from kmk.keys import make_key +from kmk.utils import Debug + +debug = Debug(__name__) class statusLED(Extension): @@ -20,7 +23,8 @@ def __init__( try: self._leds.append(pwmio.PWMOut(led)) except Exception as e: - print(e) + if debug.enabled: + debug(e) raise InvalidExtensionEnvironment( 'Unable to create pulseio.PWMOut() instance with provided led_pin' ) diff --git a/kmk/handlers/stock.py b/kmk/handlers/stock.py index 6921908c1..5ac5d0193 100644 --- a/kmk/handlers/stock.py +++ b/kmk/handlers/stock.py @@ -25,12 +25,10 @@ def bootloader(*args, **kwargs): def debug_pressed(key, keyboard, KC, *args, **kwargs): - if keyboard.debug_enabled: - print('DebugDisable()') - else: - print('DebugEnable()') + from kmk.utils import Debug - keyboard.debug_enabled = not keyboard.debug_enabled + debug = Debug() + debug.enabled = not debug.enabled return keyboard diff --git a/kmk/modules/adns9800.py b/kmk/modules/adns9800.py index 108b1dd52..6b0302332 100644 --- a/kmk/modules/adns9800.py +++ b/kmk/modules/adns9800.py @@ -7,6 +7,9 @@ from kmk.keys import AX from kmk.modules import Module from kmk.modules.adns9800_firmware import firmware +from kmk.utils import Debug + +debug = Debug(__name__) class REG: @@ -177,17 +180,17 @@ def during_bootup(self, keyboard): self.adns_write(REG.Configuration_I, 0x10) microcontroller.delay_us(self.tsww) - if keyboard.debug_enabled: - print('ADNS: Product ID ', hex(self.adns_read(REG.Product_ID))) + if debug.enabled: + debug('ADNS: Product ID ', hex(self.adns_read(REG.Product_ID))) microcontroller.delay_us(self.tsrr) - print('ADNS: Revision ID ', hex(self.adns_read(REG.Revision_ID))) + debug('ADNS: Revision ID ', hex(self.adns_read(REG.Revision_ID))) microcontroller.delay_us(self.tsrr) - print('ADNS: SROM ID ', hex(self.adns_read(REG.SROM_ID))) + debug('ADNS: SROM ID ', hex(self.adns_read(REG.SROM_ID))) microcontroller.delay_us(self.tsrr) if self.adns_read(REG.Observation) & 0x20: - print('ADNS: Sensor is running SROM') + debug('ADNS: Sensor is running SROM') else: - print('ADNS: Error! Sensor is not running SROM!') + debug('ADNS: Error! Sensor is not running SROM!') return @@ -208,8 +211,8 @@ def before_matrix_scan(self, keyboard): if delta_y: AX.Y.move(keyboard, delta_y) - if keyboard.debug_enabled: - print('Delta: ', delta_x, ' ', delta_y) + if debug.enabled: + debug('Delta: ', delta_x, ' ', delta_y) def after_matrix_scan(self, keyboard): return diff --git a/kmk/modules/encoder.py b/kmk/modules/encoder.py index 43c18b338..ca63610a2 100644 --- a/kmk/modules/encoder.py +++ b/kmk/modules/encoder.py @@ -5,6 +5,9 @@ from supervisor import ticks_ms from kmk.modules import Module +from kmk.utils import Debug + +debug = Debug(__name__) # NB : not using rotaryio as it requires the pins to be consecutive @@ -107,7 +110,6 @@ def button_event(self): # return knob velocity as milliseconds between position changes (detents) # for backwards compatibility def vel_report(self): - # print(self._velocity) return self._velocity @@ -178,7 +180,8 @@ def __init__(self, i2c, address, is_inverted=False): try: from adafruit_seesaw import digitalio, neopixel, rotaryio, seesaw except ImportError: - print('seesaw missing') + if debug.enabled: + debug('seesaw missing') return super().__init__(is_inverted) @@ -189,7 +192,8 @@ def __init__(self, i2c, address, is_inverted=False): seesaw_product = (self.seesaw.get_version() >> 16) & 0xFFFF if seesaw_product != 4991: - print('Wrong firmware loaded? Expected 4991') + if debug.enabled: + debug('Wrong firmware loaded? Expected 4991') self.encoder = rotaryio.IncrementalEncoder(self.seesaw) self.seesaw.pin_mode(24, self.seesaw.INPUT_PULLUP) @@ -281,7 +285,8 @@ def during_bootup(self, keyboard): ) self.encoders.append(new_encoder) except Exception as e: - print(e) + if debug.enabled: + debug(e) return def on_move_do(self, keyboard, encoder_id, state): diff --git a/kmk/modules/midi.py b/kmk/modules/midi.py index 2cee603c0..ca339f983 100644 --- a/kmk/modules/midi.py +++ b/kmk/modules/midi.py @@ -10,6 +10,9 @@ from kmk.keys import Key, make_argumented_key from kmk.modules import Module +from kmk.utils import Debug + +debug = Debug(__name__) class MidiKey(Key): @@ -73,8 +76,8 @@ def __init__(self): self.midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=0) except IndexError: self.midi = None - # if debug_enabled: - print('No midi device found.') + if debug.enabled: + debug('No midi device found.') def during_bootup(self, keyboard): return None diff --git a/kmk/modules/split.py b/kmk/modules/split.py index bbf9cfc67..a54b3e93f 100644 --- a/kmk/modules/split.py +++ b/kmk/modules/split.py @@ -10,6 +10,9 @@ from kmk.hid import HIDModes from kmk.kmktime import check_deadline from kmk.modules import Module +from kmk.utils import Debug + +debug = Debug(__name__) class SplitSide: @@ -38,7 +41,6 @@ def __init__( data_pin2=None, uart_flip=True, use_pio=False, - debug_enabled=False, ): self._is_target = True self._uart_buffer = [] @@ -53,7 +55,6 @@ def __init__( self._use_pio = use_pio self._uart = None self._uart_interval = uart_interval - self._debug_enabled = debug_enabled self.uart_header = bytearray([0xB2]) # Any non-zero byte should work if self.split_type == SplitType.BLE: @@ -68,7 +69,8 @@ def __init__( self.ProvideServicesAdvertisement = ProvideServicesAdvertisement self.UARTService = UARTService except ImportError: - print('BLE Import error') + if debug.enabled: + debug('BLE Import error') return # BLE isn't supported on this platform self._ble_last_scan = ticks_ms() - 5000 self._connection_count = 0 @@ -158,8 +160,9 @@ def during_bootup(self, keyboard): cm.append(cols_to_calc * (rows_to_calc + ridx) + cidx) keyboard.coord_mapping = tuple(cm) - else: - print('Error: please provide coord_mapping for custom scanner') + + if not keyboard.coord_mapping and debug.enabled: + debug('Error: please provide coord_mapping for custom scanner') if self.split_side == SplitSide.RIGHT: offset = self.split_offset @@ -190,7 +193,8 @@ def after_matrix_scan(self, keyboard): elif self.split_type == SplitType.ONEWIRE: pass # Protocol needs written else: - print('Unexpected case in after_matrix_scan') + if debug.enabled: + debug('Unexpected case in after_matrix_scan') return @@ -264,21 +268,21 @@ def _initiator_scan(self): break if not self._uart: - if self._debug_enabled: - print('Scanning') + if debug.enabled: + debug('Scanning') self._ble.stop_scan() for adv in self._ble.start_scan( self.ProvideServicesAdvertisement, timeout=20 ): - if self._debug_enabled: - print('Scanning') + if debug.enabled: + debug('Scanning') if self.UARTService in adv.services and adv.rssi > -70: self._uart_connection = self._ble.connect(adv) self._uart_connection.connection_interval = 11.25 self._uart = self._uart_connection[self.UARTService] self._ble.stop_scan() - if self._debug_enabled: - print('Scan complete') + if debug.enabled: + debug('Scan complete') break self._ble.stop_scan() @@ -287,8 +291,8 @@ def _target_advertise(self): # Give previous advertising some time to complete if self._advertising: if self._check_if_split_connected(): - if self._debug_enabled: - print('Advertising complete') + if debug.enabled: + debug('Advertising complete') self._ble.stop_advertising() self._advertising = False return @@ -296,12 +300,12 @@ def _target_advertise(self): if not self.ble_rescan_timer(): return - if self._debug_enabled: - print('Advertising not answered') + if debug.enabled: + debug('Advertising not answered') self._ble.stop_advertising() - if self._debug_enabled: - print('Advertising') + if debug.enabled: + debug('Advertising') # Uart must not change on this connection if reconnecting if not self._uart: self._uart = self.UARTService() @@ -337,11 +341,11 @@ def _send_ble(self, update): try: self._uart.disconnect() except: # noqa: E722 - if self._debug_enabled: - print('UART disconnect failed') + if debug.enabled: + debug('UART disconnect failed') - if self._debug_enabled: - print('Connection error') + if debug.enabled: + debug('Connection error') self._uart_connection = None self._uart = None diff --git a/kmk/utils.py b/kmk/utils.py index f6e6494ea..e0830d9d2 100644 --- a/kmk/utils.py +++ b/kmk/utils.py @@ -46,3 +46,4 @@ def enabled(self) -> bool: def enabled(self, enabled: bool): global _debug_enabled _debug_enabled = enabled + self('debug.enabled=', enabled)