Skip to content

Commit 74aff26

Browse files
authored
This should make using the WS2812 examples a bit easier. (#84)
* This should make using the WS2812 examples a bit easier. Moved parameters to the top as defines: - Number of LEDs in your NeoPixel/WS2812 strip/ring - Flag if the LEDs are WRGB or not Changed PIN to 2 as pin 0 is used for serial UART.
1 parent ad51837 commit 74aff26

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

pio/ws2812/ws2812.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
#include "hardware/clocks.h"
1313
#include "ws2812.pio.h"
1414

15+
#define IS_RGBW true
16+
#define NUM_PIXELS 150
17+
#ifndef PICO_DEFAULT_WS2812_PIN
18+
#warning "no WS2812 default PIN defined for board, please check if pin 2 is okay"
19+
#define PICO_DEFAULT_WS2812_PIN 2
20+
#endif
21+
1522
static inline void put_pixel(uint32_t pixel_grb) {
1623
pio_sm_put_blocking(pio0, 0, pixel_grb << 8u);
1724
}
@@ -71,8 +78,6 @@ const struct {
7178
{pattern_greys, "Greys"},
7279
};
7380

74-
const int PIN_TX = 0;
75-
7681
int main() {
7782
//set_sys_clock_48();
7883
stdio_init_all();
@@ -83,7 +88,7 @@ int main() {
8388
int sm = 0;
8489
uint offset = pio_add_program(pio, &ws2812_program);
8590

86-
ws2812_program_init(pio, sm, offset, PIN_TX, 800000, true);
91+
ws2812_program_init(pio, sm, offset, PICO_DEFAULT_WS2812_PIN, 800000, IS_RGBW);
8792

8893
int t = 0;
8994
while (1) {
@@ -92,9 +97,9 @@ int main() {
9297
puts(pattern_table[pat].name);
9398
puts(dir == 1 ? "(forward)" : "(backward)");
9499
for (int i = 0; i < 1000; ++i) {
95-
pattern_table[pat].pat(150, t);
100+
pattern_table[pat].pat(NUM_PIXELS, t);
96101
sleep_ms(10);
97102
t += dir;
98103
}
99104
}
100-
}
105+
}

pio/ws2812/ws2812_parallel.c

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
#include "ws2812.pio.h"
1717

1818
#define FRAC_BITS 4
19-
#define PIN_TX 0
19+
#define NUM_PIXELS 64
20+
#define WS2812_PIN_BASE 2
2021

2122
// horrible temporary hack to avoid changing pattern code
2223
static uint8_t *current_string_out;
@@ -174,17 +175,15 @@ void dither_values(const value_bits_t *colors, value_bits_t *state, const value_
174175
}
175176
}
176177

177-
#define MAX_LENGTH 100
178-
179-
// requested colors * 4 to allow for WRGB
180-
static value_bits_t colors[MAX_LENGTH * 4];
178+
// requested colors * 4 to allow for RGBW
179+
static value_bits_t colors[NUM_PIXELS * 4];
181180
// double buffer the state of the string, since we update next version in parallel with DMAing out old version
182-
static value_bits_t states[2][MAX_LENGTH * 4];
181+
static value_bits_t states[2][NUM_PIXELS * 4];
183182

184183
// example - string 0 is RGB only
185-
static uint8_t string0_data[MAX_LENGTH * 3];
186-
// example - string 1 is WRGB
187-
static uint8_t string1_data[MAX_LENGTH * 4];
184+
static uint8_t string0_data[NUM_PIXELS * 3];
185+
// example - string 1 is RGBW
186+
static uint8_t string1_data[NUM_PIXELS * 4];
188187

189188
string_t string0 = {
190189
.data = string0_data,
@@ -213,7 +212,7 @@ string_t *strings[] = {
213212
#define DMA_CHANNELS_MASK (DMA_CHANNEL_MASK | DMA_CB_CHANNEL_MASK)
214213

215214
// start of each value fragment (+1 for NULL terminator)
216-
static uintptr_t fragment_start[MAX_LENGTH * 4 + 1];
215+
static uintptr_t fragment_start[NUM_PIXELS * 4 + 1];
217216

218217
// posted when it is safe to output a new set of values
219218
static struct semaphore reset_delay_complete_sem;
@@ -286,7 +285,7 @@ int main() {
286285
int sm = 0;
287286
uint offset = pio_add_program(pio, &ws2812_parallel_program);
288287

289-
ws2812_parallel_program_init(pio, sm, offset, PIN_TX, count_of(strings), 800000);
288+
ws2812_parallel_program_init(pio, sm, offset, WS2812_PIN_BASE, count_of(strings), 800000);
290289

291290
sem_init(&reset_delay_complete_sem, 1, 1); // initially posted so we don't block first time
292291
dma_init(pio, sm);
@@ -300,19 +299,17 @@ int main() {
300299
int brightness = 0;
301300
uint current = 0;
302301
for (int i = 0; i < 1000; ++i) {
303-
int n = 64;
304-
305302
current_string_out = string0.data;
306303
current_string_4color = false;
307-
pattern_table[pat].pat(n, t);
304+
pattern_table[pat].pat(NUM_PIXELS, t);
308305
current_string_out = string1.data;
309306
current_string_4color = true;
310-
pattern_table[pat].pat(n, t);
307+
pattern_table[pat].pat(NUM_PIXELS, t);
311308

312-
transform_strings(strings, count_of(strings), colors, n * 4, brightness);
313-
dither_values(colors, states[current], states[current ^ 1], n * 4);
309+
transform_strings(strings, count_of(strings), colors, NUM_PIXELS * 4, brightness);
310+
dither_values(colors, states[current], states[current ^ 1], NUM_PIXELS * 4);
314311
sem_acquire_blocking(&reset_delay_complete_sem);
315-
output_strings_dma(states[current], n * 4);
312+
output_strings_dma(states[current], NUM_PIXELS * 4);
316313

317314
current ^= 1;
318315
t += dir;

0 commit comments

Comments
 (0)