M5Unit-THERMO 0.2.0 git rev:e387a6b
Loading...
Searching...
No Matches
unit_NCIR2.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_NCIR2_HPP
11#define M5_UNIT_THERMO_UNIT_NCIR2_HPP
12
13#include <M5UnitComponent.hpp>
14#include <m5_utility/container/circular_buffer.hpp>
15#include <limits> // NaN
16#include <array>
17
18namespace m5 {
19namespace unit {
20
25namespace ncir2 {
31struct Data {
32 std::array<uint8_t, 2> raw{0x00, 0x80}; // Raw [0]:low byte [1]:high byte
34 inline int16_t value() const
35 {
36 return (raw[1] << 8 | raw[0]);
37 }
39 inline float temperature() const
40 {
41 return value() * 0.01f;
42 }
44 inline float celsius() const
45 {
46 return temperature();
47 }
49 inline float fahrenheit() const
50 {
51 return celsius() * 9.0f / 5.0f + 32.f;
52 }
53};
54} // namespace ncir2
55
60class UnitNCIR2 : public Component, public PeriodicMeasurementAdapter<UnitNCIR2, ncir2::Data> {
61 M5_UNIT_COMPONENT_HPP_BUILDER(UnitNCIR2, 0x5A);
62
63public:
68 struct config_t {
70 bool start_periodic{true};
72 uint32_t interval{250};
74 uint32_t button_interval{20};
75 };
76
77 explicit UnitNCIR2(const uint8_t addr = DEFAULT_ADDRESS)
78 : Component(addr), _data{new m5::container::CircularBuffer<ncir2::Data>(1)}
79 {
80 auto ccfg = component_config();
81 ccfg.clock = 100 * 1000U;
82 component_config(ccfg);
83 }
84 virtual ~UnitNCIR2()
85 {
86 }
87
88 virtual bool begin() override;
89 virtual void update(const bool force = false) override;
90
93
95 {
96 return _cfg;
97 }
99 inline void config(const config_t& cfg)
100 {
101 _cfg = cfg;
102 }
104
107
111 inline float temperature() const
112 {
113 return !empty() ? oldest().temperature() : std::numeric_limits<float>::quiet_NaN();
114 }
119 inline float celsius() const
120 {
121 return !empty() ? oldest().celsius() : std::numeric_limits<float>::quiet_NaN();
122 }
127 inline float fahrenheit() const
128 {
129 return !empty() ? oldest().fahrenheit() : std::numeric_limits<float>::quiet_NaN();
130 }
132
135
140 inline bool startPeriodicMeasurement(const uint32_t interval)
141 {
142 return PeriodicMeasurementAdapter<UnitNCIR2, ncir2::Data>::startPeriodicMeasurement(interval);
143 }
146 {
147 return PeriodicMeasurementAdapter<UnitNCIR2, ncir2::Data>::startPeriodicMeasurement();
148 }
154 {
155 return PeriodicMeasurementAdapter<UnitNCIR2, ncir2::Data>::stopPeriodicMeasurement();
156 }
158
161
170
173
178 bool readEmissivity(uint16_t& raw);
184 bool readEmissivity(float& e);
190 bool writeEmissivity(const uint16_t raw);
196 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
197 inline bool writeEmissivity(const T e)
198 {
199 return write_emissivity(static_cast<float>(e));
200 }
202
205
211 bool readAlarmTemperature(const bool highlow, int16_t& raw);
219 bool readAlarmTemperature(const bool highlow, float& celsius);
226 bool writeAlarmTemperature(const bool highlow, const int16_t raw);
234 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
235 inline bool writeAlarmTemperature(const bool highlow, const T celsius)
236 {
237 return write_alarm_temperature(highlow, static_cast<float>(celsius));
238 }
239
246 bool readAlarmLED(const bool highlow, uint32_t& rgb);
253 inline bool writeAlarmLED(const bool highlow, const uint32_t rgb)
254 {
255 return writeAlarmLED(highlow, rgb >> 16, rgb >> 8, rgb & 0xFF);
256 }
265 bool writeAlarmLED(const bool highlow, const uint8_t r, const uint8_t g, const uint8_t b);
266
275 bool readAlarmBuzzer(const bool highlow, uint16_t& freq, uint16_t& interval, uint8_t& rawDuty);
284 bool readAlarmBuzzer(const bool highlow, uint16_t& freq, uint16_t& interval, float& duty);
293 bool writeAlarmBuzzer(const bool highlow, const uint16_t freq, const uint16_t interval, const uint8_t rawDuty);
302 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
303 inline bool writeAlarmBuzzer(const bool highlow, const uint16_t freq, const uint16_t interval, const T duty)
304 {
305 return write_alarm_buzzer(highlow, freq, interval, static_cast<float>(duty));
306 }
308
311
317 bool readBuzzer(uint16_t& freq, uint8_t& rawDuty);
324 bool readBuzzer(uint16_t& freq, float& duty);
325
332 bool writeBuzzer(const uint16_t freq, const uint8_t rawDuty);
339 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type = nullptr>
340 inline bool writeBuzzer(const uint16_t freq, const T duty)
341 {
342 return write_buzzer(freq, static_cast<float>(duty));
343 }
344
350 bool readBuzzerControl(bool& enabled);
356 bool writeBuzzerControl(const bool enabled);
358
361
366 bool readLED(uint32_t& rgb);
372 inline bool writeLED(const uint32_t rgb)
373 {
374 return writeLED(rgb >> 16, rgb >> 8, rgb & 0xFF);
375 }
383 bool writeLED(const uint8_t r, const uint8_t g, const uint8_t b);
385
388
396
399
404 bool writeConfig();
406
409
414 bool readButtonStatus(bool& press);
420 inline bool isPressed() const
421 {
422 return _button;
423 }
429 inline bool wasPressed() const
430 {
431 return _button && (_button != _prev_button);
432 }
438 inline bool wasReleased()
439 {
440 return !_button && (_button != _prev_button);
441 }
443
449 bool readFirmwareVersion(uint8_t& ver);
450
454
460 bool changeI2CAddress(const uint8_t i2c_address);
466 bool readI2CAddress(uint8_t& i2c_address);
468
469protected:
470 bool start_periodic_measurement(const uint32_t interval);
471 bool start_periodic_measurement();
472 bool stop_periodic_measurement();
473
474 bool read_temperature(const uint8_t reg, uint8_t v[2]);
475
476 bool write_emissivity(const float e);
477 bool write_alarm_temperature(const bool highlow, const float celsius);
478 bool write_alarm_buzzer(const bool highlow, const uint16_t freq, const uint16_t interval, const float duty);
479 bool write_buzzer(const uint16_t freq, const float duty);
480
481 M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitNCIR2, ncir2::Data);
482
483private:
484 std::unique_ptr<m5::container::CircularBuffer<ncir2::Data>> _data{};
485 bool _button{}, _prev_button{};
486 uint32_t _button_interval{20};
487 types::elapsed_time_t _latest_button{};
488 config_t _cfg{};
489};
490
491namespace ncir2 {
493namespace command {
494constexpr uint8_t TEMPERATURE_REG{0x00}; // R
495constexpr uint8_t EMISSIVITY_REG{0x10}; // R/W
496constexpr uint8_t ALARM_TEMPERATURE_REG{0x20}; // R/W
497constexpr uint8_t ALARM_LED_REG{0x30}; // R/W
498constexpr uint8_t ALARM_BUZZER_REG{0x40}; // W
499constexpr uint8_t ALARM_BUZZER_LOW_FREQ_REG{0x40}; // R
500constexpr uint8_t ALARM_BUZZER_LOW_INTERVAL_REG{0x42}; // R
501constexpr uint8_t ALARM_BUZZER_LOW_DUTY_REG{0x44}; // R
502constexpr uint8_t ALARM_BUZZER_HIGH_FREQ_REG{0x45}; // R
503constexpr uint8_t ALARM_BUZZER_HIGH_INTERVAL_REG{0x47}; // R
504constexpr uint8_t ALARM_BUZZER_HIGH_DUTY_REG{0x48}; // R
505constexpr uint8_t BUZZER_REG{0x50}; // W
506constexpr uint8_t BUZZER_FREQ_REG{0x50}; // R
507constexpr uint8_t BUZZER_DUTY_REG{0x52}; // R
508constexpr uint8_t BUZZER_CONTROL_REG{0x53}; // R/W
509constexpr uint8_t LED_REG{0x60}; // R/W
510constexpr uint8_t BUTTON_REG{0x70}; // R
511constexpr uint8_t SAVE_CONFIG_REG{0x80}; // W
512constexpr uint8_t CHIP_TEMPERATURE_REG{0x90}; // R
513constexpr uint8_t FIRMWARE_VERSION_REG{0xFE}; // R
514constexpr uint8_t I2C_ADDRESS_REG{0xFF}; // R/W
515} // namespace command
517} // namespace ncir2
518
519} // namespace unit
520} // namespace m5
521
522#endif
bool startPeriodicMeasurement()
Start periodic measurement in the current settings.
Definition unit_NCIR2.hpp:145
bool readEmissivity(uint16_t &raw)
Read the emissivity.
Definition unit_NCIR2.cpp:117
bool writeBuzzer(const uint16_t freq, const uint8_t rawDuty)
Write the buzzer settings.
Definition unit_NCIR2.cpp:281
bool writeConfig()
Write configuration to inner flash.
Definition unit_NCIR2.cpp:337
bool wasPressed() const
Was button pressed?
Definition unit_NCIR2.hpp:429
bool writeAlarmLED(const bool highlow, const uint32_t rgb)
Write the alarm LED color.
Definition unit_NCIR2.hpp:253
bool wasReleased()
Was button released?
Definition unit_NCIR2.hpp:438
bool readChipTemperature(ncir2::Data &d)
Read the Chip temperature.
Definition unit_NCIR2.cpp:332
config_t config()
Gets the configration.
Definition unit_NCIR2.hpp:94
bool writeAlarmTemperature(const bool highlow, const int16_t raw)
Write the alarm temperature threshold.
Definition unit_NCIR2.cpp:175
bool readAlarmTemperature(const bool highlow, int16_t &raw)
Read the alarm temperature threshold.
Definition unit_NCIR2.cpp:150
bool readBuzzer(uint16_t &freq, uint8_t &rawDuty)
Read the buzzer settings.
Definition unit_NCIR2.cpp:263
float fahrenheit() const
Oldest fahrenheit.
Definition unit_NCIR2.hpp:127
bool stopPeriodicMeasurement()
Stop periodic measurement.
Definition unit_NCIR2.hpp:153
bool readI2CAddress(uint8_t &i2c_address)
Read device I2C address.
Definition unit_NCIR2.cpp:378
bool measureSingleshot(ncir2::Data &d)
Measurement single shot.
Definition unit_NCIR2.cpp:108
float celsius() const
Oldest celsius.
Definition unit_NCIR2.hpp:119
bool readFirmwareVersion(uint8_t &ver)
Read the firmware version.
Definition unit_NCIR2.cpp:353
bool readButtonStatus(bool &press)
Read the button status.
Definition unit_NCIR2.cpp:342
bool writeEmissivity(const T e)
Write the emissivity.
Definition unit_NCIR2.hpp:197
bool readAlarmLED(const bool highlow, uint32_t &rgb)
Read the alarm LED color.
Definition unit_NCIR2.cpp:194
bool writeBuzzerControl(const bool enabled)
Write the Buzzer control.
Definition unit_NCIR2.cpp:310
bool writeAlarmBuzzer(const bool highlow, const uint16_t freq, const uint16_t interval, const uint8_t rawDuty)
Write the alarm buzzer settings.
Definition unit_NCIR2.cpp:236
bool readBuzzerControl(bool &enabled)
Read the Buzzer control.
Definition unit_NCIR2.cpp:299
bool startPeriodicMeasurement(const uint32_t interval)
Start periodic measurement.
Definition unit_NCIR2.hpp:140
bool readLED(uint32_t &rgb)
Read the LED color.
Definition unit_NCIR2.cpp:315
bool writeBuzzer(const uint16_t freq, const T duty)
Write the buzzer settings.
Definition unit_NCIR2.hpp:340
bool writeAlarmBuzzer(const bool highlow, const uint16_t freq, const uint16_t interval, const T duty)
Write the alarm buzzer settings.
Definition unit_NCIR2.hpp:303
bool writeLED(const uint32_t rgb)
Write the LED color.
Definition unit_NCIR2.hpp:372
void config(const config_t &cfg)
Set the configration.
Definition unit_NCIR2.hpp:99
float temperature() const
Oldest temperature.
Definition unit_NCIR2.hpp:111
bool isPressed() const
Is button pressed?
Definition unit_NCIR2.hpp:420
bool writeEmissivity(const uint16_t raw)
Write the emissivity.
Definition unit_NCIR2.cpp:135
bool writeAlarmTemperature(const bool highlow, const T celsius)
Write the alarm temperature threshold.
Definition unit_NCIR2.hpp:235
bool readAlarmBuzzer(const bool highlow, uint16_t &freq, uint16_t &interval, uint8_t &rawDuty)
Read the alarm buzzer settings.
Definition unit_NCIR2.cpp:215
bool changeI2CAddress(const uint8_t i2c_address)
Change device I2C address.
Definition unit_NCIR2.cpp:358
Top level namespace of M5stack.
For UnitNCIR2.
Unit-related namespace.
Settings for begin.
Definition unit_NCIR2.hpp:68
bool start_periodic
Start periodic measurement on begin?
Definition unit_NCIR2.hpp:70
uint32_t button_interval
Button status update interval(ms)
Definition unit_NCIR2.hpp:74
uint32_t interval
Measurement interval if start on begin (ms)
Definition unit_NCIR2.hpp:72
Measurement data group.
Definition unit_NCIR2.hpp:31
float fahrenheit() const
Fahrenheit.
Definition unit_NCIR2.hpp:49
float celsius() const
Celsius.
Definition unit_NCIR2.hpp:44
float temperature() const
Temperature(Celsius)
Definition unit_NCIR2.hpp:39
int16_t value() const
Raw int16.
Definition unit_NCIR2.hpp:34