-
Notifications
You must be signed in to change notification settings - Fork 1
/
LEDString.h
320 lines (284 loc) · 11.4 KB
/
LEDString.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#ifndef LEDString_ino
#define LEDString_ino "Jan 2, 2021"
#include <FastLED.h>
/* This section only applies if you have an ambient light sensor connected */
#if USE_LIGHT_SENSOR
/* The sketch will automatically scale the light between MIN_BRIGHTNESS and
MAX_BRIGHTNESS on the ambient light values between MIN_LIGHT and MAX_LIGHT
Set MIN_BRIGHTNESS and MAX_BRIGHTNESS to the same value to achieve a simple on/off effect. */
#if LIGHT_SENSOR_TSL2561
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
#include <Wire.h>
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
#else
#define LIGHTSENSORPIN A0 // A0 is the only valid pin for an analog light sensor
#endif // LIGHT_SENSOR_TSL2561
#endif // USE_LIGHT_SENSOR
//#define DATA_PIN 5 //6 //14 // Kits shipped after March 1, 2019 should use 14. Earlier kits us 5. NodeMCU ESP 12-E modules
#define DATA_PIN 14 // Kits shipped after March 1, 2019 should use 14. Earlier kits us 5. LILON WEMOS D1 mini Lite
// ******************* NOTE Pin 14 causes compile error for NodeMCU board ********************
#define LED_TYPE WS2811
#define COLOR_ORDER RGB
// Define the array of leds
static CRGB leds[NUM_AIRPORTS];
/*
* Setup for Led String and Light sensor
*
*/
void setupLEDString(void){
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_AIRPORTS).setCorrection(TypicalLEDStrip);
pinMode(D1, OUTPUT); //Declare Pin mode
#if USE_LIGHT_SENSOR
#if LIGHT_SENSOR_TSL2561
Wire.begin(D2, D1);
if (!tsl.begin()) {
/* There was a problem detecting the TSL2561 ... check your connections */
Serial.println(F("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!"));
} else {
tsl.enableAutoRange(true);
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);
}
#else
pinMode(LIGHTSENSORPIN, INPUT);
#endif
#endif
}
/*
* AdjustBrightness()
* Working with two sensors the Digital TSL2561 or Analog
* I tested with an Analog sensor from the package of 10 Gowoops ZZ20161810
*
*/
static signed short lightOffset = 0; // can be changed using slider on station (button) page
static signed short brightness; // needs to work with '-' numbers
void adjustBrightness() {
float reading;
#if USE_LIGHT_SENSOR
#if LIGHT_SENSOR_TSL2561
sensors_event_t event;
if(tsl.getEvent(&event)) {
reading = (event.light); // sensor reading expected between 0 and 65535 lux READING NO SENSOR = 65536.00
// Using iPhone light 19600
// Bright Sun light 1260
// Dark 0
// Room 550
//reading = reading / 64; // adjust values 0 to 1024 to match analog sensor
} else {
reading = 512; // Fake-it for defective or missing sensor
}
#else // Must be using the analog sensor
reading = 1024 - analogRead(LIGHTSENSORPIN); // Invert sensor values
// sensor values:
// - dark = 1024 max light = 0
// - actual working values using iPhone light
// - dark = 1024
// - bright = 60
// - With No sensor reading = 8 sometimes 7 (using node MCU ESP12E)
// light string low (dim) = 0 high = 255
#endif // TSL2561
#else // Not using any light sensor fake it so the slider adjustment still works
reading = 512; // Half way for no sensor
#endif // USE_LIGHT_SENSOR
//brightness = 256 - (reading / 4); // the sensor values are Max light = 0 (60 for iPhone flashlight) ... Max dark = 1024
brightness = (reading / 4); // the sensor values are Max light = 0 (60 for iPhone flashlight) ... Max dark = 1024
// converts values to 256 (212) ... 0 for the LED string
// sanity check
brightness = brightness + lightOffset;
if(brightness < 20) brightness = 20;
if(brightness > 255) brightness = 255;
//Serial.print(" = LSensor = "); Serial.println(brightness);
// low high room
// DLS 20 ... 255, 105
// ALS 20 ... 234, 162
FastLED.setBrightness(brightness);
FastLED.show();
} // adjustBrightness()
/*
*
* Slider
* Make a nice html displayed slider
*
*/
void slider(){
server.sendContent(F("<h3 align=\"center\" ><form action=\"/sliderpage\" id=\"numform\" oninput=\"x.value=parseInt(a.value)\">\n"
"<input type=\"range\" id=\"a\" name=\"a\" value=\""));
server.sendContent( b2Scs(lightOffset) ); // convert signed short to c-string
server.sendContent(F("\" max=\"128\" min=\"-128\" >\n"
"<br>\n"
"<input type=\"submit\">\n"
"</form>\n"
"<output form=\"numform\" id=\"x\" name=\"x\" for=\"b\"></output><br>\n </h3>"));
}
/*
*
* Slider Page
* Called by a HTML page get for program to progress the slider values
* uses goback to return to the calling page
*
*
*/
void slider_page(){
signed short svalue=0;
if(server.hasArg("a")){
Serial.print(F("Found value for 'a' "));
svalue = server.arg("a").toInt();
Serial.println(svalue);
// FastLED.setBrightness(svalue); // scale a 0-255 value for how much to scale all LEDs before writing them out
lightOffset = svalue; // Slide value -128 ... 128
}
//const static char goBack[] PROGMEM = "<!DOCTYPE html> <script language=\"JavaScript\" type=\"text/javascript\"> setTimeout(\"window.history.go(-1)\",10); </script>";
server.send(200, "text/html", goBack); // goBack string stored in PROGMEM declared in WX_Sectional.h A it is used in 2 functions iDLED and Slider_page
server.client().stop();
}
/*
* idLED()
* call idLED(LEDNo) first
* Flashes the single LED ledNo in the string
* each call toggles ledNo white black until flashLEDCnt is 0
* on the last call flashLEDCnt == 1 restores ledNo to color saved in lastLED
*/
static byte ledNo; // flashLEDCnt = 0; // number of cycles
static CRGB lastLED; // Prevent memory frag
#define NO_OF_TIMES 25
void idLED(){
static byte flashLEDCnt = NO_OF_TIMES; // number of cycles to flash LED on call to ID
static boolean flashLEDOn;
if(flashLEDCnt > 1){
if(flashLEDOn){
leds[ledNo] = CRGB::Black; //CHSV(0,0,0);
}else{
leds[ledNo] = CRGB::White;
}
flashLEDOn = !flashLEDOn;
}else {
leds[ledNo] = lastLED;
my_Event = NO_EVENT;
flashLEDCnt = NO_OF_TIMES;
}
flashLEDCnt--;
delay(10);
FastLED.show();
}
/*
*
* idLED(number) Flash a single LED
* Setup for and the first entry to idLED()
*
*
*/
void idLED(byte led_number) {
if (led_number < NUM_AIRPORTS){ // && led_number >= 0) {
Serial.print(F("Flashing LED #"));
Serial.println(led_number);
ledNo = led_number; // Save Led Number for subsequent calls to idLED()
lastLED = leds[ledNo]; // temporarily store original color
my_Event = MY_ID;
idLED(); // make first call the rest are done from main loop
}
}
/*
*
* Test
* turn on every led red blue green and white
* NOTE: Can not spend a lot of time here need to return to main loop B4 SW Timeout
* HWD = 8.2 seconds
* SWD = About 1.5 seconds
*
*/
void test(void){
static byte testcycle = 5; // Test cycle all four test colors used to call test(byte)
CRGB testColor; // Instead of doing all tests in same loop cycle for each color
switch(testcycle){
case 5:{
Serial.print(F("Hello from test "));
Serial.print(F(" 5 ")); Serial.flush();
testColor = CRGB::Red;
testcycle--;
}
break;
case 4:{
testColor = CRGB::Blue;
Serial.print(F(" 4 ")); Serial.flush();
testcycle--;
}
break;
case 3:{
testColor = CRGB::Green;
Serial.print(F(" 3 ")); Serial.flush();
testcycle--;
}
break;
case 2:{
testColor = CRGB::White;
Serial.print(F(" 2 ")); Serial.flush();
testcycle--;
}
break;
case 1:{
testColor = CRGB::Black; //CHSV(0,0,0);
Serial.print(F(" 1 "));
testcycle = 5; // ready for next run
my_Event = NO_EVENT;
loop_time = loop_interval - 5; // force update shortly after test
Serial.println(F("Test Done")); Serial.flush();
}
break;
default:
testcycle = 5; // JIC
my_Event = NO_EVENT;
}
int i=0;
for( i=0; i < NUM_AIRPORTS; i++ ){
leds[i] = testColor;
}
delay(10);
FastLED.show();
}
/*
* CRGBtoHex
* Convert from CRGB to a hex string
* Suitable for HTML Java script color attribute
*
* Function created because I could not find one in the CRGB class in any case the return is formatted
* with '#' and ready to insert in a color attribute in the HTML page
*
*/
static char hexval[8];
char* CRGBtoHex(CRGB c){
// Removed calls to sprintf they were causing memory fragmentation
//sprintf(hexval, "#%02X%02X%02X",c.r,c.g,c.b);
//Serial.print("\nTo Hex ");
//Serial.print(hexval);
hexval[0] = '#';
hexval[1] = '\0';
strncat(hexval, byteToHex(c.r), 2);
strncat(hexval, byteToHex(c.g), 2);
strncat(hexval, byteToHex(c.b), 2);
return hexval;
}
/*
doLighting()
Process Lighting flash the LED(s) about once a second (every loop)
Moved outside of LEDSectional allowing increase of the LED lighting flash rate without flooding the WX server.
*/
void doLighting() {
// TODO Using vector may be causing memory fragmentation need to investigate
if (DO_LIGHTNING && lightningLeds.size() > 0) {
std::vector<CRGB> lightning(lightningLeds.size());
for (unsigned short int i = 0; i < lightningLeds.size(); ++i) {
unsigned short int currentLed = lightningLeds[i];
lightning[i] = leds[currentLed]; // temporarily store original color
leds[currentLed] = CRGB::White; // set to white briefly
}
delay(25); my_yield(); // extra delay seems necessary with light sensor
FastLED.show();
delay(75); my_yield();
for (unsigned short int i = 0; i < lightningLeds.size(); ++i) {
unsigned short int currentLed = lightningLeds[i];
leds[currentLed] = lightning[i]; // restore original color
}
FastLED.show();
}
} // doLighting()
#endif