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>
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};
39constexpr uint8_t enabled_function_buzzer{0x01};
40constexpr uint8_t enabled_function_led{0x02};
41constexpr uint8_t enabled_function_auto_refresh{0x04};
73inline static uint16_t celsius_to_raw(
const float f)
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));
79inline static float raw_to_celsius(
const uint16_t u16)
81 return u16 / 128.0f - 64;
95 uint16_t median_temperature;
96 uint16_t average_temperature;
97 uint16_t most_diff_temperature;
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;
111 inline float medianTemperature()
const
113 return thermal2::raw_to_celsius(temp[0]);
115 inline float averageTemperature()
const
117 return thermal2::raw_to_celsius(temp[1]);
119 inline float mostDiffTemperature()
const
121 return thermal2::raw_to_celsius(temp[2]);
123 inline float lowestTemperature()
const
125 return thermal2::raw_to_celsius(temp[4]);
127 inline float highestTemperature()
const
129 return thermal2::raw_to_celsius(temp[6]);
133 inline float temperature(
const uint_fast16_t idx)
const
135 return (idx < 384) ? thermal2::raw_to_celsius(raw[idx]) : std::numeric_limits<float>::quiet_NaN();
146class UnitThermal2 :
public Component,
public PeriodicMeasurementAdapter<UnitThermal2, thermal2::Data> {
169 explicit UnitThermal2(
const uint8_t addr = DEFAULT_ADDRESS)
170 : Component(addr), _data{new
m5::container::CircularBuffer<
thermal2::Data>(1)}
172 auto ccfg = component_config();
173 ccfg.clock = 400 * 1000U;
174 component_config(ccfg);
176 virtual ~UnitThermal2()
180 virtual bool begin()
override;
181 virtual void update(
const bool force =
false)
override;
206 return PeriodicMeasurementAdapter<UnitThermal2, thermal2::Data>::startPeriodicMeasurement(rate);
211 return PeriodicMeasurementAdapter<UnitThermal2, thermal2::Data>::startPeriodicMeasurement();
219 return PeriodicMeasurementAdapter<UnitThermal2, thermal2::Data>::stopPeriodicMeasurement();
250 return read_function_control_bit(thermal2::enabled_function_buzzer, enabled);
255 return read_function_control_bit(thermal2::enabled_function_led, enabled);
268 return write_function_control_bit(thermal2::enabled_function_buzzer, enabled);
273 return write_function_control_bit(thermal2::enabled_function_led, enabled);
365 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type =
nullptr>
386 return writeAlarmLED(highlow, rgb >> 16, rgb >> 8, rgb & 0xFF);
396 bool writeAlarmLED(
const bool highlow,
const uint8_t r,
const uint8_t g,
const uint8_t b);
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);
429 bool readBuzzer(uint16_t& freq, uint8_t& duty);
439 bool writeBuzzer(
const uint16_t freq,
const uint8_t duty,
const bool verify =
true);
484 inline bool writeLED(
const uint32_t rgb,
const bool verify =
true)
486 return writeLED(rgb >> 16, rgb >> 8, rgb & 0xFF);
496 bool writeLED(
const uint8_t r,
const uint8_t g,
const uint8_t b,
const bool verify =
true);
515 return _button & thermal2::button_is_pressed;
524 return _button & thermal2::button_was_pressed;
533 return _button & thermal2::button_was_released;
542 return _button & thermal2::button_was_clicked;
551 return _button & thermal2::button_was_hold;
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);
594 bool read_data_status(uint8_t s[2]);
598 bool start_periodic_measurement();
599 bool stop_periodic_measurement();
601 inline bool read_register(
const uint8_t reg, uint8_t* v,
const uint32_t len)
603 return readRegister(reg, v, len, 0,
false );
606 inline bool read_register8(
const uint8_t reg, uint8_t& v)
608 return readRegister8(reg, v, 0,
false );
611 inline bool read_register16LE(
const uint8_t reg, uint16_t& v)
613 return readRegister16LE(reg, v, 0,
false );
616 inline bool read_register16BE(
const uint8_t reg, uint16_t& v)
618 return readRegister16BE(reg, v, 0,
false );
621 M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitThermal2, thermal2::Data);
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{};
634constexpr uint8_t BUTTON_STATUS_REG{0x00};
635constexpr uint8_t TEMPERATURE_ALARM_STATUS_REG{0x01};
637constexpr uint8_t DEVICE_ID_REG{0x04};
638constexpr uint8_t FIRMWARE_VERSION_REG{0x06};
639constexpr uint8_t I2C_ADDRESS_REG{0x08};
640constexpr uint8_t FUNCTION_CONTROL_REG{0x0A};
641constexpr uint8_t REFRESH_RATE_CONFIG_REG{0x0B};
642constexpr uint8_t NOISE_FILTER_CONFIG_REG{0x0C};
644constexpr uint8_t TEMPERATURE_MONITOR_SIZE_REG{0x10};
645constexpr uint8_t ENABLE_TEMPERATURE_ALARM_REG{0x11};
646constexpr uint8_t BUZZER_FREQ_REG{0x12};
647constexpr uint8_t BUZZER_DUTY_REG{0x14};
648constexpr uint8_t LED_REG{0x15};
650constexpr uint8_t LOW_ALARM_THERSHOLD_REG{0x20};
651constexpr uint8_t LOW_ALARM_BUZZER_FREQ_REG{0x22};
652constexpr uint8_t LOW_ALARM_INTERVAL_REG{0x24};
653constexpr uint8_t LOW_ALARM_LED_REG{0x25};
655constexpr uint8_t HIGH_ALARM_THERSHOLD_REG{0x30};
656constexpr uint8_t HIGH_ALARM_BUZZER_FREQ_REG{0x32};
657constexpr uint8_t HIGH_ALARM_INTERVAL_REG{0x34};
658constexpr uint8_t HIGH_ALARM_LED_REG{0x35};
660constexpr uint8_t DATA_REFRESH_CONTROL_REG{0x6E};
661constexpr uint8_t SUB_PAGE_INFORMATION_REG{0x6F};
663constexpr uint8_t MEDIAN_TEPERATURE_REG{0x70};
664constexpr uint8_t AVERAGE_TEPERATURE_REG{0x72};
665constexpr uint8_t MOST_DIFF_TEPERATURE_REG{0x74};
666constexpr uint8_t MOST_DIFF_X_POS_REG{0x76};
667constexpr uint8_t MOST_DIFF_Y_POS_REG{0x77};
668constexpr uint8_t LOWEST_TEPERATURE_REG{0x78};
669constexpr uint8_t LOWEST_DIFF_X_POS_REG{0x7A};
670constexpr uint8_t LOWEST_DIFF_Y_POS_REG{0x7B};
671constexpr uint8_t HIGHEST_TEPERATURE_REG{0x7C};
672constexpr uint8_t HIGHEST_DIFF_X_POS_REG{0x7E};
673constexpr uint8_t HIGHEST_DIFF_Y_POS_REG{0x7F};
675constexpr uint8_t TEMPERATURE_DATA_REG{0x80};
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.
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