M5Unit-METER 0.4.1 git rev:5bdc3fe
Loading...
Searching...
No Matches
unit_EEPROM.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_METER_UNIT_EEPROM_HPP
11#define M5_UNIT_METER_UNIT_EEPROM_HPP
12
13#include "unit_ADS111x.hpp"
14#include <M5UnitComponent.hpp>
15#include <m5_utility/stl/extension.hpp>
16#include <array>
17
18namespace m5 {
19namespace unit {
24namespace meter {
25
31class UnitEEPROM : public Component {
32 M5_UNIT_COMPONENT_HPP_BUILDER(UnitEEPROM, 0x00);
33
34public:
35 explicit UnitEEPROM(const uint8_t addr = DEFAULT_ADDRESS) : Component(addr)
36 {
37 // 400 kHz, the same as the ADS1115 on the same bus (the official driver also reads this EEPROM at 400 kHz)
38 auto ccfg = component_config();
39 ccfg.clock = 400 * 1000U;
40 component_config(ccfg);
41 }
42 virtual ~UnitEEPROM()
43 {
44 }
45
49 inline int16_t hope(m5::unit::ads111x::Gain gain) const
50 {
51 return _calibration[m5::stl::to_underlying(gain)].hope;
52 }
56 inline int16_t actual(m5::unit::ads111x::Gain gain) const
57 {
58 return _calibration[m5::stl::to_underlying(gain)].actual;
59 }
64 {
65 return actual(gain) ? static_cast<float>(hope(gain)) / actual(gain) : 1.0f;
66 }
67
70 bool readCalibration();
71
72protected:
73 bool read_calibration(const m5::unit::ads111x::Gain gain, int16_t& hope, int16_t& actual);
74
75private:
76 struct Calibration {
77 int16_t hope{1};
78 int16_t actual{1};
79 };
80 std::array<Calibration, 8 /*Gain*/> _calibration{};
81};
82
83} // namespace meter
84} // namespace unit
85} // namespace m5
86#endif
bool readCalibration()
Read calibration data from EEPROM.
Definition unit_EEPROM.cpp:32
int16_t hope(m5::unit::ads111x::Gain gain) const
Gets the expected (hope) ADC value for the given gain.
Definition unit_EEPROM.hpp:49
int16_t actual(m5::unit::ads111x::Gain gain) const
Gets the actual ADC value for the given gain.
Definition unit_EEPROM.hpp:56
float calibrationFactor(m5::unit::ads111x::Gain gain) const
Gets the calibration factor for the given gain.
Definition unit_EEPROM.hpp:63
Top level namespace of M5stack.
namespace for Meter
Unit-related namespace.
Base class for ADS111x families.
Gain
Programmable gain amplifier.
Definition unit_ADS111x.hpp:47