Browse Source

implement color palette support for AURA

Peter Cai 11 months ago
parent
commit
8f43b55eb2
Signed by: Peter Cai <[email protected]> GPG Key ID: 71F5FB4E4F3FD54F
6 changed files with 45 additions and 16 deletions
  1. 5
    0
      WTFRGB.ino
  2. 21
    16
      aura.cpp
  3. 3
    0
      aura.h
  4. 8
    0
      color.cpp
  5. 6
    0
      color.h
  6. 2
    0
      effect.h

+ 5
- 0
WTFRGB.ino View File

@@ -63,6 +63,11 @@ void loadEEPROM(bool forceReset) {
63 63
   for (auto& effect : effects) {
64 64
     effect->loadFromEEPROM();
65 65
   }
66
+
67
+  // Call post-init hooks when everything has been loaded.
68
+  for (auto& effect : effects) {
69
+    effect->postInit();
70
+  }
66 71
 }
67 72
 
68 73
 void writeAllToEEPROM() {

+ 21
- 16
aura.cpp View File

@@ -1,21 +1,28 @@
1 1
 #include "aura.h"
2
+#include "color.h"
2 3
 
3 4
 // ==== The Aura Rainbow Effect ====
4
-// TODO: Implement arbitrary color selection
5 5
 int auraCounter = 0;
6
-int auraWidth = NUM_LEDS / 3;
7
-int auraDecrement;
6
+int auraWidth;
7
+unsigned char paletteCounter;
8
+CRGB curColor, nextColor;
8 9
 
9 10
 void AuraEffect::reset() {
10 11
   auraCounter = 0;
12
+  paletteCounter = 0;
11 13
   auraWidth = NUM_LEDS / 3;
12
-  auraDecrement = MAX_BRIGHTNESS / auraWidth;
13 14
 }
14 15
 
15
-void AuraEffect::onUpdate() {
16
-  auraCounter = (auraCounter + 1) % (auraWidth * 3);  // 3 = R G B
17
-  int cycleProgress = auraDecrement * (auraCounter % auraWidth);
16
+void AuraEffect::stepColor() {
17
+  curColor = *getPaletteColor(paletteCounter);
18
+  nextColor = *nextPaletteColor(&paletteCounter);
19
+}
20
+
21
+void AuraEffect::postInit() {
22
+  this->stepColor();
23
+}
18 24
 
25
+void AuraEffect::onUpdate() {
19 26
   // Shift all LEDs left by 1 px
20 27
   for (int i = 0; i < NUM_LEDS - 1; i++) {
21 28
     leds[i] = CRGB(leds[i + 1].r, leds[i + 1].g, leds[i + 1].b);
@@ -23,14 +30,12 @@ void AuraEffect::onUpdate() {
23 30
 
24 31
   // Calculate the color for the last LED
25 32
   int last = NUM_LEDS - 1;
26
-  if (auraCounter < auraWidth) {
27
-    // RED
28
-    leds[last] = CRGB(MAX_BRIGHTNESS - cycleProgress, cycleProgress, 0);
29
-  } else if (auraCounter < auraWidth * 2) {
30
-    // GREEN
31
-    leds[last] = CRGB(0, MAX_BRIGHTNESS - cycleProgress, cycleProgress);
32
-  } else {
33
-    // BLUE
34
-    leds[last] = CRGB(cycleProgress, 0, MAX_BRIGHTNESS - cycleProgress);
33
+  leds[last] = blendColor(curColor, nextColor, auraCounter, auraWidth);
34
+
35
+  if (auraCounter == (auraWidth - 1)) {
36
+    this->stepColor();
35 37
   }
38
+
39
+  // Increment the counter
40
+  auraCounter = (auraCounter + 1) % auraWidth;
36 41
 }

+ 3
- 0
aura.h View File

@@ -4,8 +4,11 @@
4 4
 #include "effect.h"
5 5
 
6 6
 class AuraEffect : public Effect {
7
+ private:
8
+  void stepColor();
7 9
  public:
8 10
   void onUpdate();
11
+  void postInit();
9 12
   void reset();
10 13
 };
11 14
 

+ 8
- 0
color.cpp View File

@@ -0,0 +1,8 @@
1
+#include "color.h"
2
+
3
+CRGB blendColor(CRGB color1, CRGB color2, int dividend, int divisor) {
4
+  return CRGB(
5
+    color1.r - color1.r * dividend / divisor + color2.r * dividend / divisor,
6
+    color1.g - color1.g * dividend / divisor + color2.g * dividend / divisor,
7
+    color1.b - color1.b * dividend / divisor + color2.b * dividend / divisor);
8
+}

+ 6
- 0
color.h View File

@@ -0,0 +1,6 @@
1
+#ifndef COLOR_H
2
+#define COLOR_H
3
+#include "ws2812.h"
4
+
5
+CRGB blendColor(CRGB color1, CRGB color2, int dividend, int divisor);
6
+#endif

+ 2
- 0
effect.h View File

@@ -15,6 +15,8 @@ class Effect {
15 15
   // Load all values from EEPROM
16 16
   virtual void loadFromEEPROM(){};
17 17
   virtual void writeToEEPROM(){};
18
+  // Called when everything is loaded from EEPROM (i.e. initialization finished)
19
+  virtual void postInit(){};
18 20
   // Serial command events
19 21
   virtual void handleCommand(char *cmdBuf, int len) {};
20 22
 };

Loading…
Cancel
Save