M5Unit-THERMO 0.2.0 git rev:e387a6b
Loading...
Searching...
No Matches
unit_MLX90614.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_THERMO_UNIT_MLX90614_HPP
11#define M5_UNIT_THERMO_UNIT_MLX90614_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 mlx90614 {
26
31enum class Output : uint8_t {
32 TA_TO1,
33 TA_TO2,
35 TO1_TO2,
36};
37
42enum class IIR : uint8_t {
43 Filter50,
44 Filter25,
45 Filter17,
46 Filter13,
47 Filter100,
48 Filter80,
49 Filter67,
50 Filter57,
51};
52
57enum class FIR : uint8_t {
58 Filter8,
59 Filter16,
60 Filter32,
61 Filter64,
62 Filter128,
63 Filter256,
64 Filter512,
66
67};
68
73enum class Gain : uint8_t {
74 Coeff1,
75 Coeff3,
76 Coeff6,
77 Coeff12_5,
78 Coeff25,
79 Coeff50,
80 Coeff100,
81};
82
87enum class IRSensor : uint8_t {
88 Single,
89 Dual
90};
91
96struct Data {
97 std::array<uint16_t, 3> raw{}; // linearized raw [0]:Ambient [1]:Object1 [2]:Object2
98
99 inline float ambientKelvin() const
100 {
101 return ((raw[0] & 0x8000) == 0) ? raw[0] * 0.02f : std::numeric_limits<float>::quiet_NaN();
102 }
103 inline float ambientTemperature() const
104 {
105 return ambientCelsius();
106 }
107 inline float ambientCelsius() const
108 {
109 return ambientKelvin() - 273.15f;
110 }
111 inline float ambientFahrenheit() const
112 {
113 return ambientCelsius() * 9.0f / 5.0f + 32.f;
114 }
115
116 inline float objectKelvin1() const
117 {
118 return ((raw[1] & 0x8000) == 0) ? raw[1] * 0.02f : std::numeric_limits<float>::quiet_NaN();
119 }
120 inline float objectTemperature1() const
121 {
122 return objectCelsius1();
123 }
124 inline float objectCelsius1() const
125 {
126 return objectKelvin1() - 273.15f;
127 }
128 inline float objectFahrenheit1() const
129 {
130 return objectCelsius1() * 9.0f / 5.0f + 32.f;
131 }
132
133 inline float objectKelvin2() const
134 {
135 return ((raw[2] & 0x8000) == 0) ? raw[2] * 0.02f : std::numeric_limits<float>::quiet_NaN();
136 }
137 inline float objectTemperature2() const
138 {
139 return objectCelsius2();
140 }
141 inline float objectCelsius2() const
142 {
143 return objectKelvin2() - 273.15f;
144 }
145 inline float objectFahrenheit2() const
146 {
147 return objectCelsius2() * 9.0f / 5.0f + 32.f;
148 }
149};
150
155struct EEPROM {
156 uint16_t toMax{}, toMin{},
162 id[4]{};
163};
164
165} // namespace mlx90614
166
174class UnitMLX90614 : public Component, public PeriodicMeasurementAdapter<UnitMLX90614, mlx90614::Data> {
175 M5_UNIT_COMPONENT_HPP_BUILDER(UnitMLX90614, 0x5A);
176
177public:
182 struct config_t {
184 bool start_periodic{true};
186 mlx90614::IIR iir{mlx90614::IIR::Filter100};
188 mlx90614::FIR fir{mlx90614::FIR::Filter1024};
190 mlx90614::Gain gain{mlx90614::Gain::Coeff12_5};
192 mlx90614::IRSensor irs{mlx90614::IRSensor::Single};
194 float emissivity{1.0f};
195 };
196
197 explicit UnitMLX90614(const uint8_t addr = DEFAULT_ADDRESS)
198 : Component(addr), _data{new m5::container::CircularBuffer<mlx90614::Data>(1)}
199 {
200 auto ccfg = component_config();
201 ccfg.clock = 100 * 1000U;
202 component_config(ccfg);
203 }
204 virtual ~UnitMLX90614()
205 {
206 }
207
208 virtual bool begin() override;
209 virtual void update(const bool force = false) override;
210
213
215 {
216 return _cfg;
217 }
219 inline void config(const config_t& cfg)
220 {
221 _cfg = cfg;
222 }
224
227 {
228 return _eeprom;
229 }
230
234 inline float ambientKelvin() const
235 {
236 return !empty() ? oldest().ambientKelvin() : std::numeric_limits<float>::quiet_NaN();
237 }
239 inline float ambientTemperature() const
240 {
241 return !empty() ? oldest().ambientTemperature() : std::numeric_limits<float>::quiet_NaN();
242 }
244 inline float ambientCelsius() const
245 {
246 return !empty() ? oldest().ambientCelsius() : std::numeric_limits<float>::quiet_NaN();
247 }
249 inline float ambientFahrenheit() const
250 {
251 return !empty() ? oldest().ambientFahrenheit() : std::numeric_limits<float>::quiet_NaN();
252 }
254 inline float objectKelvin1() const
255 {
256 return !empty() ? oldest().objectKelvin1() : std::numeric_limits<float>::quiet_NaN();
257 }
259 inline float objectTemperature1() const
260 {
261 return !empty() ? oldest().objectTemperature1() : std::numeric_limits<float>::quiet_NaN();
262 }
264 inline float objectCelsius1() const
265 {
266 return !empty() ? oldest().objectCelsius1() : std::numeric_limits<float>::quiet_NaN();
267 }
269 inline float objectFahrenheit1() const
270 {
271 return !empty() ? oldest().objectFahrenheit1() : std::numeric_limits<float>::quiet_NaN();
272 }
274 inline float objectKelvin2() const
275 {
276 return !empty() ? oldest().objectKelvin2() : std::numeric_limits<float>::quiet_NaN();
277 }
279 inline float objectTemperature2() const
280 {
281 return !empty() ? oldest().objectTemperature2() : std::numeric_limits<float>::quiet_NaN();
282 }
284 inline float objectCelsius2() const
285 {
286 return !empty() ? oldest().objectCelsius2() : std::numeric_limits<float>::quiet_NaN();
287 }
289 inline float objectFahrenheit2() const
290 {
291 return !empty() ? oldest().objectFahrenheit2() : std::numeric_limits<float>::quiet_NaN();
292 }
294
297
305 inline bool startPeriodicMeasurement(const mlx90614::IIR iir, const mlx90614::FIR fir, const mlx90614::Gain gain,
306 const mlx90614::IRSensor irs)
307 {
308 return PeriodicMeasurementAdapter<UnitMLX90614, mlx90614::Data>::startPeriodicMeasurement(iir, fir, gain, irs);
309 }
312 {
313 return PeriodicMeasurementAdapter<UnitMLX90614, mlx90614::Data>::startPeriodicMeasurement();
314 }
320 {
321 return PeriodicMeasurementAdapter<UnitMLX90614, mlx90614::Data>::stopPeriodicMeasurement();
322 }
324
329
334 bool readConfig(uint16_t& v);
342 bool writeConfig(const uint16_t v, const bool apply = true);
358 bool writeOutput(const mlx90614::Output o, const bool apply = true);
364 bool readIIR(mlx90614::IIR& iir);
372 bool writeIIR(const mlx90614::IIR iir, const bool apply = true);
378 bool readFIR(mlx90614::FIR& fir);
386 bool writeFIR(const mlx90614::FIR fir, const bool apply = true);
392 bool readGain(mlx90614::Gain& gain);
400 bool writeGain(const mlx90614::Gain gain, const bool apply = true);
415 bool writeIRSensor(const mlx90614::IRSensor irs, const bool apply = true);
421 bool readPositiveKs(bool& pos);
429 bool writePositiveKs(const bool pos, const bool apply = true);
435 bool readPositiveKf2(bool& pos);
443 bool writePositiveKf2(const bool pos, const bool apply = true);
445
450
456 bool readObjectMinMax(uint16_t& toMin, uint16_t& toMax);
463 bool readObjectMinMax(float& toMin, float& toMax);
471 template <typename T, typename std::enable_if<std::is_integral<T>::value, std::nullptr_t>::type = nullptr>
472 inline bool writeObjectMinMax(const T toMin, const T toMax, const bool apply = true)
473 {
474 return write_object_minmax((uint16_t)toMin, (uint16_t)toMax, apply);
475 }
484 bool writeObjectMinMax(const float toMin, const float toMax, const bool apply = true);
491 bool readAmbientMinMax(uint8_t& taMin, uint8_t& taMax);
498 bool readAmbientMinMax(float& taMin, float& taMax);
506 template <typename T, typename std::enable_if<std::is_integral<T>::value, std::nullptr_t>::type = nullptr>
507 inline bool writeAmbientMinMax(const T taMin, const T taMax, const bool apply = true)
508 {
509 return write_ambient_minmax((uint8_t)taMin, (uint8_t)taMax, apply);
510 }
519 bool writeAmbientMinMax(const float taMin, const float taMax, const bool apply = true);
521
525
530 bool readEmissivity(uint16_t& emiss);
536 bool readEmissivity(float& emiss);
544 template <typename T, typename std::enable_if<std::is_integral<T>::value, std::nullptr_t>::type = nullptr>
545 inline bool writeEmissivity(const T emiss, const bool apply = true)
546 {
547 return write_emissivity((uint16_t)emiss, apply);
548 }
556 bool writeEmissivity(const float emiss, const bool apply = true);
558
562
567 bool changeI2CAddress(const uint8_t i2c_address);
573 bool readI2CAddress(uint8_t& i2c_address);
575
580 bool sleep();
585 bool wakeup();
591 inline bool applySettings()
592 {
593 return sleep() && wakeup();
594 }
595
596protected:
597 bool read_eeprom(mlx90614::EEPROM& e);
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);
604 bool read_measurement(mlx90614::Data& d, const uint16_t config);
605
606 bool start_periodic_measurement(const mlx90614::IIR iir, const mlx90614::FIR fir, const mlx90614::Gain gain,
607 const mlx90614::IRSensor irs);
608 bool start_periodic_measurement();
609 bool stop_periodic_measurement();
610
611 M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitMLX90614, mlx90614::Data);
612
613 virtual uint32_t get_interval(const mlx90614::IIR iir, const mlx90614::FIR fir);
614 virtual bool has_dual_sensors() const
615 {
616 return false;
617 }
618
619private:
620 std::unique_ptr<m5::container::CircularBuffer<mlx90614::Data>> _data{};
621 mlx90614::EEPROM _eeprom{};
622 config_t _cfg{};
623};
624
630 M5_UNIT_COMPONENT_HPP_BUILDER(UnitMLX90614BAA, 0x5A);
631
632public:
633 explicit UnitMLX90614BAA(const uint8_t addr = DEFAULT_ADDRESS) : UnitMLX90614(addr)
634 {
635 }
636
637protected:
638 virtual uint32_t get_interval(const mlx90614::IIR iir, const mlx90614::FIR fir);
639 inline virtual bool has_dual_sensors() const
640 {
641 return true;
642 }
643};
644
646namespace mlx90614 {
647namespace command {
648// Category bits
649constexpr uint8_t COMMAND_RAM{0x00}; // 000xxxxx
650constexpr uint8_t COMMAND_EEPROM{0x20}; // 001xxxxx
651constexpr uint8_t COMMAND_READ_FLAGS{0xF0}; // 11110000
652constexpr uint8_t COMMAND_ENTER_SLEEP{0xFF}; // 11111111 This mode is not available for the 5V supply version
653// RAM
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};
660// EEPROM
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};
672
673} // namespace command
674} // namespace mlx90614
676
677} // namespace unit
678} // namespace m5
679
680#endif
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.
For mlx90614.
Unit-related namespace.
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)