-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatterns.h
305 lines (267 loc) · 7.63 KB
/
patterns.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
#ifndef patterns_h
#define patterns_h
#include "genome_mapper.h"
class Pattern
{
public:
virtual void operator()(GenomeMapper const& gm, unsigned start, unsigned end) { }
protected:
virtual CRGB color_for(char base) const
{
switch (base)
{
case 'a': return CRGB::Red;
case 'c': return CRGB::Yellow;
case 'g': return CRGB::Green;
case 't': return CRGB::Blue;
default : return CRGB::Black;
}
}
char complementary_base(char base)
{
switch (base)
{
case 'a': return 't';
case 't': return 'a';
case 'c': return 'g';
case 'g': return 'c';
default: return 0;
}
}
};
class PalettePattern : public Pattern
{
CRGBPalette16 _palette = RainbowColors_p;
void operator()(GenomeMapper const& gm, unsigned start, unsigned end) override
{
float offset = gm.offset();
for (unsigned i = start; i < end; ++i)
{
leds[i] = ColorFromPalette(_palette, color_index_for(gm.base_at(i), gm.base_at(i + 1), offset), 255, LINEARBLEND);
leds[i + LEDS_PER_PIN] = ColorFromPalette(_palette,
color_index_for(complementary_base(gm.base_at(i)), complementary_base(gm.base_at(i + 1)), offset), 255, LINEARBLEND);
}
}
private:
unsigned color_index_for(char base, char next, float offset)
{
float integer;
return offset_for_base(base) + 32 * modf(offset, &integer);
}
unsigned offset_for_base(char base)
{
switch (base)
{
case 'a': return 0;
case 'c': return 64;
case 'g': return 192;
case 't': return 128;
default: return 0;
}
}
} palette_pattern;
class BasicDiscretePairs : public Pattern
{
void operator()(GenomeMapper const& gm, unsigned start, unsigned end) override
{
for (unsigned i = start; i < end; ++i)
{
leds[i] = color_for(gm.base_at(i));
leds[i + LEDS_PER_PIN] = color_for(complementary_base(gm.base_at(i)));
}
}
} basic_discrete_pairs;
class SlowRainbowPairs : public Pattern
{
protected:
/* Working state. */
byte _hue_base = 0;
bool _increasing = true;
protected:
void operator()(GenomeMapper const& gm, unsigned start, unsigned end) override
{
EVERY_N_MILLISECONDS(250)
{
if (_increasing)
{
if (_hue_base == 63)
{
_increasing = false;
_hue_base = 62;
}
else
++_hue_base;
}
else
{
if (_hue_base == 0)
{
_increasing = true;
_hue_base = 1;
}
else
--_hue_base;
}
}
for (unsigned i = start; i < end; ++i)
{
leds[i] = color_for(gm.base_at(i));
leds[i + LEDS_PER_PIN] = color_for(complementary_base(gm.base_at(i)));
}
}
CRGB color_for(char base) const override
{
switch (base)
{
case 'g': return CHSV(_hue_base, 255, 255);
case 't': return CHSV(_hue_base + 64, 255, 255);
case 'c': return CHSV(_hue_base + 128, 255, 255);
case 'a': return CHSV(_hue_base + 192, 255, 255);
}
}
} slow_rainbow_pairs;
class SlowRainbowWithSparkles : public SlowRainbowPairs
{
void operator()(GenomeMapper const& gm, unsigned start, unsigned end) override
{
EVERY_N_MILLISECONDS(20)
{
SlowRainbowPairs::operator()(gm, start, end);
if (millis() % 30000 > 15000 && millis() % 30000 < 25000)
{
for (unsigned i = 0; i < 2; ++i)
{
unsigned target = random(LEDS_COUNT);
if (target < start || target >= end)
continue;
if (random(2))
continue;
target += random(2) ? LEDS_PER_PIN : 0;
leds[target] = CRGB::White;
}
}
}
}
} slow_rainbow_with_sparkles;
class SlowRainbowWithFastMovingDropout : public SlowRainbowPairs
{
/* Working state. */
unsigned _dropout = 0;
protected:
void operator()(GenomeMapper const& gm, unsigned start, unsigned end) override
{
SlowRainbowPairs::operator()(gm, start, end);
EVERY_N_MILLISECONDS(50)
{
if (_dropout <= 0)
_dropout = LEDS_PER_PIN - 1;
else
--_dropout;
}
if (_dropout < start || _dropout > end - 1)
return;
leds[_dropout] = CRGB::Black;
leds[_dropout + LEDS_PER_PIN] = CRGB::Black;
}
} slow_rainbow_with_fast_moving_dropout;
class SlowRainbowWithEvenFasterMovingDropout : public SlowRainbowWithFastMovingDropout
{
/* Working state. */
unsigned _faster_dropout = 0;
void operator()(GenomeMapper const& gm, unsigned start, unsigned end) override
{
SlowRainbowWithFastMovingDropout::operator()(gm, start, end);
EVERY_N_MILLISECONDS(5)
{
if (_faster_dropout <= 0)
_faster_dropout = LEDS_PER_PIN - 1;
else
--_faster_dropout;
}
if (_faster_dropout < start || _faster_dropout > end - 1)
return;
leds[_faster_dropout] = CRGB::White;
leds[_faster_dropout + LEDS_PER_PIN] = CRGB::White;
}
} slow_rainbow_with_even_faster_moving_dropout;
class SlowRainbowSingleSided : public SlowRainbowPairs
{
/* Working state. */
bool _side;
void operator()(GenomeMapper const& gm, unsigned start, unsigned end) override
{
SlowRainbowPairs::operator()(gm, start, end);
for (unsigned i = start; i < end; ++i)
leds[i + LEDS_PER_PIN] = CRGB::Black;
}
} slow_rainbow_single_sided;
class RainbowBreathe : public SlowRainbowPairs
{
void operator()(GenomeMapper const& gm, unsigned start, unsigned end) override
{
EVERY_N_MILLISECONDS(10)
{
SlowRainbowPairs::operator()(gm, start, end);
}
for (unsigned ctr = start; ctr < end; ++ctr)
{
fadeToBlackBy(leds + ctr, 1, beatsin16(16, 0, 240, 0));
fadeToBlackBy(leds + LEDS_PER_PIN + ctr, 1, beatsin16(16, 0, 240, 0, 32767));
}
}
CRGB color_for(char base) const override
{
switch (base)
{
case 'a': return CHSV(_hue_base + 48, 255, 255);
case 'c': return CHSV(_hue_base + 112, 255, 255);
case 'g': return CHSV(_hue_base + 176, 255, 255);
case 't': return CHSV(_hue_base + 250, 255, 255);
}
}
} rainbow_breathe;
class SingleBasesLit : public SlowRainbowPairs
{
/* Working state. */
char _base_a = 'a';
char _base_b = 'c';
void operator()(GenomeMapper const& gm, unsigned start, unsigned end) override
{
SlowRainbowPairs::operator()(gm, start, end);
for (unsigned i = start; i < end; ++i)
{
if (gm.base_at(i) != _base_a && gm.base_at(i) != _base_b)
leds[i] = CRGB::Black;
if (complementary_base(gm.base_at(i)) != _base_a && complementary_base(gm.base_at(i)) != _base_b)
leds[i + LEDS_PER_PIN] = CRGB::Black;
}
EVERY_N_SECONDS(5)
{
_base_a = next_base();
_base_b = alternate(_base_a);
}
}
private:
char next_base()
{
const char bases[] = { 'a', 'c', 'g', 't' };
return bases[random(4)];
}
char alternate(char base)
{
if (base == 'a' || base == 't')
return random(2) ? 'c' : 'g';
return random(2) ? 'a' : 't';
}
} single_base_lit;
Pattern* patterns[] = {
&basic_discrete_pairs,
&rainbow_breathe,
&slow_rainbow_with_sparkles,
&single_base_lit,
&palette_pattern,
&slow_rainbow_with_even_faster_moving_dropout,
&slow_rainbow_single_sided,
};
unsigned num_patterns = sizeof(patterns) / sizeof(patterns[0]);
#endif