Skip to content

Commit

Permalink
Refactor InfoSubRegisters: Use IntEnum in favour of NamedInts
Browse files Browse the repository at this point in the history
  • Loading branch information
some_developer authored and pfps committed Jan 1, 2025
1 parent 378175f commit 96364d2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
19 changes: 9 additions & 10 deletions lib/logitech_receiver/hidpp10_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,15 @@ class Registers(IntEnum):


# Subregisters for receiver_info register
INFO_SUBREGISTERS = NamedInts(
serial_number=0x01, # not found on many receivers
fw_version=0x02,
receiver_information=0x03,
pairing_information=0x20, # 0x2N, by connected device
extended_pairing_information=0x30, # 0x3N, by connected device
device_name=0x40, # 0x4N, by connected device
bolt_pairing_information=0x50, # 0x5N, by connected device
bolt_device_name=0x60, # 0x6N01, by connected device,
)
class InfoSubRegisters(IntEnum):
SERIAL_NUMBER = 0x01 # not found on many receivers
FW_VERSION = 0x02
RECEIVER_INFORMATION = 0x03
PAIRING_INFORMATION = 0x20 # 0x2N, by connected device
EXTENDED_PAIRING_INFORMATION = 0x30 # 0x3N, by connected device
DEVICE_NAME = 0x40 # 0x4N, by connected device
BOLT_PAIRING_INFORMATION = 0x50 # 0x5N, by connected device
BOLT_DEVICE_NAME = 0x60 # 0x6N01, by connected device


class DeviceFeature(Flag):
Expand Down
14 changes: 7 additions & 7 deletions lib/logitech_receiver/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from .common import Alert
from .common import Notification
from .device import Device
from .hidpp10_constants import InfoSubRegisters
from .hidpp10_constants import NotificationFlag
from .hidpp10_constants import Registers

Expand All @@ -47,7 +48,6 @@
logger = logging.getLogger(__name__)

_hidpp10 = hidpp10.Hidpp10()
_IR = hidpp10_constants.INFO_SUBREGISTERS


class LowLevelInterface(Protocol):
Expand Down Expand Up @@ -127,7 +127,7 @@ def __init__(

def initialize(self, product_info: dict):
# read the receiver information subregister, so we can find out max_devices
serial_reply = self.read_register(Registers.RECEIVER_INFO, _IR.receiver_information)
serial_reply = self.read_register(Registers.RECEIVER_INFO, InfoSubRegisters.RECEIVER_INFORMATION)
if serial_reply:
self.serial = serial_reply[1:5].hex().upper()
self.max_devices = serial_reply[6]
Expand Down Expand Up @@ -193,7 +193,7 @@ def enable_connection_notifications(self, enable=True):
return flag_bits

def device_codename(self, n):
codename = self.read_register(Registers.RECEIVER_INFO, _IR.device_name + n - 1)
codename = self.read_register(Registers.RECEIVER_INFO, InfoSubRegisters.DEVICE_NAME + n - 1)
if codename:
codename = codename[2 : 2 + ord(codename[1:2])]
return codename.decode("ascii")
Expand All @@ -218,7 +218,7 @@ def device_pairing_information(self, n: int) -> dict:
polling_rate = ""
serial = None
power_switch = "(unknown)"
pair_info = self.read_register(Registers.RECEIVER_INFO, _IR.pairing_information + n - 1)
pair_info = self.read_register(Registers.RECEIVER_INFO, InfoSubRegisters.PAIRING_INFORMATION + n - 1)
if pair_info: # a receiver that uses Unifying-style pairing registers
wpid = pair_info[3:5].hex().upper()
kind = hidpp10_constants.DEVICE_KIND[pair_info[7] & 0x0F]
Expand All @@ -233,7 +233,7 @@ def device_pairing_information(self, n: int) -> dict:
raise exceptions.NoSuchDevice(number=n, receiver=self, error="read pairing information - non-unifying")
else:
raise exceptions.NoSuchDevice(number=n, receiver=self, error="read pairing information")
pair_info = self.read_register(Registers.RECEIVER_INFO, _IR.extended_pairing_information + n - 1)
pair_info = self.read_register(Registers.RECEIVER_INFO, InfoSubRegisters.EXTENDED_PAIRING_INFORMATION + n - 1)
if pair_info:
power_switch = hidpp10_constants.PowerSwitchLocation(pair_info[9] & 0x0F)
serial = pair_info[1:5].hex().upper()
Expand Down Expand Up @@ -416,13 +416,13 @@ def initialize(self, product_info: dict):
self.max_devices = product_info.get("max_devices", 1)

def device_codename(self, n):
codename = self.read_register(Registers.RECEIVER_INFO, _IR.bolt_device_name + n, 0x01)
codename = self.read_register(Registers.RECEIVER_INFO, InfoSubRegisters.BOLT_DEVICE_NAME + n, 0x01)
if codename:
codename = codename[3 : 3 + min(14, ord(codename[2:3]))]
return codename.decode("ascii")

def device_pairing_information(self, n: int) -> dict:
pair_info = self.read_register(Registers.RECEIVER_INFO, _IR.bolt_pairing_information + n)
pair_info = self.read_register(Registers.RECEIVER_INFO, InfoSubRegisters.BOLT_PAIRING_INFORMATION + n)
if pair_info:
wpid = (pair_info[3:4] + pair_info[2:3]).hex().upper()
kind = hidpp10_constants.DEVICE_KIND[pair_info[1] & 0x0F]
Expand Down
2 changes: 1 addition & 1 deletion lib/solaar/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def _notifications_handler(self, n):
if (
self.receiver.read_register(
hidpp10_constants.Registers.RECEIVER_INFO,
hidpp10_constants.INFO_SUBREGISTERS.pairing_information + n.devnumber - 1,
hidpp10_constants.InfoSubRegisters.PAIRING_INFORMATION + n.devnumber - 1,
)
is None
):
Expand Down

0 comments on commit 96364d2

Please sign in to comment.