From 70d718a4502eca82816d85763eae0c47389cf064 Mon Sep 17 00:00:00 2001 From: Miskler Date: Sat, 31 Aug 2024 12:03:41 +0300 Subject: [PATCH 1/9] 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/9] 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/9] 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 From e93dcbd9e5750a22edf3ba00753f67427d80f651 Mon Sep 17 00:00:00 2001 From: Fabien LOISON Date: Mon, 2 Sep 2024 13:13:31 +0200 Subject: [PATCH 4/9] Fixed codding style (black fix) --- rivalcfg/mouse.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/rivalcfg/mouse.py b/rivalcfg/mouse.py index 77077a2..b18a5bd 100644 --- a/rivalcfg/mouse.py +++ b/rivalcfg/mouse.py @@ -81,13 +81,17 @@ class Mouse: _MIN_COMMAND_APPROVE_DELAY = 0.001 _command_approve_delay = None - def __init__(self, hid_device, mouse_profile, mouse_settings, command_approve_delay=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 if command_approve_delay < 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") + 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 @@ -103,8 +107,10 @@ def command_approve_delay(self): @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") - + 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 From 4bd57b8f868402931afafc87d05df5d71b9576d0 Mon Sep 17 00:00:00 2001 From: Fabien LOISON Date: Mon, 2 Sep 2024 13:18:10 +0200 Subject: [PATCH 5/9] Removed duplicated code --- rivalcfg/mouse.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/rivalcfg/mouse.py b/rivalcfg/mouse.py index b18a5bd..9e633e2 100644 --- a/rivalcfg/mouse.py +++ b/rivalcfg/mouse.py @@ -88,12 +88,7 @@ def __init__( self._hid_device = hid_device self.mouse_profile = mouse_profile self.mouse_settings = mouse_settings - if command_approve_delay < 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" - ) - else: - self._command_approve_delay = command_approve_delay + self.command_approve_delay = command_approve_delay @property def command_approve_delay(self): From 9e383704a21d8337ae4987ece91a5e86bbbb82b4 Mon Sep 17 00:00:00 2001 From: Fabien LOISON Date: Mon, 2 Sep 2024 13:21:53 +0200 Subject: [PATCH 6/9] Renamed command_approve_delay to command_delay --- rivalcfg/mouse.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/rivalcfg/mouse.py b/rivalcfg/mouse.py index 9e633e2..7bb10e8 100644 --- a/rivalcfg/mouse.py +++ b/rivalcfg/mouse.py @@ -78,32 +78,29 @@ class Mouse: #: 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 + _MIN_COMMAND_DELAY = 0.001 _command_approve_delay = None - def __init__( - self, hid_device, mouse_profile, mouse_settings, command_approve_delay=0.05 - ): + def __init__(self, hid_device, mouse_profile, mouse_settings, command_delay=0.05): """Constructor.""" self._hid_device = hid_device self.mouse_profile = mouse_profile self.mouse_settings = mouse_settings - self.command_approve_delay = command_approve_delay + self.command_delay = command_delay @property - def command_approve_delay(self): - """ - Waiting time for the mouse to process and approve the command + def command_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 + return self._command_delay - @command_approve_delay.setter - def command_approve_delay(self, new_value): - if new_value < self._MIN_COMMAND_APPROVE_DELAY: + @command_delay.setter + def command_delay(self, new_value): + if new_value < self._MIN_COMMAND_DELAY: raise ValueError( - f"command_approve_delay is unsafe to use, with a delay of less than {self._MIN_COMMAND_APPROVE_DELAY} seconds" + f"command_delay is unsafe to use, with a delay of less than {self._MIN_COMMAND_DELAY} seconds" ) self._command_approve_delay = new_value From eb1da1d0a90948de4f40ad87cf1c44e2cac83733 Mon Sep 17 00:00:00 2001 From: Fabien LOISON Date: Mon, 2 Sep 2024 13:34:06 +0200 Subject: [PATCH 7/9] Fixed and improved command delay doc --- rivalcfg/mouse.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/rivalcfg/mouse.py b/rivalcfg/mouse.py index 7bb10e8..c8aec6d 100644 --- a/rivalcfg/mouse.py +++ b/rivalcfg/mouse.py @@ -55,6 +55,8 @@ class Mouse: :func:`rivalcfg.usbhid.open_device`). :param mouse_profile: One of the rivalcfg mouse profile (provided by :func:`rivalcfg.devices.get_profile`). + :param float command_delay: Waiting time beween two commands to not + overload the device. >>> from rivalcfg import usbhid >>> from rivalcfg import devices @@ -76,8 +78,6 @@ class Mouse: #: The mouse settings (:class:`rivalcfg.mouse_settings.MouseSettings`) mouse_settings = 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_DELAY = 0.001 _command_approve_delay = None @@ -90,9 +90,13 @@ def __init__(self, hid_device, mouse_profile, mouse_settings, command_delay=0.05 @property def command_delay(self): - """Waiting time for the mouse to process and approve the command + """Waiting time beween two commands to not overload the device. - Setting the value too low may cause the device to freeze and potentially even break. + .. WARNING:: + + Setting this value too low can hang the device. Some mice like the + Kinzu v2 are known to become laggy or even to crash when commands + are sent too quickly. """ return self._command_delay From 8f69816401873dbeea8a13f4cc0faf77c2a38f62 Mon Sep 17 00:00:00 2001 From: Fabien LOISON Date: Mon, 2 Sep 2024 13:45:56 +0200 Subject: [PATCH 8/9] Added an env var to speedup the tests --- doc/env.rst | 7 +++++++ noxfile.py | 1 + rivalcfg/mouse.py | 4 +++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/doc/env.rst b/doc/env.rst index ec04510..5ab3cd4 100644 --- a/doc/env.rst +++ b/doc/env.rst @@ -14,6 +14,12 @@ Usage:: RIVALCFG_DRY=1 +RIVALCFG_DEBUG_NO_COMMAND_DELAY +------------------------------- + +When set, this remove the delay between commands. This is only usefull to allow tests to run faster, you may not use this with real devices, they may hang or even crash. See :py:attr:`~rivalcfg.mouse.Mouse.command_delay`. + + RIVALCFG_PROFILE ---------------- @@ -26,3 +32,4 @@ Usage:: For example, to load the Rival 100 profile and list its CLI options:: RIVALCFG_PROFILE=1038:1702 RIVALCFG_DRY=1 rivalcfg --help + diff --git a/noxfile.py b/noxfile.py index 63c99f1..bdbb9ec 100644 --- a/noxfile.py +++ b/noxfile.py @@ -33,6 +33,7 @@ def test(session): "test", env={ "RIVALCFG_DRY": "1", + "RIVALCFG_DEBUG_NO_COMMAND_DELAY": "1", }, ) diff --git a/rivalcfg/mouse.py b/rivalcfg/mouse.py index c8aec6d..a0a77db 100644 --- a/rivalcfg/mouse.py +++ b/rivalcfg/mouse.py @@ -1,3 +1,4 @@ +import os import time from . import usbhid @@ -291,7 +292,8 @@ def _hid_write( raise ValueError("Invalid HID report type: %2x" % report_type) # Avoids sending multiple commands to quickly - time.sleep(self._command_approve_delay) + if "RIVALCFG_DEBUG_NO_COMMAND_DELAY" not in os.environ: + time.sleep(self._command_approve_delay) def __getattr__(self, name): # Handle every set_xxx methods generated from device's profiles From 2b99bb3f0aafc3afb47a8ee54eaa2ca70b475d79 Mon Sep 17 00:00:00 2001 From: Fabien LOISON Date: Mon, 2 Sep 2024 17:51:37 +0200 Subject: [PATCH 9/9] docs: Updated changelog --- README.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index b323ef7..145a48d 100644 --- a/README.rst +++ b/README.rst @@ -248,7 +248,8 @@ Changelog * **[NEXT]** (changes on ``master`` that have not been released yet): - * Nothing yet ;) + * feat: Allow to reduce the delay between two commands sent to devices (@Miskler, #236) + * feat: Added an env var to remove the delay between commands to speedup the tests (@flozz) * **v4.13.0:**