0

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?

FERIIXU
  • 23
  • 3
  • 1
    This question was asked in the nordic devozone: https://devzone.nordicsemi.com/f/nordic-q-a/64362/thingy52-color-characteristic-0205 I hope the linked java code helps you to decode it in python – Michael Kotzjan Feb 08 '21 at 13:24

1 Answers1

1

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

ukBaz
  • 6,985
  • 2
  • 8
  • 31
  • Thank you, I couldn't find the right documentation. I'd like to note that this gives values in the range of 0-65535, not the usual 0-255. – FERIIXU Feb 08 '21 at 14:04
  • 1
    There is a method to convert the values to 'real' RGB: https://github.com/NordicSemiconductor/Android-Nordic-Thingy/blob/cdf5612dce002e31fafedc0c07fa3b5b3091f62d/app/src/main/java/no/nordicsemi/android/nrfthingy/EnvironmentServiceFragment.java#L951 – Michael Kotzjan Feb 08 '21 at 14:27