M5Unit-COLOR 0.2.0 git rev:d6fe6e1
Loading...
Searching...
No Matches
unit_TCS3472x_data.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
12#ifndef M5_UNIT_COLOR_UNIT_TCS3472X_DATA_HPP
13#define M5_UNIT_COLOR_UNIT_TCS3472X_DATA_HPP
14
15#include <algorithm>
16#include <array>
17#include <cstdint>
18
19namespace m5 {
20namespace unit {
21
26namespace tcs3472x {
27
32enum class Persistence : uint8_t {
33 Every,
34 Cycle1,
35 Cycle2,
36 Cycle3,
37 Cycle5,
38 Cycle10,
39 Cycle15,
40 Cycle20,
41 Cycle25,
42 Cycle30,
43 Cycle35,
44 Cycle40,
45 Cycle45,
46 Cycle50,
47 Cycle55,
48 Cycle60,
49};
50
55enum class Gain : uint8_t {
56 Controlx1,
57 Controlx4,
60};
61
66struct Data {
67 std::array<uint8_t, 8> raw{};
68
72 inline uint16_t R16() const
73 {
74 return (static_cast<uint16_t>(raw[3]) << 8) | raw[2];
75 }
77 inline uint16_t G16() const
78 {
79 return (static_cast<uint16_t>(raw[5]) << 8) | raw[4];
80 }
82 inline uint16_t B16() const
83 {
84 return (static_cast<uint16_t>(raw[7]) << 8) | raw[6];
85 }
87 inline uint16_t C16() const
88 {
89 return (static_cast<uint16_t>(raw[1]) << 8) | raw[0];
90 }
92 inline uint16_t RnoIR16() const
93 {
94 return std::max(std::min(R16() - IR(), static_cast<int32_t>(0xFFFF)), static_cast<int32_t>(0x0000));
95 }
97 inline uint16_t GnoIR16() const
98 {
99 return std::max(std::min(G16() - IR(), static_cast<int32_t>(0xFFFF)), static_cast<int32_t>(0x0000));
100 }
102 inline uint16_t BnoIR16() const
103 {
104 return std::max(std::min(B16() - IR(), static_cast<int32_t>(0xFFFF)), static_cast<int32_t>(0x0000));
105 }
107 inline uint16_t CnoIR16() const
108 {
109 return std::max(std::min(C16() - IR(), static_cast<int32_t>(0xFFFF)), static_cast<int32_t>(0x0000));
110 }
112
116 inline uint8_t R8() const
117 {
118 return raw_to_uint8(R16(), C16());
119 }
121 inline uint8_t G8() const
122 {
123 return raw_to_uint8(G16(), C16());
124 }
126 inline uint8_t B8() const
127 {
128 return raw_to_uint8(B16(), C16());
129 }
131 inline uint8_t RnoIR8() const
132 {
133 return raw_to_uint8(RnoIR16(), CnoIR16());
134 }
136 inline uint8_t GnoIR8() const
137 {
138 return raw_to_uint8(GnoIR16(), CnoIR16());
139 }
141 inline uint8_t BnoIR8() const
142 {
143 return raw_to_uint8(BnoIR16(), CnoIR16());
144 }
145
147 inline uint16_t RGB565() const
148 {
149 return color565(R8(), G8(), B8());
150 }
152 inline uint32_t RGB888() const
153 {
154 return color888(R8(), G8(), B8());
155 }
157 inline uint16_t RGBnoIR565() const
158 {
159 return color565(RnoIR8(), GnoIR8(), BnoIR8());
160 }
162 inline uint32_t RGBnoIR888() const
163 {
164 return color888(RnoIR8(), GnoIR8(), BnoIR8());
165 }
167
175 inline int32_t IR(const bool usingCache = true) const
176 {
177 if (!usingCache || !_cacheValid) {
178 _cache = static_cast<int32_t>((static_cast<int32_t>(R16()) + static_cast<int32_t>(G16()) +
179 static_cast<int32_t>(B16()) - static_cast<int32_t>(C16())) *
180 0.5f);
181 _cacheValid = true;
182 }
183 return _cache;
184 }
185
192 inline static uint8_t raw_to_uint8(const int32_t v, const int32_t c)
193 {
194 return std::max(std::min(static_cast<int>(c ? (static_cast<float>(v) / c) * 255.f : 0), 0xFF), 0x00);
195 }
196
199
206 inline static constexpr uint8_t color332(const uint8_t r, const uint8_t g, const uint8_t b)
207 {
208 return ((((r >> 5) << 3) + (g >> 5)) << 2) + (b >> 6);
209 }
217 inline static constexpr uint16_t color565(const uint8_t r, const uint8_t g, const uint8_t b)
218 {
219 return (r >> 3) << 11 | (g >> 2) << 5 | b >> 3;
220 }
228 inline static constexpr uint32_t color888(const uint8_t r, const uint8_t g, const uint8_t b)
229 {
230 return r << 16 | g << 8 | b;
231 }
239 inline static constexpr uint16_t swap565(const uint8_t r, const uint8_t g, const uint8_t b)
240 {
241 return (((r >> 3) << 3) + (g >> 5)) | (((g >> 2) << 5) | (b >> 3)) << 8;
242 }
250 inline static constexpr uint32_t swap888(const uint8_t r, const uint8_t g, const uint8_t b)
251 {
252 return b << 16 | g << 8 | r;
253 }
255
256private:
257 mutable int32_t _cache{}; // IR component value cache
258 mutable bool _cacheValid{}; // True if _cache holds a computed value
259};
260
261} // namespace tcs3472x
262
263} // namespace unit
264} // namespace m5
265#endif
Top level namespace of M5Stack.
For TCS3472x.
Unit-related namespace.
Measurement data group.
Definition unit_TCS3472x_data.hpp:66
uint16_t RGBnoIR565() const
Gets the value in RGB565 format without IR component.
Definition unit_TCS3472x_data.hpp:157
uint8_t G8() const
Gets the green value (0-255)
Definition unit_TCS3472x_data.hpp:121
std::array< uint8_t, 8 > raw
Raw data ClCh/RlRh/GlGh/BlBh.
Definition unit_TCS3472x_data.hpp:67
static constexpr uint8_t color332(const uint8_t r, const uint8_t g, const uint8_t b)
Converts 8-bit RGB channels to RGB332 format.
Definition unit_TCS3472x_data.hpp:206
static uint8_t raw_to_uint8(const int32_t v, const int32_t c)
Raw to uint8_t.
Definition unit_TCS3472x_data.hpp:192
static constexpr uint16_t swap565(const uint8_t r, const uint8_t g, const uint8_t b)
Converts 8-bit RGB channels to byte-swapped RGB565 format.
Definition unit_TCS3472x_data.hpp:239
uint32_t RGBnoIR888() const
Gets the value in RGB888 format without IR component.
Definition unit_TCS3472x_data.hpp:162
uint16_t RnoIR16() const
Gets the raw red value without IR component.
Definition unit_TCS3472x_data.hpp:92
uint16_t RGB565() const
Gets the value in RGB565 format.
Definition unit_TCS3472x_data.hpp:147
static constexpr uint32_t swap888(const uint8_t r, const uint8_t g, const uint8_t b)
Converts 8-bit RGB channels to byte-swapped RGB888 format.
Definition unit_TCS3472x_data.hpp:250
uint16_t G16() const
Gets the raw green value.
Definition unit_TCS3472x_data.hpp:77
uint16_t R16() const
Gets the raw red value.
Definition unit_TCS3472x_data.hpp:72
uint16_t B16() const
Gets the raw blue value.
Definition unit_TCS3472x_data.hpp:82
uint8_t BnoIR8() const
Gets the blue value without IR component (0-255)
Definition unit_TCS3472x_data.hpp:141
uint8_t GnoIR8() const
Gets the green value without IR component (0-255)
Definition unit_TCS3472x_data.hpp:136
int32_t IR(const bool usingCache=true) const
Gets the IR component.
Definition unit_TCS3472x_data.hpp:175
uint16_t BnoIR16() const
Gets the raw blue value without IR component.
Definition unit_TCS3472x_data.hpp:102
uint16_t CnoIR16() const
Gets the raw clear value without IR component.
Definition unit_TCS3472x_data.hpp:107
uint16_t GnoIR16() const
Gets the raw green value without IR component.
Definition unit_TCS3472x_data.hpp:97
uint32_t RGB888() const
Gets the value in RGB888 format.
Definition unit_TCS3472x_data.hpp:152
uint8_t R8() const
Gets the red value (0-255)
Definition unit_TCS3472x_data.hpp:116
static constexpr uint32_t color888(const uint8_t r, const uint8_t g, const uint8_t b)
Converts 8-bit RGB channels to RGB888 format.
Definition unit_TCS3472x_data.hpp:228
uint8_t RnoIR8() const
Gets the red value without IR component (0-255)
Definition unit_TCS3472x_data.hpp:131
static constexpr uint16_t color565(const uint8_t r, const uint8_t g, const uint8_t b)
Converts 8-bit RGB channels to RGB565 format.
Definition unit_TCS3472x_data.hpp:217
uint16_t C16() const
Gets the raw clear value.
Definition unit_TCS3472x_data.hpp:87
uint8_t B8() const
Gets the blue value (0-255)
Definition unit_TCS3472x_data.hpp:126
Gain
RGBC Gain Control.
Definition unit_TCS3472x_data.hpp:55
Persistence
Interrupt persistence.
Definition unit_TCS3472x_data.hpp:32
@ Cycle35
35 clear channel value outside of threshold range
@ Cycle15
15 clear channel value outside of threshold range
@ Cycle45
45 clear channel value outside of threshold range
@ Cycle40
40 clear channel value outside of threshold range
@ Cycle3
3 clear channel value outside of threshold range
@ Cycle55
55 clear channel value outside of threshold range
@ Cycle5
5 clear channel value outside of threshold range
@ Cycle10
10 clear channel value outside of threshold range
@ Cycle30
30 clear channel value outside of threshold range
@ Cycle1
1 clear channel value outside of threshold range
@ Cycle2
2 clear channel value outside of threshold range
@ Cycle25
25 clear channel value outside of threshold range
@ Cycle50
50 clear channel value outside of threshold range
@ Cycle60
60 clear channel value outside of threshold range
@ Cycle20
20 clear channel value outside of threshold range
@ Every
Every RGBC cycle generates an interrupt.