Newer
Older
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
/*
* Fadecandy Firmware
*
* Copyright (c) 2013 Micah Elizabeth Scott
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <math.h>
#include "OctoWS2811z.h"
#include "fc_usb.h"
#include "fc_defs.h"
// USB data buffers
static fcBuffers buffers;
// Double-buffered DMA memory for raw bit planes of output
static DMAMEM int ledBuffer[LEDS_PER_STRIP * 12];
static OctoWS2811z leds(LEDS_PER_STRIP, ledBuffer, WS2811_800kHz);
// Residuals for temporal dithering
static int8_t residual[CHANNELS_TOTAL];
static uint32_t updatePixel(unsigned n)
{
const uint8_t *pixel = buffers.fbNext->pixel(n);
return (pixel[1] << 16) | (pixel[0] << 8) | pixel[2];
}
static void updateDrawBuffer()
{
/*
* Update the LED draw buffer. In one step, we do the interpolation,
* gamma correction, dithering, and we convert packed-pixel data to the
* planar format used for OctoWS2811 DMAs.
*/
// For each pixel, this is a 24-byte stream of bits (6 words)
uint32_t *out = (uint32_t*) leds.getDrawBuffer();
for (int i = 0; i < LEDS_PER_STRIP; ++i) {
uint32_t plane0 = updatePixel(i + LEDS_PER_STRIP * 0);
uint32_t plane1 = updatePixel(i + LEDS_PER_STRIP * 1);
uint32_t plane2 = updatePixel(i + LEDS_PER_STRIP * 2);
uint32_t plane3 = updatePixel(i + LEDS_PER_STRIP * 3);
uint32_t plane4 = updatePixel(i + LEDS_PER_STRIP * 4);
uint32_t plane5 = updatePixel(i + LEDS_PER_STRIP * 5);
uint32_t plane6 = updatePixel(i + LEDS_PER_STRIP * 6);
uint32_t plane7 = updatePixel(i + LEDS_PER_STRIP * 7);
*(out++) = ( (plane0 >> 23) & (1 << 0 ) ) | // Bit 23
( (plane1 >> 22) & (1 << 1 ) ) |
( (plane2 >> 21) & (1 << 2 ) ) |
( (plane3 >> 20) & (1 << 3 ) ) |
( (plane4 >> 19) & (1 << 4 ) ) |
( (plane5 >> 18) & (1 << 5 ) ) |
( (plane6 >> 17) & (1 << 6 ) ) |
( (plane7 >> 16) & (1 << 7 ) ) |
( (plane0 >> 14) & (1 << 8 ) ) | // Bit 22
( (plane1 >> 13) & (1 << 9 ) ) |
( (plane2 >> 12) & (1 << 10) ) |
( (plane3 >> 11) & (1 << 11) ) |
( (plane4 >> 10) & (1 << 12) ) |
( (plane5 >> 9 ) & (1 << 13) ) |
( (plane6 >> 8 ) & (1 << 14) ) |
( (plane7 >> 7 ) & (1 << 15) ) |
( (plane0 >> 5 ) & (1 << 16) ) | // Bit 21
( (plane1 >> 4 ) & (1 << 17) ) |
( (plane2 >> 3 ) & (1 << 18) ) |
( (plane3 >> 2 ) & (1 << 19) ) |
( (plane4 >> 1 ) & (1 << 20) ) |
( (plane5 ) & (1 << 21) ) |
( (plane6 << 1 ) & (1 << 22) ) |
( (plane7 << 2 ) & (1 << 23) ) |
( (plane0 << 4 ) & (1 << 24) ) | // Bit 20
( (plane1 << 5 ) & (1 << 25) ) |
( (plane2 << 6 ) & (1 << 26) ) |
( (plane3 << 7 ) & (1 << 27) ) |
( (plane4 << 8 ) & (1 << 28) ) |
( (plane5 << 9 ) & (1 << 29) ) |
( (plane6 << 10) & (1 << 30) ) |
( (plane7 << 11) & (1 << 31) ) ;
*(out++) = ( (plane0 >> 19) & (1 << 0 ) ) | // Bit 19
( (plane1 >> 18) & (1 << 1 ) ) |
( (plane2 >> 17) & (1 << 2 ) ) |
( (plane3 >> 16) & (1 << 3 ) ) |
( (plane4 >> 15) & (1 << 4 ) ) |
( (plane5 >> 14) & (1 << 5 ) ) |
( (plane6 >> 13) & (1 << 6 ) ) |
( (plane7 >> 12) & (1 << 7 ) ) |
( (plane0 >> 10) & (1 << 8 ) ) | // Bit 18
( (plane1 >> 9 ) & (1 << 9 ) ) |
( (plane2 >> 8 ) & (1 << 10) ) |
( (plane3 >> 7 ) & (1 << 11) ) |
( (plane4 >> 6 ) & (1 << 12) ) |
( (plane5 >> 5 ) & (1 << 13) ) |
( (plane6 >> 4 ) & (1 << 14) ) |
( (plane7 >> 3 ) & (1 << 15) ) |
( (plane0 >> 1 ) & (1 << 16) ) | // Bit 17
( (plane1 ) & (1 << 17) ) |
( (plane2 << 1 ) & (1 << 18) ) |
( (plane3 << 2 ) & (1 << 19) ) |
( (plane4 << 3 ) & (1 << 20) ) |
( (plane5 << 4 ) & (1 << 21) ) |
( (plane6 << 5 ) & (1 << 22) ) |
( (plane7 << 6 ) & (1 << 23) ) |
( (plane0 << 8 ) & (1 << 24) ) | // Bit 16
( (plane1 << 9 ) & (1 << 25) ) |
( (plane2 << 10) & (1 << 26) ) |
( (plane3 << 11) & (1 << 27) ) |
( (plane4 << 12) & (1 << 28) ) |
( (plane5 << 13) & (1 << 29) ) |
( (plane6 << 14) & (1 << 30) ) |
( (plane7 << 15) & (1 << 31) ) ;
*(out++) = ( (plane0 >> 15) & (1 << 0 ) ) | // Bit 15
( (plane1 >> 14) & (1 << 1 ) ) |
( (plane2 >> 13) & (1 << 2 ) ) |
( (plane3 >> 12) & (1 << 3 ) ) |
( (plane4 >> 11) & (1 << 4 ) ) |
( (plane5 >> 10) & (1 << 5 ) ) |
( (plane6 >> 9 ) & (1 << 6 ) ) |
( (plane7 >> 8 ) & (1 << 7 ) ) |
( (plane0 >> 6 ) & (1 << 8 ) ) | // Bit 14
( (plane1 >> 5 ) & (1 << 9 ) ) |
( (plane2 >> 4 ) & (1 << 10) ) |
( (plane3 >> 3 ) & (1 << 11) ) |
( (plane4 >> 2 ) & (1 << 12) ) |
( (plane5 >> 1 ) & (1 << 13) ) |
( (plane6 ) & (1 << 14) ) |
( (plane7 << 1 ) & (1 << 15) ) |
( (plane0 << 3 ) & (1 << 16) ) | // Bit 13
( (plane1 << 4 ) & (1 << 17) ) |
( (plane2 << 5 ) & (1 << 18) ) |
( (plane3 << 6 ) & (1 << 19) ) |
( (plane4 << 7 ) & (1 << 20) ) |
( (plane5 << 8 ) & (1 << 21) ) |
( (plane6 << 9 ) & (1 << 22) ) |
( (plane7 << 10) & (1 << 23) ) |
( (plane0 << 12) & (1 << 24) ) | // Bit 12
( (plane1 << 13) & (1 << 25) ) |
( (plane2 << 14) & (1 << 26) ) |
( (plane3 << 15) & (1 << 27) ) |
( (plane4 << 16) & (1 << 28) ) |
( (plane5 << 17) & (1 << 29) ) |
( (plane6 << 18) & (1 << 30) ) |
( (plane7 << 19) & (1 << 31) ) ;
*(out++) = ( (plane0 >> 11) & (1 << 0 ) ) | // Bit 11
( (plane1 >> 10) & (1 << 1 ) ) |
( (plane2 >> 9 ) & (1 << 2 ) ) |
( (plane3 >> 8 ) & (1 << 3 ) ) |
( (plane4 >> 7 ) & (1 << 4 ) ) |
( (plane5 >> 6 ) & (1 << 5 ) ) |
( (plane6 >> 5 ) & (1 << 6 ) ) |
( (plane7 >> 4 ) & (1 << 7 ) ) |
( (plane0 >> 2 ) & (1 << 8 ) ) | // Bit 10
( (plane1 >> 1 ) & (1 << 9 ) ) |
( (plane2 ) & (1 << 10) ) |
( (plane3 << 1 ) & (1 << 11) ) |
( (plane4 << 2 ) & (1 << 12) ) |
( (plane5 << 3 ) & (1 << 13) ) |
( (plane6 << 4 ) & (1 << 14) ) |
( (plane7 << 5 ) & (1 << 15) ) |
( (plane0 << 7 ) & (1 << 16) ) | // Bit 9
( (plane1 << 8 ) & (1 << 17) ) |
( (plane2 << 9 ) & (1 << 18) ) |
( (plane3 << 10) & (1 << 19) ) |
( (plane4 << 11) & (1 << 20) ) |
( (plane5 << 12) & (1 << 21) ) |
( (plane6 << 13) & (1 << 22) ) |
( (plane7 << 14) & (1 << 23) ) |
( (plane0 << 16) & (1 << 24) ) | // Bit 8
( (plane1 << 17) & (1 << 25) ) |
( (plane2 << 18) & (1 << 26) ) |
( (plane3 << 19) & (1 << 27) ) |
( (plane4 << 20) & (1 << 28) ) |
( (plane5 << 21) & (1 << 29) ) |
( (plane6 << 22) & (1 << 30) ) |
( (plane7 << 23) & (1 << 31) ) ;
*(out++) = ( (plane0 >> 7 ) & (1 << 0 ) ) | // Bit 7
( (plane1 >> 6 ) & (1 << 1 ) ) |
( (plane2 >> 5 ) & (1 << 2 ) ) |
( (plane3 >> 4 ) & (1 << 3 ) ) |
( (plane4 >> 3 ) & (1 << 4 ) ) |
( (plane5 >> 2 ) & (1 << 5 ) ) |
( (plane6 >> 1 ) & (1 << 6 ) ) |
( (plane7 ) & (1 << 7 ) ) |
( (plane0 << 2 ) & (1 << 8 ) ) | // Bit 6
( (plane1 << 3 ) & (1 << 9 ) ) |
( (plane2 << 4 ) & (1 << 10) ) |
( (plane3 << 5 ) & (1 << 11) ) |
( (plane4 << 6 ) & (1 << 12) ) |
( (plane5 << 7 ) & (1 << 13) ) |
( (plane6 << 8 ) & (1 << 14) ) |
( (plane7 << 9 ) & (1 << 15) ) |
( (plane0 << 11) & (1 << 16) ) | // Bit 5
( (plane1 << 12) & (1 << 17) ) |
( (plane2 << 13) & (1 << 18) ) |
( (plane3 << 14) & (1 << 19) ) |
( (plane4 << 15) & (1 << 20) ) |
( (plane5 << 16) & (1 << 21) ) |
( (plane6 << 17) & (1 << 22) ) |
( (plane7 << 18) & (1 << 23) ) |
( (plane0 << 20) & (1 << 24) ) | // Bit 4
( (plane1 << 21) & (1 << 25) ) |
( (plane2 << 22) & (1 << 26) ) |
( (plane3 << 23) & (1 << 27) ) |
( (plane4 << 24) & (1 << 28) ) |
( (plane5 << 25) & (1 << 29) ) |
( (plane6 << 26) & (1 << 30) ) |
( (plane7 << 27) & (1 << 31) ) ;
*(out++) = ( (plane0 >> 3 ) & (1 << 0 ) ) | // Bit 3
( (plane1 >> 2 ) & (1 << 1 ) ) |
( (plane2 >> 1 ) & (1 << 2 ) ) |
( (plane3 ) & (1 << 3 ) ) |
( (plane4 << 1 ) & (1 << 4 ) ) |
( (plane5 << 2 ) & (1 << 5 ) ) |
( (plane6 << 3 ) & (1 << 6 ) ) |
( (plane7 << 4 ) & (1 << 7 ) ) |
( (plane0 << 6 ) & (1 << 8 ) ) | // Bit 2
( (plane1 << 7 ) & (1 << 9 ) ) |
( (plane2 << 8 ) & (1 << 10) ) |
( (plane3 << 9 ) & (1 << 11) ) |
( (plane4 << 10) & (1 << 12) ) |
( (plane5 << 11) & (1 << 13) ) |
( (plane6 << 12) & (1 << 14) ) |
( (plane7 << 13) & (1 << 15) ) |
( (plane0 << 15) & (1 << 16) ) | // Bit 1
( (plane1 << 16) & (1 << 17) ) |
( (plane2 << 17) & (1 << 18) ) |
( (plane3 << 18) & (1 << 19) ) |
( (plane4 << 19) & (1 << 20) ) |
( (plane5 << 20) & (1 << 21) ) |
( (plane6 << 21) & (1 << 22) ) |
( (plane7 << 22) & (1 << 23) ) |
( (plane0 << 24) & (1 << 24) ) | // Bit 0
( (plane1 << 25) & (1 << 25) ) |
( (plane2 << 26) & (1 << 26) ) |
( (plane3 << 27) & (1 << 27) ) |
( (plane4 << 28) & (1 << 28) ) |
( (plane5 << 29) & (1 << 29) ) |
( (plane6 << 30) & (1 << 30) ) |
( (plane7 << 31) & (1 << 31) ) ;
}
}
extern "C" int main()
{
leds.begin();
while (1) {
buffers.handleUSB();
updateDrawBuffer();
leds.show();
}
}