Skip to content

Commit

Permalink
Replace print() with guarded debug()
Browse files Browse the repository at this point in the history
  • Loading branch information
xs5871 committed Nov 19, 2024
1 parent 8a318c5 commit cacf8a1
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 47 deletions.
7 changes: 5 additions & 2 deletions kmk/extensions/led.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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'
)
Expand Down
12 changes: 9 additions & 3 deletions kmk/extensions/peg_rgb_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand All @@ -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):
Expand Down
6 changes: 5 additions & 1 deletion kmk/extensions/statusled.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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'
)
Expand Down
8 changes: 3 additions & 5 deletions kmk/handlers/stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 11 additions & 8 deletions kmk/modules/adns9800.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
13 changes: 9 additions & 4 deletions kmk/modules/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 5 additions & 2 deletions kmk/modules/midi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
48 changes: 26 additions & 22 deletions kmk/modules/split.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 = []
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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()

Expand All @@ -287,21 +291,21 @@ 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

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()
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions kmk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ def enabled(self) -> bool:
def enabled(self, enabled: bool):
global _debug_enabled
_debug_enabled = enabled
self('debug.enabled=', enabled)

0 comments on commit cacf8a1

Please sign in to comment.