M5Unit-METER 0.0.3 git rev:c518ea3
Loading...
Searching...
No Matches
unit_KmeterISO.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_UNIFIED_KMETERISO_UNIT_KMETERISO_HPP
11#define M5_UNIT_UNIFIED_KMETERISO_UNIT_KMETERISO_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
21namespace kmeterISO {
22
31
36struct Data {
37 std::array<uint8_t, 4> raw{}; // raw data
38 inline float temperature() const
39 {
40 return static_cast<int32_t>(((uint32_t)raw[3] << 24) | ((uint32_t)raw[2] << 16) | ((uint32_t)raw[1] << 8) |
41 ((uint32_t)raw[0] << 0)) *
42 0.01f;
43 }
44};
45} // namespace kmeterISO
46
52class UnitKmeterISO : public Component, public PeriodicMeasurementAdapter<UnitKmeterISO, kmeterISO::Data> {
53 M5_UNIT_COMPONENT_HPP_BUILDER(UnitKmeterISO, 0x66);
54
55public:
60 struct config_t {
62 bool start_periodic{true};
64 uint32_t interval{100};
66 kmeterISO::MeasurementUnit measurement_unit{kmeterISO::MeasurementUnit::Celsius};
67 };
68
69 explicit UnitKmeterISO(const uint8_t addr = DEFAULT_ADDRESS)
70 : Component(addr), _data{new m5::container::CircularBuffer<kmeterISO::Data>(1)}
71 {
72 auto ccfg = component_config();
73 ccfg.clock = 100 * 1000U;
74 component_config(ccfg);
75 }
76 virtual ~UnitKmeterISO()
77 {
78 }
79
80 virtual bool begin() override;
81 virtual void update(const bool force = false) override;
82
85
87 {
88 return _cfg;
89 }
91 inline void config(const config_t& cfg)
92 {
93 _cfg = cfg;
94 }
96
99
101 {
102 return _munit;
103 }
106 {
107 _munit = munit;
108 }
110
114 inline float temperature() const
115 {
116 return !empty() ? oldest().temperature() : std::numeric_limits<float>::quiet_NaN();
117 }
119
122
127 {
128 return PeriodicMeasurementAdapter<UnitKmeterISO, kmeterISO::Data>::startPeriodicMeasurement();
129 }
136 inline bool startPeriodicMeasurement(const uint32_t interval,
137 const kmeterISO::MeasurementUnit munit = kmeterISO::Celsius)
138 {
139 return PeriodicMeasurementAdapter<UnitKmeterISO, kmeterISO::Data>::startPeriodicMeasurement(interval, munit);
140 }
146 {
147 return PeriodicMeasurementAdapter<UnitKmeterISO, kmeterISO::Data>::stopPeriodicMeasurement();
148 }
150
156 bool readStatus(uint8_t& status);
161 bool isReady()
162 {
163 uint8_t s{};
164 return readStatus(s) && (s == 0U);
165 }
166
172 bool readFirmwareVersion(uint8_t& ver);
173
176
183 bool measureSingleshot(kmeterISO::Data& d, kmeterISO::MeasurementUnit munit = kmeterISO::MeasurementUnit::Celsius,
184 const uint32_t timeoutMs = 0);
193 kmeterISO::MeasurementUnit munit = kmeterISO::MeasurementUnit::Celsius,
194 const uint32_t timeoutMs = 0);
196
200
206 bool changeI2CAddress(const uint8_t i2c_address);
212 bool readI2CAddress(uint8_t& i2c_address);
214
215protected:
216 bool start_periodic_measurement();
217 bool start_periodic_measurement(const uint32_t interval,
218 const kmeterISO::MeasurementUnit munit = kmeterISO::Celsius);
219 bool stop_periodic_measurement();
220
221 bool read_measurement(kmeterISO::Data& d, const kmeterISO::MeasurementUnit munit);
222 bool read_internal_measurement(kmeterISO::Data& d, const kmeterISO::MeasurementUnit munit);
223
224 M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitKmeterISO, kmeterISO::Data);
225
226protected:
227 std::unique_ptr<m5::container::CircularBuffer<kmeterISO::Data>> _data{};
228 kmeterISO::MeasurementUnit _munit{kmeterISO::MeasurementUnit::Celsius};
229 config_t _cfg{};
230};
231
232namespace kmeterISO {
233namespace command {
235// See also https://github.com/m5stack/M5Unit-KMeterISO/blob/main/docs/Unit_KmeterISO_I2C_Protocol.pdf
236constexpr uint8_t TEMPERATURE_CELSIUS_VAL_REG{0X00};
237constexpr uint8_t TEMPERATURE_FAHRENHEIT_VAL_REG{0X04};
238constexpr uint8_t INTERNAL_TEMPERATURE_CELSIUS_VAL_REG{0X10};
239constexpr uint8_t INTERNAL_TEMPERATURE_FAHRENHEIT_VAL_REG{0X14};
240constexpr uint8_t ERROR_STATUS_REG{0x20};
241constexpr uint8_t TEMPERATURE_CELSIUS_STRING_REG{0x30};
242constexpr uint8_t TEMPERATURE__FAHRENHEIT_STRING_REG{0x40};
243constexpr uint8_t INTERNAL_TEMPERATURE_CELSIUS_STRING_REG{0x50};
244constexpr uint8_t INTERNAL_TEMPERATURE_FAHRENHEIT_STRING_REG{0x60};
245constexpr uint8_t FIRMWARE_VERSION_REG{0xFE};
246constexpr uint8_t I2C_ADDRESS_REG{0xFF};
248} // namespace command
249} // namespace kmeterISO
250
251} // namespace unit
252} // namespace m5
253#endif
KMeterISO unitis an integrated K-type thermocouple sensor unit that integrates the functions of "acqu...
Definition unit_KmeterISO.hpp:52
void setMeasurementUnit(const kmeterISO::MeasurementUnit munit)
Definition unit_KmeterISO.hpp:105
bool isReady()
Ready to read data?
Definition unit_KmeterISO.hpp:161
bool readFirmwareVersion(uint8_t &ver)
Read firmware version.
Definition unit_KmeterISO.cpp:117
bool measureSingleshot(kmeterISO::Data &d, kmeterISO::MeasurementUnit munit=kmeterISO::MeasurementUnit::Celsius, const uint32_t timeoutMs=0)
Measure temperature single shot.
Definition unit_KmeterISO.cpp:122
bool readI2CAddress(uint8_t &i2c_address)
Read device I2C address.
Definition unit_KmeterISO.cpp:171
bool stopPeriodicMeasurement()
Stop periodic measurement.
Definition unit_KmeterISO.hpp:145
kmeterISO::MeasurementUnit measurementUnit() const
Definition unit_KmeterISO.hpp:100
bool readStatus(uint8_t &status)
Read status.
Definition unit_KmeterISO.cpp:111
bool startPeriodicMeasurement()
Start periodic measurement in the current settings.
Definition unit_KmeterISO.hpp:126
bool changeI2CAddress(const uint8_t i2c_address)
Change device I2C address.
Definition unit_KmeterISO.cpp:151
config_t config()
Gets the configration.
Definition unit_KmeterISO.hpp:86
void config(const config_t &cfg)
Set the configration.
Definition unit_KmeterISO.hpp:91
bool startPeriodicMeasurement(const uint32_t interval, const kmeterISO::MeasurementUnit munit=kmeterISO::Celsius)
Start periodic measurement.
Definition unit_KmeterISO.hpp:136
bool measureInternalSingleshot(kmeterISO::Data &d, kmeterISO::MeasurementUnit munit=kmeterISO::MeasurementUnit::Celsius, const uint32_t timeoutMs=0)
Measure internal temperature single shot.
Definition unit_KmeterISO.cpp:136
float temperature() const
Oldest temperature.
Definition unit_KmeterISO.hpp:114
Top level namespace of M5stack.
Unit-related namespace.
Settings for begin.
Definition unit_KmeterISO.hpp:60
kmeterISO::MeasurementUnit measurement_unit
//!< measurement unit if start on begin
Definition unit_KmeterISO.hpp:66
uint32_t interval
periodic interval(ms) if start on begin
Definition unit_KmeterISO.hpp:64
bool start_periodic
Start periodic measurement on begin?
Definition unit_KmeterISO.hpp:62
Measurement data group.
Definition unit_ADS111x.hpp:204
Measurement data group.
Definition unit_KmeterISO.hpp:36
MeasurementUnit
measurement unit on periodic measurement
Definition unit_KmeterISO.hpp:27
@ Fahrenheit
Temperature is fahrenheit.
Definition unit_KmeterISO.hpp:29
@ Celsius
Temperature is celsius.
Definition unit_KmeterISO.hpp:28