-
Notifications
You must be signed in to change notification settings - Fork 0
/
spider_85.cpp
281 lines (207 loc) · 6.91 KB
/
spider_85.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
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
#if defined ARDUINO_attiny || defined ARDUINO_AVR_DIGISPARK
////////////////////////////////////////////////////////////////////////////////
// INCLUDES
////////////////////////////////////////////////////////////////////////////////
#include "spider.h"
#include "coms.h"
#include "progmem.h"
#include "elapsedMillis.h"
////////////////////////////////////////////////////////////////////////////////
// LIST OF COLORS
////////////////////////////////////////////////////////////////////////////////
const color_t color_table[] = {
color_t::white(),
color_t::red(),
color_t::yellow(),
color_t::lime(),
color_t::aqua(),
color_t::blue(),
color_t::fuchsia(),
color_t::orange(),
color_t::gray(),
color_t::silver(),
color_t::maroon(),
color_t::olive(),
color_t::green(),
color_t::teal(),
color_t::navy(),
color_t::purple(),
color_t::black(),
};
////////////////////////////////////////////////////////////////////////////////
// MAIN CONFIGURATION AND DEFAULTS
////////////////////////////////////////////////////////////////////////////////
#define TOTAL 100
#define BRIGHT 0xff
uint16_t offset = 0;
volatile uint16_t pause = 15;
volatile uint8_t mode = 0;
volatile uint8_t direction = 0;
volatile color_t color = color_table[6];
elapsedMillis timeout = 0;
////////////////////////////////////////////////////////////////////////////////
// MAIN OBJECTS
////////////////////////////////////////////////////////////////////////////////
#ifdef ARDUINO_AVR_DIGISPARK
ws2812b strip(2, TOTAL);
coms spi(0, 1, 4, 3);
#else
ws2812b strip(4, TOTAL);
coms spi(0, 1, 2, 3);
#endif
////////////////////////////////////////////////////////////////////////////////
// INTERRUPT HANDLER
////////////////////////////////////////////////////////////////////////////////
ISR(PCINT0_vect) {
pinMode(spi._ss, INPUT);
if (spi.ss()) return;
uint32_b data = spi.process();
// mode = data.byte_2;
//}
// uint8_t xor_0 = data.byte_0 ^ data.byte_1 ^ data.byte_2 ^ 0b10101010;
// if (xor_0 != data.byte_3) return;
switch (data.byte_2) {
case 0x01: mode = data.word_0; break;
case 0x02: pause = data.word_0; break;
case 0x03: direction = !!data.word_0; break;
case 0x04: offset = data.word_0; break;
case 0x05:
color = color_table[data.word_0 & 0x0F];
break;
case 0x06:
color = color_t(data.word_0);
break;
}
}
////////////////////////////////////////////////////////////////////////////////
// MAIN SETUP ROUTINE
////////////////////////////////////////////////////////////////////////////////
void setup_avr() {
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
strip.clear();
spi.enable();
timeout = 0;
}
////////////////////////////////////////////////////////////////////////////////
// rainbow equally distributed throughout
// TODO: A SECOND VERSION OF THIS WHERE THE RAINBOW REPEATS X NUMBER OF TIMES IN A SINGLE STRIP
////////////////////////////////////////////////////////////////////////////////
void rainbowCycle() {
uint16_t segments = (256*3) / strip.total();
for(uint16_t i=0; i<strip.total(); i++) {
strip.pixel(
color_t::hue((i * segments) + offset)
);
}
}
////////////////////////////////////////////////////////////////////////////////
// ??
////////////////////////////////////////////////////////////////////////////////
void wave(uint8_t r, uint8_t g, uint8_t b) {
r = r ? 0xff : 0x00;
g = g ? 0xff : 0x00;
b = b ? 0xff : 0x00;
for (uint16_t i=0; i<strip.total(); i++) {
switch ((i+offset) & 0x000F) {
case 0x00: strip.pixel((BRIGHT>>7)&r, (BRIGHT>>7)&g, (BRIGHT>>7)&b); break;
case 0x01: strip.pixel((BRIGHT>>6)&r, (BRIGHT>>6)&g, (BRIGHT>>6)&b); break;
case 0x02: strip.pixel((BRIGHT>>5)&r, (BRIGHT>>5)&g, (BRIGHT>>5)&b); break;
case 0x03: strip.pixel((BRIGHT>>4)&r, (BRIGHT>>4)&g, (BRIGHT>>4)&b); break;
case 0x04: strip.pixel((BRIGHT>>3)&r, (BRIGHT>>3)&g, (BRIGHT>>3)&b); break;
case 0x05: strip.pixel((BRIGHT>>2)&r, (BRIGHT>>2)&g, (BRIGHT>>2)&b); break;
case 0x06: strip.pixel((BRIGHT>>1)&r, (BRIGHT>>1)&g, (BRIGHT>>1)&b); break;
case 0x07: strip.pixel((BRIGHT>>0)&r, (BRIGHT>>0)&g, (BRIGHT>>0)&b); break;
case 0x08: strip.pixel((BRIGHT>>1)&r, (BRIGHT>>1)&g, (BRIGHT>>1)&b); break;
case 0x09: strip.pixel((BRIGHT>>2)&r, (BRIGHT>>2)&g, (BRIGHT>>2)&b); break;
case 0x0A: strip.pixel((BRIGHT>>3)&r, (BRIGHT>>3)&g, (BRIGHT>>3)&b); break;
case 0x0B: strip.pixel((BRIGHT>>4)&r, (BRIGHT>>4)&g, (BRIGHT>>4)&b); break;
case 0x0C: strip.pixel((BRIGHT>>5)&r, (BRIGHT>>5)&g, (BRIGHT>>5)&b); break;
case 0x0D: strip.pixel((BRIGHT>>6)&r, (BRIGHT>>6)&g, (BRIGHT>>6)&b); break;
case 0x0E: strip.pixel((BRIGHT>>7)&r, (BRIGHT>>7)&g, (BRIGHT>>7)&b); break;
case 0x0F: strip.pixel(0, 0, 0); break;
}
}
}
////////////////////////////////////////////////////////////////////////////////
// ??
////////////////////////////////////////////////////////////////////////////////
void rgbwcmyk() {
for (uint16_t i=0; i<strip.total(); i++) {
switch ((i+offset) & 0x0007) {
case 0: strip.pixel(BRIGHT, 0, 0); break;
case 1: strip.pixel(0, BRIGHT, 0); break;
case 2: strip.pixel(0, 0, BRIGHT); break;
case 3: strip.pixel(BRIGHT>>1, BRIGHT>>1, BRIGHT>>1); break;
case 4: strip.pixel(0, BRIGHT, BRIGHT); break;
case 5: strip.pixel(BRIGHT, 0, BRIGHT); break;
case 6: strip.pixel(BRIGHT, BRIGHT, 0); break;
case 7: strip.pixel(0, 0, 0); break;
}
}
}
////////////////////////////////////////////////////////////////////////////////
// ??
////////////////////////////////////////////////////////////////////////////////
void chase() {
uint16_t total = strip.total() << 2;
color_t cx;
for(uint16_t i=0; i<total; i+=4) {
cx = (color_t) color;
switch ((i+offset) & 0x1f) {
case 0x00:
case 0x0c:
strip.pixel(cx.right(6));
break;
case 0x01:
case 0x0b:
strip.pixel(cx.right(5));
break;
case 0x02:
case 0x0a:
strip.pixel(cx.right(4));
break;
case 0x03:
case 0x09:
strip.pixel(cx.right(3));
break;
case 0x04:
case 0x08:
strip.pixel(cx.right(2));
break;
case 0x05:
case 0x07:
strip.pixel(cx.right(1));
break;
case 0x06:
strip.pixel(cx);
break;
default:
strip.pixel(color_t::black());
}
}
}
////////////////////////////////////////////////////////////////////////////////
// ??
////////////////////////////////////////////////////////////////////////////////
void loop_avr() {
direction ? offset++ : offset--;
strip.begin();
switch (mode) {
case 0x00: strip.clear(); break;
case 0x01: rgbwcmyk(); break;
case 0x02: rainbowCycle(); break;
case 0x03: chase(); break;
case 0x10: wave(1, 0, 0); break;
case 0x11: wave(0, 1, 0); break;
case 0x12: wave(0, 0, 1); break;
case 0x13: wave(0, 1, 1); break;
case 0x14: wave(1, 0, 1); break;
case 0x15: wave(1, 1, 0); break;
case 0x16: wave(1, 1, 1); break;
case 0xFF: wave(1, 1, 1); break;
}
strip.end();
while (timeout < pause) {}
timeout -= pause;
}
#endif //ARDUINO_attiny || ARDUINO_AVR_DIGISPARK