-
Notifications
You must be signed in to change notification settings - Fork 7
/
leds.cpp
88 lines (72 loc) · 1.89 KB
/
leds.cpp
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
#include "assert.h"
#include "leds.h"
namespace pixl {
LEDStrip::LEDStrip(int length)
: length(length)
{
// Initialze one array to hold LED data for all the strips
leds = new CRGB[length];
for (int i = 0; i < length; i++) {
leds[i] = CRGB::Red;
}
}
LEDStrip::~LEDStrip() {
delete[] leds;
}
LEDs::LEDs(LEDStrip* strip, int start, int length, bool reverse) {
LEDStrip* strips[] = {strip};
int starts[] = {start};
int lengths[] = {length};
init(1, strips, starts, lengths, reverse);
}
LEDs::LEDs(int num_strips, LEDStrip* strips[], int* starts, int* lengths,
bool reverse) {
init(num_strips, strips, starts, lengths, reverse);
}
void LEDs::init(int num_strips, LEDStrip* strips[], int* starts, int* lengths,
bool reverse) {
strips_ = new LEDStrip*[num_strips];
starts_ = new int[num_strips];
lengths_ = new int[num_strips];
num_strips_ = num_strips;
length_ = 0;
reverse_ = reverse;
for (int i = 0; i < num_strips; i++) {
LEDStrip* strip = strips[i];
int start = starts[i];
int length = lengths[i];
assert(start >= 0 && start < strip->length);
assert(start + length - 1 < strip->length);
strips_[i] = strip;
starts_[i] = start;
lengths_[i] = length;
length_ += length;
}
}
LEDs::~LEDs() {
delete[] strips_;
delete[] starts_;
delete[] lengths_;
}
CRGB& LEDs::operator[](int index) {
assert(index >= 0 && index < length_);
for (int i = 0; i < num_strips_; i++) {
if (index < lengths_[i]) {
if (reverse_) {
index = starts_[i] + lengths_[i] - 1 - index;
} else {
index = starts_[i] + index;
}
return strips_[i]->leds[index];
}
index -= lengths_[i];
}
}
void LEDs::fillSolid(CRGB color) {
for (int i = 0; i < num_strips_; i++) {
for (int j = starts_[i]; j < lengths_[i]; j++) {
strips_[i]->leds[j] = color;
}
}
}
} // end namespace pixl