YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
ws2812_rp2350.h
1// ---------------------------------------------
2// This file is part of
3// _ _ __ _ _ __ __
4// ( \/ ) /__\ ( )_( ) /__\ ( )
5// \ / /(__)\ ) _ ( /(__)\ )(__
6// (__)(__)(__)(_) (_)(__)(__)(____)
7//
8// Yet Another HW Abstraction Library
9// Copyright (C) Andreas Terstegge
10// BSD Licensed (see file LICENSE)
11//
12// ---------------------------------------------
13//
14// Driver class for WS2812-based LED strips
15//
16#ifndef _WS2812_RP2040_H_
17#define _WS2812_RP2040_H_
18
19#include <cstdint>
20#include <cassert>
21#include "led_interface.h"
22#include "gpio_rp2350.h"
23#include "pio_rp2350.h"
24
26public:
27 // Nested LED class to control a single LED
28 class LED : public led_rgb_interface {
29 friend class ws2812_rp2350;
30 public:
31 void on() override;
32 void off() override;
33 void toggle() override;
34 bool is_on() override;
35 void set_color(uint32_t rgb) override;
36 void set_on_color(uint32_t rgb) override;
37
38 private:
39 ws2812_rp2350 * _ws2812_rp2350 {nullptr};
40 uint16_t _index {0};
41 uint32_t _color {0};
42 uint32_t _on_color {0x180000}; // Default dark red
43 };
44
45 // CTOR takes the GPIO number connected to the
46 // WS2812 LEDs and the size of the LED array
47 ws2812_rp2350(gpio_pin_t led_pin, uint16_t size);
48
49 virtual ~ws2812_rp2350();
50
51 // Index operator to access a single LEDs
52 inline led_rgb_interface & operator[](uint16_t i) {
53 assert(i < _size);
54 return _leds[i];
55 }
56
57 // Set multiple LEDs at once
58 void set_colors(uint32_t * values, uint16_t size);
59
60private:
61 bool _init {false};
62 gpio_rp2350 _gpio;
63 LED * _leds;
64 uint16_t _size;
65 SM * _sm {nullptr};
66
67 void init();
68 static uint32_t xRGB_to_GRBx(uint32_t rgb);
69 void update(uint16_t index);
70};
71
72#endif // _WS2812_RP2040_H_