Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor hidpp and introduce classes #2346

Merged
merged 5 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/logitech_receiver/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@

import hidapi as _hid

from . import exceptions
from . import exceptions, hidpp20
from . import hidpp10_constants as _hidpp10_constants
from . import hidpp20 as _hidpp20
from . import hidpp20_constants as _hidpp20_constants
from .base_usb import ALL as _RECEIVER_USB_IDS
from .common import strhex as _strhex
from .descriptors import DEVICES as _DEVICES

logger = logging.getLogger(__name__)

_hidpp20 = hidpp20.Hidpp20()

#
#
#
Expand Down
38 changes: 20 additions & 18 deletions lib/logitech_receiver/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

logger = logging.getLogger(__name__)

_hidpp10 = hidpp10.Hidpp10()
_hidpp20 = hidpp20.Hidpp20()
_R = hidpp10_constants.REGISTERS
_IR = hidpp10_constants.INFO_SUBREGISTERS

Expand All @@ -42,7 +44,7 @@ class Device:
def __init__(self, receiver, number, online, pairing_info=None, handle=None, device_info=None, setting_callback=None):
assert receiver or device_info
if receiver:
assert number > 0 and number <= 15 # some receivers have devices past their max # of devices
assert 0 < number <= 15 # some receivers have devices past their max # of devices
self.number = number # will be None at this point for directly connected devices
self.online = online
self.descriptor = None
Expand Down Expand Up @@ -141,7 +143,7 @@ def codename(self):
if not self.online: # be very defensive
self.ping()
if self.online and self.protocol >= 2.0:
self._codename = hidpp20.get_friendly_name(self)
self._codename = _hidpp20.get_friendly_name(self)
if not self._codename:
self._codename = self.name.split(" ", 1)[0] if self.name else None
if not self._codename and self.receiver:
Expand All @@ -161,11 +163,11 @@ def name(self):
except exceptions.NoSuchDevice:
pass
if self.online and self.protocol >= 2.0:
self._name = hidpp20.get_name(self)
self._name = _hidpp20.get_name(self)
return self._name or self._codename or ("Unknown device %s" % (self.wpid or self.product_id))

def get_ids(self):
ids = hidpp20.get_ids(self)
ids = _hidpp20.get_ids(self)
if ids:
self._unitId, self._modelId, self._tid_map = ids
if logger.isEnabledFor(logging.INFO) and self._serial and self._serial != self._unitId:
Expand All @@ -192,16 +194,16 @@ def tid_map(self):
@property
def kind(self):
if not self._kind and self.online and self.protocol >= 2.0:
self._kind = hidpp20.get_kind(self)
self._kind = _hidpp20.get_kind(self)
return self._kind or "?"

@property
def firmware(self):
if self._firmware is None and self.online:
if self.protocol >= 2.0:
self._firmware = hidpp20.get_firmware(self)
self._firmware = _hidpp20.get_firmware(self)
else:
self._firmware = hidpp10.get_firmware(self)
self._firmware = _hidpp10.get_firmware(self)
return self._firmware or ()

@property
Expand All @@ -219,7 +221,7 @@ def power_switch_location(self):
@property
def polling_rate(self):
if self.online and self.protocol >= 2.0:
rate = hidpp20.get_polling_rate(self)
rate = _hidpp20.get_polling_rate(self)
self._polling_rate = rate if rate else self._polling_rate
return self._polling_rate

Expand All @@ -233,14 +235,14 @@ def led_effects(self):
def keys(self):
if not self._keys:
if self.online and self.protocol >= 2.0:
self._keys = hidpp20.get_keys(self) or ()
self._keys = _hidpp20.get_keys(self) or ()
return self._keys

@property
def remap_keys(self):
if self._remap_keys is None:
if self.online and self.protocol >= 2.0:
self._remap_keys = hidpp20.get_remap_keys(self) or ()
self._remap_keys = _hidpp20.get_remap_keys(self) or ()
return self._remap_keys

@property
Expand All @@ -249,21 +251,21 @@ def gestures(self):
with self._gestures_lock:
if self._gestures is None:
if self.online and self.protocol >= 2.0:
self._gestures = hidpp20.get_gestures(self) or ()
self._gestures = _hidpp20.get_gestures(self) or ()
return self._gestures

@property
def backlight(self):
if self._backlight is None:
if self.online and self.protocol >= 2.0:
self._backlight = hidpp20.get_backlight(self)
self._backlight = _hidpp20.get_backlight(self)
return self._backlight

@property
def profiles(self):
if self._profiles is None:
if self.online and self.protocol >= 2.0:
self._profiles = hidpp20.get_profiles(self)
self._profiles = _hidpp20.get_profiles(self)
return self._profiles

@property
Expand Down Expand Up @@ -300,7 +302,7 @@ def settings(self):

def set_configuration(self, configuration, no_reply=False):
if self.online and self.protocol >= 2.0:
hidpp20.config_change(self, configuration, no_reply=no_reply)
_hidpp20.config_change(self, configuration, no_reply=no_reply)

def reset(self, no_reply=False):
self.set_configuration(0, no_reply)
Expand All @@ -315,11 +317,11 @@ def persister(self):

def battery(self): # None or level, next, status, voltage
if self.protocol < 2.0:
return hidpp10.get_battery(self)
return _hidpp10.get_battery(self)
else:
battery_feature = self.persister.get("_battery", None) if self.persister else None
if battery_feature != 0:
result = hidpp20.get_battery(self, battery_feature)
result = _hidpp20.get_battery(self, battery_feature)
try:
feature, level, next, status, voltage = result
if self.persister and battery_feature is None:
Expand All @@ -344,11 +346,11 @@ def enable_connection_notifications(self, enable=True):
)
else:
set_flag_bits = 0
ok = hidpp10.set_notification_flags(self, set_flag_bits)
ok = _hidpp10.set_notification_flags(self, set_flag_bits)
if not ok:
logger.warning("%s: failed to %s device notifications", self, "enable" if enable else "disable")

flag_bits = hidpp10.get_notification_flags(self)
flag_bits = _hidpp10.get_notification_flags(self)
flag_names = None if flag_bits is None else tuple(hidpp10_constants.NOTIFICATION_FLAG.flag_names(flag_bits))
if logger.isEnabledFor(logging.INFO):
logger.info("%s: device notifications %s %s", self, "enabled" if enable else "disabled", flag_names)
Expand Down
Loading
Loading