Remove callback, update docs

This commit is contained in:
Matthew Hendrix 2022-08-08 09:47:48 -04:00 committed by xs5871
parent ad3c261c1a
commit 7f1e7a2708
2 changed files with 26 additions and 21 deletions

View File

@ -24,30 +24,39 @@ when the lock is enabled and `False` otherwise.
## React to Lock Status Changes ## React to Lock Status Changes
Lock Status will accept a callback function that is invoked when the status of The best way to react to changes in lock status is to extend
a lock changes. When the function is invoked, it will have a Lock Status object the LockStatus class. When a lock status change happens,
passed to it. the 'after_hid_send' function is envoked so you would override
LockStatus's to inject your own logic. Be aware though that
this function is also critically important to the functionality
of LockStatus so be sure to invoke the 'super()' version of your
class to trigger the default functionality of LockStatus.
```python ```python
import board # in your main.py
from kb import KMKKeyboard from kb import KMKKeyboard
from kmk.extensions.lock_status import LockStatus from kmk.extensions.lock_status import LockStatus
from kmk.extensions.LED import LED from kmk.extensions.LED import LED
keyboard = KMKKeyboard() keyboard = KMKKeyboard()
keyboard.extensions.append(LED(led_pin=[board.GP27, board.GP28])) leds = LED(led_pin=[board.GP27, board.GP28])
def toggle_lock_leds(self): class LEDLockStatus(LockStatus):
if self.get_caps_lock(): def set_lock_leds(self):
keyboard.leds.set_brightness(50, leds=[0]) if self.get_caps_lock():
else: leds.set_brightness(50, leds=[0])
keyboard.leds.set_brightness(0, leds=[0]) else:
leds.set_brightness(0, leds=[0])
if self.get_scroll_lock(): if self.get_scroll_lock():
keyboard.leds.set_brightness(50, leds=[1]) leds.set_brightness(50, leds=[1])
else: else:
keyboard.leds.set_brightness(0, leds=[1]) leds.set_brightness(0, leds=[1])
keyboard.extensions.append(LockStatus(toggle_lock_leds)) def after_hid_send(self, sandbox):
super().after_hid_send() # Critically important. Do not forget
self.set_lock_leds()
keyboard.extensions.append(leds)
keyboard.extensions.append(LEDLockStatus())
``` ```

View File

@ -14,10 +14,9 @@ class LockCode:
class LockStatus(Extension): class LockStatus(Extension):
def __init__(self, fn=None): def __init__(self):
self.report = None self.report = None
self.hid = None self.hid = None
self.fn = fn
for device in usb_hid.devices: for device in usb_hid.devices:
if device.usage == HIDUsage.KEYBOARD: if device.usage == HIDUsage.KEYBOARD:
self.hid = device self.hid = device
@ -43,8 +42,6 @@ class LockStatus(Extension):
report = self.hid.get_last_received_report() report = self.hid.get_last_received_report()
if report[0] != self.report: if report[0] != self.report:
self.report = report[0] self.report = report[0]
if self.fn:
self.fn(self)
return return
def on_powersave_enable(self, sandbox): def on_powersave_enable(self, sandbox):
@ -67,4 +64,3 @@ class LockStatus(Extension):
def get_kana(self): def get_kana(self):
return bool(self.report & LockCode.KANA) return bool(self.report & LockCode.KANA)