M5Unit-RGB 0.0.2 git rev:51dfbe6
Loading...
Searching...
No Matches
unit_RectLED.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_RECT_LED_HPP
11#define M5_UNIT_RGB_UNIT_RECT_LED_HPP
12
13#include "unit_LED.hpp"
14#include "unit_SK6812.hpp"
15#include "unit_WS2812.hpp"
16
17namespace m5 {
18namespace unit {
19
27template <const uint8_t WIDTH, const uint8_t HEIGHT, class BaseClass>
28class UnitRectLED : public BaseClass {
29 static_assert(WIDTH > 0, "WIDTH must be greater than 0");
30 static_assert(HEIGHT > 0, "HEIGHT must be greater than 0");
31 static_assert(std::is_base_of<UnitLED, BaseClass>::value, "BaseClass must derive from UnitLED");
32 M5_UNIT_COMPONENT_HPP_BUILDER(UnitRectLED, 0x00);
33
34public:
35 using BaseClass::BaseClass;
37 UnitRectLED() : BaseClass(WIDTH * HEIGHT)
38 {
39 }
40 static constexpr uint8_t LED_WIDTH{WIDTH};
41 static constexpr uint8_t LED_HEIGHT{HEIGHT};
42
47 inline virtual uint16_t width() const override
48 {
49 return (this->rotation() & 1) ? HEIGHT : WIDTH;
50 }
55 inline virtual uint16_t height() const override
56 {
57 return (this->rotation() & 1) ? WIDTH : HEIGHT;
58 }
59
64 inline virtual uint8_t maxRotation() const override
65 {
66 // 0:0 1:90 2:180 3:270
67 return 4;
68 }
69
70protected:
71 virtual uint16_t index_of(const uint16_t n) const override
72 {
73 switch (this->rotation()) {
74 case 1: { // 90° CW (logical grid: HEIGHT x WIDTH)
75 uint8_t x = n % HEIGHT;
76 uint8_t y = n / HEIGHT;
77 return x * WIDTH + (WIDTH - 1 - y);
78 }
79 case 2: { // 180° (logical grid: WIDTH x HEIGHT)
80 uint8_t x = n % WIDTH;
81 uint8_t y = n / WIDTH;
82 return (HEIGHT - 1 - y) * WIDTH + (WIDTH - 1 - x);
83 }
84 case 3: { // 270° CW (logical grid: HEIGHT x WIDTH)
85 uint8_t x = n % HEIGHT;
86 uint8_t y = n / HEIGHT;
87 return (HEIGHT - 1 - x) * WIDTH + y;
88 }
89 default: { // 0°
90 return n;
91 }
92 }
93 }
94};
95
97using m5::utility::mmh3::operator"" _mmh3;
98// class UnitRectLED
99template <const uint8_t WIDTH, const uint8_t HEIGHT, class BaseClass>
100const char UnitRectLED<WIDTH, HEIGHT, BaseClass>::name[] = "UnitRectLED";
101template <const uint8_t WIDTH, const uint8_t HEIGHT, class BaseClass>
102const types::uid_t UnitRectLED<WIDTH, HEIGHT, BaseClass>::uid{"UnitRectLED"_mmh3};
103template <const uint8_t WIDTH, const uint8_t HEIGHT, class BaseClass>
104const types::attr_t UnitRectLED<WIDTH, HEIGHT, BaseClass>::attr{types::attribute::AccessGPIO};
106
107} // namespace unit
108} // namespace m5
109#endif
virtual uint8_t maxRotation() const override
Get the max rotation (4 steps: 0/90/180/270 deg)
Definition unit_RectLED.hpp:64
UnitRectLED()
Default constructor (uses WIDTH x HEIGHT pixels)
Definition unit_RectLED.hpp:37
virtual uint16_t height() const override
Logical height considering rotation (0/180: HEIGHT, 90/270: WIDTH)
Definition unit_RectLED.hpp:55
virtual uint16_t width() const override
Logical width considering rotation (0/180: WIDTH, 90/270: HEIGHT)
Definition unit_RectLED.hpp:47
Top level namespace of M5Stack.
Unit-related namespace.
LED base unit for M5UnitUnified.
SK6812 unit for M5UnitUnified.
WS2812 unit for M5UnitUnified.