M5Unit-COLOR 0.1.0 git rev:ba99933
Loading...
Searching...
No Matches
unit_TCS3472x.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_COLOR_UNIT_TCS3472_HPP
11#define M5_UNIT_COLOR_UNIT_TCS3472_HPP
12
13#include <M5UnitComponent.hpp>
14#include <m5_utility/container/circular_buffer.hpp>
15#include <array>
16
17namespace m5 {
18namespace unit {
19
24namespace tcs3472x {
25
30enum class Persistence : uint8_t {
31 Every,
32 Cycle1,
33 Cycle2,
34 Cycle3,
35 Cycle5,
36 Cycle10,
37 Cycle15,
38 Cycle20,
39 Cycle25,
40 Cycle30,
41 Cycle35,
42 Cycle40,
43 Cycle45,
44 Cycle50,
45 Cycle55,
46 Cycle60,
47};
48
53enum class Gain : uint8_t {
54 Controlx1,
55 Controlx4,
58};
59
64struct Data {
65 std::array<uint8_t, 8> raw{}; // Raw data ClCh/RlRh/GlGh/BlBh
66
70 inline uint16_t R16() const
71 {
72 return ((uint16_t)raw[3] << 8) | raw[2];
73 }
75 inline uint16_t G16() const
76 {
77 return ((uint16_t)raw[5] << 8) | raw[4];
78 }
80 inline uint16_t B16() const
81 {
82 return ((uint16_t)raw[7] << 8) | raw[6];
83 }
85 inline uint16_t C16() const
86 {
87 return ((uint16_t)raw[1] << 8) | raw[0];
88 }
90 inline uint16_t RnoIR16() const
91 {
92 return std::max(std::min(R16() - IR(), (int32_t)0xFFFF), (int32_t)0x0000);
93 }
95 inline uint16_t GnoIR16() const
96 {
97 return std::max(std::min(G16() - IR(), (int32_t)0xFFFF), (int32_t)0x0000);
98 }
100 inline uint16_t BnoIR16() const
101 {
102 return std::max(std::min(B16() - IR(), (int32_t)0xFFFF), (int32_t)0x0000);
103 }
105 inline uint16_t CnoIR16() const
106 {
107 return std::max(std::min(C16() - IR(), (int32_t)0xFFFF), (int32_t)0x0000);
108 }
110
114 inline uint8_t R8() const
115 {
116 return raw_to_uint8(R16(), C16());
117 }
119 inline uint8_t G8() const
120 {
121 return raw_to_uint8(G16(), C16());
122 }
124 inline uint8_t B8() const
125 {
126 return raw_to_uint8(B16(), C16());
127 }
129 inline uint8_t RnoIR8() const
130 {
131 return raw_to_uint8(R16() - IR(), C16() - IR());
132 }
134 inline uint8_t GnoIR8() const
135 {
136 return raw_to_uint8(G16() - IR(), C16() - IR());
137 }
139 inline uint8_t BnoIR8() const
140 {
141 return raw_to_uint8(B16() - IR(), C16() - IR());
142 }
143
145 inline uint16_t RGB565() const
146 {
147 return color565(R8(), G8(), B8());
148 }
150 inline uint32_t RGB888() const
151 {
152 return color888(R8(), G8(), B8());
153 }
155 inline uint16_t RGBnoIR565() const
156 {
157 return color565(RnoIR8(), GnoIR8(), BnoIR8());
158 }
160 inline uint32_t RGBnoIR888() const
161 {
162 return color888(RnoIR8(), GnoIR8(), BnoIR8());
163 }
165
167 inline int32_t IR(bool usingCache = true) const
168 {
169 return (usingCache && _cache)
170 ? _cache
171 : (_cache = static_cast<int32_t>(
172 ((int32_t)R16() + (int32_t)G16() + (int32_t)B16() - (int32_t)C16()) * 0.5f));
173 }
174
176 inline static uint8_t raw_to_uint8(const int32_t v, const int32_t c)
177 {
178 return std::max(std::min(static_cast<int>(c ? ((float)v / c) * 255.f : 0), 0xFF), 0x00);
179 }
180
183 inline static constexpr uint8_t color332(uint8_t r, uint8_t g, uint8_t b)
184 {
185 return ((((r >> 5) << 3) + (g >> 5)) << 2) + (b >> 6);
186 }
187 inline static constexpr uint16_t color565(uint8_t r, uint8_t g, uint8_t b)
188 {
189 return (r >> 3) << 11 | (g >> 2) << 5 | b >> 3;
190 }
191 inline static constexpr uint32_t color888(uint8_t r, uint8_t g, uint8_t b)
192 {
193 return r << 16 | g << 8 | b;
194 }
195 inline static constexpr uint16_t swap565(uint8_t r, uint8_t g, uint8_t b)
196 {
197 return (((r >> 3) << 3) + (g >> 5)) | (((g >> 2) << 5) | (b >> 3)) << 8;
198 }
199 inline static constexpr uint32_t swap888(uint8_t r, uint8_t g, uint8_t b)
200 {
201 return b << 16 | g << 8 | r;
202 }
204
205private:
206 mutable int32_t _cache{}; // IR componet value cache
207};
208
209} // namespace tcs3472x
210
216class UnitTCS3472x : public Component, public PeriodicMeasurementAdapter<UnitTCS3472x, tcs3472x::Data> {
217 M5_UNIT_COMPONENT_HPP_BUILDER(UnitTCS3472x, 0x00);
218
219public:
224 struct config_t {
226 bool start_periodic{true};
228 float atime{614.f};
230 float wtime{2.4f};
232 tcs3472x::Gain gain{tcs3472x::Gain::Controlx4};
233 };
234
235 explicit UnitTCS3472x(const uint8_t addr = DEFAULT_ADDRESS)
236 : Component(addr), _data{new m5::container::CircularBuffer<tcs3472x::Data>(1)}
237 {
238 auto ccfg = component_config();
239 ccfg.clock = 400 * 1000U;
240 component_config(ccfg);
241 }
242 virtual ~UnitTCS3472x()
243 {
244 }
245
246 virtual bool begin() override;
247 virtual void update(const bool force = false) override;
248
251
253 {
254 return _cfg;
255 }
257 inline void config(const config_t& cfg)
258 {
259 _cfg = cfg;
260 }
262
266 inline uint8_t R8() const
267 {
268 return !empty() ? oldest().R8() : 0U;
269 }
271 inline uint8_t G8() const
272 {
273 return !empty() ? oldest().G8() : 0U;
274 }
276 inline uint8_t B8() const
277 {
278 return !empty() ? oldest().B8() : 0U;
279 }
281 inline uint16_t RGB565() const
282 {
283 return !empty() ? oldest().RGB565() : 0U;
284 }
286
289
306 bool readGain(tcs3472x::Gain& gc);
312 bool writeGain(const tcs3472x::Gain gc);
318 bool readAtime(uint8_t& raw);
324 bool readAtime(float& ms);
330 template <typename T, typename std::enable_if<std::is_integral<T>::value, std::nullptr_t>::type = nullptr>
331 inline bool writeAtime(const T raw)
332 {
333 return write_atime((uint8_t)raw);
334 }
342 bool writeAtime(const float ms);
349 bool readWtime(uint8_t& raw, bool& wlong);
355 bool readWtime(float& ms);
362 bool writeWtime(const uint8_t raw, const bool wlong);
370 bool writeWtime(const float ms);
372
375
382 inline bool startPeriodicMeasurement(const tcs3472x::Gain gc, const float atime, const float wtime)
383 {
384 return PeriodicMeasurementAdapter<UnitTCS3472x, tcs3472x::Data>::startPeriodicMeasurement(gc, atime, wtime);
385 }
388 {
389 return PeriodicMeasurementAdapter<UnitTCS3472x, tcs3472x::Data>::startPeriodicMeasurement();
390 }
396 inline bool stopPeriodicMeasurement(const bool power_off = true)
397 {
398 return PeriodicMeasurementAdapter<UnitTCS3472x, tcs3472x::Data>::stopPeriodicMeasurement(power_off);
399 }
401
404
413 bool measureSingleshot(tcs3472x::Data& d, const tcs3472x::Gain gc, const float atime);
417
420
425 bool readInterrupt(bool& enable);
431 bool writeInterrupt(const bool enable);
438 bool readInterruptThreshold(uint16_t& low, uint16_t& high);
445 bool writeInterruptThreshold(const uint16_t low, const uint16_t high);
446
448 bool clearInterrupt();
450
451 bool readStatus(uint8_t& status);
452
453protected:
454 inline virtual bool is_valid_id(const uint8_t id)
455 {
456 return false;
457 }
458
459 bool read_register8(const uint8_t reg, uint8_t& val);
460 bool write_register8(const uint8_t reg, const uint8_t val);
461 // bool read_register16(const uint8_t reg, uint16_t& val);
462 // bool write_register16(const uint8_t reg, const uint16_t val);
463 bool read_register(const uint8_t reg, uint8_t* buf, const uint32_t len);
464 bool write_register(const uint8_t reg, const uint8_t* buf, const uint32_t len);
465
466 bool start_periodic_measurement(const tcs3472x::Gain gc, const float atime, const float wtime);
467 bool start_periodic_measurement();
468 bool stop_periodic_measurement(const bool power_off);
469
470 bool is_data_ready();
471 bool read_measurement(tcs3472x::Data& d);
472
473 bool write_atime(const uint8_t raw);
474
475 M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitTCS3472x, tcs3472x::Data);
476
477private:
478 std::unique_ptr<m5::container::CircularBuffer<tcs3472x::Data>> _data{};
479 config_t _cfg{};
480};
481
487 M5_UNIT_COMPONENT_HPP_BUILDER(UnitTCS34725, 0x29);
488
489public:
490 explicit UnitTCS34725(const uint8_t addr = DEFAULT_ADDRESS) : UnitTCS3472x(addr)
491 {
492 }
493 virtual ~UnitTCS34725()
494 {
495 }
496 static constexpr uint8_t UNIT_ID{0x44};
497
498protected:
499 inline virtual bool is_valid_id(const uint8_t id) override
500 {
501 return id == UNIT_ID;
502 }
503};
504
510 M5_UNIT_COMPONENT_HPP_BUILDER(UnitTCS34727, 0x29);
511
512public:
513 explicit UnitTCS34727(const uint8_t addr = DEFAULT_ADDRESS) : UnitTCS3472x(addr)
514 {
515 }
516 virtual ~UnitTCS34727()
517 {
518 }
519 static constexpr uint8_t UNIT_ID{0x4D};
520
521protected:
522 inline virtual bool is_valid_id(const uint8_t id) override
523 {
524 return id == UNIT_ID;
525 }
526};
527
529namespace tcs3472x {
530namespace command {
531// R/W
532constexpr uint8_t ENABLE_REG{0x00};
533constexpr uint8_t ATIME_REG{0x01};
534constexpr uint8_t WTIME_REG{0x03};
535constexpr uint8_t AILTL_REG{0x04};
536constexpr uint8_t AILTH_REG{0x05};
537constexpr uint8_t AIHTL_REG{0x06};
538constexpr uint8_t AIHTH_REG{0x07};
539constexpr uint8_t PERS_REG{0x0C};
540constexpr uint8_t CONFIG_REG{0x0D};
541constexpr uint8_t CONTROL_REG{0x0F};
542// R
543constexpr uint8_t ID_REG{0x12};
544constexpr uint8_t STATUS_REG{0x13};
545constexpr uint8_t CDATAL_REG{0x14};
546constexpr uint8_t CDATAH_REG{0x15};
547constexpr uint8_t RDATAL_REG{0x16};
548constexpr uint8_t RDATAH_REG{0x17};
549constexpr uint8_t GDATAL_REG{0x18};
550constexpr uint8_t GDATAH_REG{0x19};
551constexpr uint8_t BDATAL_REG{0x1A};
552constexpr uint8_t BDATAH_REG{0x1B};
553
554} // namespace command
555} // namespace tcs3472x
557
558} // namespace unit
559} // namespace m5
560#endif
Color recognition unit.
Definition unit_TCS3472x.hpp:486
Color recognition unit.
Definition unit_TCS3472x.hpp:509
Color recognition unit.
Definition unit_TCS3472x.hpp:216
bool readAtime(uint8_t &raw)
Read the The RGBC integration time (ATIME)
Definition unit_TCS3472x.cpp:274
bool writeInterruptThreshold(const uint16_t low, const uint16_t high)
Write the interrupt thresholds for clear channel.
Definition unit_TCS3472x.cpp:390
uint8_t R8() const
Oldest measured Red.
Definition unit_TCS3472x.hpp:266
bool readWtime(uint8_t &raw, bool &wlong)
Read the wait time (WTIME)
Definition unit_TCS3472x.cpp:312
uint8_t B8() const
Oldest measured Blue.
Definition unit_TCS3472x.hpp:276
uint8_t G8() const
Oldest measured Green.
Definition unit_TCS3472x.hpp:271
bool stopPeriodicMeasurement(const bool power_off=true)
Stop periodic measurement.
Definition unit_TCS3472x.hpp:396
bool clearInterrupt()
Clear interrupt.
Definition unit_TCS3472x.cpp:400
bool writeAtime(const T raw)
Write the The RGBC integration time (ATIME)
Definition unit_TCS3472x.hpp:331
uint16_t RGB565() const
Oldest measured RGB565.
Definition unit_TCS3472x.hpp:281
bool writeWtime(const uint8_t raw, const bool wlong)
Write the wait time (WTIME)
Definition unit_TCS3472x.cpp:333
bool startPeriodicMeasurement()
Start periodic measurement using current settings.
Definition unit_TCS3472x.hpp:387
bool writeGain(const tcs3472x::Gain gc)
Write the gain control.
Definition unit_TCS3472x.cpp:265
bool writeInterrupt(const bool enable)
Write the interrupt status.
Definition unit_TCS3472x.cpp:369
bool readInterrupt(bool &enable)
Read the interrupt status.
Definition unit_TCS3472x.cpp:359
void config(const config_t &cfg)
Set the configration.
Definition unit_TCS3472x.hpp:257
bool readInterruptThreshold(uint16_t &low, uint16_t &high)
Read the interrupt thresholds for clear channel.
Definition unit_TCS3472x.cpp:379
bool writePersistence(const tcs3472x::Persistence pers)
Write the persistence.
Definition unit_TCS3472x.cpp:244
bool readGain(tcs3472x::Gain &gc)
Read the gain control.
Definition unit_TCS3472x.cpp:255
bool measureSingleshot(tcs3472x::Data &d, const tcs3472x::Gain gc, const float atime)
Measurement single shot.
Definition unit_TCS3472x.cpp:196
config_t config()
Gets the configration.
Definition unit_TCS3472x.hpp:252
bool readPersistence(tcs3472x::Persistence &pers)
Read the persistence.
Definition unit_TCS3472x.cpp:234
bool startPeriodicMeasurement(const tcs3472x::Gain gc, const float atime, const float wtime)
Start periodic measurement.
Definition unit_TCS3472x.hpp:382
Top level namespace of M5stack.
For TCS3472x.
Unit-related namespace.
Settings for begin.
Definition unit_TCS3472x.hpp:224
float wtime
Wait time(ms) if start on begin.
Definition unit_TCS3472x.hpp:230
float atime
RGBC integration time(ms) if start on begin.
Definition unit_TCS3472x.hpp:228
bool start_periodic
Start periodic measurement on begin?
Definition unit_TCS3472x.hpp:226
tcs3472x::Gain gain
Gain if start on begin.
Definition unit_TCS3472x.hpp:232
Measurement data group.
Definition unit_TCS3472x.hpp:64
uint16_t RGBnoIR565() const
Gets the value in RGB565 format without IR component.
Definition unit_TCS3472x.hpp:155
uint8_t G8() const
Gets the green value (0-255)
Definition unit_TCS3472x.hpp:119
static uint8_t raw_to_uint8(const int32_t v, const int32_t c)
Raw to uint8_t.
Definition unit_TCS3472x.hpp:176
int32_t IR(bool usingCache=true) const
Gets the IR component.
Definition unit_TCS3472x.hpp:167
uint32_t RGBnoIR888() const
Gets the value in RGB888 format without IR component.
Definition unit_TCS3472x.hpp:160
uint16_t RnoIR16() const
Gets the raw red value without IR component.
Definition unit_TCS3472x.hpp:90
uint16_t RGB565() const
Gets the value in RGB565 format.
Definition unit_TCS3472x.hpp:145
uint16_t G16() const
Gets the raw green value.
Definition unit_TCS3472x.hpp:75
uint16_t R16() const
Gets the raw red value.
Definition unit_TCS3472x.hpp:70
uint16_t B16() const
Gets the raw blue value.
Definition unit_TCS3472x.hpp:80
uint8_t BnoIR8() const
Gets the blue value without IR component.
Definition unit_TCS3472x.hpp:139
uint8_t GnoIR8() const
Gets the green value without IR component.
Definition unit_TCS3472x.hpp:134
uint16_t BnoIR16() const
Gets the raw blue value without IR component.
Definition unit_TCS3472x.hpp:100
uint16_t CnoIR16() const
Gets the raw clear value without IR component.
Definition unit_TCS3472x.hpp:105
uint16_t GnoIR16() const
Gets the raw green value without IR component.
Definition unit_TCS3472x.hpp:95
uint32_t RGB888() const
Gets the value in RGB888 format.
Definition unit_TCS3472x.hpp:150
uint8_t R8() const
Gets the red value (0-255)
Definition unit_TCS3472x.hpp:114
uint8_t RnoIR8() const
Gets the red value without IR component.
Definition unit_TCS3472x.hpp:129
uint16_t C16() const
Gets the raw clear value.
Definition unit_TCS3472x.hpp:85
uint8_t B8() const
Gets the blue value (0-255)
Definition unit_TCS3472x.hpp:124
Gain
RGBC Gain Control.
Definition unit_TCS3472x.hpp:53
Persistence
Interrupt persistence.
Definition unit_TCS3472x.hpp:30
@ 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.