2020#define WS2812_PIN_BASE 2
2121
2222// 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 ;
2525
2626static 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?
3232 }
3333}
3434
@@ -83,9 +83,9 @@ void pattern_solid(uint len, uint t) {
8383 }
8484}
8585
86- int level = 8 ;
8786
8887void pattern_fade (uint len , uint t ) {
88+ const int level = 8 ;
8989 uint shift = 4 ;
9090
9191 uint max = 16 ; // let's not draw too much current!
@@ -95,7 +95,7 @@ void pattern_fade(uint len, uint t) {
9595 slow_t = level ;
9696 slow_t %= max ;
9797
98- static int error ;
98+ static int error = 0 ;
9999 slow_t += error ;
100100 error = slow_t & ((1u << shift ) - 1 );
101101 slow_t >>= shift ;
@@ -121,7 +121,7 @@ const struct {
121121
122122#define VALUE_PLANE_COUNT (8 + FRAC_BITS)
123123// 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 .
125125typedef struct {
126126 // stored MSB first
127127 uint32_t planes [VALUE_PLANE_COUNT ];
@@ -149,17 +149,17 @@ typedef struct {
149149 uint8_t * data ;
150150 uint data_len ;
151151 uint frac_brightness ; // 256 = *1.0;
152- } string_t ;
152+ } strip_t ;
153153
154154// 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 ,
156156 uint frac_brightness ) {
157157 for (uint v = 0 ; v < value_length ; v ++ ) {
158158 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 ) {
161161 // 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 ;
163163 value = (value * frac_brightness ) >> 8u ;
164164 for (int j = 0 ; j < VALUE_PLANE_COUNT && value ; j ++ , value >>= 1u ) {
165165 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_
177177
178178// requested colors * 4 to allow for RGBW
179179static 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
181181static value_bits_t states [2 ][NUM_PIXELS * 4 ];
182182
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 ];
187187
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 ),
191191 .frac_brightness = 0x40 ,
192192};
193193
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 ),
197197 .frac_brightness = 0x100 ,
198198};
199199
200- string_t * strings [] = {
201- & string0 ,
202- & string1 ,
200+ strip_t * strips [] = {
201+ & strip0 ,
202+ & strip1 ,
203203};
204204
205205// bit plane content dma channel
@@ -266,7 +266,7 @@ void dma_init(PIO pio, uint sm) {
266266 irq_set_enabled (DMA_IRQ_0 , true);
267267}
268268
269- void output_strings_dma (value_bits_t * bits , uint value_length ) {
269+ void output_strips_dma (value_bits_t * bits , uint value_length ) {
270270 for (uint i = 0 ; i < value_length ; i ++ ) {
271271 fragment_start [i ] = (uintptr_t ) bits [i ].planes ; // MSB first
272272 }
@@ -285,7 +285,7 @@ int main() {
285285 int sm = 0 ;
286286 uint offset = pio_add_program (pio , & ws2812_parallel_program );
287287
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 );
289289
290290 sem_init (& reset_delay_complete_sem , 1 , 1 ); // initially posted so we don't block first time
291291 dma_init (pio , sm );
@@ -299,17 +299,17 @@ int main() {
299299 int brightness = 0 ;
300300 uint current = 0 ;
301301 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;
304304 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;
307307 pattern_table [pat ].pat (NUM_PIXELS , t );
308308
309- transform_strings ( strings , count_of (strings ), colors , NUM_PIXELS * 4 , brightness );
309+ transform_strips ( strips , count_of (strips ), colors , NUM_PIXELS * 4 , brightness );
310310 dither_values (colors , states [current ], states [current ^ 1 ], NUM_PIXELS * 4 );
311311 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 );
313313
314314 current ^= 1 ;
315315 t += dir ;
0 commit comments