20
20
#define WS2812_PIN_BASE 2
21
21
22
22
// horrible temporary hack to avoid changing pattern code
23
- static uint8_t * current_string_out ;
24
- static bool current_string_4color ;
23
+ static uint8_t * current_strip_out ;
24
+ static bool current_strip_4color ;
25
25
26
26
static inline void put_pixel (uint32_t pixel_grb ) {
27
- * current_string_out ++ = pixel_grb & 0xffu ;
28
- * current_string_out ++ = (pixel_grb >> 8u ) & 0xffu ;
29
- * current_string_out ++ = (pixel_grb >> 16u ) & 0xffu ;
30
- if (current_string_4color ) {
31
- * current_string_out ++ = 0 ; // todo adjust?
27
+ * current_strip_out ++ = pixel_grb & 0xffu ;
28
+ * current_strip_out ++ = (pixel_grb >> 8u ) & 0xffu ;
29
+ * current_strip_out ++ = (pixel_grb >> 16u ) & 0xffu ;
30
+ if (current_strip_4color ) {
31
+ * current_strip_out ++ = 0 ; // todo adjust?
32
32
}
33
33
}
34
34
@@ -83,9 +83,9 @@ void pattern_solid(uint len, uint t) {
83
83
}
84
84
}
85
85
86
- int level = 8 ;
87
86
88
87
void pattern_fade (uint len , uint t ) {
88
+ const int level = 8 ;
89
89
uint shift = 4 ;
90
90
91
91
uint max = 16 ; // let's not draw too much current!
@@ -95,7 +95,7 @@ void pattern_fade(uint len, uint t) {
95
95
slow_t = level ;
96
96
slow_t %= max ;
97
97
98
- static int error ;
98
+ static int error = 0 ;
99
99
slow_t += error ;
100
100
error = slow_t & ((1u << shift ) - 1 );
101
101
slow_t >>= shift ;
@@ -121,7 +121,7 @@ const struct {
121
121
122
122
#define VALUE_PLANE_COUNT (8 + FRAC_BITS)
123
123
// we store value (8 bits + fractional bits of a single color (R/G/B/W) value) for multiple
124
- // strings , in bit planes. bit plane N has the Nth bit of each string .
124
+ // strips of pixels , in bit planes. bit plane N has the Nth bit of each strip of pixels .
125
125
typedef struct {
126
126
// stored MSB first
127
127
uint32_t planes [VALUE_PLANE_COUNT ];
@@ -149,17 +149,17 @@ typedef struct {
149
149
uint8_t * data ;
150
150
uint data_len ;
151
151
uint frac_brightness ; // 256 = *1.0;
152
- } string_t ;
152
+ } strip_t ;
153
153
154
154
// takes 8 bit color values, multiply by brightness and store in bit planes
155
- void transform_strings ( string_t * * strings , uint num_strings , value_bits_t * values , uint value_length ,
155
+ void transform_strips ( strip_t * * strips , uint num_strips , value_bits_t * values , uint value_length ,
156
156
uint frac_brightness ) {
157
157
for (uint v = 0 ; v < value_length ; v ++ ) {
158
158
memset (& values [v ], 0 , sizeof (values [v ]));
159
- for (int i = 0 ; i < num_strings ; i ++ ) {
160
- if (v < strings [i ]-> data_len ) {
159
+ for (int i = 0 ; i < num_strips ; i ++ ) {
160
+ if (v < strips [i ]-> data_len ) {
161
161
// todo clamp?
162
- uint32_t value = (strings [i ]-> data [v ] * strings [i ]-> frac_brightness ) >> 8u ;
162
+ uint32_t value = (strips [i ]-> data [v ] * strips [i ]-> frac_brightness ) >> 8u ;
163
163
value = (value * frac_brightness ) >> 8u ;
164
164
for (int j = 0 ; j < VALUE_PLANE_COUNT && value ; j ++ , value >>= 1u ) {
165
165
if (value & 1u ) values [v ].planes [VALUE_PLANE_COUNT - 1 - j ] |= 1u << i ;
@@ -177,29 +177,29 @@ void dither_values(const value_bits_t *colors, value_bits_t *state, const value_
177
177
178
178
// requested colors * 4 to allow for RGBW
179
179
static value_bits_t colors [NUM_PIXELS * 4 ];
180
- // double buffer the state of the string , since we update next version in parallel with DMAing out old version
180
+ // double buffer the state of the pixel strip , since we update next version in parallel with DMAing out old version
181
181
static value_bits_t states [2 ][NUM_PIXELS * 4 ];
182
182
183
- // example - string 0 is RGB only
184
- static uint8_t string0_data [NUM_PIXELS * 3 ];
185
- // example - string 1 is RGBW
186
- static uint8_t string1_data [NUM_PIXELS * 4 ];
183
+ // example - strip 0 is RGB only
184
+ static uint8_t strip0_data [NUM_PIXELS * 3 ];
185
+ // example - strip 1 is RGBW
186
+ static uint8_t strip1_data [NUM_PIXELS * 4 ];
187
187
188
- string_t string0 = {
189
- .data = string0_data ,
190
- .data_len = sizeof (string0_data ),
188
+ strip_t strip0 = {
189
+ .data = strip0_data ,
190
+ .data_len = sizeof (strip0_data ),
191
191
.frac_brightness = 0x40 ,
192
192
};
193
193
194
- string_t string1 = {
195
- .data = string1_data ,
196
- .data_len = sizeof (string1_data ),
194
+ strip_t strip1 = {
195
+ .data = strip1_data ,
196
+ .data_len = sizeof (strip1_data ),
197
197
.frac_brightness = 0x100 ,
198
198
};
199
199
200
- string_t * strings [] = {
201
- & string0 ,
202
- & string1 ,
200
+ strip_t * strips [] = {
201
+ & strip0 ,
202
+ & strip1 ,
203
203
};
204
204
205
205
// bit plane content dma channel
@@ -266,7 +266,7 @@ void dma_init(PIO pio, uint sm) {
266
266
irq_set_enabled (DMA_IRQ_0 , true);
267
267
}
268
268
269
- void output_strings_dma (value_bits_t * bits , uint value_length ) {
269
+ void output_strips_dma (value_bits_t * bits , uint value_length ) {
270
270
for (uint i = 0 ; i < value_length ; i ++ ) {
271
271
fragment_start [i ] = (uintptr_t ) bits [i ].planes ; // MSB first
272
272
}
@@ -285,7 +285,7 @@ int main() {
285
285
int sm = 0 ;
286
286
uint offset = pio_add_program (pio , & ws2812_parallel_program );
287
287
288
- ws2812_parallel_program_init (pio , sm , offset , WS2812_PIN_BASE , count_of (strings ), 800000 );
288
+ ws2812_parallel_program_init (pio , sm , offset , WS2812_PIN_BASE , count_of (strips ), 800000 );
289
289
290
290
sem_init (& reset_delay_complete_sem , 1 , 1 ); // initially posted so we don't block first time
291
291
dma_init (pio , sm );
@@ -299,17 +299,17 @@ int main() {
299
299
int brightness = 0 ;
300
300
uint current = 0 ;
301
301
for (int i = 0 ; i < 1000 ; ++ i ) {
302
- current_string_out = string0 .data ;
303
- current_string_4color = false;
302
+ current_strip_out = strip0 .data ;
303
+ current_strip_4color = false;
304
304
pattern_table [pat ].pat (NUM_PIXELS , t );
305
- current_string_out = string1 .data ;
306
- current_string_4color = true;
305
+ current_strip_out = strip1 .data ;
306
+ current_strip_4color = true;
307
307
pattern_table [pat ].pat (NUM_PIXELS , t );
308
308
309
- transform_strings ( strings , count_of (strings ), colors , NUM_PIXELS * 4 , brightness );
309
+ transform_strips ( strips , count_of (strips ), colors , NUM_PIXELS * 4 , brightness );
310
310
dither_values (colors , states [current ], states [current ^ 1 ], NUM_PIXELS * 4 );
311
311
sem_acquire_blocking (& reset_delay_complete_sem );
312
- output_strings_dma (states [current ], NUM_PIXELS * 4 );
312
+ output_strips_dma (states [current ], NUM_PIXELS * 4 );
313
313
314
314
current ^= 1 ;
315
315
t += dir ;
0 commit comments