Skip to content

Commit

Permalink
Merge pull request #39 from jepler/signed-bits
Browse files Browse the repository at this point in the history
RWBits: Add support for signed registers
  • Loading branch information
siddacious committed Jul 17, 2020
2 parents 9f86b51 + 3cc3e8e commit 6143ec2
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions adafruit_register/i2c_bits.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,18 @@ class RWBits:
:param type lowest_bit: The lowest bits index within the byte at ``register_address``
:param int register_width: The number of bytes in the register. Defaults to 1.
:param bool lsb_first: Is the first byte we read from I2C the LSB? Defaults to true
:param bool signed: If True, the value is a "two's complement" signed value.
If False, it is unsigned.
"""

def __init__( # pylint: disable=too-many-arguments
self, num_bits, register_address, lowest_bit, register_width=1, lsb_first=True,
self,
num_bits,
register_address,
lowest_bit,
register_width=1,
lsb_first=True,
signed=False,
):
self.bit_mask = ((1 << num_bits) - 1) << lowest_bit
# print("bitmask: ",hex(self.bit_mask))
Expand All @@ -58,6 +66,7 @@ def __init__( # pylint: disable=too-many-arguments
self.buffer = bytearray(1 + register_width)
self.buffer[0] = register_address
self.lsb_first = lsb_first
self.sign_bit = (1 << (num_bits - 1)) if signed else 0

def __get__(self, obj, objtype=None):
with obj.i2c_device as i2c:
Expand All @@ -69,7 +78,11 @@ def __get__(self, obj, objtype=None):
order = reversed(order)
for i in order:
reg = (reg << 8) | self.buffer[i]
return (reg & self.bit_mask) >> self.lowest_bit
reg = (reg & self.bit_mask) >> self.lowest_bit
# If the value is signed and negative, convert it
if reg & self.sign_bit:
reg -= 2 * self.sign_bit
return reg

def __set__(self, obj, value):
value <<= self.lowest_bit # shift the value over to the right spot
Expand Down

0 comments on commit 6143ec2

Please sign in to comment.