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