// Generate an LED gamma-correction table for Arduino sketches.
// Written in Processing (www.processing.org), NOT for Arduino!
// Copy-and-paste the program's output into an Arduino sketch.
// NOTE: For ESPs a 'gamma' variable is always declared in an ESP tool source code.
float GammaVal = 2; // Correction factor (1.5 - 3.0 for LEDs)
uint16_t max_in = 255, // Top end of INPUT range (number of elements in the table)
max_out = 255, // Top end of OUTPUT range
newLn = 16; // Create new line after this number of elements
/* for gamma = 2.0 the formular is quiet simple as:
int x = readAnalogInput(); /// 0 - 255
int brightness = (x*x)/255.0;
*/
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.print("Creating gamma correction table for LEDs! Used gamma: ");
Serial.println(GammaVal);
Serial.print("const uint8_t PROGMEM gamma[] = {");
for(int i=0; i <= max_in; i++) {
if(i > 0) Serial.print(',');
if((i % newLn) == 0) Serial.print("\n ");
uint16_t v = (uint16_t)(pow((float)i / (float)max_in, GammaVal) * max_out + 0.5);
Serial.printf("%3u", v);
}
Serial.println(" };");
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}