10#ifndef M5_UNIT_THERMO_UNIT_NCIR2_HPP
11#define M5_UNIT_THERMO_UNIT_NCIR2_HPP
13#include <M5UnitComponent.hpp>
14#include <m5_utility/container/circular_buffer.hpp>
32 std::array<uint8_t, 2> raw{0x00, 0x80};
36 return (raw[1] << 8 | raw[0]);
41 return value() * 0.01f;
51 return celsius() * 9.0f / 5.0f + 32.f;
60class UnitNCIR2 :
public Component,
public PeriodicMeasurementAdapter<UnitNCIR2, ncir2::Data> {
61 M5_UNIT_COMPONENT_HPP_BUILDER(UnitNCIR2, 0x5A);
77 explicit UnitNCIR2(
const uint8_t addr = DEFAULT_ADDRESS)
78 : Component(addr), _data{new
m5::container::CircularBuffer<
ncir2::Data>(1)}
80 auto ccfg = component_config();
81 ccfg.clock = 100 * 1000U;
82 component_config(ccfg);
88 virtual bool begin()
override;
89 virtual void update(
const bool force =
false)
override;
113 return !empty() ? oldest().temperature() : std::numeric_limits<float>::quiet_NaN();
121 return !empty() ? oldest().celsius() : std::numeric_limits<float>::quiet_NaN();
129 return !empty() ? oldest().fahrenheit() : std::numeric_limits<float>::quiet_NaN();
142 return PeriodicMeasurementAdapter<UnitNCIR2, ncir2::Data>::startPeriodicMeasurement(interval);
147 return PeriodicMeasurementAdapter<UnitNCIR2, ncir2::Data>::startPeriodicMeasurement();
155 return PeriodicMeasurementAdapter<UnitNCIR2, ncir2::Data>::stopPeriodicMeasurement();
196 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type =
nullptr>
199 return write_emissivity(
static_cast<float>(e));
234 template <typename T, typename std::enable_if<std::is_floating_point<T>::value, std::nullptr_t>::type =
nullptr>
237 return write_alarm_temperature(highlow,
static_cast<float>(
celsius));
255 return writeAlarmLED(highlow, rgb >> 16, rgb >> 8, rgb & 0xFF);
265 bool writeAlarmLED(
const bool highlow,
const uint8_t r,
const uint8_t g,
const uint8_t b);
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)
305 return write_alarm_buzzer(highlow, freq, interval,
static_cast<float>(duty));
317 bool readBuzzer(uint16_t& freq, uint8_t& rawDuty);
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>
342 return write_buzzer(freq,
static_cast<float>(duty));
374 return writeLED(rgb >> 16, rgb >> 8, rgb & 0xFF);
383 bool writeLED(
const uint8_t r,
const uint8_t g,
const uint8_t b);
431 return _button && (_button != _prev_button);
440 return !_button && (_button != _prev_button);
470 bool start_periodic_measurement(
const uint32_t interval);
471 bool start_periodic_measurement();
472 bool stop_periodic_measurement();
474 bool read_temperature(
const uint8_t reg, uint8_t v[2]);
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);
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{};
494constexpr uint8_t TEMPERATURE_REG{0x00};
495constexpr uint8_t EMISSIVITY_REG{0x10};
496constexpr uint8_t ALARM_TEMPERATURE_REG{0x20};
497constexpr uint8_t ALARM_LED_REG{0x30};
498constexpr uint8_t ALARM_BUZZER_REG{0x40};
499constexpr uint8_t ALARM_BUZZER_LOW_FREQ_REG{0x40};
500constexpr uint8_t ALARM_BUZZER_LOW_INTERVAL_REG{0x42};
501constexpr uint8_t ALARM_BUZZER_LOW_DUTY_REG{0x44};
502constexpr uint8_t ALARM_BUZZER_HIGH_FREQ_REG{0x45};
503constexpr uint8_t ALARM_BUZZER_HIGH_INTERVAL_REG{0x47};
504constexpr uint8_t ALARM_BUZZER_HIGH_DUTY_REG{0x48};
505constexpr uint8_t BUZZER_REG{0x50};
506constexpr uint8_t BUZZER_FREQ_REG{0x50};
507constexpr uint8_t BUZZER_DUTY_REG{0x52};
508constexpr uint8_t BUZZER_CONTROL_REG{0x53};
509constexpr uint8_t LED_REG{0x60};
510constexpr uint8_t BUTTON_REG{0x70};
511constexpr uint8_t SAVE_CONFIG_REG{0x80};
512constexpr uint8_t CHIP_TEMPERATURE_REG{0x90};
513constexpr uint8_t FIRMWARE_VERSION_REG{0xFE};
514constexpr uint8_t I2C_ADDRESS_REG{0xFF};
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.
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