Skip to content

Commit 5b42717

Browse files
committed
Backlighting Support
1 parent c9e9ef2 commit 5b42717

12 files changed

+419
-79
lines changed

Backlight.cpp

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#include <FastLED.h>
2+
#include <EEPROM.h>
3+
#include "Config.h"
4+
#include "Backlight.h"
5+
#include "Lighting.h"
6+
7+
#define BACKLIGHT_NUM_LEDS 2*(BACKLIGHT_WIDTH + BACKLIGHT_HEIGHT)
8+
9+
CRGB effectTable[ BACKLIGHT_WIDTH + BACKLIGHT_HEIGHT ]; //for pre-computing values to lookup and reduce computations
10+
CRGB backlight_leds[ BACKLIGHT_NUM_LEDS ];
11+
int ledIndex[ BACKLIGHT_NUM_LEDS ];
12+
int effectID = 0;
13+
uint64_t lastSave = 0;
14+
byte backlightBrightness = 255;
15+
16+
CRGB backlight1=CRGB::Black,
17+
backlight2=CRGB::Black;
18+
19+
//Set indicies to a standard order
20+
void initBacklight(){
21+
#if defined(BACKLIGHT_TOP_LEFT) && defined(BACKLIGHT_CLOCKWISE)
22+
for(int i=0;i<BACKLIGHT_NUM_LEDS;i++)
23+
ledIndex[i] = i;
24+
25+
#elif defined(BACKLIGHT_TOP_RIGHT) && defined(BACKLIGHT_CLOCKWISE)
26+
for(int i=0;i<BACKLIGHT_NUM_LEDS;i++)
27+
ledIndex[i] = (i+BACKLIGHT_WIDTH) % BACKLIGHT_NUM_LEDS ;
28+
29+
#elif defined(BACKLIGHT_TOP_LEFT) && defined(BACKLIGHT_COUNTERCLOCKWISE)
30+
for(int i=0;i<BACKLIGHT_NUM_LEDS;i++)
31+
ledIndex[i] = i-BACKLIGHT_NUM_LEDS-1 ;
32+
33+
#elif defined(BACKLIGHT_TOP_RIGHT) && defined(BACKLIGHT_COUNTERCLOCKWISE)
34+
for(int i=0;i<BACKLIGHT_NUM_LEDS;i++)
35+
ledIndex[i] = (BACKLIGHT_WIDTH-1-i) % BACKLIGHT_NUM_LEDS;
36+
37+
#else
38+
#error "Backlight enabled but settings are invalid. Please check if you set options to BACKLIGHT_TOP_RIGHT or BACKLIGHT_TOP_LEFT, and BACKLIGHT_CLOCKWISE or BACKLIGHT_COUNTERCLOCKWISE"
39+
#endif
40+
FastLED.addLeds<LED_TYPE, BACKLIGHT_PIN, COLOR_ORDER>(backlight_leds, BACKLIGHT_NUM_LEDS).setCorrection(TypicalLEDStrip);
41+
EEPROM.begin(512);
42+
byte addr = 36 + WIDTH*HEIGHT*3; //Offset
43+
effectID = EEPROM.read(addr++);
44+
backlightBrightness = EEPROM.read(addr++);
45+
backlight1 = CRGB(EEPROM.read(addr),EEPROM.read(addr+1),EEPROM.read(addr+2)); addr+=3;
46+
backlight2 = CRGB(EEPROM.read(addr),EEPROM.read(addr+1),EEPROM.read(addr+2)); addr+=3;
47+
48+
}
49+
void setBacklightEffectID(int val){effectID = val;}
50+
int getBacklightEffectID(){return effectID;}
51+
void setBacklightBrightness(byte val){backlightBrightness = val;}
52+
void setBacklightColor(byte pos, CRGB color){
53+
if (pos == 0) backlight1 = color;
54+
else if (pos == 1) backlight2 = color;
55+
}
56+
57+
void saveBLEEPROM(){
58+
byte addr = 36 + WIDTH*HEIGHT*3; //Offset
59+
EEPROM.begin(512);
60+
EEPROM.write(addr ,effectID);
61+
EEPROM.write(addr+1,backlightBrightness);
62+
EEPROM.write(addr+2,backlight1.red);
63+
EEPROM.write(addr+3,backlight1.green);
64+
EEPROM.write(addr+4,backlight1.blue);
65+
EEPROM.write(addr+5,backlight2.red);
66+
EEPROM.write(addr+6,backlight2.green);
67+
EEPROM.write(addr+7,backlight2.blue);
68+
EEPROM.commit();
69+
}
70+
71+
int loopIndex = 0;
72+
byte sparkle_chance = 10; //increase this to spawn more sparkles
73+
74+
void showBacklight(){
75+
switch(effectID){
76+
case 0: //off
77+
for(int i=0;i<BACKLIGHT_NUM_LEDS;i++)
78+
backlight_leds[ledIndex[i]] = CRGB::Black;
79+
fadeToBlackBy(backlight_leds, BACKLIGHT_NUM_LEDS , 255 - backlightBrightness );
80+
break;
81+
case 1: //solid
82+
for(int i=0;i<BACKLIGHT_NUM_LEDS;i++)
83+
backlight_leds[ledIndex[i]] = backlight1;
84+
fadeToBlackBy(backlight_leds, BACKLIGHT_NUM_LEDS , 255 - backlightBrightness );
85+
break;
86+
case 2: //rainbow
87+
fill_rainbow(effectTable,BACKLIGHT_WIDTH + BACKLIGHT_HEIGHT,hueOffset,rainbowRate);
88+
pushEffectTable();
89+
fadeToBlackBy(backlight_leds, BACKLIGHT_NUM_LEDS , 255 - backlightBrightness );
90+
break;
91+
case 3: //gradient
92+
fill_gradient_RGB(effectTable,BACKLIGHT_WIDTH + BACKLIGHT_HEIGHT,backlight1,backlight2);
93+
pushEffectTable();
94+
fadeToBlackBy(backlight_leds, BACKLIGHT_NUM_LEDS , 255 - backlightBrightness );
95+
break;
96+
case 4: //rain TODO
97+
break;
98+
case 5: //sparkle
99+
while (1) {
100+
byte randomVal = random8(128);
101+
if (sparkle_chance > randomVal) {
102+
uint16_t randNum = random16(BACKLIGHT_NUM_LEDS); //pick random index within the specified range
103+
sparkle_chance -= randomVal; //decrement counter so we could spawn more than 1 per iteration
104+
backlight_leds[randNum] = backlight1; //apply sparkle to segments and spotlights if the value of ledStart and len permit
105+
fadeToBlackBy(backlight_leds+randNum, 1 , 255 - backlightBrightness ); //dim that sparkle
106+
} else break;
107+
}
108+
fadeToBlackBy(backlight_leds, BACKLIGHT_NUM_LEDS , 60 );
109+
break;
110+
case 6: //fire TODO
111+
break;
112+
case 255: //loop
113+
fadeToBlackBy(backlight_leds, BACKLIGHT_NUM_LEDS , 255 - backlightBrightness );
114+
backlight_leds[ledIndex[loopIndex++]] = backlight1;
115+
break;
116+
default:
117+
effectID=0; //in case it was invalid
118+
}
119+
}
120+
121+
// Translates the data in effectTable to the background LED's
122+
void pushEffectTable(){
123+
//horizontal
124+
for(int i=0;i<BACKLIGHT_WIDTH;i++){
125+
//top horizontal
126+
backlight_leds[ledIndex[i]] = effectTable[i];
127+
//bottom horizontal
128+
backlight_leds[ledIndex[BACKLIGHT_NUM_LEDS - BACKLIGHT_HEIGHT - 1 - i]] = effectTable[i+BACKLIGHT_HEIGHT];
129+
}
130+
//vertical
131+
for(int i=0;i<BACKLIGHT_WIDTH;i++){
132+
//right vertical
133+
backlight_leds[ledIndex[i+BACKLIGHT_WIDTH] ] = effectTable[i + BACKLIGHT_WIDTH];
134+
//left vertical
135+
backlight_leds[ledIndex[BACKLIGHT_NUM_LEDS - 1 - i]] = effectTable[i];
136+
}
137+
}

