Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4909

Other RP2040 boards • Writing directly to Neopixels using the Waveshare RP2040

$
0
0
Hello,
This is a follow-up to the thread about writing directly to Neopixels using the RP2040:

viewtopic.php?t=342029

The attached code will work for the Waveshare RP2040 5x5 Matrix RGB board.

Code:

#include <stdio.h>#include <stdlib.h>#include "pico/stdlib.h"#include "hardware/pio.h"#include "hardware/clocks.h"#include "ws2812.pio.h"#define IS_RGBW false#define NUM_PIXELS 25#define MAX_VAL 255// Ref: // https://forums.raspberrypi.com/viewtopic.php?t=342029#define WS2812_PIN 16uint32_t urgb_u32(uint8_t r, uint8_t g, uint8_t b) {    return            ((uint32_t) (r) << 8) |            ((uint32_t) (g) << 16) |            (uint32_t) (b);}uint32_t led_data[NUM_PIXELS];void set_led(uint8_t led, uint8_t r, uint8_t g, uint8_t b){    led_data[led] = urgb_u32(g, r, b);}void put_pixel(uint32_t pixel_grb) {    pio_sm_put_blocking(pio0, 0, pixel_grb << 8u);}int main() {    stdio_init_all();    printf("WS2812 Test on WaveShare RP2040 Matrix. LED order is GRB, using pin %d", WS2812_PIN);    PIO pio = pio0;    int sm = 0;    // Set brightness level. 256 is too bright.    // Keep dividing by 8 for lower values per color    uint br = 8;    uint offset = pio_add_program(pio, &ws2812_program);    ws2812_program_init(pio, sm, offset, WS2812_PIN, 800000, IS_RGBW);    // Reset all LEDs to 0    for (uint i=0;i<NUM_PIXELS;i++)        set_led(i, 0, 0, 0);    uint num_sets = NUM_PIXELS/3;        // Entire LED array has to be sent out the the LED strip    while (1) {        // Setup pattern        for (uint i=0;i<num_sets;i++){            set_led((i*3) + 0, br, 0, 0);            set_led((i*3) + 1, 0, br, 0);            set_led((i*3) + 2, 0, 0, br);        }        set_led(NUM_PIXELS - 1, br, 0, 0);        // Write matrix data to all LEDs        for (uint i=0;i<NUM_PIXELS;i++)            put_pixel(led_data[i]);        sleep_ms(1000);        // Setup pattern        for (uint i=0;i<num_sets;i++){            set_led((i*3) + 0, 0, 0, br);            set_led((i*3) + 1, br, 0, 0);            set_led((i*3) + 2, 0, br, 0);        }        set_led(NUM_PIXELS - 1, 0, 0, br);                // Write matrix data to all LEDs        for (uint i=0;i<NUM_PIXELS;i++)            put_pixel(led_data[i]);        sleep_ms(1000);                 // Setup pattern        for (uint i=0;i<num_sets;i++){            set_led((i*3) + 0, 0, br, 0);            set_led((i*3) + 1, 0, 0, br);            set_led((i*3) + 2, br, 0, 0);        }        set_led(NUM_PIXELS - 1, 0, br, 0);                // Write matrix data to all LEDs        for (uint i=0;i<NUM_PIXELS;i++)            put_pixel(led_data[i]);        sleep_ms(1000);    }}

Statistics: Posted by electronicsguy — Sat Nov 16, 2024 7:16 pm



Viewing all articles
Browse latest Browse all 4909

Trending Articles