Skip to content

Commit

Permalink
Treat all sensor values and signed integers
Browse files Browse the repository at this point in the history
Based on @dlech's suggestion that in LEGO BT protocol
all sensor values are signed, this commit changes
`Peripheral._convert_bytes()` to treat values as signed
(as opposed to unsigned).

This commit fixes issue #7.
  • Loading branch information
janvrany committed Jan 8, 2020
1 parent a908b98 commit b3b85d3
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions bricknil/sensor/peripheral.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ def _convert_bytes(self, msg_bytes:bytearray, byte_count):
If multiple values, then a list of those values
Value can be either uint8, uint16, or uint32 depending on value of `byte_count`
"""
if byte_count == 1: # just a uint8
val = msg_bytes[0]
elif byte_count == 2: # uint16 little-endian
val = struct.unpack('<H', msg_bytes)[0]
elif byte_count == 4: # uint32 little-endian
val = struct.unpack('<I', msg_bytes)[0]
if byte_count == 1: # just an int8
val = struct.unpack('<b', msg_bytes)[0]
elif byte_count == 2: # int16 little-endian
val = struct.unpack('<h', msg_bytes)[0]
elif byte_count == 4: # int32 little-endian
val = struct.unpack('<i', msg_bytes)[0]
else:
self.message_error(f'Cannot convert array of {msg_bytes} length {len(msg_bytes)} to python datatype')
val = None
Expand Down

0 comments on commit b3b85d3

Please sign in to comment.