Backlight.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef BACKLIGHT_H
2+
#define BACKLIGHT_H
3+
#include <FastLED.h>
4+
void initBacklight();
5+
void showBacklight();
6+
void pushEffectTable();
7+
int getBacklightEffectID();
8+
void setBacklightEffectID(int val);
9+
void setBacklightColor(byte pos, CRGB color);
10+
void setBacklightBrightness(byte val);
11+
void saveBLEEPROM();
12+
#endif

Config.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@
3535
extern byte FRAMES_PER_SECOND; // here you can control the speed. With the Access Point / Web Server the animations run a bit slower.
3636
// Originally constant, but can be changed using commands
3737

38+
/* Optional Backlight Example:
39+
* Width: 11
40+
* Height: 3
41+
* top left clockwise led0 >> 0 1 2 3 4 5 6 7 8 9 10 << top right counterclockwise led0
42+
* top left counterclockwise led0 >> 27 11 << top right clockwise led0
43+
* 26 12
44+
* 25 13
45+
* 24 23 22 21 20 19 18 17 16 15 14 */
46+
#define BACKLIGHT //If you have backlight behind the clock, define here
47+
#ifdef BACKLIGHT
48+
#define BACKLIGHT_PIN 14 //D5 on a NodeMCU
49+
//Where the wiring starts
50+
#define BACKLIGHT_TOP_LEFT //Options: BACKLIGHT_TOP_LEFT, BACKLIGHT_TOP_RIGHT
51+
//Wiring direction
52+
#define BACKLIGHT_CLOCKWISE //Options: BACKLIGHT_CLOCKWISE, BACKLIGHT_COUNTERCLOCKWISE
53+
//Dimensions
54+
#define BACKLIGHT_WIDTH 120 //Number of horizontal LED's on the top and bottom
55+
#define BACKLIGHT_HEIGHT 60 //Number of vertical LED's on the left and right side
56+
#endif
57+
3858
//Assume direction is from top left to bottom right. Negative if pointing in opposite direction
3959
//numbers here are what the wiring maps to the abstracted array below. Index is what LED segment they are. Add +1 to give 0 a +- sign
4060
//Wiring Index
@@ -49,7 +69,6 @@ extern byte FRAMES_PER_SECOND; // here you can control the speed. With the Acce
4969
const int PROGMEM m_ten[] = {3,9,10,16,22,23,29};
5070
const int PROGMEM h_one[] = {1,7,8,14,20,21,27};
5171
const int PROGMEM h_ten[] = {-1,-1,6,-1,-1,19,-1}; //-1 means undefined since 12hr doesnt have this segment
52-
5372
#endif
5473
#ifdef _24_HR_CLOCK
5574
//1-indexed

