endianess goddammit
# endianess goddammit
Let’s say we want to transmit RGB-565 data over the wire using SPI with the ESP32. How does that look like?
Let’s assume your target is the ST7796, to make that pixel beautifully pastel pink 0xF435
, it expects the data to arrive like this:
|
|
We use tiny jpeg, to decode a jpeg file into its raw bytes. Crucially, this section:
|
|
The pixel data is set into a uint16_t
variable as you would expect, in RGB order, with R as the MSB. ESP32 is little endian (least significant byte is at a lower address), so here are the bytes when placed in a contiguous array of memory uint16_t arr[]
|
|
The SPI master transmit order is most significant bit first, transmitting 1 byte at a time in a for loop through arr
, we send:
|
|
To fix this, tiny jpeg has a flag swap_color_bytes
which turns our data into:
|
|
Note: I found that my display would be set in RGB mode (according to the datasheet, there’s a register named MADCTL where bit 3 would configure it as RGB/BGR), but display colors in BGR instead. This made me rethink the underlying chain of events leading to the image transfer, culminating in this post.