M5Unit-RGB 0.0.1 git rev:55cb205
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;
36 UnitRectLED() : BaseClass(WIDTH * HEIGHT)
37 {
38 }
39 static constexpr uint8_t LED_WIDTH{WIDTH};
40 static constexpr uint8_t LED_HEIGHT{HEIGHT};
41
43 inline virtual uint16_t width() const override
44 {
45 return (this->rotation() & 1) ? HEIGHT : WIDTH;
46 }
48 inline virtual uint16_t height() const override
49 {
50 return (this->rotation() & 1) ? WIDTH : HEIGHT;
51 }
52
53 inline virtual uint8_t maxRotation() const
54 {
55 // 0:0 1:90 2:180 3:270
56 return 4;
57 }
58
59protected:
60 virtual uint16_t index_of(const uint16_t n) const override
61 {
62 switch (this->rotation()) {
63 case 1: { // 90° CW (logical grid: HEIGHT x WIDTH)
64 uint8_t x = n % HEIGHT;
65 uint8_t y = n / HEIGHT;
66 return x * WIDTH + (WIDTH - 1 - y);
67 }
68 case 2: { // 180° (logical grid: WIDTH x HEIGHT)
69 uint8_t x = n % WIDTH;
70 uint8_t y = n / WIDTH;
71 return (HEIGHT - 1 - y) * WIDTH + (WIDTH - 1 - x);
72 }
73 case 3: { // 270° CW (logical grid: HEIGHT x WIDTH)
74 uint8_t x = n % HEIGHT;
75 uint8_t y = n / HEIGHT;
76 return (HEIGHT - 1 - x) * WIDTH + y;
77 }
78 default: { // 0°
79 return n;
80 }
81 }
82 }
83};
84
86using m5::utility::mmh3::operator"" _mmh3;
87// class UnitRectLED
88template <const uint8_t WIDTH, const uint8_t HEIGHT, class BaseClass>
89const char UnitRectLED<WIDTH, HEIGHT, BaseClass>::name[] = "UnitRectLED";
90template <const uint8_t WIDTH, const uint8_t HEIGHT, class BaseClass>
91const types::uid_t UnitRectLED<WIDTH, HEIGHT, BaseClass>::uid{"UnitRectLED"_mmh3};
92template <const uint8_t WIDTH, const uint8_t HEIGHT, class BaseClass>
93const types::attr_t UnitRectLED<WIDTH, HEIGHT, BaseClass>::attr{types::attribute::AccessGPIO};
95
96} // namespace unit
97} // namespace m5
98#endif
virtual uint16_t height() const override
Logical height considering rotation (0/180: HEIGHT, 90/270: WIDTH)
Definition unit_RectLED.hpp:48
virtual uint16_t width() const override
Logical width considering rotation (0/180: WIDTH, 90/270: HEIGHT)
Definition unit_RectLED.hpp:43
Top level namespace of M5stack.
Unit-related namespace.
LED base unit for M5UnitUnified.
SK6812 unit for M5UnitUnified.
WS2812 unit for M5UnitUnified.