From adff02e88a3dd995287a392a39150b7e3008dbe0 Mon Sep 17 00:00:00 2001 From: xs5871 Date: Thu, 9 Mar 2023 20:51:38 +0000 Subject: [PATCH] Add deinit method to modules and extensions --- kmk/extensions/__init__.py | 3 +++ kmk/extensions/rgb.py | 4 ++++ kmk/kmk_keyboard.py | 15 +++++++++++++++ kmk/modules/__init__.py | 3 +++ 4 files changed, 25 insertions(+) diff --git a/kmk/extensions/__init__.py b/kmk/extensions/__init__.py index 1eba5e5..33eea68 100644 --- a/kmk/extensions/__init__.py +++ b/kmk/extensions/__init__.py @@ -49,3 +49,6 @@ class Extension: def on_powersave_disable(self, keyboard): raise NotImplementedError + + def deinit(self, keyboard): + pass diff --git a/kmk/extensions/rgb.py b/kmk/extensions/rgb.py index 6f71593..9c06466 100644 --- a/kmk/extensions/rgb.py +++ b/kmk/extensions/rgb.py @@ -249,6 +249,10 @@ class RGB(Extension): def on_powersave_disable(self, sandbox): self._do_update() + def deinit(self, sandbox): + for pixel in self.pixels: + pixel.deinit() + def set_hsv(self, hue, sat, val, index): ''' Takes HSV values and displays it on a single LED/Neopixel diff --git a/kmk/kmk_keyboard.py b/kmk/kmk_keyboard.py index e5a6f9c..44646ac 100644 --- a/kmk/kmk_keyboard.py +++ b/kmk/kmk_keyboard.py @@ -500,6 +500,20 @@ class KMKKeyboard: if debug.enabled: debug(f'Error in {ext}.powersave_disable: {err}') + def deinit(self) -> None: + for module in self.modules: + try: + module.deinit(self) + except Exception as err: + if debug.enabled: + debug(f'Error in {module}.deinit: {err}') + for ext in self.extensions: + try: + ext.deinit(self.sandbox) + except Exception as err: + if debug.enabled: + debug(f'Error in {ext}.deinit: {err}') + def go(self, hid_type=HIDModes.USB, secondary_hid_type=None, **kwargs) -> None: self._init(hid_type=hid_type, secondary_hid_type=secondary_hid_type, **kwargs) try: @@ -508,6 +522,7 @@ class KMKKeyboard: finally: debug('Unexpected error: cleaning up') self._deinit_hid() + self.deinit() def _init( self, diff --git a/kmk/modules/__init__.py b/kmk/modules/__init__.py index b616f07..dd587c3 100644 --- a/kmk/modules/__init__.py +++ b/kmk/modules/__init__.py @@ -41,3 +41,6 @@ class Module: def on_powersave_disable(self, keyboard): raise NotImplementedError + + def deinit(self, keyboard): + pass