-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEffect.cpp
More file actions
304 lines (254 loc) · 5.53 KB
/
Effect.cpp
File metadata and controls
304 lines (254 loc) · 5.53 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
#include "avr/pgmspace.h"
#include "stdlib.h"
#include "Effect.hpp"
#include "messages.hpp"
#include "avr_tlc5940.hpp"
#include "avr_rtc.hpp"
#include "Effect_sets.hpp"
const long vmax = 4095;
uint8_t Effect::get_delay(const uint8_t lut[], size_t len)
{
if (slave_id < len)
{
uint8_t d = pgm_read_byte( &(lut[slave_id]));
return d;
}
return 0xff;
}
Effect::Effect(uint16_t slave)
{
slave_id = slave;
srand(slave);
my_rand = rand();
for (size_t i=0; i<sizeof(noise); i++)
noise[i] = rand();
reset();
};
void Effect::init(uint8_t* buff)
{
uint8_t new_id;
uint32_t new_start_time;
uint32_t new_duration;
messages::decode_start_effect(buff, new_id, new_start_time, new_duration);
if (new_id >= max_effect)
{
reset();
return;
}
// If we are already running this command, don't start over...
if (new_id !=0 && new_id == id && new_start_time == start_time && new_duration == duration)
return;
id = new_id;
start_time = new_start_time;
duration = new_duration;
state = pending;
}
void Effect::start(uint8_t _id, uint32_t _duration)
{
if (_id >= max_effect)
{
reset();
return;
}
id = _id;
start_time = avr_rtc::t_ms;
duration = _duration;
state = pending;
}
void Effect::execute()
{
if (state==stopped)
return;
dt = avr_rtc::t_ms - start_time;
if (dt < 0)
return;
if (dt>duration && state!=stopped)
{
reset();
return;
}
if (state==pending)
state=running;
switch(id)
{
case 0: reset(); break;
case 1: flash_id(); break;
case 2: pulse(); break;
case 3: group_1_on(); break;
case 4: group_2_on(); break;
case 5: group_2_white_sparkle(); break;
case 6: group_3_on(); break;
case 7: group_3_white_sparkle(); break;
default: break;
}
prev_dt = dt;
}
void Effect::all_stop(uint8_t* buff)
{
uint8_t new_id;
uint32_t new_start_time;
uint32_t new_duration;
messages::decode_start_effect(buff, new_id, new_start_time, new_duration);
reset();
}
void set_red(uint16_t v)
{
for (unsigned ch=1; ch<12; ch+=3)
avr_tlc5940::set_channel(ch, v);
}
void set_green(uint16_t v)
{
for (unsigned ch=2; ch<12; ch+=3)
avr_tlc5940::set_channel(ch, v);
}
void set_blue(uint16_t v)
{
for (unsigned ch=0; ch<12; ch+=3)
avr_tlc5940::set_channel(ch, v);
}
void set_rgb(uint16_t r, uint16_t g, uint16_t b)
{
set_red(r);
set_green(g);
set_blue(b);
}
void set_all(uint16_t v)
{
for (unsigned ch=0; ch<14; ch++)
avr_tlc5940::set_channel(ch, v);
}
void Effect::reset()
{
state = stopped;
id = messages::all_stop_id;
start_time = 0;
duration = 0;
set_all(0);
}
/*
A generic flash
_________vmax
/| |\
/ | | \________vmin
/ | | |
______/ | | |
T 0 1 2 3
*/
// rise dwell fall dwell
uint16_t flash(long t1, long t2, long t3, long dt, uint16_t vmax, uint16_t vmin)
{
if (dt < t1)
return vmax * dt / t1;
else if (dt < t2)
return vmax;
else if (dt < t3)
return (vmax * (t3-dt) + vmin*(dt-t2)) / (t3-t2);
return vmin;
}
/* Flash the unit ID as an 8 bit code
id 19 = 00010100
0: __-_____
1: __------
Each bit is 500 ms long, whole code takes 4 seconds
*/
void Effect::flash_id()
{
int n = dt / 500;
int m = dt % 500;
int mask = 1 << n;
int b = slave_id & mask; // really want this to be marcher ID
int v = vmax / 64;
if (m<100)
set_all(0);
else if (m<120)
set_all(v);
else if (b)
set_all(v);
else
set_all(0);
}
/*
All slaves flash all channels on full intensity and fade out over a the duration
rgb:
|\
______| \____
*/
void Effect::pulse()
{
const int vm = vmax / 2;
int v = vm - dt * vm / duration;
if (v<0)
v=0;
set_all(v);
}
/*
Selected slaves quickly turn on all channels 50% and stay on for duration
rgb: _________
/ |
______/ |_____________
T: 0 1 2
*/
void Effect::group_1_on()
{
const uint8_t s=get_delay(section, sizeof(section));
if (!(s & 0x1))
return;
int v = flash(100, duration - 100, 0, dt, vmax/2, 0);
set_all(v);
}
/*
Slaves quickly turn on all channels 50% and stay on for duration
*/
void Effect::group_2_on()
{
const uint8_t s=get_delay(section, sizeof(section));
if (!(s & 0x2))
return;
int v = flash(100, duration - 100, 0, dt, vmax/2, 0);
set_all(v);
}
/*
Slaves sparkle randomly for duration
___________
| |
______| |
T 0 1 2
Cycle length = T2
*/
void Effect::white_sparkle()
{
const long v = vmax/2;
size_t n = 0x1f & (dt>>8);
const long t1 = 128 + 16*(noise[n]>>4); // 128 to 256 ms
const long t2 = t1 + 64 + 4*(0xf & noise[n]); // on for 64..192 ms
long cldt = dt<=0 ? 0 : dt % t2;
if (cldt < t1)
set_all(0);
else if (cldt < t2)
set_all(v);
else
set_all(0);
}
void Effect::group_2_white_sparkle()
{
const uint8_t s=get_delay(section, sizeof(section));
if (s & 0x2)
white_sparkle();
}
/*
Slaves quickly turn on all channels 50% and stay on for duration
*/
void Effect::group_3_on()
{
const uint8_t s=get_delay(section, sizeof(section));
if (!(s & 0x4))
return;
int v = flash(100, duration - 100, 0, dt, vmax/2, 0);
set_all(v);
}
void Effect::group_3_white_sparkle()
{
const uint8_t s=get_delay(section, sizeof(section));
if (s & 0x4)
white_sparkle();
}