M5Unit-RGB 0.1.0 git rev:b7fdb9a
Loading...
Searching...
No Matches
unit_LED.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_RGB_UNIT_LED_HPP
11#define M5_UNIT_RGB_UNIT_LED_HPP
12
13#include "unit_LED_data.hpp"
14#include <M5UnitComponent.hpp>
15#include <vector>
16
17namespace m5 {
18namespace unit {
19
20namespace rgb {
21
27enum class Order : int8_t {
28 unknown = -1,
29 // 24 bits (no white channel)
30 rgb24,
31 rbg24,
32 grb24,
33 gbr24,
34 brg24,
35 bgr24,
36 // 32 bits (with white channel)
37 hasWhite,
38 //
40 wrbg32,
41 wgrb32,
42 wgbr32,
43 wbrg32,
44 wbgr32,
45 //
46 rwgb32,
47 rwbg32,
48 rgwb32,
49 rgbw32,
50 rbwg32,
51 rbgw32,
52 //
53 gwrb32,
54 gwbr32,
55 grwb32,
56 grbw32,
57 gbwr32,
58 gbrw32,
59 //
60 bwrg32,
61 bwgr32,
62 brwg32,
63 brgw32,
64 bgwr32,
65 bgrw32,
66};
67
73int8_t offsetR(const Order f);
79int8_t offsetG(const Order f);
85int8_t offsetB(const Order f);
91int8_t offsetW(const Order f);
92} // namespace rgb
93
98class UnitLED : public Component {
99 M5_UNIT_COMPONENT_HPP_BUILDER(UnitLED, 0x00);
100
102 class PixelProxy {
103 public:
104 PixelProxy(UnitLED& u, const uint32_t i) : _u(u), _i(i), _c{u.color(i)}
105 {
106 }
107
108 inline operator rgb::Color() const
109 {
110 return _c;
111 }
112
113 PixelProxy& operator=(const rgb::Color& c)
114 {
115 _c = c;
116 _u.setPixelColor(_i, _c);
117 return *this;
118 }
119 PixelProxy& operator=(const PixelProxy& o)
120 {
121 _c = static_cast<rgb::Color>(o);
122 _u.setPixelColor(_i, _c);
123 return *this;
124 }
125
126 private:
127 UnitLED& _u;
128 uint32_t _i;
129 rgb::Color _c; // Color at the time of Proxy creation
130 };
132
133public:
134 using container_type = std::vector<uint8_t>;
135 using item_container_type = std::vector<gpio::m5_rmt_item_t>;
136
141 struct config_t {
142 uint8_t brightness{10};
144 uint8_t mem_blocks{0};
146 bool with_dma{false};
147 };
148
151
155 inline config_t config() const
156 {
157 return _cfg;
158 }
163 inline void config(const config_t& cfg)
164 {
165 _cfg = cfg;
166 }
168
170 virtual ~UnitLED()
171 {
172 }
173
181 bool add(UnitLED& c, const int16_t channel);
182
187 virtual bool begin() override;
188
191
195 inline uint16_t numPixels() const
196 {
197 return _numPixels;
198 }
203 inline uint8_t brightness() const
204 {
205 return _brightness;
206 }
207
212 inline virtual uint16_t width() const
213 {
214 return numPixels();
215 }
220 inline virtual uint16_t height() const
221 {
222 return 1;
223 }
224
229 inline bool hasWhite() const
230 {
231 return (_order >= rgb::Order::hasWhite);
232 }
237 inline rgb::Order order() const
238 {
239 return _order;
240 }
245 inline int8_t offsetR() const
246 {
247 return _offsetR;
248 }
253 inline int8_t offsetG() const
254 {
255 return _offsetG;
256 }
261 inline int8_t offsetB() const
262 {
263 return _offsetB;
264 }
269 inline int8_t offsetW() const
270 {
271 return _offsetW;
272 }
273
278 const container_type& pixels() const
279 {
280 return _pixels;
281 }
288 inline rgb::Color color(const uint16_t n) const
289 {
290 return (n < _numPixels) ? get_pixel_color(index_of(n)) : rgb::Color();
291 }
298 inline rgb::Color absoluteColor(const uint16_t n) const
299 {
300 return (n < _numPixels) ? get_pixel_color(n) : rgb::Color();
301 }
303
306
310 bool writePixels();
315 inline bool show()
316 {
317 return writePixels();
318 }
320
324
330 inline void setBrightness(const uint8_t b)
331 {
332 _brightness = b;
333 }
344 bool setPixelColor(const uint16_t n, const uint8_t r, const uint8_t g, const uint8_t b);
356 inline bool setPixelColor(const uint16_t n, const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t w)
357 {
358 return setPixelColor(n, rgb::Color(r, g, b, w));
359 }
368 inline bool setPixelColor(const uint16_t n, const rgb::wrgb32_color_t c)
369 {
370 return setPixelColor(n, rgb::Color(c));
371 }
380 inline bool setPixelColor(const uint16_t n, const rgb::Color& c)
381 {
382 if (n >= _numPixels) {
383 return false;
384 }
385 return set_pixel_color(index_of(n), c);
386 }
396 bool fill(const rgb::Color& c, const uint16_t first = 0, const uint16_t count = 0);
401 inline void clear(void)
402 {
403 fill(rgb::Color());
404 }
413 bool fillRainbow(const uint16_t first = 0, const uint16_t count = 0, const uint8_t firstHue = 0);
423 bool fillGradation(const rgb::Color& firstClr, const rgb::Color& lastClr, const uint16_t first = 0,
424 const uint16_t count = 0);
432 bool fillRandom(const uint16_t first = 0, const uint16_t count = 0);
439 inline void shiftLeft(const uint16_t count = 1)
440 {
441 shift_left(count, false);
442 }
449 inline void shiftRight(const uint16_t count = 1)
450 {
451 shift_right(count, false);
452 }
458 inline void rotateLeft(const uint16_t count = 1)
459 {
460 shift_left(count, true);
461 }
467 inline void rotateRight(const uint16_t count = 1)
468 {
469 shift_right(count, true);
470 }
472
478
482 inline virtual uint8_t maxRotation() const
483 {
484 return 2; // 0,180 deg
485 }
490 inline float rotationDegree() const
491 {
492 return 360.f / maxRotation();
493 }
498 inline int8_t rotation() const
499 {
500 return _rot;
501 }
506 void setRotation(const int8_t rot);
508
512
517 rgb::Color operator[](const uint16_t i) const
518 {
519 return color(i);
520 }
526 PixelProxy operator[](const uint16_t i)
527 {
528 return PixelProxy(*this, i);
529 }
531
533 // Class that abstracts the interaction between classes and adapters
534 class Interface {
535 public:
536 explicit Interface(UnitLED& u) : _unit{u}
537 {
538 }
539 virtual ~Interface()
540 {
541 }
542 virtual bool write(const uint8_t*, const uint32_t) = 0;
543 // For RGB888
544 virtual UnitLED::container_type rgb888_items()
545 {
546 return {};
547 }
548 virtual UnitLED::container_type rgb888_items_from_root()
549 {
550 return {};
551 }
552
553 // Write pixels directly via the protocol-specific method
554 virtual bool writePixelsDirect()
555 {
556 return false;
557 }
558
559 protected:
560 UnitLED& _unit;
561 };
563
564protected:
565 inline virtual types::category_t unit_category() const override
566 {
567 return types::category_t::UnitLED;
568 }
569 inline virtual gpio::adapter_config_t rmt_config()
570 {
571 return gpio::adapter_config_t{};
572 }
573 inline virtual gpio::m5_rmt_item_t rmt_item_one()
574 {
575 return gpio::m5_rmt_item_t{};
576 }
577 inline virtual gpio::m5_rmt_item_t rmt_item_zero()
578 {
579 return gpio::m5_rmt_item_t{};
580 }
581 inline virtual gpio::m5_rmt_item_t rmt_item_reset()
582 {
583 return gpio::m5_rmt_item_t{};
584 }
585 // RMT TX resolution (Hz) used to convert durations to ticks.
586 // The actual one from M5UnitUnified 0.6.4 or later, otherwise the one requested by rmt_config().tx.tick_ns
587 uint32_t rmt_resolution_hz();
588 // RMT item of a data bit: high for high_ns, then low for low_ns
589 gpio::m5_rmt_item_t rmt_item_from_ns(const uint32_t high_ns, const uint32_t low_ns);
590 // RMT item that holds the line low for low_ns (reset / latch)
591 gpio::m5_rmt_item_t rmt_item_low_from_ns(const uint32_t low_ns);
592 inline container_type& pixels()
593 {
594 return _pixels;
595 }
596
597 // Not affected by rotation
598 rgb::Color get_pixel_color(const uint16_t n) const;
599 bool set_pixel_color(const uint16_t n, const rgb::Color& c);
600
601 inline uint8_t rotation_of(const int8_t r) const
602 {
603 return (r % maxRotation() + maxRotation()) % maxRotation();
604 }
605
606 inline virtual uint16_t index_of(const uint16_t n) const
607 {
608 // 0 or 180 deg
609 return _rot ? _numPixels - (n % _numPixels) - 1 : n;
610 }
611
612 container_type rgb888_items();
613
614 void shift_left(const uint16_t count, const bool wrap);
615 void shift_right(const uint16_t count, const bool wrap);
616
617protected:
618 // Protected so only derived types with hardcoded Order (UnitRGB / UnitPuzzle / ...) can construct.
619 explicit UnitLED(const uint16_t num_of_pixels, const rgb::Order order)
620 : Component(0x00), _order{order}, _numPixels{num_of_pixels}
621 {
622 _offsetR = rgb::offsetR(_order);
623 _offsetG = rgb::offsetG(_order);
624 _offsetB = rgb::offsetB(_order);
625 _offsetW = rgb::offsetW(_order);
626 _pixels.resize(_numPixels * (3 + hasWhite()));
627 }
628
629private:
630 rgb::Order _order{rgb::Order::unknown};
631 uint8_t _brightness{10};
632 int8_t _rot{};
633 int8_t _offsetR{-1}, _offsetG{-1}, _offsetB{-1}, _offsetW{-1};
634 uint16_t _numPixels{};
635 bool _ready{false};
636 container_type _pixels{}; // Order by rgb::Order
637 std::unique_ptr<Interface> _interface{};
638 config_t _cfg{};
639 uint32_t _rmt_resolution_hz{}; // 0 = unknown, see rmt_resolution_hz()
640
641 friend class InterfaceGPIO;
642 friend class InterfacePbHub;
643};
644
645} // namespace unit
646} // namespace m5
647#endif
LED base unit.
bool fillRandom(const uint16_t first=0, const uint16_t count=0)
Fill the specified range to random color pixels.
Definition unit_LED.cpp:615
bool setPixelColor(const uint16_t n, const rgb::wrgb32_color_t c)
Set pixel color.
Definition unit_LED.hpp:368
int8_t rotation() const
Get the rotation.
Definition unit_LED.hpp:498
void setBrightness(const uint8_t b)
Set the brightness.
Definition unit_LED.hpp:330
int8_t offsetW() const
Offset of white.
Definition unit_LED.hpp:269
rgb::Color color(const uint16_t n) const
Get the Color.
Definition unit_LED.hpp:288
virtual uint16_t height() const
Logical height in pixels.
Definition unit_LED.hpp:220
void rotateRight(const uint16_t count=1)
Rotate pixels right.
Definition unit_LED.hpp:467
std::vector< uint8_t > container_type
pixel container
Definition unit_LED.hpp:134
void shiftRight(const uint16_t count=1)
Shift pixels right.
Definition unit_LED.hpp:449
virtual bool begin() override
Begin the unit.
Definition unit_LED.cpp:460
int8_t offsetB() const
Offset of blue.
Definition unit_LED.hpp:261
PixelProxy operator[](const uint16_t i)
lvalue
Definition unit_LED.hpp:526
int8_t offsetR() const
Offset of red.
Definition unit_LED.hpp:245
bool setPixelColor(const uint16_t n, const rgb::Color &c)
Set pixel color.
Definition unit_LED.hpp:380
bool add(UnitLED &c, const int16_t channel)
Connect an LED unit as a daisy-chained child.
Definition unit_LED.cpp:455
void setRotation(const int8_t rot)
Set the rotation.
Definition unit_LED.cpp:629
int8_t offsetG() const
Offset of green.
Definition unit_LED.hpp:253
bool fill(const rgb::Color &c, const uint16_t first=0, const uint16_t count=0)
Fill the specified range with the specified color.
Definition unit_LED.cpp:559
virtual uint16_t width() const
Logical width in pixels.
Definition unit_LED.hpp:212
bool show()
Write pixel color to LED (alias)
Definition unit_LED.hpp:315
bool hasWhite() const
Has white data?
Definition unit_LED.hpp:229
uint8_t brightness() const
Brightness.
Definition unit_LED.hpp:203
void clear(void)
Clear pixels.
Definition unit_LED.hpp:401
rgb::Color operator[](const uint16_t i) const
rvalue
Definition unit_LED.hpp:517
void config(const config_t &cfg)
Set the configuration.
Definition unit_LED.hpp:163
virtual uint8_t maxRotation() const
Get the max rotation.
Definition unit_LED.hpp:482
bool setPixelColor(const uint16_t n, const uint8_t r, const uint8_t g, const uint8_t b)
Set pixel color (RGB)
Definition unit_LED.cpp:546
void shiftLeft(const uint16_t count=1)
Shift pixels left.
Definition unit_LED.hpp:439
config_t config() const
Gets the configuration.
Definition unit_LED.hpp:155
bool fillRainbow(const uint16_t first=0, const uint16_t count=0, const uint8_t firstHue=0)
Fill the specified range to rainbow.
Definition unit_LED.cpp:572
rgb::Color absoluteColor(const uint16_t n) const
Get the Color.
Definition unit_LED.hpp:298
void rotateLeft(const uint16_t count=1)
Rotate pixels left.
Definition unit_LED.hpp:458
rgb::Order order() const
Color order.
Definition unit_LED.hpp:237
uint16_t numPixels() const
Number of the pixels.
Definition unit_LED.hpp:195
bool writePixels()
Write pixel color to LED.
Definition unit_LED.cpp:522
virtual ~UnitLED()
Destructor.
Definition unit_LED.hpp:170
std::vector< gpio::m5_rmt_item_t > item_container_type
RMT item container (unused, for compatibility)
Definition unit_LED.hpp:135
const container_type & pixels() const
pixels container
Definition unit_LED.hpp:278
float rotationDegree() const
Get the rotation angle per 1.
Definition unit_LED.hpp:490
bool fillGradation(const rgb::Color &firstClr, const rgb::Color &lastClr, const uint16_t first=0, const uint16_t count=0)
Fill the specified range to gradation pixels.
Definition unit_LED.cpp:590
bool setPixelColor(const uint16_t n, const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t w)
Set pixel color (RGBW)
Definition unit_LED.hpp:356
Top level namespace of M5Stack.
For M5Unit-RGB.
Unit-related namespace.
Settings for begin.
Definition unit_LED.hpp:141
bool with_dma
Use DMA for RMT (RMT v2 only, e.g. ESP32-S3/C6). Ignored via PbHub.
Definition unit_LED.hpp:146
uint8_t mem_blocks
RMT memory blocks (0 = use chip default). Ignored via PbHub.
Definition unit_LED.hpp:144
uint8_t brightness
Definition unit_LED.hpp:142
WRGB 32bits.
Definition unit_LED_data.hpp:35
Order
Color order.
Definition unit_LED.hpp:27
@ wrgb32
Same value as hasWhite.
@ hasWhite
First 32-bit order; orders >= hasWhite carry a white channel.
@ unknown
Not set (begin() fails)
Color type and data line timing for M5Unit-RGB.
uint32_t wrgb32_color_t
WRGB 32bits.
Definition unit_LED_data.hpp:29