From 77721bcd00c6b3b8c48b36e6fd18754fcea8f639 Mon Sep 17 00:00:00 2001
From: Jan-Henrik Bruhn <hi@jhbruhn.de>
Date: Fri, 24 Apr 2020 00:38:05 +0200
Subject: [PATCH] Implement gamma correction for volume LED

---
 stereo_mix/drivers/leds.h             |  5 +-
 stereo_mix/resources.cc               | 72 +++++++++++++++++++++++++++
 stereo_mix/resources.h                |  5 ++
 stereo_mix/resources/lookup_tables.py |  9 ++++
 stereo_mix/resources/resources.py     |  2 +
 5 files changed, 92 insertions(+), 1 deletion(-)

diff --git a/stereo_mix/drivers/leds.h b/stereo_mix/drivers/leds.h
index 21f0082..eeb638f 100644
--- a/stereo_mix/drivers/leds.h
+++ b/stereo_mix/drivers/leds.h
@@ -1,8 +1,11 @@
 #pragma once
 
+#include "../resources.h"
 #include "stm32f0xx_hal_gpio.h"
 #include <stm32f0xx_hal.h>
 
+using namespace stereo_mix;
+
 const uint8_t kNumChannels = 4;
 
 static const uint16_t kGpioPins[] = { GPIO_PIN_7, GPIO_PIN_15, GPIO_PIN_13, GPIO_PIN_14 };
@@ -26,7 +29,7 @@ class Leds {
     pwm_counter += 2;
 
     for (size_t i = 0; i < kNumChannels; i++) {
-      if (intensities[0] && intensities[i] >= pwm_counter) {
+      if (intensities[0] && lut_led_gamma[intensities[i]] >= pwm_counter) {
         HAL_GPIO_WritePin(kGpioPorts[i], kGpioPins[i], GPIO_PIN_SET);
       } else {
         HAL_GPIO_WritePin(kGpioPorts[i], kGpioPins[i], GPIO_PIN_RESET);
diff --git a/stereo_mix/resources.cc b/stereo_mix/resources.cc
index 61a5e35..e5ddd32 100644
--- a/stereo_mix/resources.cc
+++ b/stereo_mix/resources.cc
@@ -3120,5 +3120,77 @@ const uint16_t* lookup_table_u16_table[] = {
   lut_right_cos_pan,
 };
 
+const uint8_t lut_led_gamma[] = {
+       0,      0,      0,      0,
+       0,      0,      0,      0,
+       0,      0,      0,      0,
+       0,      0,      1,      1,
+       1,      1,      1,      1,
+       1,      1,      1,      2,
+       2,      2,      2,      2,
+       2,      3,      3,      3,
+       3,      3,      4,      4,
+       4,      4,      5,      5,
+       5,      5,      6,      6,
+       6,      7,      7,      7,
+       8,      8,      8,      9,
+       9,      9,     10,     10,
+      11,     11,     11,     12,
+      12,     13,     13,     14,
+      14,     14,     15,     15,
+      16,     16,     17,     17,
+      18,     18,     19,     20,
+      20,     21,     21,     22,
+      22,     23,     24,     24,
+      25,     25,     26,     27,
+      27,     28,     29,     29,
+      30,     31,     31,     32,
+      33,     33,     34,     35,
+      36,     36,     37,     38,
+      39,     40,     40,     41,
+      42,     43,     44,     44,
+      45,     46,     47,     48,
+      49,     50,     51,     51,
+      52,     53,     54,     55,
+      56,     57,     58,     59,
+      60,     61,     62,     63,
+      64,     65,     66,     67,
+      68,     69,     70,     71,
+      72,     73,     75,     76,
+      77,     78,     79,     80,
+      81,     83,     84,     85,
+      86,     87,     88,     90,
+      91,     92,     93,     95,
+      96,     97,     98,    100,
+     101,    102,    104,    105,
+     106,    107,    109,    110,
+     112,    113,    114,    116,
+     117,    118,    120,    121,
+     123,    124,    126,    127,
+     129,    130,    131,    133,
+     134,    136,    137,    139,
+     141,    142,    144,    145,
+     147,    148,    150,    151,
+     153,    155,    156,    158,
+     160,    161,    163,    165,
+     166,    168,    170,    171,
+     173,    175,    176,    178,
+     180,    182,    183,    185,
+     187,    189,    191,    192,
+     194,    196,    198,    200,
+     202,    203,    205,    207,
+     209,    211,    213,    215,
+     217,    219,    221,    223,
+     225,    226,    228,    230,
+     232,    234,    236,    238,
+     241,    243,    245,    247,
+     249,    251,    253,    255,
+};
+
+
+const uint8_t* lookup_table_u8_table[] = {
+  lut_led_gamma,
+};
+
 
 }  // namespace stereo_mix
diff --git a/stereo_mix/resources.h b/stereo_mix/resources.h
index 1d51b1b..7259056 100644
--- a/stereo_mix/resources.h
+++ b/stereo_mix/resources.h
@@ -44,15 +44,20 @@ typedef uint8_t ResourceId;
 
 extern const uint16_t* lookup_table_u16_table[];
 
+extern const uint8_t* lookup_table_u8_table[];
+
 extern const uint16_t lut_linear_to_exp[];
 extern const uint16_t lut_left_sin_pan[];
 extern const uint16_t lut_right_cos_pan[];
+extern const uint8_t lut_led_gamma[];
 #define LUT_LINEAR_TO_EXP 0
 #define LUT_LINEAR_TO_EXP_SIZE 4096
 #define LUT_LEFT_SIN_PAN 1
 #define LUT_LEFT_SIN_PAN_SIZE 4096
 #define LUT_RIGHT_COS_PAN 2
 #define LUT_RIGHT_COS_PAN_SIZE 4096
+#define LUT_LED_GAMMA 0
+#define LUT_LED_GAMMA_SIZE 256
 
 }  // namespace stereo_mix
 
diff --git a/stereo_mix/resources/lookup_tables.py b/stereo_mix/resources/lookup_tables.py
index 6fcd8fe..289e4d0 100644
--- a/stereo_mix/resources/lookup_tables.py
+++ b/stereo_mix/resources/lookup_tables.py
@@ -1,5 +1,6 @@
 import numpy as np
 lookup_tables_u16 = []
+lookup_tables_u8 = []
 
 ADC_RESOLUTION = 4096
 OUTPUT_RESOLUTION = 2 ** 16 - 1
@@ -26,3 +27,11 @@ r_pan = np.round(r_pan * OUTPUT_RESOLUTION)
 lookup_tables_u16.append(('left_sin_pan', l_pan))
 lookup_tables_u16.append(('right_cos_pan', r_pan))
 
+# led gamma correction
+gamma = 2.1
+max_in = 255
+max_out = 255
+input_vals = np.linspace(0, max_in, num=max_in + 1)
+gamma_correction = ((input_vals / max_in) ** gamma) * max_out + 0.5
+lookup_tables_u8.append(('led_gamma', np.floor(gamma_correction)))
+
diff --git a/stereo_mix/resources/resources.py b/stereo_mix/resources/resources.py
index ab7e0fc..b77eb32 100755
--- a/stereo_mix/resources/resources.py
+++ b/stereo_mix/resources/resources.py
@@ -74,4 +74,6 @@ create_specialized_manager = True
 resources = [
   (lookup_tables.lookup_tables_u16,
    'lookup_table_u16', 'LUT', 'uint16_t', int, False),
+  (lookup_tables.lookup_tables_u8,
+   'lookup_table_u8', 'LUT', 'uint8_t', int, False),
 ]