From ba6556bec0920957b89ef4c69decb9b15d81a6f0 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sun, 9 Jul 2023 22:01:14 +0200 Subject: Handle multiple pixel strands --- pixelhub.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 63 insertions(+), 23 deletions(-) diff --git a/pixelhub.cpp b/pixelhub.cpp index f876e08..506e1a1 100644 --- a/pixelhub.cpp +++ b/pixelhub.cpp @@ -25,63 +25,103 @@ #include const int dmx_rx_pin = 16; -const int pixels_pin = 2; -const int num_pixels = 8; -const PIO led_pio = pio0; -uint led_pio_sm; + +struct pixel_strip +{ + int pin; + int num_pixels; + PIO pio; + uint pio_sm; + uint offset; + uint32_t pixelbuf[256]; + int rgbw; +}; -static inline void put_pixel(uint32_t pixel_grb) { - pio_sm_put_blocking(led_pio, led_pio_sm, pixel_grb << 8u); +static inline void put_pixel(struct pixel_strip *p, uint32_t pixel_grb) { + pio_sm_put_blocking(p->pio, p->pio_sm, pixel_grb); } static inline uint32_t urgb_u32(uint8_t r, uint8_t g, uint8_t b) { - return ((uint32_t) (r) << 8) | ((uint32_t) (g) << 16) | (uint32_t) (b); + return ((uint32_t)(g)<<24) | ((uint32_t)(r)<<16) | ((uint32_t)(b)<<8); +} + + +static inline uint32_t wrgb_u32(uint8_t r, uint8_t g, uint8_t b, uint8_t w) { + return ((uint32_t)(g)<<24) | ((uint32_t)(r)<<16) | ((uint32_t)(b)<<8) | (uint32_t)(w); +} + + +static void init_pixel_strip(struct pixel_strip *p) +{ + int i; + + p->pio_sm = pio_claim_unused_sm(p->pio, true); + p->offset = pio_add_program(p->pio, &ws2812_program); + ws2812_program_init(p->pio, p->pio_sm, p->offset, p->pin, 800000, p->rgbw); + + for ( i=0; inum_pixels; i++ ) { + p->pixelbuf[i] = 0; + } + } int main() { DmxInput dmx_in; - uint offset; + struct pixel_strip p1; + struct pixel_strip p2; uint8_t dmxbuf[512]; - uint32_t pixelbuf[num_pixels]; int i; int blink = 1; + p1.pin = 2; + p1.num_pixels = 8; + p1.rgbw = false; + p1.pio = pio0; + + p2.pin = 3; + p2.num_pixels = 1; + p2.rgbw = true; + p2.pio = pio0; + /* Initialise DMX */ dmx_in.begin(dmx_rx_pin, 1, 512); /* Initialise Neopixels */ - led_pio_sm = pio_claim_unused_sm(led_pio, true); - offset = pio_add_program(led_pio, &ws2812_program); - ws2812_program_init(led_pio, led_pio_sm, offset, pixels_pin, 800000, false); + init_pixel_strip(&p1); + init_pixel_strip(&p2); + int npx = 8; /* Status LED */ gpio_init(PICO_DEFAULT_LED_PIN); gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); - for ( i=0; i