ESPHome  2024.12.2
honeywellabp2.cpp
Go to the documentation of this file.
1 #include "honeywellabp2.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/helpers.h"
4 
5 namespace esphome {
6 namespace honeywellabp2_i2c {
7 
8 static const uint8_t STATUS_BIT_POWER = 6;
9 static const uint8_t STATUS_BIT_BUSY = 5;
10 static const uint8_t STATUS_BIT_ERROR = 2;
11 static const uint8_t STATUS_MATH_SAT = 0;
12 
13 static const char *const TAG = "honeywellabp2";
14 
16  if (this->read(raw_data_, 7) != i2c::ERROR_OK) {
17  ESP_LOGE(TAG, "Communication with ABP2 failed!");
18  this->status_set_warning("couldn't read sensor data");
19  return;
20  }
21  float press_counts = encode_uint24(raw_data_[1], raw_data_[2], raw_data_[3]); // calculate digital pressure counts
22  float temp_counts = encode_uint24(raw_data_[4], raw_data_[5], raw_data_[6]); // calculate digital temperature counts
23 
24  this->last_pressure_ = (((press_counts - this->min_count_) / (this->max_count_ - this->min_count_)) *
25  (this->max_pressure_ - this->min_pressure_)) +
26  this->min_pressure_;
27  this->last_temperature_ = (temp_counts * 200 / 16777215) - 50;
28  this->status_clear_warning();
29 }
30 
32  if (this->write(i2c_cmd_, 3) != i2c::ERROR_OK) {
33  ESP_LOGE(TAG, "Communication with ABP2 failed!");
34  this->status_set_warning("couldn't start measurement");
35  return;
36  }
37  this->measurement_running_ = true;
38 }
39 
41  if (this->read(raw_data_, 1) != i2c::ERROR_OK) {
42  ESP_LOGE(TAG, "Communication with ABP2 failed!");
43  this->status_set_warning("couldn't check measurement");
44  return false;
45  }
46  if ((raw_data_[0] & (0x1 << STATUS_BIT_BUSY)) > 0) {
47  return false;
48  }
49  this->measurement_running_ = false;
50  return true;
51 }
52 
54  ESP_LOGE(TAG, "Timeout!");
55  this->measurement_running_ = false;
56  this->status_set_warning("measurement timed out");
57 }
58 
60 
62 
64  if (this->measurement_running_) {
65  if (this->is_measurement_ready()) {
66  this->cancel_timeout("meas_timeout");
67 
68  this->read_sensor_data();
69  if (pressure_sensor_ != nullptr) {
71  }
72  if (temperature_sensor_ != nullptr) {
74  }
75  }
76  }
77 }
78 
80  ESP_LOGV(TAG, "Update Honeywell ABP2 Sensor");
81 
82  this->start_measurement();
83  this->set_timeout("meas_timeout", 100, [this] { this->measurement_timeout(); });
84 }
85 
87  ESP_LOGCONFIG(TAG, " Min Pressure Range: %0.1f", this->min_pressure_);
88  ESP_LOGCONFIG(TAG, " Max Pressure Range: %0.1f", this->max_pressure_);
90  ESP_LOGCONFIG(TAG, " Transfer function A");
91  } else {
92  ESP_LOGCONFIG(TAG, " Transfer function B");
93  }
94  LOG_UPDATE_INTERVAL(this);
95 }
96 
98  this->transfer_function_ = transfer_function;
100  this->max_count_ = this->max_count_b_;
101  this->min_count_ = this->min_count_b_;
102  } else {
103  this->max_count_ = this->max_count_a_;
104  this->min_count_ = this->min_count_a_;
105  }
106 }
107 
108 } // namespace honeywellabp2_i2c
109 } // namespace esphome
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
Definition: component.cpp:73
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition: i2c.h:160
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition: component.cpp:69
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition: i2c.h:186
No error found during execution of method.
Definition: i2c_bus.h:13
void status_clear_warning()
Definition: component.cpp:166
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
Definition: helpers.h:192
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_transfer_function(ABP2TRANFERFUNCTION transfer_function)