LED_Clock.ino

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "NTPTime.h"
44
#include "Lighting.h"
55
#include "WebServer.h"
6+
#include "Backlight.h"
67

78
byte FRAMES_PER_SECOND = 30; //will be overwritten later on by EEPROM or default settings
89
unsigned long frameStart; //For fps counter
@@ -28,6 +29,9 @@ void setup(){
2829

2930
Serial.println("Initializing data structures for lighting effects");
3031
lightingInit();
32+
#ifdef BACKLIGHT
33+
initBacklight();
34+
#endif
3135

3236
//Website filesystem setup
3337
LittleFS.begin();
@@ -56,6 +60,9 @@ void loop() {
5660
//Pretty much the only 2 lines that get called aside from the last 2 FastLED calls
5761
updateServer();
5862
showLightingEffects();
63+
#ifdef BACKLIGHT
64+
showBacklight();
65+
#endif
5966

6067
//Track FPS
6168
if(counter>=FRAMES_PER_SECOND*5){
@@ -73,6 +80,9 @@ void loop() {
7380
lastUpdate = 0;
7481
updateSettings = false;
7582
storeEEPROM();
83+
#ifdef BACKLIGHT
84+
saveBLEEPROM();
85+
#endif
7686
}
7787

7888
// insert a delay to maintain framerate.

Lighting.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const int PROGMEM NUM_SEGMENTS = 2*WIDTH*HEIGHT + WIDTH + HEIGHT;
1414

1515
//Love me some global variables
1616
extern int rainbowRate;
17+
extern int hueOffset;
1718
extern uint32_t lastUpdate;
1819
extern bool updateSettings;
1920

0 commit comments

Comments
 (0)