-
Notifications
You must be signed in to change notification settings - Fork 0
/
led.h
759 lines (595 loc) · 13.7 KB
/
led.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
#ifndef LED_H
#define LED_H
#include "Arduino.h"
#include <inttypes.h>
#include "color.h"
// tri-color PWM RGB LED class
class TriLED {
public:
Color color;
unsigned int pr : 7, pg : 7, pb : 7;
TriLED(uint8_t redPin, uint8_t greenPin, uint8_t bluePin):
pr(redPin), pg(greenPin), pb(bluePin), color(0,0,0)
{
pinMode(pr, OUTPUT);
pinMode(pg, OUTPUT);
pinMode(pb, OUTPUT);
}
TriLED(uint8_t redPin, uint8_t greenPin, uint8_t bluePin, Color _clr):
pr(redPin), pg(greenPin), pb(bluePin), color(_clr)
{
pinMode(pr, OUTPUT);
pinMode(pg, OUTPUT);
pinMode(pb, OUTPUT);
}
// burn `color` into the led (apply the values)
void refresh(){
analogWrite(pr, color.r);
analogWrite(pg, color.g);
analogWrite(pb, color.b);
}
// change and apply the color
void setColor(const Color& clr){
color = clr;
analogWrite(pr, color.r);
analogWrite(pg, color.g);
analogWrite(pb, color.b);
}
void setColor(DigitalColor clr){
color.r = clr.r;
color.g = clr.g;
color.b = clr.b;
digitalWrite(pr, clr.r);
digitalWrite(pg, clr.g);
digitalWrite(pb, clr.b);
}
void setColor()
{ refresh(); }
void set(const Color& clr)
{ setColor(clr); }
void set()
{ refresh(); }
void set(uint8_t val){
color.r = color.g = color.b = val;
refresh();
}
void set(uint8_t r, uint8_t g, uint8_t b){
color.r = r;
color.g = g;
color.b = b;
refresh();
}
void set(DigitalColor clr)
{ setColor(clr); }
// change the color without applying the changes
void push(const Color& clr)
{ color = clr; }
void push(const DigitalColor clr){
color.r = clr.r ? 255 : 0;
color.g = clr.g ? 255 : 0;
color.b = clr.b ? 255 : 0;
}
void push(uint8_t val)
{ color.r = color.g = color.b = val; }
void push(uint8_t r, uint8_t g, uint8_t b){
color.r = r;
color.g = g;
color.b = b;
}
// sets output to zero without clearing the values
void off(){
digitalWrite(pr, 0);
digitalWrite(pg, 0);
digitalWrite(pb, 0);
}
// set values to 0 but don't call refresh()
void setNull()
{ color = Color(0, 0, 0); }
// hardcore OOP users use this :
Color& getColor()
{ return color; }
// uses a scheme string to determine the color pattern.
void colorCycle(const char order[4], uint8_t& curHi, uint8_t incr = 1){
// I don't like this solution, but it works...
// it's better than my 300+ SLOC branching solution.
uint8_t *c1, *c2, *c3;
if (*order == 'r')
c1 = &color.r;
else if (*order == 'g')
c1 = &color.g;
else if (*order == 'b')
c1 = &color.b;
else
c1 = &color.r;
order++; //next char
if (*order == 'r')
c2 = &color.r;
else if (*order == 'g')
c2 = &color.g;
else if (*order == 'b')
c2 = &color.b;
else
c2 = &color.g;
order++; //next char
if (*order == 'r')
c3 = &color.r;
else if (*order == 'g')
c3 = &color.g;
else if (*order == 'b')
c3 = &color.b;
else
c3 = &color.b;
while (incr-- > 0)
color::cycle3(*c1, *c2, *c3, curHi);
/*
// for debugging only (uses too much resources)
Serial.print("r:");
Serial.print(color.r, DEC);
Serial.print(" g:");
Serial.print(color.g, DEC);
Serial.print(" b:");
Serial.print(color.b, DEC);
Serial.print(" CurHi: ");
Serial.println(curHi, DEC);
*/
refresh();
}
// wrapper for color::cycle3
void colorCycle(const char order[4], uint8_t incr = 1){
static uint8_t curHi = 0;
colorCycle(order, curHi, incr);
}
// wrapper for color::cycle3
void inverseColorCycle(uint8_t incr = 1){
static uint8_t curHi = 0;
while (incr-- > 0)
color::cycle3(color.r, color.g, color.b, curHi);
writeInverse();
}
// wrapper for color::cycle3
void colorCycle(uint8_t incr = 1){
static uint8_t curHi = 0;
while (incr-- > 0)
color::cycle3(color.r, color.g, color.b, curHi);
refresh();
}
// wrapper for color::cycle3
void colorCycle(uint8_t& curHi, uint8_t incr = 1){
while (incr-- > 0)
color::cycle3(color.r, color.g, color.b, curHi);
refresh();
}
// sets the analog equivalent of the digital value
void digitalSetColor(const Color& clr){
color.r = clr.r ? 255 : 0;
color.g = clr.g ? 255 : 0;
color.b = clr.b ? 255 : 0;
refresh();
}
void digitalSetColor(DigitalColor clr){
color.r = clr.r ? 255 : 0;
color.g = clr.g ? 255 : 0;
color.b = clr.b ? 255 : 0;
refresh();
}
// uses digitalWrite instead of analogWrite
void digitalRefresh() {
//color.r = color.r ? 255 : 0;
//color.g = color.g ? 255 : 0;
//color.b = color.b ? 255 : 0;
digitalWrite(pr, color.r);
digitalWrite(pg, color.g);
digitalWrite(pb, color.b);
}
// same as in DigitalTriLED
void digitalColorCycle(char order[4]){
char curHi = color.r?
'r' : color.g?
'g' : color.b?
'b': *order;
uint8_t i = 0;
while (curHi != *(order + i) && i <= 4)
i++;
i++;
curHi = (i < 3) ? *(order + i) : *order;
switch (curHi) {
case 'r':
color.r = HIGH;
color.g = LOW;
color.b = LOW;
break;
case 'g':
color.r = LOW;
color.g = HIGH;
color.b = LOW;
break;
case 'b':
color.r = LOW;
color.g = LOW;
color.b = HIGH;
break;
}
digitalRefresh();
}
// same as in DigitalTriLED
void digitalColorCycle(){
char nextHi = color.g?
'b' : color.b?
'r' : 'g';
switch (nextHi) {
case 'r':
color.r = HIGH;
color.g = LOW;
color.b = LOW;
break;
case 'g':
color.r = LOW;
color.g = HIGH;
color.b = LOW;
break;
case 'b':
color.r = LOW;
color.g = LOW;
color.b = HIGH;
break;
}
digitalRefresh();
}
// changes the color to its inverse
void invert(){
color.r = 255 - color.r;
color.g = 255 - color.g;
color.b = 255 - color.b;
refresh();
}
// writes the inverse without modifying the values
void writeInverse(){
analogWrite(pr, 255 - color.r);
analogWrite(pg, 255 - color.g);
analogWrite(pb, 255 - color.b);
}
// this will never get used...
void swapPins(uint8_t redPin, uint8_t bluePin, uint8_t greenPin){
pr = redPin;
pg = greenPin;
pb = bluePin;
// set them as output
pinMode(pr, OUTPUT);
pinMode(pg, OUTPUT);
pinMode(pb, OUTPUT);
refresh();
}
};
// tri-color RGB LED class
class DigitalTriLED {
public:
bool r : 1, g : 1, b : 1;
unsigned int pr : 7, pg : 7, pb : 7;
DigitalTriLED(uint8_t redPin, uint8_t greenPin, uint8_t bluePin):
pr(redPin), pg(greenPin), pb(bluePin), r(LOW), g(LOW), b(LOW)
{
pinMode(pr, OUTPUT);
pinMode(pg, OUTPUT);
pinMode(pb, OUTPUT);
}
DigitalTriLED(uint8_t redPin, uint8_t greenPin, uint8_t bluePin, Color _clr):
pr(redPin), pg(greenPin), pb(bluePin), r(_clr.r), g(_clr.g), b(_clr.b)
{
pinMode(pr, OUTPUT);
pinMode(pg, OUTPUT);
pinMode(pb, OUTPUT);
}
// apply values
void refresh(){
digitalWrite(pr, r);
digitalWrite(pg, g);
digitalWrite(pb, b);
}
// apply a color
void setColor(bool red, bool green, bool blue){
r = red;
g = green;
b = blue;
refresh();
}
void setColor(const Color& clr){
r = clr.r;
g = clr.g;
b = clr.b;
refresh();
}
void setColor(DigitalColor clr){
r = clr.r;
g = clr.g;
b = clr.b;
refresh();
}
void setColor()
{ refresh(); }
void set()
{ refresh(); }
void set(const Color& clr)
{ setColor(clr); }
void set(DigitalColor clr)
{ setColor(clr);}
void set(bool red, bool green, bool blue)
{ return setColor(red, green, blue); }
void set(const bool val){
r = val;
g = val;
b = val;
refresh();
}
// pushes values but doesn't refresh
void push(const Color& clr){
r = clr.r;
g = clr.g;
b = clr.g;
}
void push(DigitalColor clr){
r = clr.r;
g = clr.g;
b = clr.g;
}
// sets output to zero without clearing the values
void off(){
digitalWrite(pr, 0);
digitalWrite(pg, 0);
digitalWrite(pb, 0);
}
// set all values to 0 (no refresh)
void setNull(){
r = LOW;
g = LOW;
b = LOW;
}
void colorCycle(char order[4]){
// determine which pin is on HIGH
char curHi = r ?
'r' : g ?
'g' : b?
'b': *order;
// find fist occurance of current color (this is difficult to achieve)
uint8_t i = 0;
while (curHi != *(order + i) && i <= 4)
i++;
i++; // we want the next color to change to
// prevents errors
curHi = (i < 3) ? *(order + i) : *order;
// set correct color
switch (curHi) {
case 'r':
r = HIGH;
g = LOW;
b = LOW;
break;
case 'g':
r = LOW;
g = HIGH;
b = LOW;
break;
case 'b':
r = LOW;
g = LOW;
b = HIGH;
break;
}
refresh();
}
// follow a scheme string to cycle between color values
void colorCycle(char* order, uint8_t len){
// determine curhi based on which color is currently HIGH
char curHi = r?
'r' : g?
'g' : b?
'b': *order;
// find position in string where we are currrently (very inaccurate)
uint8_t i = 0;
while (curHi != *(order + i) && i < len)
i++;
i++; // we want the color after where we are..
// just to prevent problems
curHi = (i < len) ? *(order + i) : *order;
// set in the correct value
switch (curHi) {
case 'r':
r = HIGH;
g = LOW;
b = LOW;
break;
case 'g':
r = LOW;
g = HIGH;
b = LOW;
break;
case 'b':
r = LOW;
g = LOW;
b = HIGH;
break;
}
refresh();
}
// cycle rgb
void colorCycle(){
// determine nextHi based on which color is HIGH
char nextHi = g ? 'b' : ( b? 'r': 'g');
// set the LED correct color
switch (nextHi) {
case 'r':
r = HIGH;
g = LOW;
b = LOW;
break;
case 'g':
r = LOW;
g = HIGH;
b = LOW;
break;
case 'b':
r = LOW;
g = LOW;
b = HIGH;
break;
}
refresh();
}
// invert current color
void invert(){
r = !r;
g = !g;
b = !b;
}
// this will never get used...
void swapPins(uint8_t redPin, uint8_t bluePin, uint8_t greenPin){
pr = redPin;
pg = greenPin;
pb = bluePin;
// set them as output
pinMode(pr, OUTPUT);
pinMode(pg, OUTPUT);
pinMode(pb, OUTPUT);
refresh();
}
};
// these will no longer get used...
// but they're still useful...
// 2 digital LEDs
class BiLED {
public:
unsigned int p0 : 7, p1 : 7;
bool v0 : 1, v1 : 1; // could be replaced by uint8_t for PWM output
BiLED(uint8_t pin0, uint8_t pin1):
p0(pin0), p1(pin1), v0(0), v1(0)
{
pinMode(p0, OUTPUT);
pinMode(p1, OUTPUT);
}
BiLED(uint8_t pin0, uint8_t pin1, const bool val0, const bool val1):
p0(pin0), p1(pin1), v0(val0), v1(val1)
{
pinMode(p0, OUTPUT);
pinMode(p1, OUTPUT);
}
// burn in values
void refresh(){
digitalWrite(p0,v0);
digitalWrite(p1,v1);
}
// push and apply values
void set(uint8_t val0, uint8_t val1){
v0 = val0;
v1 = val1;
refresh();
}
// push and apply the same value for v0 & v1
void set(uint8_t val){
v0 = val;
v1 = val;
refresh();
}
// sets output to zero without clearing the values
void off(){
digitalWrite(p0, 0);
digitalWrite(p1, 0);
}
// replace v0 and v1
void swap(){
bool temp = v0;
v0 = v1;
v1 = temp;
refresh();
}
// this will never get used...
void swapPins(const uint8_t pin0, const uint8_t pin1){
// note: this doesn't set(0) before swapping the pins.
// set them as output
pinMode(pin0, OUTPUT);
pinMode(pin1, OUTPUT);
p0 = pin0;
p1 = pin1;
// burn in current color
refresh();
}
};
// 2 LEDs with PWM capabilities
class BiLED_pwm {
public:
uint8_t p0, p1; // making this a bitfield wouldn't improve anything
uint8_t v0, v1;
BiLED_pwm(uint8_t pin0, uint8_t pin1):
p0(pin0), p1(pin1), v0(0), v1(0)
{
pinMode(p0, OUTPUT);
pinMode(p1, OUTPUT);
}
BiLED_pwm(uint8_t pin0, uint8_t pin1, uint8_t val0, uint8_t val1):
p0(pin0), p1(pin1), v0(val0), v1(val1)
{
pinMode(p0, OUTPUT);
pinMode(p1, OUTPUT);
}
// burns in values
void refresh(){
analogWrite(p0, v0);
analogWrite(p1, v1);
}
// sets values and applys
void set(uint8_t val0, uint8_t val1){
v0 = val0;
v1 = val1;
refresh();
}
// sets output to zero without clearing the values
void off(){
analogWrite(p0, 0);
analogWrite(p1, 0);
}
// switch v0 and v1
void swap(){
uint8_t temp = v0;
v0 = v1;
v1 = temp;
refresh();
}
// reciprocate between v0 and v1
void seeSaw(uint8_t incr = 1){
static bool curHi = 0;
while (incr-- > 0) {
if (v0 == 0|| v1 == 0)
curHi = !curHi;
if (!curHi) {
v0--; v1++;
} else {
v1--; v0++;
}
}
refresh();
}
// reciprocate between v0 and v1
void seeSaw(bool& curHi, uint8_t incr = 1){
while (incr-- > 0) {
// switch directions
if (v0 == 0 || v1 == 0)
curHi = !curHi;
if (!curHi) {
v0--; v1++;
} else {
v1--; v0++;
}
}
refresh();
}
// this will never get used...
void swapPins(uint8_t pin0, uint8_t pin1){
// note: this doesn't set(0) before swapping the pins.
p0 = pin0;
p1 = pin1;
// set them as output
pinMode(p0, OUTPUT);
pinMode(p1, OUTPUT);
// burn in current color
refresh();
}
};
#endif