ESPHome  2024.12.2
nau7802.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include <cinttypes>
9 
10 namespace esphome {
11 namespace nau7802 {
12 
15  NAU7802_GAIN_64 = 0b110,
16  NAU7802_GAIN_32 = 0b101,
17  NAU7802_GAIN_16 = 0b100,
18  NAU7802_GAIN_8 = 0b011,
19  NAU7802_GAIN_4 = 0b010,
20  NAU7802_GAIN_2 = 0b001,
21  NAU7802_GAIN_1 = 0b000,
22 };
23 
24 enum NAU7802SPS {
25  NAU7802_SPS_320 = 0b111,
26  NAU7802_SPS_80 = 0b011,
27  NAU7802_SPS_40 = 0b010,
28  NAU7802_SPS_20 = 0b001,
29  NAU7802_SPS_10 = 0b000,
30 };
31 
32 enum NAU7802LDO {
33  NAU7802_LDO_2V4 = 0b111,
34  NAU7802_LDO_2V7 = 0b110,
35  NAU7802_LDO_3V0 = 0b101,
36  NAU7802_LDO_3V3 = 0b100,
37  NAU7802_LDO_3V6 = 0b011,
38  NAU7802_LDO_3V9 = 0b010,
39  NAU7802_LDO_4V2 = 0b001,
40  NAU7802_LDO_4V5 = 0b000,
41  // Never write this to a register
43 };
44 
49 };
50 
52  public:
53  void set_samples_per_second(NAU7802SPS sps) { this->sps_ = sps; }
54  void set_ldo_voltage(NAU7802LDO ldo) { this->ldo_ = ldo; }
55  void set_gain(NAU7802Gain gain) { this->gain_ = gain; }
56  void set_gain_calibration(float gain_calibration) { this->gain_calibration_ = gain_calibration; }
57  void set_offset_calibration(int32_t offset_calibration) { this->offset_calibration_ = offset_calibration; }
61 
62  void setup() override;
63  void loop() override;
64  bool can_proceed() override;
65  void dump_config() override;
66  float get_setup_priority() const override;
67  void update() override;
68 
69  protected:
70  //
71  // Internal state
72  //
73  enum class CalibrationState : uint8_t {
74  INACTIVE,
75  OFFSET,
76  GAIN,
78 
83  bool setup_complete_ = false;
84 
85  //
86  // Config values
87  //
91 
92  //
93  // Internal Methods
94  //
96  void complete_setup_();
97  void write_value_(uint8_t start_reg, size_t size, int32_t value);
98  int32_t read_value_(uint8_t start_reg, size_t size);
99  bool is_data_ready_();
100  void set_calibration_failure_(bool failed);
101 };
102 
103 template<typename... Ts>
104 class NAU7802CalbrateExternalOffsetAction : public Action<Ts...>, public Parented<NAU7802Sensor> {
105  public:
106  void play(Ts... x) override { this->parent_->calibrate_external_offset(); }
107 };
108 
109 template<typename... Ts>
110 class NAU7802CalbrateInternalOffsetAction : public Action<Ts...>, public Parented<NAU7802Sensor> {
111  public:
112  void play(Ts... x) override { this->parent_->calibrate_internal_offset(); }
113 };
114 
115 template<typename... Ts> class NAU7802CalbrateGainAction : public Action<Ts...>, public Parented<NAU7802Sensor> {
116  public:
117  void play(Ts... x) override { this->parent_->calibrate_gain(); }
118 };
119 
120 } // namespace nau7802
121 } // namespace esphome
void set_gain_calibration(float gain_calibration)
Definition: nau7802.h:56
void set_samples_per_second(NAU7802SPS sps)
Definition: nau7802.h:53
void set_calibration_failure_(bool failed)
Definition: nau7802.cpp:260
uint16_t x
Definition: tt21100.cpp:17
This class simplifies creating components that periodically check a state.
Definition: component.h:283
void set_ldo_voltage(NAU7802LDO ldo)
Definition: nau7802.h:54
void set_offset_calibration(int32_t offset_calibration)
Definition: nau7802.h:57
AlsGain501 gain
float get_setup_priority() const override
Definition: nau7802.cpp:300
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:183
bool calibrate_(enum NAU7802CalibrationModes mode)
Definition: nau7802.cpp:242
void write_value_(uint8_t start_reg, size_t size, int32_t value)
Definition: nau7802.cpp:220
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_gain(NAU7802Gain gain)
Definition: nau7802.h:55
int32_t read_value_(uint8_t start_reg, size_t size)
Definition: nau7802.cpp:228
Base-class for all sensors.
Definition: sensor.h:57
This Class provides the methods to read/write bytes from/to an i2c device.
Definition: i2c.h:133
Helper class to easily give an object a parent of type T.
Definition: helpers.h:530