M5Unit-THERMO 0.4.0 git rev:7cce598
Loading...
Searching...
No Matches
unit_Thermal2_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 */
13#ifndef M5_UNIT_THERMO_UNIT_THERMAL2_DATA_HPP
14#define M5_UNIT_THERMO_UNIT_THERMAL2_DATA_HPP
15
16#include <algorithm>
17#include <cmath>
18#include <cstdint>
19#include <limits> // NaN
20
21namespace m5 {
22namespace unit {
23
28namespace thermal2 {
29
33constexpr uint8_t button_is_pressed{0x01};
34constexpr uint8_t button_was_pressed{0x02};
35constexpr uint8_t button_was_released{0x04};
36constexpr uint8_t button_was_clicked{0x08};
37constexpr uint8_t button_was_hold{0x10};
39
43constexpr uint8_t enabled_function_buzzer{0x01};
44constexpr uint8_t enabled_function_led{0x02};
45constexpr uint8_t enabled_function_auto_refresh{0x04};
47
51constexpr uint8_t enabled_low_temperature_low{0x01};
52constexpr uint8_t enabled_med_temperature_low{0x02};
53constexpr uint8_t enabled_ave_temperature_low{0x04};
54constexpr uint8_t enabled_high_temperature_low{0x08};
55constexpr uint8_t enabled_low_temperature_high{0x10};
56constexpr uint8_t enabled_med_temperature_high{0x20};
57constexpr uint8_t enabled_ave_temperature_high{0x40};
58constexpr uint8_t enabled_high_temperature_high{0x80};
60
65enum class Refresh : uint8_t {
66 Rate0_5Hz,
67 Rate1Hz,
68 Rate2Hz,
69 Rate4Hz,
70 Rate8Hz,
71 Rate16Hz,
72 Rate32Hz,
73 Rate64Hz,
74};
75
81inline uint16_t celsius_to_raw(const float f)
82{
83 // Clamp in float before converting, so that NaN or a huge value does not overflow the integer conversion
84 const float v{std::fmax(std::fmin((f + 64) * 128, 65535.f), 0.f)};
85 return static_cast<uint16_t>(std::round(v));
86}
92inline float raw_to_celsius(const uint16_t u16)
93{
94 return u16 / 128.0f - 64;
95}
96
97#pragma pack(push)
98#pragma pack(1)
103struct Data {
104 uint8_t subpage{};
105 union {
106 uint16_t temp[8]{};
107 struct {
111 uint8_t most_diff_x;
112 uint8_t most_diff_y;
119 };
120 };
121 uint16_t raw[384]{};
122
124 inline float medianTemperature() const
125 {
126 return thermal2::raw_to_celsius(temp[0]);
127 }
129 inline float averageTemperature() const
130 {
131 return thermal2::raw_to_celsius(temp[1]);
132 }
134 inline float mostDiffTemperature() const
135 {
136 return thermal2::raw_to_celsius(temp[2]);
137 }
139 inline float lowestTemperature() const
140 {
141 return thermal2::raw_to_celsius(temp[4]);
142 }
144 inline float highestTemperature() const
145 {
146 return thermal2::raw_to_celsius(temp[6]);
147 }
148
152 inline float temperature(const uint_fast16_t idx) const
153 {
154 return (idx < 384) ? thermal2::raw_to_celsius(raw[idx]) : std::numeric_limits<float>::quiet_NaN();
155 }
156};
157#pragma pack(pop)
158
159} // namespace thermal2
160
161} // namespace unit
162} // namespace m5
163#endif
Top level namespace of M5stack.
For UnitThermal2.
Unit-related namespace.
Measurement data group.
Definition unit_Thermal2_data.hpp:103
float medianTemperature() const
Gets median temperature in Celsius.
Definition unit_Thermal2_data.hpp:124
uint8_t most_diff_x
Most different pixel X.
Definition unit_Thermal2_data.hpp:111
uint16_t raw[384]
Raw pixel data (1/2)
Definition unit_Thermal2_data.hpp:121
uint16_t average_temperature
Average temperature (raw)
Definition unit_Thermal2_data.hpp:109
uint8_t most_diff_y
Most different pixel Y.
Definition unit_Thermal2_data.hpp:112
uint8_t subpage
Subpage 0:even 1:odd.
Definition unit_Thermal2_data.hpp:104
float temperature(const uint_fast16_t idx) const
Gets pixel temperature in Celsius.
Definition unit_Thermal2_data.hpp:152
uint8_t highest_diff_x
Highest pixel X.
Definition unit_Thermal2_data.hpp:117
uint16_t highest_temperature
Highest temperature (raw)
Definition unit_Thermal2_data.hpp:116
uint16_t median_temperature
Median temperature (raw)
Definition unit_Thermal2_data.hpp:108
float averageTemperature() const
Gets average temperature in Celsius.
Definition unit_Thermal2_data.hpp:129
uint16_t most_diff_temperature
Most different temperature (raw)
Definition unit_Thermal2_data.hpp:110
float highestTemperature() const
Gets highest temperature in Celsius.
Definition unit_Thermal2_data.hpp:144
float mostDiffTemperature() const
Gets most different temperature in Celsius.
Definition unit_Thermal2_data.hpp:134
uint16_t temp[8]
Temperature information.
Definition unit_Thermal2_data.hpp:106
uint8_t highest_diff_y
Highest pixel Y.
Definition unit_Thermal2_data.hpp:118
float lowestTemperature() const
Gets lowest temperature in Celsius.
Definition unit_Thermal2_data.hpp:139
uint8_t lowest_diff_x
Lowest pixel X.
Definition unit_Thermal2_data.hpp:114
uint8_t lowest_diff_y
Lowest pixel Y.
Definition unit_Thermal2_data.hpp:115
uint16_t lowest_temperature
Lowest temperature (raw)
Definition unit_Thermal2_data.hpp:113
constexpr uint8_t button_was_released
Was released.
Definition unit_Thermal2_data.hpp:35
constexpr uint8_t enabled_low_temperature_low
Low temp reached low threshold.
Definition unit_Thermal2_data.hpp:51
constexpr uint8_t enabled_high_temperature_low
High temp reached low threshold.
Definition unit_Thermal2_data.hpp:54
constexpr uint8_t enabled_ave_temperature_low
Ave temp reached low threshold.
Definition unit_Thermal2_data.hpp:53
constexpr uint8_t button_was_pressed
Was pressed.
Definition unit_Thermal2_data.hpp:34
constexpr uint8_t button_was_hold
Was held.
Definition unit_Thermal2_data.hpp:37
constexpr uint8_t enabled_function_led
LED enabled.
Definition unit_Thermal2_data.hpp:44
constexpr uint8_t button_was_clicked
Was clicked.
Definition unit_Thermal2_data.hpp:36
constexpr uint8_t enabled_med_temperature_high
Med temp reached high threshold.
Definition unit_Thermal2_data.hpp:56
constexpr uint8_t enabled_high_temperature_high
High temp reached high threshold.
Definition unit_Thermal2_data.hpp:58
float raw_to_celsius(const uint16_t u16)
Raw temperature value to celsius.
Definition unit_Thermal2_data.hpp:92
constexpr uint8_t enabled_low_temperature_high
Low temp reached high threshold.
Definition unit_Thermal2_data.hpp:55
constexpr uint8_t button_is_pressed
Is pressed now.
Definition unit_Thermal2_data.hpp:33
constexpr uint8_t enabled_ave_temperature_high
Ave temp reached high threshold.
Definition unit_Thermal2_data.hpp:57
constexpr uint8_t enabled_med_temperature_low
Med temp reached low threshold.
Definition unit_Thermal2_data.hpp:52
Refresh
Refresh rate(Hz)
Definition unit_Thermal2_data.hpp:65
constexpr uint8_t enabled_function_buzzer
Buzzer enabled.
Definition unit_Thermal2_data.hpp:43
constexpr uint8_t enabled_function_auto_refresh
Auto refresh (periodic measurement) enabled.
Definition unit_Thermal2_data.hpp:45
uint16_t celsius_to_raw(const float f)
Celsius to raw temperature value.
Definition unit_Thermal2_data.hpp:81