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 29, 2024
1 parent 22a59b6 commit 4dd4bd3
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 27 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)] = _special_keys.COLORSPLUS["No change"] # 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 != _special_keys.COLORSPLUS["No change"]: # 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 != _special_keys.COLORSPLUS["No change"]: # 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
101 changes: 81 additions & 20 deletions tests/logitech_receiver/test_setting_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class FeatureTest:
matched_calls: int = 1
offset: int = 0x04
version: int = 0x00
rewrite: bool = False


simple_tests = [
Expand Down Expand Up @@ -596,12 +597,7 @@ def test_simple_template(test, mocker, mock_gethostname):
hidpp.Response("01010300", 0x0440, "01010300"),
),
Setup(
FeatureTest(
settings_templates.Gesture2Params,
{4: {"scale": 256}},
{4: {"scale": 128}},
2,
),
FeatureTest(settings_templates.Gesture2Params, {4: {"scale": 256}}, {4: {"scale": 128}}, 2),
*hidpp.responses_gestures,
hidpp.Response("000100FF000000000000000000000000", 0x0480, "000100FF"),
hidpp.Response("000080FF000000000000000000000000", 0x0480, "000080FF"),
Expand All @@ -615,29 +611,89 @@ def test_simple_template(test, mocker, mock_gethostname):
hidpp.Response("E010", 0x0430, "02E010"),
hidpp.Response("E018", 0x0430, "02E018"),
),
Setup(
FeatureTest(settings_templates.PerKeyLighting, {1: -1, 2: -1, 9: -1, 10: -1, 113: -1}, {2: 0xFF0000}, 5, 4, 0, 1),
{
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,
},
hidpp.Response("00000606000000000000000000000000", 0x0400, "0000"), # first group of keys
hidpp.Response("00000200000000000000000000000000", 0x0400, "0001"), # second group of keys
hidpp.Response("00000000000000000000000000000000", 0x0400, "0002"), # last group of keys
hidpp.Response("00", 0x0470, "00"), # finish
hidpp.Response("02FF0000", 0x0410, "02FF0000"), # write one value
hidpp.Response("00", 0x0470, "00"), # finish
hidpp.Response("02FF0000", 0x0410, "02FF0000"), # write one value
hidpp.Response("00", 0x0470, "00"), # finish
),
Setup(
FeatureTest(
settings_templates.PerKeyLighting,
{1: -1, 2: -1, 9: -1, 10: -1, 113: -1},
{2: 0xFF0000, 9: 0xFF0000, 10: 0xFF0000},
9,
4,
0,
1,
),
{
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,
},
hidpp.Response("00000606000000000000000000000000", 0x0400, "0000"), # first group of keys
hidpp.Response("00000200000000000000000000000000", 0x0400, "0001"), # second group of keys
hidpp.Response("00000000000000000000000000000000", 0x0400, "0002"), # last group of keys
hidpp.Response("00", 0x0470, "00"), # finish
hidpp.Response("02FF0000", 0x0410, "02FF0000"), # write one value
hidpp.Response("00", 0x0470, "00"), # finish
hidpp.Response("09FF0000", 0x0410, "09FF0000"), # write one value
hidpp.Response("00", 0x0470, "00"), # finish
hidpp.Response("0AFF0000", 0x0410, "0AFF0000"), # write one value
hidpp.Response("00", 0x0470, "00"), # finish
hidpp.Response("02FF000009FF00000AFF0000", 0x0410, "02FF000009FF00000AFF0000"), # write three values
hidpp.Response("00", 0x0470, "00"), # finish
),
Setup(
FeatureTest(
settings_templates.PerKeyLighting,
{1: 0xFFFFFF, 2: 0xFFFFFF, 9: 0xFFFFFF, 10: 0xFFFFFF, 113: 0xFFFFFF, 114: 0xFFFFFF},
{2: 0xFF0000},
5,
{1: -1, 2: -1, 9: -1, 10: -1, 113: -1, 114: -1},
{2: 0xFF0000, 9: 0xFF0000, 10: 0xFF0000, 113: 0xFF0000, 114: 0x00FF00},
14,
4,
0,
1,
),
{
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
hidpp.Response("09FF0000", 0x0410, "09FF0000"), # write one value
hidpp.Response("00", 0x0470, "00"), # finish
hidpp.Response("0AFF0000", 0x0410, "0AFF0000"), # write one value
hidpp.Response("00", 0x0470, "00"), # finish
hidpp.Response("71FF0000", 0x0410, "71FF0000"), # write one value
hidpp.Response("00", 0x0470, "00"), # finish
hidpp.Response("7200FF00", 0x0410, "7200FF00"), # write one value
hidpp.Response("00", 0x0470, "00"), # finish
hidpp.Response("02FF000009FF00000AFF000071FF0000", 0x0410, "02FF000009FF00000AFF000071FF0000"), # write four values
hidpp.Response("7200FF00", 0x0410, "7200FF00"), # write one value
hidpp.Response("00", 0x0470, "00"), # finish
),
Setup(
FeatureTest(settings_templates.ExtendedAdjustableDpi, {0: 256}, {0: 512}, 2, offset=0x9),
Expand Down Expand Up @@ -690,6 +746,7 @@ def test_key_template(test, mocker):
device = hidpp.Device(responses=test.responses, feature=tst.sclass.feature, offset=tst.offset, version=tst.version)
spy_request = mocker.spy(device, "request")

print("FEATURE", tst.sclass.feature)
setting = settings_templates.check_feature(device, tst.sclass)
assert setting is not None
if isinstance(setting, list):
Expand All @@ -706,9 +763,13 @@ def test_key_template(test, mocker):
write_value = setting.write(value)
assert write_value == tst.initial_value

for key, value in tst.write_value.items():
write_value = setting.write_key_value(key, value)
assert write_value == value
for key, val in tst.write_value.items():
write_value = setting.write_key_value(key, val)
assert write_value == val
value[key] = val

if tst.rewrite:
write_value = setting.write(value)

hidpp.match_requests(tst.matched_calls, test.responses, spy_request.call_args_list)

Expand Down

0 comments on commit 4dd4bd3

Please sign in to comment.