Skip to content

Commit

Permalink
device: better handing of unknown values in profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
pfps committed Feb 6, 2024
1 parent 0cd5393 commit f8b25df
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lib/logitech_receiver/hidpp20.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,10 +1313,10 @@ def from_bytes(cls, bytes):
value = ButtonConsumerKeys[(bytes[2] << 8) + bytes[3]]
result = cls(behavior=behavior, type=mapping_type, value=value)
elif behavior == ButtonBehaviors.Function:
value = ButtonFunctions[bytes[1]]
value = ButtonFunctions[bytes[1]] if ButtonFunctions[bytes[1]] is not None else bytes[1]
result = cls(behavior=behavior, value=value)
else:
result = cls(behavior=None)
result = cls(behavior=bytes[0] >> 4, bytes=bytes)
return result

def to_bytes(self):
Expand All @@ -1336,7 +1336,7 @@ def to_bytes(self):
elif self.behavior == ButtonBehaviors.Function:
bytes += _int2bytes(self.value, 1) + b'\xff\x00'
else:
bytes = b'\xff\xff\xff\xff'
bytes = self.bytes if self.bytes else b'\xff\xff\xff\xff'
return bytes

def __repr__(self):
Expand All @@ -1349,7 +1349,6 @@ def __repr__(self):
_yaml.add_representer(Button, Button.to_yaml)


# Doesn't handle light information (feature x8070)
class OnboardProfile:
"""A single onboard profile"""

Expand Down Expand Up @@ -1380,9 +1379,10 @@ def from_bytes(cls, sector, enabled, buttons, gbuttons, bytes):
blue=bytes[15],
power_mode=bytes[16],
angle_snap=bytes[17],
reserved=bytes[18:32],
buttons=[Button.from_bytes(bytes[32 + i * 4:32 + i * 4 + 4]) for i in range(0, buttons)],
gbuttons=[Button.from_bytes(bytes[96 + i * 4:96 + i * 4 + 4]) for i in range(0, gbuttons)],
name=bytes[160:208].decode('utf-16-be').rstrip('\x00').rstrip('\uFFFF'),
name=bytes[160:208].decode('utf-16le').rstrip('\x00').rstrip('\uFFFF'),
lighting=[LEDEffectSetting.from_bytes(bytes[208 + i * 11:219 + i * 11]) for i in range(0, 4)]
)

Expand All @@ -1396,13 +1396,13 @@ def to_bytes(self, length):
bytes += _int2bytes(self.resolution_default_index, 1) + _int2bytes(self.resolution_shift_index, 1)
bytes += b''.join([self.resolutions[i].to_bytes(2, 'little') for i in range(0, 5)])
bytes += _int2bytes(self.red, 1) + _int2bytes(self.green, 1) + _int2bytes(self.blue, 1)
bytes += _int2bytes(self.power_mode, 1) + _int2bytes(self.angle_snap, 1) + b'\xff' * 14
bytes += _int2bytes(self.power_mode, 1) + _int2bytes(self.angle_snap, 1) + self.reserved
for i in range(0, 16):
bytes += self.buttons[i].to_bytes() if i < len(self.buttons) else b'\xff\xff\xff\xff'
for i in range(0, 16):
bytes += self.gbuttons[i].to_bytes() if i < len(self.gbuttons) else b'\xff\xff\xff\xff'
if self.enabled:
bytes += self.name[0:24].ljust(24, '\x00').encode('utf-16be')
bytes += self.name[0:24].ljust(24, '\x00').encode('utf-16le')
else:
bytes += b'\xff' * 48
for i in range(0, 4):
Expand Down Expand Up @@ -1430,10 +1430,10 @@ def dump(self):
_yaml.SafeLoader.add_constructor('!OnboardProfile', OnboardProfile.from_yaml)
_yaml.add_representer(OnboardProfile, OnboardProfile.to_yaml)

OnboardProfilesVersion = 1
OnboardProfilesVersion = 2


# Doesn't handle macros or lighting
# Doesn't handle macros
class OnboardProfiles:
"""The entire onboard profiles information"""

Expand Down

0 comments on commit f8b25df

Please sign in to comment.