M5Unit-THERMO 0.2.0 git rev:e387a6b
Loading...
Searching...
No Matches
unit_Thermal2.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_THERMO_UNIT_THERMAL2_HPP
11#define M5_UNIT_THERMO_UNIT_THERMAL2_HPP
12#include <M5UnitComponent.hpp>
13#include <m5_utility/container/circular_buffer.hpp>
14#include <limits> // NaN
15#include <cmath>
16#include <array>
17
18namespace m5 {
19namespace unit {
24namespace thermal2 {
25
29constexpr uint8_t button_is_pressed{0x01};
30constexpr uint8_t button_was_pressed{0x02};
31constexpr uint8_t button_was_released{0x04};
32constexpr uint8_t button_was_clicked{0x08};
33constexpr uint8_t button_was_hold{0x10};
35
39constexpr uint8_t enabled_function_buzzer{0x01};
40constexpr uint8_t enabled_function_led{0x02};
41constexpr uint8_t enabled_function_auto_refresh{0x04};
43
47constexpr uint8_t enabled_low_temperature_low{0x01};
48constexpr uint8_t enabled_med_temperature_low{0x02};
49constexpr uint8_t enabled_ave_temperature_low{0x04};
50constexpr uint8_t enabled_high_temperature_low{0x08};
51constexpr uint8_t enabled_low_temperature_high{0x10};
52constexpr uint8_t enabled_med_temperature_high{0x20};
53constexpr uint8_t enabled_ave_temperature_high{0x40};
54constexpr uint8_t enabled_high_temperature_high{0x80};
56
61enum class Refresh : uint8_t {
62 Rate0_5Hz,
63 Rate1Hz,
64 Rate2Hz,
65 Rate4Hz,
66 Rate8Hz,
67 Rate16Hz,
68 Rate32Hz,
69 Rate64Hz,
70};
71
73inline static uint16_t celsius_to_raw(const float f)
74{
75 int i = std::round((f + 64) * 128);
76 return static_cast<uint16_t>(std::max(std::min(i, (int)std::numeric_limits<uint16_t>::max()), 0));
77}
79inline static float raw_to_celsius(const uint16_t u16)
80{
81 return u16 / 128.0f - 64;
82}
83
84#pragma pack(push)
85#pragma pack(1)
90struct Data {
91 uint8_t subpage{}; // Subpage 0:even 1:odd
92 union {
93 uint16_t temp[8]{}; // Temperature information
94 struct {
95 uint16_t median_temperature;
96 uint16_t average_temperature;
97 uint16_t most_diff_temperature;
98 uint8_t most_diff_x;
99 uint8_t most_diff_y;
100 uint16_t lowest_temperature;
101 uint8_t lowest_diff_x;
102 uint8_t lowest_diff_y;
103 uint16_t highest_temperature;
104 uint8_t highest_diff_x;
105 uint8_t highest_diff_y;
106 };
107 };
108 uint16_t raw[384]{}; // Raw pixel data (1/2)
109
110 // temperture information
111 inline float medianTemperature() const
112 {
113 return thermal2::raw_to_celsius(temp[0]);
114 }
115 inline float averageTemperature() const
116 {
117 return thermal2::raw_to_celsius(temp[1]);
118 }
119 inline float mostDiffTemperature() const
120 {
121 return thermal2::raw_to_celsius(temp[2]);
122 }
123 inline float lowestTemperature() const
124 {
125 return thermal2::raw_to_celsius(temp[4]);
126 }
127 inline float highestTemperature() const
128 {
129 return thermal2::raw_to_celsius(temp[6]);
130 }
131
132 // piexl temperature
133 inline float temperature(const uint_fast16_t idx) const
134 {
135 return (idx < 384) ? thermal2::raw_to_celsius(raw[idx]) : std::numeric_limits<float>::quiet_NaN();
136 }
137};
138#pragma pack(pop)
139
140} // namespace thermal2
141
146class UnitThermal2 : public Component, public PeriodicMeasurementAdapter<UnitThermal2, thermal2::Data> {
147 M5_UNIT_COMPONENT_HPP_BUILDER(UnitThermal2, 0x32);
148
149public:
154 struct config_t {
156 bool start_periodic{true};
158 thermal2::Refresh rate{thermal2::Refresh::Rate16Hz};
160 uint8_t monitor_width{15};
162 uint8_t monitor_height{11};
164 uint8_t function_control{thermal2::enabled_function_led};
166 uint32_t button_interval{20};
167 };
168
169 explicit UnitThermal2(const uint8_t addr = DEFAULT_ADDRESS)
170 : Component(addr), _data{new m5::container::CircularBuffer<thermal2::Data>(1)}
171 {
172 auto ccfg = component_config();
173 ccfg.clock = 400 * 1000U;
174 component_config(ccfg);
175 }
176 virtual ~UnitThermal2()
177 {
178 }
179
180 virtual bool begin() override;
181 virtual void update(const bool force = false) override;
182
185
187 {
188 return _cfg;
189 }
191 inline void config(const config_t& cfg)
192 {
193 _cfg = cfg;
194 }
196
199
205 {
206 return PeriodicMeasurementAdapter<UnitThermal2, thermal2::Data>::startPeriodicMeasurement(rate);
207 }
210 {
211 return PeriodicMeasurementAdapter<UnitThermal2, thermal2::Data>::startPeriodicMeasurement();
212 }
218 {
219 return PeriodicMeasurementAdapter<UnitThermal2, thermal2::Data>::stopPeriodicMeasurement();
220 }
222
225
237
240
246 bool readFunctionControl(uint8_t& value);
248 inline bool readBuzzerEnabled(bool& enabled)
249 {
250 return read_function_control_bit(thermal2::enabled_function_buzzer, enabled);
251 }
253 inline bool readLEDEnabled(bool& enabled)
254 {
255 return read_function_control_bit(thermal2::enabled_function_led, enabled);
256 }
264 bool writeFunctionControl(const uint8_t value, const bool verify = true);
266 inline bool writeBuzzerEnabled(const bool enabled)
267 {
268 return write_function_control_bit(thermal2::enabled_function_buzzer, enabled);
269 }
271 inline bool writeLEDEnabled(const bool enabled)
272 {
273 return write_function_control_bit(thermal2::enabled_function_led, enabled);
274 }
275
281 bool readRefreshRate(thermal2 ::Refresh& rate);
288 bool writeRefreshRate(const thermal2::Refresh rate);
289
296 bool readNoiseFilterLevel(uint8_t& level);
303 bool writeNoiseFilterLevel(const uint8_t level);
304
311 bool readTemeratureMonitorSize(uint8_t& wid, uint8_t& hgt);
318 bool writeTemeratureMonitorSize(const uint8_t wid, const uint8_t hgt);
320
323
328 bool readAlarmEnabled(uint8_t& enabled_bits);
334 bool writeAlarmEnabled(const uint8_t enabled_bits);
335
342 bool readAlarmTemperature(const bool highlow, uint16_t& raw);
349 bool readAlarmTemperature(const bool highlow, float& celsius);
358 bool writeAlarmTemperature(const bool highlow, const uint16_t raw);
365 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
366 inline bool writeAlarmTemperature(const bool highlow, const T celsius)
367 {
368 return writeAlarmTemperature(highlow, thermal2::celsius_to_raw(static_cast<float>(celsius)));
369 }
370
377 bool readAlarmLED(const bool highlow, uint32_t& rgb);
384 inline bool writeAlarmLED(const bool highlow, const uint32_t rgb)
385 {
386 return writeAlarmLED(highlow, rgb >> 16, rgb >> 8, rgb & 0xFF);
387 }
396 bool writeAlarmLED(const bool highlow, const uint8_t r, const uint8_t g, const uint8_t b);
397
406 bool readAlarmBuzzer(const bool highlow, uint16_t& freq, uint8_t& interval);
415 bool writeAlarmBuzzer(const bool highlow, const uint16_t freq, const uint8_t interval);
417
421
429 bool readBuzzer(uint16_t& freq, uint8_t& duty);
439 bool writeBuzzer(const uint16_t freq, const uint8_t duty, const bool verify = true);
447 bool writeBuzzerDuty(const uint8_t duty);
448
454 inline bool readBuzzerControl(bool& enabled)
455 {
456 return readBuzzerEnabled(enabled);
457 }
463 inline bool writeBuzzerControl(const bool enabled)
464 {
465 return writeBuzzerEnabled(enabled);
466 }
468
472
477 bool readLED(uint32_t& rgb);
484 inline bool writeLED(const uint32_t rgb, const bool verify = true)
485 {
486 return writeLED(rgb >> 16, rgb >> 8, rgb & 0xFF);
487 }
496 bool writeLED(const uint8_t r, const uint8_t g, const uint8_t b, const bool verify = true);
498
501
506 bool readButtonStatus(uint8_t& bs);
507
513 inline bool isPressed() const
514 {
515 return _button & thermal2::button_is_pressed;
516 }
522 inline bool wasPressed()
523 {
524 return _button & thermal2::button_was_pressed;
525 }
531 inline bool wasReleased()
532 {
533 return _button & thermal2::button_was_released;
534 }
540 inline bool wasClicked()
541 {
542 return _button & thermal2::button_was_clicked;
543 }
549 inline bool wasHold()
550 {
551 return _button & thermal2::button_was_hold;
552 }
558 inline bool isHolding()
559 {
560 return isPressed() && _holding;
561 }
563
569 bool readFirmwareVersion(uint16_t& ver);
570
574
580 bool changeI2CAddress(const uint8_t i2c_address);
586 bool readI2CAddress(uint8_t& i2c_address);
588
589protected:
590 bool read_function_control_bit(const uint8_t bit, bool& enabled);
591 bool write_function_control_bit(const uint8_t bit, const bool enabled);
592
593 bool request_data();
594 bool read_data_status(uint8_t s[2]); // [0]:data refresh ctrl, [1]subpage information
595 bool read_data(thermal2::Data& data);
596
597 bool start_periodic_measurement(const thermal2::Refresh rate);
598 bool start_periodic_measurement();
599 bool stop_periodic_measurement();
600
601 inline bool read_register(const uint8_t reg, uint8_t* v, const uint32_t len)
602 {
603 return readRegister(reg, v, len, 0, false /* stopbit false */);
604 }
605
606 inline bool read_register8(const uint8_t reg, uint8_t& v)
607 {
608 return readRegister8(reg, v, 0, false /* stopbit false */);
609 }
610
611 inline bool read_register16LE(const uint8_t reg, uint16_t& v)
612 {
613 return readRegister16LE(reg, v, 0, false /* stopbit false */);
614 }
615
616 inline bool read_register16BE(const uint8_t reg, uint16_t& v)
617 {
618 return readRegister16BE(reg, v, 0, false /* stopbit false */);
619 }
620
621 M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitThermal2, thermal2::Data);
622
623private:
624 std::unique_ptr<m5::container::CircularBuffer<thermal2::Data>> _data{};
625 uint8_t _button{}, _holding{};
626 uint32_t _button_interval{20};
627 types::elapsed_time_t _latest_button{};
628 config_t _cfg{};
629};
630
631namespace thermal2 {
633namespace command {
634constexpr uint8_t BUTTON_STATUS_REG{0x00}; // R/W
635constexpr uint8_t TEMPERATURE_ALARM_STATUS_REG{0x01}; // R
636// constexpr uint8_t DEVICE_STATUS_REG{0x02}; // R 2
637constexpr uint8_t DEVICE_ID_REG{0x04}; // R 2
638constexpr uint8_t FIRMWARE_VERSION_REG{0x06}; // R 2
639constexpr uint8_t I2C_ADDRESS_REG{0x08}; // R/W 2 (0x09:bit inverted) I
640constexpr uint8_t FUNCTION_CONTROL_REG{0x0A}; // R/W
641constexpr uint8_t REFRESH_RATE_CONFIG_REG{0x0B}; // R/W I
642constexpr uint8_t NOISE_FILTER_CONFIG_REG{0x0C}; // R/W I
643
644constexpr uint8_t TEMPERATURE_MONITOR_SIZE_REG{0x10}; // R/W I
645constexpr uint8_t ENABLE_TEMPERATURE_ALARM_REG{0x11}; // R/W I
646constexpr uint8_t BUZZER_FREQ_REG{0x12}; // R/W 2
647constexpr uint8_t BUZZER_DUTY_REG{0x14}; // R/W
648constexpr uint8_t LED_REG{0x15}; // R/W 3(R,G,B)
649
650constexpr uint8_t LOW_ALARM_THERSHOLD_REG{0x20}; // R/W 2 I
651constexpr uint8_t LOW_ALARM_BUZZER_FREQ_REG{0x22}; // R/W 2 I
652constexpr uint8_t LOW_ALARM_INTERVAL_REG{0x24}; // R/W I
653constexpr uint8_t LOW_ALARM_LED_REG{0x25}; // R/W 3 I
654
655constexpr uint8_t HIGH_ALARM_THERSHOLD_REG{0x30}; // R/W 2 I
656constexpr uint8_t HIGH_ALARM_BUZZER_FREQ_REG{0x32}; // R/W 2 I
657constexpr uint8_t HIGH_ALARM_INTERVAL_REG{0x34}; // R/W I
658constexpr uint8_t HIGH_ALARM_LED_REG{0x35}; // R/W 3 I
659
660constexpr uint8_t DATA_REFRESH_CONTROL_REG{0x6E}; // R/W I
661constexpr uint8_t SUB_PAGE_INFORMATION_REG{0x6F}; // R
662
663constexpr uint8_t MEDIAN_TEPERATURE_REG{0x70}; // R 2
664constexpr uint8_t AVERAGE_TEPERATURE_REG{0x72}; // R 2
665constexpr uint8_t MOST_DIFF_TEPERATURE_REG{0x74}; // R 2
666constexpr uint8_t MOST_DIFF_X_POS_REG{0x76}; // R
667constexpr uint8_t MOST_DIFF_Y_POS_REG{0x77}; // R
668constexpr uint8_t LOWEST_TEPERATURE_REG{0x78}; // R 2
669constexpr uint8_t LOWEST_DIFF_X_POS_REG{0x7A}; // R
670constexpr uint8_t LOWEST_DIFF_Y_POS_REG{0x7B}; // R
671constexpr uint8_t HIGHEST_TEPERATURE_REG{0x7C}; // R 2
672constexpr uint8_t HIGHEST_DIFF_X_POS_REG{0x7E}; // R
673constexpr uint8_t HIGHEST_DIFF_Y_POS_REG{0x7F}; // R
674
675constexpr uint8_t TEMPERATURE_DATA_REG{0x80}; // R 768
676} // namespace command
678} // namespace thermal2
679
680} // namespace unit
681} // namespace m5
682#endif
bool changeI2CAddress(const uint8_t i2c_address)
Change device I2C address.
Definition unit_Thermal2.cpp:452
bool readBuzzerControl(bool &enabled)
Read the Buzzer control.
Definition unit_Thermal2.hpp:454
bool wasReleased()
Was button released?
Definition unit_Thermal2.hpp:531
bool readNoiseFilterLevel(uint8_t &level)
Read the noise filter level.
Definition unit_Thermal2.cpp:251
bool writeBuzzerControl(const bool enabled)
Write the Buzzer control.
Definition unit_Thermal2.hpp:463
bool readLED(uint32_t &rgb)
Read the LED color.
Definition unit_Thermal2.cpp:410
bool wasClicked()
Was button clicked?
Definition unit_Thermal2.hpp:540
bool writeTemeratureMonitorSize(const uint8_t wid, const uint8_t hgt)
Write the temperature monitor size.
Definition unit_Thermal2.cpp:277
bool writeAlarmTemperature(const bool highlow, const uint16_t raw)
Write the alarm temperature threshold.
Definition unit_Thermal2.cpp:316
void config(const config_t &cfg)
Set the configration.
Definition unit_Thermal2.hpp:191
bool startPeriodicMeasurement(const thermal2::Refresh rate)
Start periodic measurement.
Definition unit_Thermal2.hpp:204
bool isPressed() const
Is button pressed?
Definition unit_Thermal2.hpp:513
bool readAlarmTemperature(const bool highlow, uint16_t &raw)
Read the alarm temperature threshold.
Definition unit_Thermal2.cpp:297
bool readLEDEnabled(bool &enabled)
Reads the LED enabled.
Definition unit_Thermal2.hpp:253
bool writeBuzzerEnabled(const bool enabled)
Write the buzzer enable status.
Definition unit_Thermal2.hpp:266
bool writeAlarmEnabled(const uint8_t enabled_bits)
Read the alarm control.
Definition unit_Thermal2.cpp:292
bool writeAlarmTemperature(const bool highlow, const T celsius)
Write the alarm temperature threshold.
Definition unit_Thermal2.hpp:366
bool writeRefreshRate(const thermal2::Refresh rate)
Write the refresh rate.
Definition unit_Thermal2.cpp:242
bool readTemeratureMonitorSize(uint8_t &wid, uint8_t &hgt)
Read the temperature monitor size.
Definition unit_Thermal2.cpp:265
bool writeNoiseFilterLevel(const uint8_t level)
Write the noise filter level.
Definition unit_Thermal2.cpp:256
bool writeAlarmLED(const bool highlow, const uint32_t rgb)
Write the alarm LED color.
Definition unit_Thermal2.hpp:384
bool stopPeriodicMeasurement()
Stop periodic measurement.
Definition unit_Thermal2.hpp:217
bool readFirmwareVersion(uint16_t &ver)
Read the firmware version.
Definition unit_Thermal2.cpp:446
bool readAlarmEnabled(uint8_t &enabled_bits)
Read the alarm control.
Definition unit_Thermal2.cpp:287
bool readAlarmLED(const bool highlow, uint32_t &rgb)
Read the alarm LED color.
Definition unit_Thermal2.cpp:322
bool readI2CAddress(uint8_t &i2c_address)
Read device I2C address.
Definition unit_Thermal2.cpp:465
bool writeLEDEnabled(const bool enabled)
Write the LED enable status.
Definition unit_Thermal2.hpp:271
bool measureSingleshot(thermal2::Data &page0, thermal2::Data &page1)
Measurement single shot.
Definition unit_Thermal2.cpp:150
bool writeBuzzer(const uint16_t freq, const uint8_t duty, const bool verify=true)
Write the buzzer settings.
Definition unit_Thermal2.cpp:385
bool writeBuzzerDuty(const uint8_t duty)
Write the buzzer duty settings.
Definition unit_Thermal2.cpp:405
bool startPeriodicMeasurement()
Start periodic measurement in the current settings.
Definition unit_Thermal2.hpp:209
bool readFunctionControl(uint8_t &value)
Read the function control.
Definition unit_Thermal2.cpp:186
bool writeFunctionControl(const uint8_t value, const bool verify=true)
Write the function control.
Definition unit_Thermal2.cpp:192
bool wasHold()
Was button hold?
Definition unit_Thermal2.hpp:549
bool readButtonStatus(uint8_t &bs)
Read the button status.
Definition unit_Thermal2.cpp:437
bool writeLED(const uint32_t rgb, const bool verify=true)
Write the LED color.
Definition unit_Thermal2.hpp:484
config_t config()
Gets the configration.
Definition unit_Thermal2.hpp:186
bool readBuzzer(uint16_t &freq, uint8_t &duty)
Read the buzzer settings.
Definition unit_Thermal2.cpp:372
bool wasPressed()
Was button pressed?
Definition unit_Thermal2.hpp:522
bool readAlarmBuzzer(const bool highlow, uint16_t &freq, uint8_t &interval)
Read the alarm buzzer settings.
Definition unit_Thermal2.cpp:343
bool readBuzzerEnabled(bool &enabled)
Read the buzzer enabled.
Definition unit_Thermal2.hpp:248
bool readRefreshRate(thermal2 ::Refresh &rate)
Read the refresh rate.
Definition unit_Thermal2.cpp:231
bool writeAlarmBuzzer(const bool highlow, const uint16_t freq, const uint8_t interval)
Write the alarm buzzer settings.
Definition unit_Thermal2.cpp:357
bool isHolding()
Is button holding?
Definition unit_Thermal2.hpp:558
Top level namespace of M5stack.
For UnitThermal2.
Unit-related namespace.
Settings for begin.
Definition unit_Thermal2.hpp:154
uint8_t function_control
Function control bits.
Definition unit_Thermal2.hpp:164
uint8_t monitor_height
Temperature monitor height.
Definition unit_Thermal2.hpp:162
uint32_t button_interval
Button status update interval(ms)
Definition unit_Thermal2.hpp:166
thermal2::Refresh rate
Refresh rate if start on begin.
Definition unit_Thermal2.hpp:158
bool start_periodic
Start periodic measurement on begin?
Definition unit_Thermal2.hpp:156
uint8_t monitor_width
Temperature monitor width.
Definition unit_Thermal2.hpp:160
Measurement data group.
Definition unit_Thermal2.hpp:90
constexpr uint8_t enabled_low_temperature_low
Low temp reached low threshold.
Definition unit_Thermal2.hpp:47
constexpr uint8_t enabled_high_temperature_low
High temp reached low threshold.
Definition unit_Thermal2.hpp:50
constexpr uint8_t enabled_ave_temperature_low
Ave temp reached low threshold.
Definition unit_Thermal2.hpp:49
constexpr uint8_t enabled_med_temperature_high
Med temp reached high threshold.
Definition unit_Thermal2.hpp:52
constexpr uint8_t enabled_high_temperature_high
High temp reached high threshold.
Definition unit_Thermal2.hpp:54
constexpr uint8_t enabled_low_temperature_high
Low temp reached high threshold.
Definition unit_Thermal2.hpp:51
constexpr uint8_t enabled_ave_temperature_high
Ave temp reached high threshold.
Definition unit_Thermal2.hpp:53
constexpr uint8_t enabled_med_temperature_low
Med temp reached low threshold.
Definition unit_Thermal2.hpp:48
Refresh
Refresh rate(Hz)
Definition unit_Thermal2.hpp:61