pythoncolorsbluetooth-lowenergydecodebtle

Nordic Thingy52 btle Color Sensor data decoding


Used lang is Python.
I am trying to decode the color sensor data from the Thingy52 to RGBA. To interface with the Thingy52 I use the thingy52.py example in https://github.com/IanHarvey/bluepy/tree/master/bluepy Four examples of the received raw data:

b'\xd4\x05\xad\x05\xae\x00\xf9\x00'
b'f\r\x8e\x11B\n\xa5\x03'
b'\x8a\r\xad\x11K\n\xa8\x03'
b'Y\rw\x11/\n\xa1\x03'

This data can somehow always be decoded to 16 Bytes with binascii.b2a_hex().
Example: b'5d06a90677013201'

This could be 4 4-byte-floats or 4 int32 / uint32, but every attempt at unpacking this as a struct with the struct standard library results in weird numbers that don't fit in the 0-255 range.

My question: How can this data be decoded?


Solution

  • If I have found the correct Nordic documentation, then they are 4 uint16 enter image description here

    So in python you could do:

    pi@raspberrypi:~ $ python3
    Python 3.7.3 (default, Jul 25 2020, 13:03:44) 
    [GCC 8.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import struct
    >>> import binascii
    >>> data = b'5d06a90677013201'
    >>> struct.unpack(b'<HHHH', binascii.unhexlify(data))
    (1629, 1705, 375, 306)
    >>> r, g, b, clear = struct.unpack(b'<HHHH', binascii.unhexlify(data))
    >>> r
    1629
    >>> print(f'{r:#06x}, {g:#06x}, {b:#06x}, {clear:#06x}')
    0x065d, 0x06a9, 0x0177, 0x0132