clean-up in get/set notification flags computation

This commit is contained in:
Daniel Pavel 2013-06-30 17:12:44 +02:00
parent 9896875180
commit 0d56cfbded
2 changed files with 19 additions and 16 deletions

View File

@ -203,10 +203,9 @@ def bytes2int(x):
""" """
assert isinstance(x, bytes) assert isinstance(x, bytes)
assert len(x) < 9 assert len(x) < 9
result = 0 qx = (b'\x00' * 8) + x
for b in x: result, = _unpack('!Q', qx[-8:])
result <<= 8 # assert x == int2bytes(result, len(x))
result |= b if isinstance(b, int) else ord(b)
return result return result
@ -216,16 +215,17 @@ def int2bytes(x, count=None):
If 'count' is not given, the necessary number of bytes is computed. If 'count' is not given, the necessary number of bytes is computed.
""" """
assert isinstance(x, int) 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) result = _pack('!Q', x)
assert isinstance(result, bytes) 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): class KwException(Exception):

View File

@ -9,6 +9,8 @@ _log = getLogger('LUR.hidpp10')
del getLogger del getLogger
from .common import (strhex as _strhex, from .common import (strhex as _strhex,
bytes2int as _bytes2int,
int2bytes as _int2bytes,
NamedInts as _NamedInts, NamedInts as _NamedInts,
FirmwareInfo as _FirmwareInfo) FirmwareInfo as _FirmwareInfo)
from .hidpp20 import FIRMWARE_KIND from .hidpp20 import FIRMWARE_KIND
@ -273,13 +275,13 @@ def get_notification_flags(device):
if device.kind is not None: if device.kind is not None:
# peripherals with protocol >= 2.0 don't support registers # peripherals with protocol >= 2.0 don't support registers
p = device.protocol p = device.protocol
if p is None or p >= 2.0: if p is not None and p >= 2.0:
return return
flags = read_register(device, 0x00) flags = read_register(device, 0x00)
if flags is not None: if flags is not None:
assert len(flags) == 3 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): def set_notification_flags(device, *flag_bits):
@ -288,9 +290,10 @@ def set_notification_flags(device, *flag_bits):
if device.kind is not None: if device.kind is not None:
# peripherals with protocol >= 2.0 don't support registers # peripherals with protocol >= 2.0 don't support registers
p = device.protocol p = device.protocol
if p is None or p >= 2.0: if p is not None and p >= 2.0:
return return
flag_bits = sum(int(b) for b in flag_bits) 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 return result is not None