]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - quantum/rgblight.c
Merge pull request #849 from hot-leaf-juice/master
[qmk_firmware.git] / quantum / rgblight.c
index 1b3c576d1835b7a4f8d64dfa9aaef0f580b8f2aa..d550c58660610540539688c28ef59229fa9eedb5 100644 (file)
@@ -6,24 +6,37 @@
 #include "rgblight.h"
 #include "debug.h"
 
+// Lightness curve using the CIE 1931 lightness formula
+//Generated by the python script provided in http://jared.geek.nz/2013/feb/linear-led-pwm
 const uint8_t DIM_CURVE[] PROGMEM = {
-  0, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3,
-  3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4,
-  4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6,
-  6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
-  8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11,
-  11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15,
-  15, 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20,
-  20, 20, 21, 21, 22, 22, 22, 23, 23, 24, 24, 25, 25, 25, 26, 26,
-  27, 27, 28, 28, 29, 29, 30, 30, 31, 32, 32, 33, 33, 34, 35, 35,
-  36, 36, 37, 38, 38, 39, 40, 40, 41, 42, 43, 43, 44, 45, 46, 47,
-  48, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
-  63, 64, 65, 66, 68, 69, 70, 71, 73, 74, 75, 76, 78, 79, 81, 82,
-  83, 85, 86, 88, 90, 91, 93, 94, 96, 98, 99, 101, 103, 105, 107, 109,
-  110, 112, 114, 116, 118, 121, 123, 125, 127, 129, 132, 134, 136, 139, 141, 144,
-  146, 149, 151, 154, 157, 159, 162, 165, 168, 171, 174, 177, 180, 183, 186, 190,
-  193, 196, 200, 203, 207, 211, 214, 218, 222, 226, 230, 234, 238, 242, 248, 255
-};
+    0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
+    1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
+    2, 2, 2, 3, 3, 3, 3, 3, 3, 3,
+    3, 4, 4, 4, 4, 4, 4, 5, 5, 5,
+    5, 5, 6, 6, 6, 6, 6, 7, 7, 7,
+    7, 8, 8, 8, 8, 9, 9, 9, 10, 10,
+    10, 10, 11, 11, 11, 12, 12, 12, 13, 13,
+    13, 14, 14, 15, 15, 15, 16, 16, 17, 17,
+    17, 18, 18, 19, 19, 20, 20, 21, 21, 22,
+    22, 23, 23, 24, 24, 25, 25, 26, 26, 27,
+    28, 28, 29, 29, 30, 31, 31, 32, 32, 33,
+    34, 34, 35, 36, 37, 37, 38, 39, 39, 40,
+    41, 42, 43, 43, 44, 45, 46, 47, 47, 48,
+    49, 50, 51, 52, 53, 54, 54, 55, 56, 57,
+    58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
+    68, 70, 71, 72, 73, 74, 75, 76, 77, 79,
+    80, 81, 82, 83, 85, 86, 87, 88, 90, 91,
+    92, 94, 95, 96, 98, 99, 100, 102, 103, 105,
+    106, 108, 109, 110, 112, 113, 115, 116, 118, 120,
+    121, 123, 124, 126, 128, 129, 131, 132, 134, 136,
+    138, 139, 141, 143, 145, 146, 148, 150, 152, 154,
+    155, 157, 159, 161, 163, 165, 167, 169, 171, 173,
+    175, 177, 179, 181, 183, 185, 187, 189, 191, 193,
+    196, 198, 200, 202, 204, 207, 209, 211, 214, 216,
+    218, 220, 223, 225, 228, 230, 232, 235, 237, 240,
+    242, 245, 247, 250, 252, 255,
+    };
+
 const uint8_t RGBLED_BREATHING_TABLE[] PROGMEM = {
   0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 9,
   10, 11, 12, 14, 15, 17, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35,
@@ -42,10 +55,16 @@ const uint8_t RGBLED_BREATHING_TABLE[] PROGMEM = {
   37, 35, 33, 31, 29, 27, 25, 23, 21, 20, 18, 17, 15, 14, 12, 11,
   10, 9, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0
 };
+
+__attribute__ ((weak))
 const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
+__attribute__ ((weak))
 const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
+__attribute__ ((weak))
 const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
+__attribute__ ((weak))
 const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
+__attribute__ ((weak))
 const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {100, 50, 20};
 
 rgblight_config_t rgblight_config;
@@ -55,15 +74,7 @@ uint8_t rgblight_inited = 0;
 
 
 void sethsv(uint16_t hue, uint8_t sat, uint8_t val, struct cRGB *led1) {
-  /* Convert hue, saturation and brightness ( HSB/HSV ) to RGB. The DIM_CURVE is
-  used only on brightness/value and on saturation (inverted). This looks the
-  most natural. */
-  uint8_t r = 0, g = 0, b = 0;
-
-  val = pgm_read_byte(&DIM_CURVE[val]);
-  sat = 255 - pgm_read_byte(&DIM_CURVE[255 - sat]);
-
-  uint8_t base;
+  uint8_t r = 0, g = 0, b = 0, base, color;
 
   if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
     r = val;
@@ -71,41 +82,46 @@ void sethsv(uint16_t hue, uint8_t sat, uint8_t val, struct cRGB *led1) {
     b = val;
   } else {
     base = ((255 - sat) * val) >> 8;
+    color = (val - base) * (hue % 60) / 60;
 
     switch (hue / 60) {
       case 0:
         r = val;
-        g = (((val - base) * hue) / 60) + base;
+        g = base + color;
         b = base;
         break;
       case 1:
-        r = (((val - base) * (60 - (hue % 60))) / 60) + base;
+        r = val - color;
         g = val;
         b = base;
         break;
       case 2:
         r = base;
         g = val;
-        b = (((val - base) * (hue % 60)) / 60) + base;
+        b = base + color;
         break;
       case 3:
         r = base;
-        g = (((val - base) * (60 - (hue % 60))) / 60) + base;
+        g = val - color;
         b = val;
         break;
       case 4:
-        r = (((val - base) * (hue % 60)) / 60) + base;
+        r = base + color;
         g = base;
         b = val;
         break;
       case 5:
         r = val;
         g = base;
-        b = (((val - base) * (60 - (hue % 60))) / 60) + base;
+        b = val - color;
         break;
     }
   }
-  setrgb(r,g,b, led1);
+  r = pgm_read_byte(&DIM_CURVE[r]);
+  g = pgm_read_byte(&DIM_CURVE[g]);
+  b = pgm_read_byte(&DIM_CURVE[b]);
+
+  setrgb(r, g, b, led1);
 }
 
 void setrgb(uint8_t r, uint8_t g, uint8_t b, struct cRGB *led1) {
@@ -346,7 +362,6 @@ void rgblight_set(void) {
   }
 }
 
-
 #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER)
 
 // Animation timer -- AVR Timer3
@@ -453,9 +468,9 @@ void rgblight_effect_snake(uint8_t interval) {
   static uint16_t last_timer = 0;
   uint8_t i, j;
   int8_t k;
-  int8_t increament = 1;
+  int8_t increment = 1;
   if (interval % 2) {
-    increament = -1;
+    increment = -1;
   }
   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
     return;
@@ -466,7 +481,7 @@ void rgblight_effect_snake(uint8_t interval) {
     led[i].g = 0;
     led[i].b = 0;
     for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
-      k = pos + j * increament;
+      k = pos + j * increment;
       if (k < 0) {
         k = k + RGBLED_NUM;
       }
@@ -476,7 +491,7 @@ void rgblight_effect_snake(uint8_t interval) {
     }
   }
   rgblight_set();
-  if (increament == 1) {
+  if (increment == 1) {
     if (pos - 1 < 0) {
       pos = RGBLED_NUM - 1;
     } else {
@@ -492,7 +507,7 @@ void rgblight_effect_knight(uint8_t interval) {
   uint8_t i, j, cur;
   int8_t k;
   struct cRGB preled[RGBLED_NUM];
-  static int8_t increament = -1;
+  static int8_t increment = -1;
   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
     return;
   }
@@ -502,7 +517,7 @@ void rgblight_effect_knight(uint8_t interval) {
     preled[i].g = 0;
     preled[i].b = 0;
     for (j = 0; j < RGBLIGHT_EFFECT_KNIGHT_LENGTH; j++) {
-      k = pos + j * increament;
+      k = pos + j * increment;
       if (k < 0) {
         k = 0;
       }
@@ -523,17 +538,17 @@ void rgblight_effect_knight(uint8_t interval) {
     }
   }
   rgblight_set();
-  if (increament == 1) {
+  if (increment == 1) {
     if (pos - 1 < 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH) {
       pos = 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH;
-      increament = -1;
+      increment = -1;
     } else {
       pos -= 1;
     }
   } else {
     if (pos + 1 > RGBLED_NUM + RGBLIGHT_EFFECT_KNIGHT_LENGTH) {
       pos = RGBLED_NUM + RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
-      increament = 1;
+      increment = 1;
     } else {
       pos += 1;
     }