kplindegaard / smbus2

A drop-in replacement for smbus-cffi/smbus-python in pure Python
MIT License
243 stars 68 forks source link

little/big endian #86

Open hikavdh opened 1 year ago

hikavdh commented 1 year ago

When I read a word from i2c, it is treated as big endian. However I want to read a temperature that is stored as little endian. I have solved this so:

BUS = SMBus(1)
value = BUS.read_word_data(address, register)
hstr = str(hex(value))[2:].rjust(4, '0')
value = int('0x%s%s' % (hstr[2:], hstr[:2]), 16)

Not very elegant. Is there a native way to handle this?

kplindegaard commented 1 year ago

Slow response, sorry. but you don't need to go via strings.

little_endian_value = ((value & 0xFF) << 8) | ((value >> 8) & 0xFF)
hikavdh commented 1 year ago

Thanks!