range

The “range” type alows to pick a value in an input range and transforms it into a value from an output range. If the input value do not correspond to one of the input range step, it is rounded to match the nearest step.

For example with an input range like [0, 1000, 100] and an output range like [0, 10, 1], you can have the following pair:

  • 0 -> 0

  • 100 -> 1

  • 110 -> 1 (110 rounded to 100)

  • 190 -> 2 (190 rounded to 200)

  • 200 -> 2

  • 300 -> 3

  • 1000 -> 10

Device Profile

Example of a range value type in a device profile:

profile = {

    # ...

    "settings": {

        "sensitivity1": {
            "label": "Sensitivity preset 1",
            "description": "Set sensitivity preset 1 (DPI)",
            "cli": ["-s", "--sensitivity1"],
            "report_type": usbhid.HID_REPORT_TYPE_OUTPUT,
            "command": [0x03, 0x01],
            "value_type": "range",
            "input_range": [200, 7200, 100],
            "output_range": [0x04, 0xA7, 2],
            "range_length_byte": 1,  # optional
            "default": 1000,
        },

    },

    # ...

}

CLI

Example of CLI option generated with this handler:

-s SENSITIVITY1, --sensitivity1 SENSITIVITY1
                      Set sensitivity preset 1 (DPI) (from 200 to 7200,
                      default: 1000)

Example of CLI usage:

rivalcfg --sensitivity1=1000

Functions

rivalcfg.handlers.range.add_cli_option(cli_parser, setting_name, setting_info)

Add the given “range” type setting to the given CLI arguments parser.

Parameters:
  • cli_parser (ArgumentParser) – An ArgumentParser instance.

  • setting_name (str) – The name of the setting.

  • setting_info (dict) – The information dict of the setting from the device profile.

rivalcfg.handlers.range.custom_range(start, stop, step)

Helper function that generates a range of integers but allowing to have a float as step.

I am not very proud of this but the Rival 110 requires a step of ~2.33…

Parameters:
  • start (int) – The start of the range.

  • stop (int) – The end of the range.

  • step (float) – The gap between two value in the range.

Return type:

generator(int)

>>> list(custom_range(4, 168, 2.33))
[4, 6, 8, 10, 13, 15, ...160, 162, 164, 167]
rivalcfg.handlers.range.matches_value_in_range(range_start, range_stop, range_step, value)

Helper function that matches the value with the nearest value in the given range.

Parameters:
  • range_start (int) – The start of the range.

  • range_stop (int) – The end of the range.

  • range_step (int) – The gap between two value in the range.

  • value (int) – The value to process.

Return type:

int

>>> matches_value_in_range(0, 100, 10, 40)
40
>>> matches_value_in_range(0, 100, 10, 42)
40
>>> matches_value_in_range(0, 1000, 100, 51)
100
>>> matches_value_in_range(42, 1000, 100, 150)
142
>>> matches_value_in_range(500, 1000, 100, 100)
500
>>> matches_value_in_range(500, 1000, 100, 4000)
1000
rivalcfg.handlers.range.process_range(setting_info, value)

Called by the “range” functions to process ‘value’ with the specified range settings in ‘setting_info’.

Parameters:
  • setting_info (dict) – The information dict of the setting from the device profile.

  • value – The input value.

Return type:

int

rivalcfg.handlers.range.process_value(setting_info, value)

Called by the rivalcfg.mouse.Mouse class when processing a “range” type setting.

Parameters:
  • setting_info (dict) – The information dict of the setting from the device profile.

  • value – The input value.

Return type:

list[int]