M5Unit-METER 0.3.0 git rev:2c09d95
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 }
38 virtual ~UnitEEPROM()
39 {
40 }
41
45 inline int16_t hope(m5::unit::ads111x::Gain gain) const
46 {
47 return _calibration[m5::stl::to_underlying(gain)].hope;
48 }
52 inline int16_t actual(m5::unit::ads111x::Gain gain) const
53 {
54 return _calibration[m5::stl::to_underlying(gain)].actual;
55 }
60 {
61 return actual(gain) ? static_cast<float>(hope(gain)) / actual(gain) : 1.0f;
62 }
63
66 bool readCalibration();
67
68protected:
69 bool read_calibration(const m5::unit::ads111x::Gain gain, int16_t& hope, int16_t& actual);
70
71private:
72 struct Calibration {
73 int16_t hope{1};
74 int16_t actual{1};
75 };
76 std::array<Calibration, 8 /*Gain*/> _calibration{};
77};
78
79} // namespace meter
80} // namespace unit
81} // namespace m5
82#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:45
int16_t actual(m5::unit::ads111x::Gain gain) const
Gets the actual ADC value for the given gain.
Definition unit_EEPROM.hpp:52
float calibrationFactor(m5::unit::ads111x::Gain gain) const
Gets the calibration factor for the given gain.
Definition unit_EEPROM.hpp:59
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