Remove NamedInts: Convert Spec to enum

Related #2273
This commit is contained in:
MattHag 2024-11-05 02:06:51 +01:00 committed by Peter F. Patel-Schneider
parent f54eeb7998
commit 5ca9c0a6ba
2 changed files with 31 additions and 23 deletions

View File

@ -22,6 +22,7 @@ import struct
import threading import threading
from enum import Flag from enum import Flag
from enum import IntEnum
from typing import Any from typing import Any
from typing import Dict from typing import Dict
from typing import Generator from typing import Generator
@ -671,20 +672,24 @@ SUB_PARAM = { # (byte count, minimum, maximum)
ParamId.SCALE_FACTOR: (SubParam("scale", 2, 0x002E, 0x01FF, "Scale"),), ParamId.SCALE_FACTOR: (SubParam("scale", 2, 0x002E, 0x01FF, "Scale"),),
} }
# Spec Ids for feature GESTURE_2
SPEC = common.NamedInts( class SpecGesture(IntEnum):
DVI_field_width=1, """Spec IDs for feature GESTURE_2."""
field_widths=2,
period_unit=3, DVI_FIELD_WIDTH = 1
resolution=4, FIELD_WIDTHS = 2
multiplier=5, PERIOD_UNIT = 3
sensor_size=6, RESOLUTION = 4
finger_width_and_height=7, MULTIPLIER = 5
finger_major_minor_axis=8, SENSOR_SIZE = 6
finger_force=9, FINGER_WIDTH_AND_HEIGHT = 7
zone=10, FINGER_MAJOR_MINOR_AXIS = 8
) FINGER_FORCE = 9
SPEC._fallback = lambda x: f"unknown:{x:04X}" ZONE = 10
def __str__(self):
return f"{self.name.replace('_', ' ').lower()}"
# Action Ids for feature GESTURE_2 # Action Ids for feature GESTURE_2
ACTION_ID = common.NamedInts( ACTION_ID = common.NamedInts(
@ -836,10 +841,13 @@ class Param:
class Spec: class Spec:
def __init__(self, device, low, high): def __init__(self, device, low: int, high):
self._device = device self._device = device
self.id = low self.id = low
self.spec = SPEC[low] try:
self.spec = SpecGesture(low)
except ValueError:
self.spec = f"unknown:{low:04X}"
self.byte_count = high & 0x0F self.byte_count = high & 0x0F
self._value = None self._value = None

View File

@ -560,14 +560,14 @@ def test_param(responses, prm, id, index, size, value, default_value, write1, wr
@pytest.mark.parametrize( @pytest.mark.parametrize(
"responses, id, s, byte_count, value, string", "responses, id, s, byte_count, expected_value, expected_string",
[ [
(fake_hidpp.responses_gestures, 1, "DVI field width", 1, 8, "[DVI field width=8]"), (fake_hidpp.responses_gestures, 1, hidpp20.SpecGesture.DVI_FIELD_WIDTH, 1, 8, "[dvi field width=8]"),
(fake_hidpp.responses_gestures, 2, "field widths", 1, 8, "[field widths=8]"), (fake_hidpp.responses_gestures, 2, hidpp20.SpecGesture.FIELD_WIDTHS, 1, 8, "[field widths=8]"),
(fake_hidpp.responses_gestures, 3, "period unit", 2, 2048, "[period unit=2048]"), (fake_hidpp.responses_gestures, 3, hidpp20.SpecGesture.PERIOD_UNIT, 2, 2048, "[period unit=2048]"),
], ],
) )
def test_Spec(responses, id, s, byte_count, value, string): def test_spec(responses, id, s, byte_count, expected_value, expected_string):
device = fake_hidpp.Device("GESTURE", responses=responses, feature=hidpp20_constants.SupportedFeature.GESTURE_2) device = fake_hidpp.Device("GESTURE", responses=responses, feature=hidpp20_constants.SupportedFeature.GESTURE_2)
gestures = _hidpp20.get_gestures(device) gestures = _hidpp20.get_gestures(device)
@ -576,8 +576,8 @@ def test_Spec(responses, id, s, byte_count, value, string):
assert spec.id == id assert spec.id == id
assert spec.spec == s assert spec.spec == s
assert spec.byte_count == byte_count assert spec.byte_count == byte_count
assert spec.value == value assert spec.value == expected_value
assert repr(spec) == string assert repr(spec) == expected_string
def test_Gestures(): def test_Gestures():