From 0d56cfbded1a4a63fa4e8a6d4cd00cf1e85178e3 Mon Sep 17 00:00:00 2001 From: Daniel Pavel Date: Sun, 30 Jun 2013 17:12:44 +0200 Subject: [PATCH] clean-up in get/set notification flags computation --- lib/logitech/unifying_receiver/common.py | 24 +++++++++++------------ lib/logitech/unifying_receiver/hidpp10.py | 11 +++++++---- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/lib/logitech/unifying_receiver/common.py b/lib/logitech/unifying_receiver/common.py index 8f9dd332..dafc5e09 100644 --- a/lib/logitech/unifying_receiver/common.py +++ b/lib/logitech/unifying_receiver/common.py @@ -203,10 +203,9 @@ def bytes2int(x): """ assert isinstance(x, bytes) assert len(x) < 9 - result = 0 - for b in x: - result <<= 8 - result |= b if isinstance(b, int) else ord(b) + qx = (b'\x00' * 8) + x + result, = _unpack('!Q', qx[-8:]) + # assert x == int2bytes(result, len(x)) return result @@ -216,16 +215,17 @@ def int2bytes(x, count=None): If 'count' is not given, the necessary number of bytes is computed. """ assert isinstance(x, int) - if count is None: - no_bits = x.bit_length() - count = (no_bits // 8) + (1 if no_bits % 8 else 0) - else: - assert isinstance(count, int) - assert count > 0 - assert x.bit_length() <= count * 8 result = _pack('!Q', x) assert isinstance(result, bytes) - return result[:-count] + # assert x == bytes2int(result) + + if count is None: + return result.lstrip(b'\x00') + + assert isinstance(count, int) + assert count > 0 + assert x.bit_length() <= count * 8 + return result[-count:] class KwException(Exception): diff --git a/lib/logitech/unifying_receiver/hidpp10.py b/lib/logitech/unifying_receiver/hidpp10.py index 9962c27a..41d48137 100644 --- a/lib/logitech/unifying_receiver/hidpp10.py +++ b/lib/logitech/unifying_receiver/hidpp10.py @@ -9,6 +9,8 @@ _log = getLogger('LUR.hidpp10') del getLogger from .common import (strhex as _strhex, + bytes2int as _bytes2int, + int2bytes as _int2bytes, NamedInts as _NamedInts, FirmwareInfo as _FirmwareInfo) from .hidpp20 import FIRMWARE_KIND @@ -273,13 +275,13 @@ def get_notification_flags(device): if device.kind is not None: # peripherals with protocol >= 2.0 don't support registers p = device.protocol - if p is None or p >= 2.0: + if p is not None and p >= 2.0: return flags = read_register(device, 0x00) if flags is not None: assert len(flags) == 3 - return ord(flags[0:1]) << 16 | ord(flags[1:2]) << 8 | ord(flags[2:3]) + return _bytes2int(flags) def set_notification_flags(device, *flag_bits): @@ -288,9 +290,10 @@ def set_notification_flags(device, *flag_bits): if device.kind is not None: # peripherals with protocol >= 2.0 don't support registers p = device.protocol - if p is None or p >= 2.0: + if p is not None and p >= 2.0: return flag_bits = sum(int(b) for b in flag_bits) - result = write_register(device, 0x00, 0xFF & (flag_bits >> 16), 0xFF & (flag_bits >> 8), 0xFF & flag_bits) + assert flag_bits & 0x00FFFFFF == flag_bits + result = write_register(device, 0x00, _int2bytes(flag_bits, 3)) return result is not None