ESPHome  2024.4.0
honeywellabp.cpp
Go to the documentation of this file.
1 #include "honeywellabp.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace honeywellabp {
6 
7 static const char *const TAG = "honeywellabp";
8 
9 const float MIN_COUNT = 1638.4; // 1638 counts (10% of 2^14 counts or 0x0666)
10 const float MAX_COUNT = 14745.6; // 14745 counts (90% of 2^14 counts or 0x3999)
11 
13  ESP_LOGD(TAG, "Setting up Honeywell ABP Sensor ");
14  this->spi_setup();
15 }
16 
18  // Polls the sensor for new data.
19  // transfer 4 bytes (the last two are temperature only used by some sensors)
20  this->enable();
21  buf_[0] = this->read_byte();
22  buf_[1] = this->read_byte();
23  buf_[2] = this->read_byte();
24  buf_[3] = this->read_byte();
25  this->disable();
26 
27  // Check the status codes:
28  // status = 0 : normal operation
29  // status = 1 : device in command mode
30  // status = 2 : stale data
31  // status = 3 : diagnostic condition
32  status_ = buf_[0] >> 6 & 0x3;
33  ESP_LOGV(TAG, "Sensor status %d", status_);
34 
35  // if device is normal and there is new data, bitmask and save the raw data
36  if (status_ == 0) {
37  // 14 - bit pressure is the last 6 bits of byte 0 (high bits) & all of byte 1 (lowest 8 bits)
38  pressure_count_ = ((uint16_t) (buf_[0]) << 8 & 0x3F00) | ((uint16_t) (buf_[1]) & 0xFF);
39  // 11 - bit temperature is all of byte 2 (lowest 8 bits) and the first three bits of byte 3
40  temperature_count_ = (((uint16_t) (buf_[2]) << 3) & 0x7F8) | (((uint16_t) (buf_[3]) >> 5) & 0x7);
41  ESP_LOGV(TAG, "Sensor pressure_count_ %d", pressure_count_);
42  ESP_LOGV(TAG, "Sensor temperature_count_ %d", temperature_count_);
43  }
44  return status_;
45 }
46 
47 // returns status
49 
50 // The pressure value from the most recent reading in raw counts
52 
53 // The temperature value from the most recent reading in raw counts
55 
56 // Converts a digital pressure measurement in counts to pressure measured
57 float HONEYWELLABPSensor::countstopressure_(const int counts, const float min_pressure, const float max_pressure) {
58  return ((((float) counts - MIN_COUNT) * (max_pressure - min_pressure)) / (MAX_COUNT - MIN_COUNT)) + min_pressure;
59 }
60 
61 // Converts a digital temperature measurement in counts to temperature in C
62 // This will be invalid if sensore daoes not have temperature measurement capability
63 float HONEYWELLABPSensor::countstotemperatures_(const int counts) { return (((float) counts / 2047.0) * 200.0) - 50.0; }
64 
65 // Pressure value from the most recent reading in units
68 }
69 
70 // Temperature value from the most recent reading in degrees C
72 
74  ESP_LOGV(TAG, "Update Honeywell ABP Sensor");
75  if (readsensor_() == 0) {
76  if (this->pressure_sensor_ != nullptr)
78  if (this->temperature_sensor_ != nullptr)
80  }
81 }
82 
84 
86  // LOG_SENSOR("", "HONEYWELLABP", this);
87  LOG_PIN(" CS Pin: ", this->cs_);
88  ESP_LOGCONFIG(TAG, " Min Pressure Range: %0.1f", honeywellabp_min_pressure_);
89  ESP_LOGCONFIG(TAG, " Max Pressure Range: %0.1f", honeywellabp_max_pressure_);
90  LOG_UPDATE_INTERVAL(this);
91 }
92 
94  this->honeywellabp_min_pressure_ = min_pressure;
95 }
96 
98  this->honeywellabp_max_pressure_ = max_pressure;
99 }
100 
101 } // namespace honeywellabp
102 } // namespace esphome
void set_honeywellabp_min_pressure(float min_pressure)
const float LATE
For components that should be initialized at the very end of the setup process.
Definition: component.cpp:28
void set_honeywellabp_max_pressure(float max_pressure)
GPIOPin * cs_
Definition: spi.h:395
float countstopressure_(int counts, float min_pressure, float max_pressure)
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7