10#ifndef M5_UNIT_THERMO_UNIT_MLX90614_HPP
11#define M5_UNIT_THERMO_UNIT_MLX90614_HPP
13#include <M5UnitComponent.hpp>
14#include <m5_utility/container/circular_buffer.hpp>
42enum class IIR : uint8_t {
57enum class FIR : uint8_t {
97 std::array<uint16_t, 3> raw{};
99 inline float ambientKelvin()
const
101 return ((raw[0] & 0x8000) == 0) ? raw[0] * 0.02f : std::numeric_limits<float>::quiet_NaN();
103 inline float ambientTemperature()
const
105 return ambientCelsius();
107 inline float ambientCelsius()
const
109 return ambientKelvin() - 273.15f;
111 inline float ambientFahrenheit()
const
113 return ambientCelsius() * 9.0f / 5.0f + 32.f;
116 inline float objectKelvin1()
const
118 return ((raw[1] & 0x8000) == 0) ? raw[1] * 0.02f : std::numeric_limits<float>::quiet_NaN();
120 inline float objectTemperature1()
const
122 return objectCelsius1();
124 inline float objectCelsius1()
const
126 return objectKelvin1() - 273.15f;
128 inline float objectFahrenheit1()
const
130 return objectCelsius1() * 9.0f / 5.0f + 32.f;
133 inline float objectKelvin2()
const
135 return ((raw[2] & 0x8000) == 0) ? raw[2] * 0.02f : std::numeric_limits<float>::quiet_NaN();
137 inline float objectTemperature2()
const
139 return objectCelsius2();
141 inline float objectCelsius2()
const
143 return objectKelvin2() - 273.15f;
145 inline float objectFahrenheit2()
const
147 return objectCelsius2() * 9.0f / 5.0f + 32.f;
174class UnitMLX90614 :
public Component,
public PeriodicMeasurementAdapter<UnitMLX90614, mlx90614::Data> {
175 M5_UNIT_COMPONENT_HPP_BUILDER(UnitMLX90614, 0x5A);
197 explicit UnitMLX90614(
const uint8_t addr = DEFAULT_ADDRESS)
198 : Component(addr), _data{new
m5::container::CircularBuffer<
mlx90614::Data>(1)}
200 auto ccfg = component_config();
201 ccfg.clock = 100 * 1000U;
202 component_config(ccfg);
204 virtual ~UnitMLX90614()
208 virtual bool begin()
override;
209 virtual void update(
const bool force =
false)
override;
236 return !empty() ? oldest().ambientKelvin() : std::numeric_limits<float>::quiet_NaN();
241 return !empty() ? oldest().ambientTemperature() : std::numeric_limits<float>::quiet_NaN();
246 return !empty() ? oldest().ambientCelsius() : std::numeric_limits<float>::quiet_NaN();
251 return !empty() ? oldest().ambientFahrenheit() : std::numeric_limits<float>::quiet_NaN();
256 return !empty() ? oldest().objectKelvin1() : std::numeric_limits<float>::quiet_NaN();
261 return !empty() ? oldest().objectTemperature1() : std::numeric_limits<float>::quiet_NaN();
266 return !empty() ? oldest().objectCelsius1() : std::numeric_limits<float>::quiet_NaN();
271 return !empty() ? oldest().objectFahrenheit1() : std::numeric_limits<float>::quiet_NaN();
276 return !empty() ? oldest().objectKelvin2() : std::numeric_limits<float>::quiet_NaN();
281 return !empty() ? oldest().objectTemperature2() : std::numeric_limits<float>::quiet_NaN();
286 return !empty() ? oldest().objectCelsius2() : std::numeric_limits<float>::quiet_NaN();
291 return !empty() ? oldest().objectFahrenheit2() : std::numeric_limits<float>::quiet_NaN();
308 return PeriodicMeasurementAdapter<UnitMLX90614, mlx90614::Data>::startPeriodicMeasurement(iir, fir, gain, irs);
313 return PeriodicMeasurementAdapter<UnitMLX90614, mlx90614::Data>::startPeriodicMeasurement();
321 return PeriodicMeasurementAdapter<UnitMLX90614, mlx90614::Data>::stopPeriodicMeasurement();
342 bool writeConfig(
const uint16_t v,
const bool apply =
true);
471 template <typename T, typename std::enable_if<std::is_integral<T>::value, std::nullptr_t>::type =
nullptr>
474 return write_object_minmax((uint16_t)toMin, (uint16_t)toMax, apply);
484 bool writeObjectMinMax(
const float toMin,
const float toMax,
const bool apply =
true);
506 template <typename T, typename std::enable_if<std::is_integral<T>::value, std::nullptr_t>::type =
nullptr>
509 return write_ambient_minmax((uint8_t)taMin, (uint8_t)taMax, apply);
519 bool writeAmbientMinMax(
const float taMin,
const float taMax,
const bool apply =
true);
544 template <typename T, typename std::enable_if<std::is_integral<T>::value, std::nullptr_t>::type =
nullptr>
547 return write_emissivity((uint16_t)emiss, apply);
598 bool read_register16(
const uint8_t reg, uint16_t& v,
const bool stopbit =
false);
599 bool write_register16(
const uint8_t reg,
const uint16_t val);
600 bool write_eeprom(
const uint8_t reg,
const uint16_t val,
const bool apply =
true);
601 bool write_object_minmax(
const uint16_t toMin,
const uint16_t toMax,
const bool apply =
true);
602 bool write_ambient_minmax(
const uint8_t taMin,
const uint8_t taMax,
const bool apply =
true);
603 bool write_emissivity(
const uint16_t emiss,
const bool apply =
true);
608 bool start_periodic_measurement();
609 bool stop_periodic_measurement();
614 virtual bool has_dual_sensors()
const
620 std::unique_ptr<m5::container::CircularBuffer<mlx90614::Data>> _data{};
621 mlx90614::EEPROM _eeprom{};
639 inline virtual bool has_dual_sensors()
const
649constexpr uint8_t COMMAND_RAM{0x00};
650constexpr uint8_t COMMAND_EEPROM{0x20};
651constexpr uint8_t COMMAND_READ_FLAGS{0xF0};
652constexpr uint8_t COMMAND_ENTER_SLEEP{0xFF};
654constexpr uint8_t READ_RAW_AMBIENT{0x03};
655constexpr uint8_t READ_RAW_IR1{0x04};
656constexpr uint8_t READ_RAW_IR2{0x05};
657constexpr uint8_t READ_TAMBIENT{0x06};
658constexpr uint8_t READ_TOBJECT_1{0x07};
659constexpr uint8_t READ_TOBJECT_2{0x08};
661constexpr uint8_t EEPROM_TO_MAX{0x20};
662constexpr uint8_t EEPROM_TO_MIN{0x21};
663constexpr uint8_t EEPROM_PWMCTRL{0x22};
664constexpr uint8_t EEPROM_TARANGE{0x23};
665constexpr uint8_t EEPROM_EMISSIVITY{0x24};
666constexpr uint8_t EEPROM_CONFIG{0x25};
667constexpr uint8_t EEPROM_ADDR{0x2E};
668constexpr uint8_t EEPROM_ID0{0x3C};
669constexpr uint8_t EEPROM_ID1{0x3D};
670constexpr uint8_t EEPROM_ID2{0x3E};
671constexpr uint8_t EEPROM_ID3{0x3F};
For UnitMLX90614BAA (NCIR using it)
Definition unit_MLX90614.hpp:629
Base class of the UnitMLX90614 series.
bool writeConfig(const uint16_t v, const bool apply=true)
Write the configuration.
Definition unit_MLX90614.cpp:293
bool startPeriodicMeasurement()
Start periodic measurement in the current settings.
Definition unit_MLX90614.hpp:311
float ambientKelvin() const
Oldest ambient kelvin.
Definition unit_MLX90614.hpp:234
const mlx90614::EEPROM & eeprom() const
Gets the EEPROM structure.
Definition unit_MLX90614.hpp:226
float objectFahrenheit1() const
Oldest object 1 temperature (Fahrenheit)
Definition unit_MLX90614.hpp:269
bool stopPeriodicMeasurement()
Stop periodic measurement.
Definition unit_MLX90614.hpp:319
float ambientTemperature() const
Oldest ambient temperature (Celsius)
Definition unit_MLX90614.hpp:239
bool readPositiveKs(bool &pos)
Read the positiveKs.
Definition unit_MLX90614.cpp:413
float objectTemperature2() const
Oldest object 2 temperature (Celsius)
Definition unit_MLX90614.hpp:279
bool startPeriodicMeasurement(const mlx90614::IIR iir, const mlx90614::FIR fir, const mlx90614::Gain gain, const mlx90614::IRSensor irs)
Start periodic measurement.
Definition unit_MLX90614.hpp:305
config_t config()
Gets the configration.
Definition unit_MLX90614.hpp:214
bool readEmissivity(uint16_t &emiss)
Read the emissivity.
Definition unit_MLX90614.cpp:540
bool readGain(mlx90614::Gain &gain)
Read the Gain.
Definition unit_MLX90614.cpp:373
float objectCelsius1() const
Oldest object 1 temperature (Celsius)
Definition unit_MLX90614.hpp:264
bool applySettings()
Apply EEPROM settings.
Definition unit_MLX90614.hpp:591
bool readConfig(uint16_t &v)
Read the configuration.
Definition unit_MLX90614.cpp:288
bool wakeup()
Wakeup.
Definition unit_MLX90614.cpp:630
bool writeEmissivity(const T emiss, const bool apply=true)
Write the emissivity.
Definition unit_MLX90614.hpp:545
bool readOutput(mlx90614::Output &o)
Read the Output.
Definition unit_MLX90614.cpp:307
bool writeFIR(const mlx90614::FIR fir, const bool apply=true)
Write the FIR.
Definition unit_MLX90614.cpp:357
float objectKelvin1() const
Oldest object 1 kelvin.
Definition unit_MLX90614.hpp:254
float objectCelsius2() const
Oldest object 2 temperature (Celsius)
Definition unit_MLX90614.hpp:284
bool writeGain(const mlx90614::Gain gain, const bool apply=true)
Write the Gain.
Definition unit_MLX90614.cpp:383
bool readIRSensor(mlx90614::IRSensor &irs)
Read the IRSensor mode.
Definition unit_MLX90614.cpp:393
bool writeOutput(const mlx90614::Output o, const bool apply=true)
Write the Output.
Definition unit_MLX90614.cpp:317
float ambientCelsius() const
Oldest ambient temperature (Celsius)
Definition unit_MLX90614.hpp:244
bool readIIR(mlx90614::IIR &iir)
Read the IIR.
Definition unit_MLX90614.cpp:327
bool writeIRSensor(const mlx90614::IRSensor irs, const bool apply=true)
Write the IRSensor mode.
Definition unit_MLX90614.cpp:403
bool sleep()
Sleep.
Definition unit_MLX90614.cpp:598
float ambientFahrenheit() const
Oldest ambient temperature (Fahrenheit)
Definition unit_MLX90614.hpp:249
bool readPositiveKf2(bool &pos)
Read the positiveKf2.
Definition unit_MLX90614.cpp:433
bool writeObjectMinMax(const T toMin, const T toMax, const bool apply=true)
Write the minimum and maximum temperatures of the measurement for the object.
Definition unit_MLX90614.hpp:472
bool writePositiveKf2(const bool pos, const bool apply=true)
Write the positiveKf2.
Definition unit_MLX90614.cpp:443
bool readI2CAddress(uint8_t &i2c_address)
Read device I2C address.
Definition unit_MLX90614.cpp:578
bool readObjectMinMax(uint16_t &toMin, uint16_t &toMax)
Read the minimum and maximum temperatures of the measurement for the object.
Definition unit_MLX90614.cpp:453
float objectKelvin2() const
Oldest object 2 kelvin.
Definition unit_MLX90614.hpp:274
float objectFahrenheit2() const
Oldest object 2 temperature (Fahrenheit)
Definition unit_MLX90614.hpp:289
bool writeAmbientMinMax(const T taMin, const T taMax, const bool apply=true)
Write the minimum and maximum temperatures of the measurement for the ambient.
Definition unit_MLX90614.hpp:507
float objectTemperature1() const
Oldest object 1 temperature (Celsius)
Definition unit_MLX90614.hpp:259
void config(const config_t &cfg)
Set the configration.
Definition unit_MLX90614.hpp:219
bool readAmbientMinMax(uint8_t &taMin, uint8_t &taMax)
Read the minimum and maximum temperatures of the measurement for the ambient.
Definition unit_MLX90614.cpp:493
bool changeI2CAddress(const uint8_t i2c_address)
Change device I2C address.
Definition unit_MLX90614.cpp:588
bool writeIIR(const mlx90614::IIR iir, const bool apply=true)
Write the IIR.
Definition unit_MLX90614.cpp:337
bool readFIR(mlx90614::FIR &fir)
Read the FIR.
Definition unit_MLX90614.cpp:347
bool writePositiveKs(const bool pos, const bool apply=true)
Write the positiveKs.
Definition unit_MLX90614.cpp:423
Top level namespace of M5stack.
Settings for begin.
Definition unit_MLX90614.hpp:182
float emissivity
Emissivity if start on begin.
Definition unit_MLX90614.hpp:194
mlx90614::Gain gain
Gain if start on begin.
Definition unit_MLX90614.hpp:190
mlx90614::IIR iir
IIR filter if start on begin.
Definition unit_MLX90614.hpp:186
mlx90614::FIR fir
FIR filter if start on begin.
Definition unit_MLX90614.hpp:188
mlx90614::IRSensor irs
IRSensor if start on begin.
Definition unit_MLX90614.hpp:192
bool start_periodic
Start periodic measurement on begin?
Definition unit_MLX90614.hpp:184
Measurement data group.
Definition unit_MLX90614.hpp:96
EEPROM values.
Definition unit_MLX90614.hpp:155
uint16_t pwmCtrl
Pulse With Modulation control.
Definition unit_MLX90614.hpp:157
uint16_t addr
I2C address(Using low byte)
Definition unit_MLX90614.hpp:161
uint16_t config
Configuration.
Definition unit_MLX90614.hpp:160
uint16_t toMin
Max,Min of the Object Temperature.
Definition unit_MLX90614.hpp:156
uint16_t taRange
Range of the Ambient Temperature (H/L)
Definition unit_MLX90614.hpp:158
uint16_t emissivity
Emissivity.
Definition unit_MLX90614.hpp:159
IRSensor
Infra-Red Sensor mode.
Definition unit_MLX90614.hpp:87
@ Single
Single IR Sensor.
IIR
Infinite Impulse Response.
Definition unit_MLX90614.hpp:42
@ Filter25
25% (a1 = 0.25, b1 = 0.75)
@ Filter50
50% (a1 = 0.5, b1 = 0.5)
@ Filter67
67% (a1 = 0.666, b1 = 0.333)
@ Filter80
80% (a1 = 0.8, b1 = 0.2)
@ Filter13
13% (a1 = 0.125, b1 = 0.875)
@ Filter100
100% (a1 = 1, b1 = 0)
@ Filter57
57% (a1 = 0.571, b1 = 0.428)
@ Filter17
17% (a1 = 0x166(6), b1 = 0x83(3))
Gain
Amplifier gain.
Definition unit_MLX90614.hpp:73
FIR
Finite Impulse Response.
Definition unit_MLX90614.hpp:57
@ Filter8
8 Not recommended
@ Filter32
32 Not recommended
@ Filter16
16 Not recommended
@ Filter64
64 Not recommended
Output
PWM output mode.
Definition unit_MLX90614.hpp:31
@ TO1_TO2
PWM1: To1 PWM2:To2 (Object 1 & 2)
@ TA_TO1
PWM1: Ta PWM2:To1 (Ambient & Object 1)
@ TA_TO2
PWM1: Ta PWM2:To2 (Ambient & Object 1)
@ TO2_Undefined
PWM1: To2 PWM2:Undefined (Object 2)