-->
Page 1 of 1

Quicker - equation or lookup table?

PostPosted: Sun Jan 17, 2016 7:23 pm
by Barnabybear
Hi guys, I have a little project at the moment which is working well, I just need to tidy things up.
I’m using: ESP8266-01, PCA9685 (I2C 16 channel PWM LED driver) and SI2302 (MOSFETs) to drive RGB LED strips (the dumb type).
The ESP receives DMX data in the form of E1.31 or sCAN amends it slightly (this is where I need the help) and sends it via I2C to the PCA9685 to display it on the connected LEDs.
The DMX value received by the ESP is an 8 bit value (0 to 255) that expresses the brightness that the LEDs should be. However the PCA9685 uses 12 bit values to control the brightness. This in its self is easy enough to get round.
Code: Select all12_bit_value = 8_bit_value <<4

It would be a wasted opportunity to not apply Gamma correction whilst doing this conversion, which for a fixed value of 0.5 Gamma equates to:
Code: Select all12_bit_value = (((8_bit_value) * (8_bit_value)) / 16)

The question, is it faster in terms of processing time to perform this calculation or to use a lookup table (which would need to consist of 256 values)? I know that lookup tables where the prefered method at one time, that was based on processor speed and memory access times, what I can't seem to find is wether this still applies with the faster processing of the ESP (160mhz) and type of memory used. If a lookup table is quicker could you point me to a good example please.
I’m currently using this code in a loop for each value (which appears to be working fine):
Code: Select allWire.write(0x00); //start LSB
Wire.write(0x00); //start MSB
12_bit_value = (((8_bit_value) * (8_bit_value)) / 16); // convert to 12 bit & gamma correct
Wire.write(12_bit_value); //end LSB
12_bit_msb = 12_bit_value >>8; //end MSB
Wire.write(12_bit_msb); //end LSB

The PCA9685 has load spreading, so you have to say when you want to turn on the LED as well as how long you want it to stay on for. Hence the first two writes to turn it on then the second two to tell it how long to stay on.

The other thought, which I don't think would be quicker would be to use nested a switch case with just the 4 Wire.write comands containing the correct values and then not need all the maths. In theory for an 8 bit number you would only need to do 8 checks (< 128, < 64, <32, <16, <8, <4, <2, <1) to get any value.

Thanks for comments.

Re: Quicker - equation or lookup table?

PostPosted: Thu Jan 28, 2016 4:20 pm
by Barnabybear
Hi, guys. Sorry to bump this, I'm hoping someone has some advice and just missed the post last time.
If not for this specific case, for 255 items would you lookup or do some maths?
Or has anyone a reliable method of mesuring the time taken to complete a series of opperations with the ESP and I'll check myself.
Thanks.