13static const uint8_t READ_BIT = 0x01;
15static const uint8_t PU_CTRL_REG = 0x00;
16static const uint8_t PU_CTRL_REGISTER_RESET = 0x01;
17static const uint8_t PU_CTRL_POWERUP_DIGITAL = 0x02;
18static const uint8_t PU_CTRL_POWERUP_ANALOG = 0x04;
19static const uint8_t PU_CTRL_POWERUP_READY = 0x08;
20static const uint8_t PU_CTRL_CYCLE_START = 0x10;
21static const uint8_t PU_CTRL_CYCLE_READY = 0x20;
22static const uint8_t PU_CTRL_AVDD_EXTERNAL = 0x80;
24static const uint8_t CTRL1_REG = 0x01;
25static const uint8_t CTRL1_LDO_SHIFT = 3;
26static const uint8_t CTRL1_LDO_MASK = (0x7 << CTRL1_LDO_SHIFT);
27static const uint8_t CTRL1_GAIN_MASK = 0x7;
29static const uint8_t CTRL2_REG = 0x02;
30static const uint8_t CTRL2_CRS_SHIFT = 4;
31static const uint8_t CTRL2_CRS_MASK = (0x7 << CTRL2_CRS_SHIFT);
32static const uint8_t CTRL2_CALS = 0x04;
33static const uint8_t CTRL2_CAL_ERR = 0x08;
34static const uint8_t CTRL2_GAIN_CALIBRATION = 0x03;
35static const uint8_t CTRL2_CONFIG_MASK = 0xF0;
37static const uint8_t OCAL1_B2_REG = 0x03;
38static const uint8_t GCAL1_B3_REG = 0x06;
39static const uint8_t GCAL1_FRACTIONAL = 23;
42static const uint8_t ADCO_B2_REG = 0x12;
44static const uint8_t ADC_REG = 0x15;
45static const uint8_t ADC_CHPS_DISABLE = 0x30;
47static const uint8_t PGA_REG = 0x1B;
48static const uint8_t PGA_LDOMODE_ESR = 0x40;
50static const uint8_t POWER_REG = 0x1C;
51static const uint8_t POWER_PGA_CAP_EN = 0x80;
53static const uint8_t DEVICE_REV = 0x1F;
60 this->
mark_failed(LOG_STR(
"Failed I2C read during setup()"));
63 ESP_LOGI(TAG,
"Setting up NAU7802 Rev %d", rev);
66 pu_ctrl |= PU_CTRL_REGISTER_RESET;
68 pu_ctrl &= ~PU_CTRL_REGISTER_RESET;
71 pu_ctrl |= PU_CTRL_POWERUP_DIGITAL;
74 if (!(pu_ctrl.
get() & PU_CTRL_POWERUP_READY)) {
75 this->
mark_failed(LOG_STR(
"Failed to reset sensor during setup()"));
84 pu_ctrl |= PU_CTRL_POWERUP_ANALOG;
92 pu_ctrl |= PU_CTRL_CYCLE_START;
95 ctrl1 &= ~CTRL1_GAIN_MASK;
100 pu_ctrl |= PU_CTRL_AVDD_EXTERNAL;
101 ctrl1 &= ~CTRL1_LDO_MASK;
102 ctrl1 |= this->
ldo_ << CTRL1_LDO_SHIFT;
106 ctrl2 &= ~CTRL2_CRS_MASK;
107 ctrl2 |= this->
sps_ << CTRL2_CRS_SHIFT;
111 adc_reg |= ADC_CHPS_DISABLE;
115 pga_reg &= ~PGA_LDOMODE_ESR;
119 pwr_reg |= POWER_PGA_CAP_EN;
123 LOG_SENSOR(
"",
"NAU7802",
this);
124 LOG_I2C_DEVICE(
this);
130 " Offset Calibration: %" PRId32
"\n"
131 " Gain Calibration: %f",
134 std::string voltage =
"unknown";
135 switch (this->
ldo_) {
161 voltage =
"External";
164 ESP_LOGCONFIG(TAG,
" LDO Voltage: %s", voltage.c_str());
166 switch (this->
gain_) {
192 ESP_LOGCONFIG(TAG,
" Gain: %dx",
gain);
194 switch (this->
sps_) {
211 ESP_LOGCONFIG(TAG,
" Samples Per Second: %d", sps);
212 LOG_UPDATE_INTERVAL(
this);
217 for (
size_t i = 0; i <
size; i++) {
218 data[i] = 0xFF & (value >> (
size - 1 - i) * 8);
227 for (
size_t i = 0; i <
size; i++) {
228 result |= data[i] << (
size - 1 - i) * 8;
236 ESP_LOGW(TAG,
"Calibration already in progress");
244 ctrl2 &= CTRL2_CONFIG_MASK;
252 switch (this->state_) {
269 if (ctrl2.
get() & CTRL2_CAL_ERR) {
272 ESP_LOGE(TAG,
"Failed to calibrate sensor");
284 ESP_LOGI(TAG,
"New Offset: %" PRId32, ocal);
286 float gcal_f = ((float) gcal / (float) (1 << GCAL1_FRACTIONAL));
287 ESP_LOGI(TAG,
"New Gain: %f", gcal_f);
293 ESP_LOGW(TAG,
"No measurements ready!");
298 int32_t result = this->
read_value_(ADCO_B2_REG, 3);
300 ESP_LOGD(TAG,
"'%s': Got value %" PRId32, LOG_STR_ARG(this->
get_log_name()), result);
BedjetMode mode
BedJet operating mode.
void mark_failed()
Mark this component as failed.
void status_clear_error()
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
This class is used to create I2CRegister objects that act as proxies to read/write internal registers...
uint8_t get() const
returns the register value
bool calibrate_(enum NAU7802CalibrationModes mode)
bool gain_calibration_failed_
int32_t offset_calibration_
void write_value_(uint8_t start_reg, size_t size, int32_t value)
int32_t read_value_(uint8_t start_reg, size_t size)
bool offset_calibration_failed_
void set_calibration_failure_(bool failed)
void dump_config() override
void publish_state(float state)
Publish a new state to the front-end.
ESPHOME_LOG_TAG(TAG, "nau7802")
constexpr int32_t sign_extend(uint32_t value)
Sign-extend the low BITS bits of value, e.g. sign_extend<24>(0xFFFFFE) == -2. Higher bits are ignored...
void HOT delay(uint32_t ms)