Why Int instead of 3 bytes for RGB?

Status
Not open for further replies.

WS2812B

Member
I have a pretty simple question: Why does the OctoWS2811 library use an array of Ints to store the RGB data? With 3 bytes for R, G and B is one byte of the Int unused. Or is anything stored in the last byte of the Int?
 
Suspect because the DMA hardware that makes OCTOWS 2811work does not support 24 bit memory use in 32 bit memory structure, it will just be trundling through memory space and producing the required waveform as it goes.
 
Why does the OctoWS2811 library use an array of Ints to store the RGB data?

It's 6 integers times the number of LEDs per pin, which is 24 bytes times the number of LEDs per pin. Since there are 8 pins each with that number of LEDs, the size works out to be 3 bytes LED. No bytes are wasted.

32 bit integers are used instead of 8 bit bytes because you get only a 25% chance the compiler will align a byte array onto a 32 bit boundary. Internally the RAM is 32 bits wide, so alignment allows for faster copy between the buffers.
 
It's 6 integers times the number of LEDs per pin, which is 24 bytes times the number of LEDs per pin. Since there are 8 pins each with that number of LEDs, the size works out to be 3 bytes LED. No bytes are wasted.

32 bit integers are used instead of 8 bit bytes because you get only a 25% chance the compiler will align a byte array onto a 32 bit boundary. Internally the RAM is 32 bits wide, so alignment allows for faster copy between the buffers.

It is maybe a stupid question but does it help saving up RAM if you use smaller size types like int16_t instead of 4byte types?
 
Seems like you don't quite understand. I'll try to explain again, this time with a specific example.

Suppose to use 500 LEDs per pin. There are 8 pins, so that means you have 4000 LEDs. Each LED needs 3 bytes, so you need a total of 12000 bytes.

Here's the code:

Code:
#define LEDS_PER_STRIP 500
DMAMEM int displayMemory[LEDS_PER_STRIP*6];

This array is 500*6 = 3000 ints which are 4 bytes each. That's exactly the required size of 12000 bytes.

Changing to a 16 bit integer or 8 bit bytes doesn't alter the need for 24 bits (3 bytes) per LED, adding up to a total of 12000 bytes.
 
Seems like you don't quite understand. I'll try to explain again, this time with a specific example.

Suppose to use 500 LEDs per pin. There are 8 pins, so that means you have 4000 LEDs. Each LED needs 3 bytes, so you need a total of 12000 bytes.

Here's the code:

Code:
#define LEDS_PER_STRIP 500
DMAMEM int displayMemory[LEDS_PER_STRIP*6];

This array is 500*6 = 3000 ints which are 4 bytes each. That's exactly the required size of 12000 bytes.

Changing to a 16 bit integer or 8 bit bytes doesn't alter the need for 24 bits (3 bytes) per LED, adding up to a total of 12000 bytes.

I understood, the second question was a general question about the Teensy 3.6. Does it save up RAM if I use a uint16_t instead of an int if the value wont go over (2^16) - 1 or will the compiler just waste the other two bytes?

EDIT: I know it saves up RAM with 8bit AVRs but my question is if it does also save up RAM with 32bit ARMs like the Teensy 3.6 I will use.
 
Status
Not open for further replies.
Back
Top