-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathledctrl.cpp
More file actions
340 lines (295 loc) · 7.89 KB
/
ledctrl.cpp
File metadata and controls
340 lines (295 loc) · 7.89 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <Arduino.h>
#include <math.h>
#include "ledctrl.h"
#include "sparcle.h"
#include "rotor.h"
#include "flow.h"
#include "rain.h"
#include "text.h"
#include "bitmap.h"
uint8_t fadeComponent(int a, int ff, int b) {
int q=(a * ff)/256 + (b * (256-ff))/256;
if (q < 0) q=0;
if (q > 255) q=255;
return (uint8_t)q;
}
uint8_t interpolate(uint8_t a, int ff, uint8_t b) {
if (ff <= 0) return b;
if (ff >= 255) return a;
ff = (ff * (int)a) / 255 + ((255-ff) * (int)b)/255;
if (ff < 0) return 0;
if (ff > 255) return 255;
return ff;
}
int minterpolate(int a, int f, int max, int b) {
return (a*(max-f) + b*f)/max;
}
uint8_t fromDouble(double d) {
int i=floor(d*255.0+0.5);
if (i < 0) i=0;
if (i > 255) i=255;
return (uint8_t) i;
}
double sqr(double a) { return a*a; }
double sin2(double a) { return sqr(sin(a)); }
void randcol(uint8_t *r, uint8_t *g, uint8_t *b) {
double col=(double)rand()/(double)RAND_MAX; // 0..1
double colr=pow(sin2( col *M_PI) ,EXPON);
double colg=pow(sin2((col+OSG)*M_PI) ,EXPON);
double colb=pow(sin2((col+OSB)*M_PI) ,EXPON);
*r = fromDouble(colr);
*g = fromDouble(colg);
*b = fromDouble(colb);
}
// #define NOP for (int i=0;i<100;i++)
// #define NOP delayMicroseconds(1)
#define NOP
#define CLOCKBIT(bit) \
digitalWrite(PINDATA,(bit)); \
NOP; \
digitalWrite(PINCLK,HIGH); \
NOP; \
digitalWrite(PINCLK,LOW);
void ledctrl::CLOCKBYTE(uint8_t b) {
uint8_t i=0x80;
while (i) {
CLOCKBIT((b&i)!=0);
i>>=1;
}
}
int ledctrl::length() {
return LEDS;
}
void ledctrl::progLeds() {
short leds;
uint8_t i;
short ff;
// Header --> 32 x LOW
for (i=0;i<4;i++)
CLOCKBYTE(0x00);
// LED Data
for (leds = 0;leds < LEDS;leds++) {
CLOCKBYTE(GLOBAL | 0xE0);
CLOCKBYTE(LEDBLUE[leds]);
CLOCKBYTE(LEDGREEN[leds]);
CLOCKBYTE(LEDRED[leds]);
}
// Footer
ff=LEDS/(2*8);
if (ff*(2*8) < LEDS) ff++;
if (ff < 4) ff=4;
for (i=0;i<ff;i++)
CLOCKBYTE(0xFF);
// digitalWrite(DATA, LOW);
}
void ledctrl::allLeds(uint8_t red, uint8_t green, uint8_t blue) {
memset(LEDRED, red, LEDS);
memset(LEDGREEN, green, LEDS);
memset(LEDBLUE, blue, LEDS);
}
void ledctrl::allOff() {
allLeds(0,0,0);
}
void ledctrl::startUpSequence() {
// printf("All LEDS red...green...blue...off\n");
GLOBAL=0x01;
allLeds(0x30,0x00,0x00);
progLeds();
delay(100);
allLeds(0x00,0x30,0x00);
progLeds();
delay(100);
allLeds(0x00,0x00,0x30);
progLeds();
delay(100);
allLeds(0x00,0x00,0x00);
progLeds();
}
void ledctrl::setGlobal(uint16_t c) {
if (c > 0x1F) c=0x1F;
GLOBAL=c;
}
void ledctrl::setRange(uint16_t first, uint16_t last, uint16_t red, uint16_t green, uint16_t blue) {
for (int i=first;i<=last;i++) {
if ((i >= 0) && (i < LEDS)) {
LEDRED[i]=red;
LEDGREEN[i]=green;
LEDBLUE[i]=blue;
}
}
}
void ledctrl::shiftEnds(char fromLeft, uint16_t red, uint16_t green, uint16_t blue) {
if (fromLeft) {
for (int i=LEDS-1;i>0;i--) {
LEDRED[i]=LEDRED[i-1];
LEDGREEN[i]=LEDGREEN[i-1];
LEDBLUE[i]=LEDBLUE[i-1];
}
LEDRED[0]=red;
LEDGREEN[0]=green;
LEDBLUE[0]=blue;
} else {
for (int i=0;i<LEDS-1;i++) {
LEDRED[i]=LEDRED[i+1];
LEDGREEN[i]=LEDGREEN[i+1];
LEDBLUE[i]=LEDBLUE[i+1];
}
LEDRED[LEDS-1]=red;
LEDGREEN[LEDS-1]=green;
LEDBLUE[LEDS-1]=blue;
}
}
void ledctrl::shiftCenter(char FlowOut, uint16_t red, uint16_t green, uint16_t blue) {
int center = LEDS/2;
if (FlowOut) {
for (int i=LEDS-1;i>center;i--) {
LEDRED[i]=LEDRED[i-1];
LEDGREEN[i]=LEDGREEN[i-1];
LEDBLUE[i]=LEDBLUE[i-1];
}
for (int i=0;i<center;i++) {
LEDRED[i]=LEDRED[i+1];
LEDGREEN[i]=LEDGREEN[i+1];
LEDBLUE[i]=LEDBLUE[i+1];
}
LEDRED[center]=red;
LEDGREEN[center]=green;
LEDBLUE[center]=blue;
} else {
for (int i=center;i<LEDS-1;i++) {
LEDRED[i]=LEDRED[i+1];
LEDGREEN[i]=LEDGREEN[i+1];
LEDBLUE[i]=LEDBLUE[i+1];
}
for (int i=center;i>0;i--) {
LEDRED[i]=LEDRED[i-1];
LEDGREEN[i]=LEDGREEN[i-1];
LEDBLUE[i]=LEDBLUE[i-1];
}
LEDRED[0]=red;
LEDGREEN[0]=green;
LEDBLUE[0]=blue;
LEDRED[LEDS-1]=red;
LEDGREEN[LEDS-1]=green;
LEDBLUE[LEDS-1]=blue;
}
}
void ledctrl::fadeTo(uint16_t ff, uint16_t red, uint16_t green, uint16_t blue) {
for (int i=0;i<LEDS;i++) {
LEDRED[i]=fadeComponent(LEDRED[i],ff,red);
LEDGREEN[i]=fadeComponent(LEDGREEN[i],ff,green);
LEDBLUE[i]=fadeComponent(LEDBLUE[i],ff,blue);
}
}
void ledctrl::cloudIn(char sym, int pos, int width, uint16_t red, uint16_t green, uint16_t blue) {
for (int i=pos- 2*width;i<=pos + 2*width;i++) {
int q=i-pos;
int val;
val=(width*width*255)/(q*q*5 + width*width);
if ((i >= 0) && (i < LEDS)) {
LEDRED[i]=interpolate(red,val,LEDRED[i]);
LEDGREEN[i]=interpolate(green,val,LEDGREEN[i]);
LEDBLUE[i]=interpolate(blue,val,LEDBLUE[i]);
}
}
}
void ledctrl::errorCode(uint8_t x) {
GLOBAL=1;
allLeds(0x00,0x00,0x00);
for (uint8_t i=0x80, j=0;i>>=1,j++;i)
LEDRED[j]=(x & i)?0x30:0x00;
progLeds();
}
void ledctrl::updatefromeffect(effect *e) {
if (e->isfield) {
for (int i=0;i<LEDS;i++) {
uint32_t index = ledindex[i];
LEDRED[i] = e->field->r[index];
LEDGREEN[i] = e->field->g[index];
LEDBLUE[i] = e->field->b[index];
}
} else {
memcpy(LEDRED, e->list->r, LEDS);
memcpy(LEDGREEN, e->list->g, LEDS);
memcpy(LEDBLUE, e->list->b, LEDS);
}
}
effect *ledctrl::getselectedeffect() {
if ((effectselect->selected_effect < 0) || (effectselect->selected_effect >= list.size()))
return NULL;
std::list<effect *>::iterator it = list.begin();
std::advance(it, effectselect->selected_effect);
return (*it);
}
void ledctrl::tick() {
// Update Globals first
for (std::list<parametric*>::iterator it = globals.begin(); it != globals.end(); ++it)
(*it)->tick();
// Then run current effect
if ((effectselect->selected_effect < 0) || (effectselect->selected_effect >= list.size())) {
allLeds(0,0,0);
} else {
// list.front()->tick();
// updatefromeffect(list.front());
std::list<effect *>::iterator it = list.begin();
std::advance(it, effectselect->selected_effect);
(*it)->tick();
updatefromeffect(*it);
}
}
effect *ledctrl::findeffect(char *e) {
for (std::list<effect*>::iterator it = list.begin(); it != list.end(); ++it) {
if (strcasecmp((*it)->name, e)==0)
return (*it);
}
return NULL;
}
parametric *ledctrl::findglobal(char *e) {
for (std::list<parametric *>::iterator it = globals.begin(); it != globals.end(); ++it) {
if (strcasecmp((*it)->name, e)==0)
return (*it);
}
return NULL;
}
parametric *ledctrl::findparametric(char *s) {
parametric *p = findglobal(s);
if (p)
return p;
effect *e = findeffect(s);
if (e)
return e;
return NULL;
}
int ledctrl::findeffectindex(effect *e) {
int i=0;
for (std::list<effect*>::iterator it = list.begin(); it != list.end(); ++it) {
if ((*it) == e) return i;
i++;
}
return -1;
}
void ledctrl::selecteffect(char *e) {
effect *eff = findeffect(e);
if (eff) {
int index = findeffectindex(eff);
if (index >= 0)
effectselect->selected_effect = index;
}
}
ledctrl::ledctrl() {
pinMode(PINCLK, OUTPUT);
pinMode(PINDATA, OUTPUT);
effectselect = new effectselector();globals.push_back(effectselect);
colorgenA = new colorgen("ColA");globals.push_back(colorgenA);
// colorgenB = new colorgen("ColB");globals.push_back(colorgenB);
sparcle *s = new sparcle();s->setGlobalCol(colorgenA);list.push_back(s);
rotor *r = new rotor();r->setGlobalCol(colorgenA);list.push_back(r);
flow *f = new flow();f->setGlobalCol(colorgenA);list.push_back(f);
rain *n = new rain();n->setGlobalCol(colorgenA);list.push_back(n);
text *t = new text();t->setGlobalCol(colorgenA);list.push_back(t);
bitmap *b = new bitmap();list.push_back(b);
startUpSequence();
effectselect->selected_effect = 0;
effectselect->total_effects = list.size();
}
// EOF