Skip to content

Commit

Permalink
Merge branch 'Miskler-add-setting-wait-time'
Browse files Browse the repository at this point in the history
  • Loading branch information
flozz committed Sep 2, 2024
2 parents d758c2a + 2b99bb3 commit a4f7e61
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:**

Expand Down
7 changes: 7 additions & 0 deletions doc/env.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------

Expand All @@ -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

1 change: 1 addition & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def test(session):
"test",
env={
"RIVALCFG_DRY": "1",
"RIVALCFG_DEBUG_NO_COMMAND_DELAY": "1",
},
)

Expand Down
33 changes: 31 additions & 2 deletions rivalcfg/mouse.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import time

from . import usbhid
Expand Down Expand Up @@ -55,6 +56,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
Expand All @@ -76,11 +79,36 @@ class Mouse:
#: The mouse settings (:class:`rivalcfg.mouse_settings.MouseSettings`)
mouse_settings = None

def __init__(self, hid_device, mouse_profile, mouse_settings):
_MIN_COMMAND_DELAY = 0.001
_command_approve_delay = None

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_delay = command_delay

@property
def command_delay(self):
"""Waiting time beween two commands to not overload the device.
.. 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

@command_delay.setter
def command_delay(self, new_value):
if new_value < self._MIN_COMMAND_DELAY:
raise ValueError(
f"command_delay is unsafe to use, with a delay of less than {self._MIN_COMMAND_DELAY} seconds"
)

self._command_approve_delay = new_value

@property
def name(self):
Expand Down Expand Up @@ -264,7 +292,8 @@ def _hid_write(
raise ValueError("Invalid HID report type: %2x" % report_type)

# Avoids sending multiple commands to quickly
time.sleep(0.05)
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
Expand Down

0 comments on commit a4f7e61

Please sign in to comment.