From 70d718a4502eca82816d85763eae0c47389cf064 Mon Sep 17 00:00:00 2001 From: Miskler Date: Sat, 31 Aug 2024 12:03:41 +0300 Subject: [PATCH 1/3] Add waiting_time varible in Mouse class, replace hardcode --- rivalcfg/mouse.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/rivalcfg/mouse.py b/rivalcfg/mouse.py index 6490478..8dc46ce 100644 --- a/rivalcfg/mouse.py +++ b/rivalcfg/mouse.py @@ -76,11 +76,15 @@ class Mouse: #: The mouse settings (:class:`rivalcfg.mouse_settings.MouseSettings`) mouse_settings = None - def __init__(self, hid_device, mouse_profile, mouse_settings): + #: Waiting time for the mouse LED to change color + waiting_led = None + + def __init__(self, hid_device, mouse_profile, mouse_settings, waiting_led=0.05): """Constructor.""" self._hid_device = hid_device self.mouse_profile = mouse_profile self.mouse_settings = mouse_settings + self.waiting_led = waiting_led @property def name(self): @@ -264,7 +268,7 @@ def _hid_write( raise ValueError("Invalid HID report type: %2x" % report_type) # Avoids sending multiple commands to quickly - time.sleep(0.05) + time.sleep(self.waiting_led) def __getattr__(self, name): # Handle every set_xxx methods generated from device's profiles From 3f9b082cbb6befc96e72a80d381aecc530c42dc3 Mon Sep 17 00:00:00 2001 From: Miskler Date: Sat, 31 Aug 2024 12:54:31 +0300 Subject: [PATCH 2/3] renaming and adding protection checks --- rivalcfg/mouse.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/rivalcfg/mouse.py b/rivalcfg/mouse.py index 8dc46ce..ccc0b0c 100644 --- a/rivalcfg/mouse.py +++ b/rivalcfg/mouse.py @@ -76,15 +76,36 @@ class Mouse: #: The mouse settings (:class:`rivalcfg.mouse_settings.MouseSettings`) mouse_settings = None - #: Waiting time for the mouse LED to change color - waiting_led = None + #: Waiting time for the mouse to process and approve the command + # Setting the value too low may cause the device to freeze and potentially even break. + _MIN_COMMAND_APPROVE_DELAY = 0.001 + _command_approve_delay = None - def __init__(self, hid_device, mouse_profile, mouse_settings, waiting_led=0.05): + def __init__(self, hid_device, mouse_profile, mouse_settings, command_approve_delay=0.05): """Constructor.""" self._hid_device = hid_device self.mouse_profile = mouse_profile self.mouse_settings = mouse_settings - self.waiting_led = waiting_led + if command_approve_delay < self._MIN_COMMAND_APPROVE_DELAY: + ValueError(f"command_approve_delay is unsafe to use, with a delay of less than {self._MIN_COMMAND_APPROVE_DELAY} seconds") + else: + self._command_approve_delay = command_approve_delay + + @property + def command_approve_delay(self): + """ + Waiting time for the mouse to process and approve the command + + Setting the value too low may cause the device to freeze and potentially even break. + """ + return self._command_approve_delay + + @command_approve_delay.setter + def command_approve_delay(self, new_value): + if new_value < self._MIN_COMMAND_APPROVE_DELAY: + raise ValueError(f"command_approve_delay is unsafe to use, with a delay of less than {self._MIN_COMMAND_APPROVE_DELAY} seconds") + + self._command_approve_delay = new_value @property def name(self): @@ -268,7 +289,7 @@ def _hid_write( raise ValueError("Invalid HID report type: %2x" % report_type) # Avoids sending multiple commands to quickly - time.sleep(self.waiting_led) + time.sleep(self._command_approve_delay) def __getattr__(self, name): # Handle every set_xxx methods generated from device's profiles From f9681b3083751b017fce254c650cc3689c48f860 Mon Sep 17 00:00:00 2001 From: Miskler Date: Sat, 31 Aug 2024 20:47:36 +0300 Subject: [PATCH 3/3] mini fix --- rivalcfg/mouse.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rivalcfg/mouse.py b/rivalcfg/mouse.py index ccc0b0c..77077a2 100644 --- a/rivalcfg/mouse.py +++ b/rivalcfg/mouse.py @@ -87,7 +87,7 @@ def __init__(self, hid_device, mouse_profile, mouse_settings, command_approve_de self.mouse_profile = mouse_profile self.mouse_settings = mouse_settings if command_approve_delay < self._MIN_COMMAND_APPROVE_DELAY: - ValueError(f"command_approve_delay is unsafe to use, with a delay of less than {self._MIN_COMMAND_APPROVE_DELAY} seconds") + raise ValueError(f"command_approve_delay is unsafe to use, with a delay of less than {self._MIN_COMMAND_APPROVE_DELAY} seconds") else: self._command_approve_delay = command_approve_delay