-
Notifications
You must be signed in to change notification settings - Fork 2
/
Neopixel.ino
175 lines (155 loc) · 4.51 KB
/
Neopixel.ino
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
#define COL1 pixels.Color(0,255,0)
#define COL2 pixels.Color(140,255,0)
#define COL3 pixels.Color(255,255,0)
#define COL4 pixels.Color(255,200,0)
#define COL5 pixels.Color(255,120,0)
#define COL6 pixels.Color(255,0,0)
#define BLUE pixels.Color(0,0,255)
uint32_t colors[] = {COL1, COL2, COL3, COL4, COL5, COL6};
#define DEFAULT_COLOR_RANGES "700 1000 1300 1600 1900 "
//uint16_t colorRanges[] = {700, 1000, 1300, 1600, 1900};
byte maxBrightness = 120;
uint32_t forcedColorMS = 0;
byte ppm2idx(uint32_t ppm) {
int rng;
if (ppm > 6000) return 1;
else if (ppm > 10000) return 2;
else if (ppm > 20000) return 3;
else if (ppm > 40000L) return 4;
else if (ppm > 80000L) return 5;
else if (ppm > 160000L) return 6;
for (byte i=0; i < 5; i++) {
EEPROM.get(EE_10B_TH + i*2, rng);
if (ppm < rng) return i;
}
return 5;
}
uint32_t idx2color(byte idx, uint32_t ppm) {
if (ppm > 6000) return BLUE;
return colors[idx];
}
void storeColorRanges(char *p) {
String num;
byte idx = 0;
if (*p == 'd') storeColorRanges(DEFAULT_COLOR_RANGES);
else {
while (idx < 5) {
if (isDigit(*p)) num += *p;
else {
EEPROM.put(EE_10B_TH + (idx++ * 2), atoi(num.c_str()));
num = "";
}
if (!*p) break;
p++;
}
}
}
void initColorRanges() {
int rng;
EEPROM.get(EE_10B_TH, rng);
if (rng == -1) storeColorRanges(DEFAULT_COLOR_RANGES);
}
void initNeopixels() {
initColorRanges();
pixels.begin();
for (int i=0; i< 6; i++) pixels.setPixelColor(i, colors[i]);
pixels.setBrightness(20);
pixels.show();
}
void debugInfoNeopixel() {
Serial << F("Brightness :") << sBrightness<< endl;
}
float getBrgFactor() {
byte bb = EEPROM.read(EE_1B_BRG_FACTOR);
return (float)(bb==255?10:bb) / 10;
}
#define BRG_CHECK_TIMEOUT 10000
uint32_t lastBrgCheck = 0;
void processBrightness() {
if (timePassed(lastBrgCheck, BRG_CHECK_TIMEOUT) == false) return;
lastBrgCheck = millis();
if (overrideBrightness == 255) {
raLight.addValue(analogReadFine(LIGHT_PIN, 1));
int maxLightRead = EEPROM.read(EE_1B_ISGRAY) == 1 ? 400 : 1000;
int light = (int)((float)raLight.getAverage() * getBrgFactor());
//Serial << "ralig: " << raLight.getAverage() << ", brg:" << getBrgFactor() << endl;
int r=0;
if (light <= 10) {
sBrightness = light;
if (!sBrightness) sBrightness = 1;
} else {
r = constrain(light, 0, maxLightRead);
sBrightness = map(r, 11, maxLightRead, 11, maxBrightness);
}
//Serial << maxLightRead << "." << r << "." << sBrightness << "." << maxBrightness << endl;
} else {
sBrightness = overrideBrightness;
//Serial << "ldr: " << analogReadFine(LIGHT_PIN, 1) << endl;
}
pixels.setBrightness(sBrightness);
pixels.show();
}
void ledHeat(bool turnOn) {
pixels.begin();
for (int i=0; i< 6; i++) {
if (turnOn) pixels.setPixelColor(i, pixels.Color(255, 255, 255));
else pixels.setPixelColor(i, pixels.Color(0, 0, 0));
}
//pixels.setBrightness(20);
pixels.show();
}
uint32_t lastNeoPixelChange = 0;
#define NEOPIXEL_TIMEOUT 1000L
#define FORCEDLED_TIMEOUT 60000L
void processColors() {
if (!sPPM) return;
byte i = 0;
int adj = 0;
if (!timePassed(forcedColorMS, FORCEDLED_TIMEOUT)) adj = -1;
Serial << "adj: " << adj << endl;
if (sPPM == -1) {
for (; i < NUMPIXELS + adj ; i++) pixels.setPixelColor(i, 0);
pixels.setPixelColor(0, pixels.Color(0, 0, 255));
pixels.show();
return;
}
byte idx = ppm2idx(sPPM);
uint32_t color = idx2color(idx, sPPM);
for (; i <= idx + adj ; i++) pixels.setPixelColor(i, color);
for (; i < NUMPIXELS + adj; i++) pixels.setPixelColor(i, 0);
pixels.show();
}
//void overrideLedsMax() {
// int col = 5;
// for (int i=0; i < col ; i++) pixels.setPixelColor(i, colors[col-1]);
// //pixels.setBrightness(200);
// pixels.show();
//
//}
void processNeopixels() {
if (timePassed(lastNeoPixelChange, NEOPIXEL_TIMEOUT) == false) return;
lastNeoPixelChange = millis();
// Serial << "neopixels" << endl;
// if (overrideLeds) {
// overrideLedsMax();
// processBrightness();
// } else {
processColors();
processBrightness();
// }
}
void onLED(String &x) {
int a = x.indexOf(F("LED##")) + 5;
Serial << a << endl;
Serial << line + a << endl;
const char *p = strchr(line, ' ');
int r = atoi(p);
p = strchr(p + 1, ' ');
int g = atoi(p);
p = strchr(p + 1, ' ');
int b = atoi(p+1);
Serial <<"LED: " << r << "," << g << "," << b << endl;
pixels.setPixelColor(5, pixels.Color(r, g, b));
pixels.show();
forcedColorMS = millis();
}