Skip to content

Commit

Permalink
settings: add and initialize per-key lighting to a special no-change …
Browse files Browse the repository at this point in the history
…value
  • Loading branch information
pfps committed Apr 28, 2024
1 parent 22a59b6 commit 7d6feeb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
18 changes: 11 additions & 7 deletions lib/logitech_receiver/settings_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1646,15 +1646,15 @@ class PerKeyLighting(_Settings):
description = _("Control per-key lighting.")
feature = _F.PER_KEY_LIGHTING_V2
keys_universe = _special_keys.KEYCODES
choices_universe = _special_keys.COLORS
choices_universe = _special_keys.COLORSPLUS

def read(self, cached=True):
self._pre_read(cached)
if cached and self._value is not None:
return self._value
reply_map = {}
for key in self._validator.choices:
reply_map[int(key)] = 0xFFFFFF # can't read so fake a value of white
reply_map[int(key)] = -1 # this signals no change
self._value = reply_map
return reply_map

Expand All @@ -1663,7 +1663,8 @@ def write(self, map, save=True):
self.update(map, save)
data_bytes = b""
for key, value in map.items():
data_bytes += key.to_bytes(1, "big") + value.to_bytes(3, "big")
if value != -1: # this signals no change
data_bytes += key.to_bytes(1, "big") + value.to_bytes(3, "big")
if len(data_bytes) >= 16: # up to four values are packed into a request
self._device.feature_request(self.feature, 0x10, data_bytes)
data_bytes = b""
Expand All @@ -1673,10 +1674,13 @@ def write(self, map, save=True):
return map

def write_key_value(self, key, value, save=True):
result = super().write_key_value(int(key), value, save)
if self._device.online:
self._device.feature_request(self.feature, 0x70, 0x00) # signal device to make the change
return result
if value != -1: # this signals no change
result = super().write_key_value(int(key), value, save)
if self._device.online:
self._device.feature_request(self.feature, 0x70, 0x00) # signal device to make the change
return result
else:
return True

class rw_class(_FeatureRWMap):
pass
Expand Down
4 changes: 4 additions & 0 deletions lib/logitech_receiver/special_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,10 @@ def persistent_keys(action_ids):
}
)

COLORSPLUS = _UnsortedNamedInts({"No change": -1})
for i in COLORS:
COLORSPLUS[int(i)] = str(i)

KEYCODES = _NamedInts(
{
"A": 1,
Expand Down
18 changes: 8 additions & 10 deletions tests/logitech_receiver/test_setting_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,23 +618,21 @@ def test_simple_template(test, mocker, mock_gethostname):
Setup(
FeatureTest(
settings_templates.PerKeyLighting,
{1: 0xFFFFFF, 2: 0xFFFFFF, 9: 0xFFFFFF, 10: 0xFFFFFF, 113: 0xFFFFFF, 114: 0xFFFFFF},
{1: -1, 2: -1, 9: -1, 10: -1, 113: -1, 114: -1},
{2: 0xFF0000},
5,
3,
),
{
common.NamedInt(1, "A"): special_keys.COLORS,
common.NamedInt(2, "B"): special_keys.COLORS,
common.NamedInt(9, "I"): special_keys.COLORS,
common.NamedInt(10, "J"): special_keys.COLORS,
common.NamedInt(113, "KEY 113"): special_keys.COLORS,
common.NamedInt(114, "KEY 114"): special_keys.COLORS,
common.NamedInt(1, "A"): special_keys.COLORSPLUS,
common.NamedInt(2, "B"): special_keys.COLORSPLUS,
common.NamedInt(9, "I"): special_keys.COLORSPLUS,
common.NamedInt(10, "J"): special_keys.COLORSPLUS,
common.NamedInt(113, "KEY 113"): special_keys.COLORSPLUS,
common.NamedInt(114, "KEY 114"): special_keys.COLORSPLUS,
},
hidpp.Response("00000606000000000000000000000000", 0x0400, "0000"), # first group of keys
hidpp.Response("00000600000000000000000000000000", 0x0400, "0001"), # second group of keys
hidpp.Response("00000000000000000000000000000000", 0x0400, "0002"), # last group of keys
hidpp.Response("01FFFFFF02FFFFFF09FFFFFF0AFFFFFF", 0x0410, "01FFFFFF02FFFFFF09FFFFFF0AFFFFFF"), # write first 4 values
hidpp.Response("71FFFFFF72FFFFFF", 0x0410, "71FFFFFF72FFFFFF"), # write last two values
hidpp.Response("00", 0x0470, "00"), # finish
hidpp.Response("02FF0000", 0x0410, "02FF0000"), # write one value
hidpp.Response("00", 0x0470, "00"), # finish
Expand Down

0 comments on commit 7d6feeb

Please sign in to comment.