-
Notifications
You must be signed in to change notification settings - Fork 0
/
masteraverbaiter.h
170 lines (140 loc) · 3.05 KB
/
masteraverbaiter.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
#ifndef MASTERAVERBAITER__LIB
#define MASTERAVERBAITER__LIB
// COMPONENTS
class Context {
public:
CRGB c;
uint16_t i;
uint32_t t;
};
class Effect {
public:
virtual void Run(Context *c){
}
};
class Composition : public Effect {
public:
Effect ** effects;
Composition(Effect ** e) {
effects = e;
}
void Run(Context *c) {
Context _c;
_c.c = c->c;
_c.i = c->i;
_c.t = c->t;
for(uint8_t e = 0; effects[e] != NULL; e++) {
effects[e]->Run(&_c);
}
c->c = _c.c;
}
};
class ColorWipe : public Effect {
public:
CRGB color;
ColorWipe(CRGB c) {
color = c;
}
void Run(Context *c){
c->c = color;
}
};
class Running : public Effect {
public:
float pixels = 1;
Running(float p) {
pixels = p;
}
void Run(Context *c) {
//Note: Adding makes it run "backwards". Subtraction "forwards"
float fps = 5; //Microseconds per frame (so this is 60fps?) (and it seems that I get hundrendths of a second, not milliseconds)
float _t = c->t;
int16_t _i = c->i;
_i = _i - pixels * _t / fps; // Param1 is lights-per-frame at 60FPS // Also, note that subtracting a positive makes it appear to move forwards, because math.
//Loop
if (_i < 0) {
_i = CAP + _i % CAP; //Remember, _i is negative here
} else if (_i > CAP) {
_i = _i % CAP;
}
c->i = _i;
}
};
class Wheel : public Effect {
public:
void Run(Context *c) {
uint8_t hue = ((float) c->i / NUM_LEDS) * 255;
c->c = CHSV(hue, 255, 255);
}
};
class Pulse : public Effect {
public:
uint16_t pos;
uint16_t inner;
uint16_t width;
CRGB pos_color;
CRGB inner_color;
Pulse(uint16_t p, uint16_t i, uint16_t w, CRGB pc, CRGB ic) {
pos = p;
inner = i;
width = w;
pos_color = pc;
inner_color = ic;
}
void Run(Context *c) {
//TODO: Doesn't wrap!
if (c->i == pos) {
c->c = pos_color;
} else if ((pos - inner) <= c->i && c->i <= (pos + inner)) {
c->c = inner_color;
} else if ((pos - width) <= c->i && c->i <= (pos + width)) {
//In the pulse
fract8 percent = 255 - 255 * abs(pos - c->i) / width;
c->c = blend(c->c, inner_color, percent);
}
}
};
// MODES
Context c;
Effect * compWheel[] = {
new Running(1),
new Wheel(),
NULL,
};
Effect * comp0[] = {
new Running(1),
new Pulse(15, 2, 6, CRGB::Yellow, CRGB::Green),
NULL
};
Effect * comp1[] = {
new Running(1.5),
new Pulse(15, 2, 6, CRGB::Black, CRGB::White),
NULL
};
Effect * comp2[] = {
//new RandomRunning(1,1),
new Running(-1),
new Pulse(15, 8, 10, CRGB::White, CRGB::Black),
NULL
};
Effect * effects[] = {
new ColorWipe(CRGB::Black),
new Composition(compWheel),
new Composition(comp0),
new Composition(comp1),
new Composition(comp2),
NULL
};
void MasterRaverBaiter() {
uint16_t _t = (millis() - _START_TIME);
for (int _i = 0; _i < NUM_LEDS; _i++) {
c.i = _i;
c.t = _t/10;
c.c = _LEDS[_i];
for (uint8_t e = 0; effects[e] != NULL; e++) {
effects[e]->Run(&c);
}
_LEDS[_i] = c.c;
}
}
#endif