hidpp20: Remove dependency to NamedInts

Replace ButtonBehaviors and ButtonMappingTypes with IntEnum.

Related #2273
This commit is contained in:
MattHag 2025-01-02 15:26:33 +01:00 committed by Peter F. Patel-Schneider
parent bd00cc97ad
commit 81567a98df
1 changed files with 40 additions and 27 deletions

View File

@ -1159,8 +1159,19 @@ class RGBEffectsInfo(LEDEffectsInfo): # effects that the LEDs can do using RGB_
self.zones.append(LEDZoneInfo(SupportedFeature.RGB_EFFECTS, 0x00, 1, 0x00, device, i)) self.zones.append(LEDZoneInfo(SupportedFeature.RGB_EFFECTS, 0x00, 1, 0x00, device, i))
ButtonBehaviors = common.NamedInts(MacroExecute=0x0, MacroStop=0x1, MacroStopAll=0x2, Send=0x8, Function=0x9) class ButtonBehavior(IntEnum):
ButtonMappingTypes = common.NamedInts(No_Action=0x0, Button=0x1, Modifier_And_Key=0x2, Consumer_Key=0x3) MACRO_EXECUTE = 0x0
MACRO_STOP = 0x1
MACRO_STOP_ALL = 0x2
SEND = 0x8
FUNCTION = 0x9
class ButtonMappingType(IntEnum):
NO_ACTION = 0x0
BUTTON = 0x1
MODIFIER_AND_KEY = 0x2
CONSUMER_KEY = 0x3
class ButtonFunctions(IntEnum): class ButtonFunctions(IntEnum):
@ -1209,27 +1220,29 @@ class Button:
@classmethod @classmethod
def from_bytes(cls, bytes_) -> Button: def from_bytes(cls, bytes_) -> Button:
behavior_id = bytes_[0] >> 4 behavior = bytes_[0] >> 4
behavior = ButtonBehaviors[behavior_id] if behavior == ButtonBehavior.MACRO_EXECUTE or behavior == ButtonBehavior.MACRO_STOP:
if behavior == ButtonBehaviors.MacroExecute or behavior == ButtonBehaviors.MacroStop:
sector = ((bytes_[0] & 0x0F) << 8) + bytes_[1] sector = ((bytes_[0] & 0x0F) << 8) + bytes_[1]
address = (bytes_[2] << 8) + bytes_[3] address = (bytes_[2] << 8) + bytes_[3]
result = cls(behavior=behavior, sector=sector, address=address) result = cls(behavior=behavior, sector=sector, address=address)
elif behavior == ButtonBehaviors.Send: elif behavior == ButtonBehavior.SEND:
mapping_type = ButtonMappingTypes[bytes_[1]] try:
if mapping_type == ButtonMappingTypes.Button: mapping_type = ButtonMappingType(bytes_[1]).value
value = ButtonButtons[(bytes_[2] << 8) + bytes_[3]] if mapping_type == ButtonMappingType.BUTTON:
result = cls(behavior=behavior, type=mapping_type, value=value) value = ButtonButtons[(bytes_[2] << 8) + bytes_[3]]
elif mapping_type == ButtonMappingTypes.Modifier_And_Key: result = cls(behavior=behavior, type=mapping_type, value=value)
modifiers = bytes_[2] elif mapping_type == ButtonMappingType.MODIFIER_AND_KEY:
value = ButtonKeys[bytes_[3]] modifiers = bytes_[2]
result = cls(behavior=behavior, type=mapping_type, modifiers=modifiers, value=value) value = ButtonKeys[bytes_[3]]
elif mapping_type == ButtonMappingTypes.Consumer_Key: result = cls(behavior=behavior, type=mapping_type, modifiers=modifiers, value=value)
value = ButtonConsumerKeys[(bytes_[2] << 8) + bytes_[3]] elif mapping_type == ButtonMappingType.CONSUMER_KEY:
result = cls(behavior=behavior, type=mapping_type, value=value) value = ButtonConsumerKeys[(bytes_[2] << 8) + bytes_[3]]
elif mapping_type == ButtonMappingTypes.No_Action: result = cls(behavior=behavior, type=mapping_type, value=value)
result = cls(behavior=behavior, type=mapping_type) elif mapping_type == ButtonMappingType.NO_ACTION:
elif behavior == ButtonBehaviors.Function: result = cls(behavior=behavior, type=mapping_type)
except Exception:
pass
elif behavior == ButtonBehavior.FUNCTION:
second_byte = bytes_[1] second_byte = bytes_[1]
try: try:
btn_func = ButtonFunctions(second_byte).value btn_func = ButtonFunctions(second_byte).value
@ -1243,20 +1256,20 @@ class Button:
def to_bytes(self): def to_bytes(self):
bytes = common.int2bytes(self.behavior << 4, 1) if self.behavior is not None else None bytes = common.int2bytes(self.behavior << 4, 1) if self.behavior is not None else None
if self.behavior == ButtonBehaviors.MacroExecute or self.behavior == ButtonBehaviors.MacroStop: if self.behavior == ButtonBehavior.MACRO_EXECUTE.value or self.behavior == ButtonBehavior.MACRO_STOP.value:
bytes = common.int2bytes((self.behavior << 12) + self.sector, 2) + common.int2bytes(self.address, 2) bytes = common.int2bytes((self.behavior << 12) + self.sector, 2) + common.int2bytes(self.address, 2)
elif self.behavior == ButtonBehaviors.Send: elif self.behavior == ButtonBehavior.SEND.value:
bytes += common.int2bytes(self.type, 1) bytes += common.int2bytes(self.type, 1)
if self.type == ButtonMappingTypes.Button: if self.type == ButtonMappingType.BUTTON:
bytes += common.int2bytes(self.value, 2) bytes += common.int2bytes(self.value, 2)
elif self.type == ButtonMappingTypes.Modifier_And_Key: elif self.type == ButtonMappingType.MODIFIER_AND_KEY:
bytes += common.int2bytes(self.modifiers, 1) bytes += common.int2bytes(self.modifiers, 1)
bytes += common.int2bytes(self.value, 1) bytes += common.int2bytes(self.value, 1)
elif self.type == ButtonMappingTypes.Consumer_Key: elif self.type == ButtonMappingType.CONSUMER_KEY:
bytes += common.int2bytes(self.value, 2) bytes += common.int2bytes(self.value, 2)
elif self.type == ButtonMappingTypes.No_Action: elif self.type == ButtonMappingType.NO_ACTION:
bytes += b"\xff\xff" bytes += b"\xff\xff"
elif self.behavior == ButtonBehaviors.Function: elif self.behavior == ButtonBehavior.FUNCTION:
data = common.int2bytes(self.data, 1) if self.data else b"\x00" data = common.int2bytes(self.data, 1) if self.data else b"\x00"
bytes += common.int2bytes(self.value, 1) + b"\xff" + data bytes += common.int2bytes(self.value, 1) + b"\xff" + data
else: else: