ESPHome  2024.11.1
bme68x_bsec2.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/defines.h"
6 
7 #ifdef USE_BSEC2
8 
9 #ifdef USE_SENSOR
11 #endif
12 
13 #ifdef USE_TEXT_SENSOR
15 #endif
16 
17 #include <cinttypes>
18 #include <queue>
19 
20 #include <bsec2.h>
21 
22 namespace esphome {
23 namespace bme68x_bsec2 {
24 
29 };
30 
34 };
35 
36 enum SampleRate {
40 };
41 
42 enum Voltage {
45 };
46 
48  public:
49  void setup() override;
50  void dump_config() override;
51  float get_setup_priority() const override;
52  void loop() override;
53 
54  void set_algorithm_output(AlgorithmOutput algorithm_output) { this->algorithm_output_ = algorithm_output; }
55  void set_operating_age(OperatingAge operating_age) { this->operating_age_ = operating_age; }
56  void set_temperature_offset(float offset) { this->temperature_offset_ = offset; }
57  void set_voltage(Voltage voltage) { this->voltage_ = voltage; }
58 
59  void set_sample_rate(SampleRate sample_rate) { this->sample_rate_ = sample_rate; }
60  void set_temperature_sample_rate(SampleRate sample_rate) { this->temperature_sample_rate_ = sample_rate; }
61  void set_pressure_sample_rate(SampleRate sample_rate) { this->pressure_sample_rate_ = sample_rate; }
62  void set_humidity_sample_rate(SampleRate sample_rate) { this->humidity_sample_rate_ = sample_rate; }
63 
64  void set_bsec2_configuration(const uint8_t *data, const uint32_t len) {
65  this->bsec2_configuration_ = data;
67  }
68 
69  void set_state_save_interval(uint32_t interval) { this->state_save_interval_ms_ = interval; }
70 
71 #ifdef USE_SENSOR
81 #endif
82 #ifdef USE_TEXT_SENSOR
84 #endif
85  virtual uint32_t get_hash() = 0;
86 
87  protected:
88  void set_config_(const uint8_t *config, u_int32_t len);
89  float calc_sensor_sample_rate_(SampleRate sample_rate);
90  void update_subscription_();
91 
92  void run_();
93  void read_(int64_t trigger_time_ns);
94  void publish_(const bsec_output_t *outputs, uint8_t num_outputs);
95  int64_t get_time_ns_();
96 
97 #ifdef USE_SENSOR
98  void publish_sensor_(sensor::Sensor *sensor, float value, bool change_only = false);
99 #endif
100 #ifdef USE_TEXT_SENSOR
101  void publish_sensor_(text_sensor::TextSensor *sensor, const std::string &value);
102 #endif
103 
104  void load_state_();
105  void save_state_(uint8_t accuracy);
106 
107  void queue_push_(std::function<void()> &&f) { this->queue_.push(std::move(f)); }
108 
109  struct bme68x_dev bme68x_;
110  bsec_bme_settings_t bsec_settings_;
111  bsec_version_t version_;
112  uint8_t bsec_instance_[BSEC_INSTANCE_SIZE];
113 
114  struct bme68x_heatr_conf bme68x_heatr_conf_;
115  uint8_t op_mode_; // operating mode of sensor
116  bsec_library_return_t bsec_status_{BSEC_OK};
117  int8_t bme68x_status_{BME68X_OK};
118 
119  int64_t last_time_ms_{0};
121 
122  std::queue<std::function<void()>> queue_;
123 
124  uint8_t const *bsec2_configuration_{nullptr};
127 
129  uint32_t state_save_interval_ms_{21600000}; // 6 hours - 4 times a day
130  uint32_t last_state_save_ms_ = 0;
131 
133 
137 
138  SampleRate sample_rate_{SAMPLE_RATE_LP}; // Core/gas sample rate
142 
143 #ifdef USE_SENSOR
153 #endif
154 #ifdef USE_TEXT_SENSOR
156 #endif
157 };
158 
159 } // namespace bme68x_bsec2
160 } // namespace esphome
161 #endif
void publish_(const bsec_output_t *outputs, uint8_t num_outputs)
void set_pressure_sample_rate(SampleRate sample_rate)
Definition: bme68x_bsec2.h:61
void set_config_(const uint8_t *config, u_int32_t len)
void publish_sensor_(sensor::Sensor *sensor, float value, bool change_only=false)
void set_gas_resistance_sensor(sensor::Sensor *sensor)
Definition: bme68x_bsec2.h:75
void set_iaq_accuracy_sensor(sensor::Sensor *sensor)
Definition: bme68x_bsec2.h:78
uint8_t bsec_instance_[BSEC_INSTANCE_SIZE]
Definition: bme68x_bsec2.h:112
void set_iaq_static_sensor(sensor::Sensor *sensor)
Definition: bme68x_bsec2.h:77
void set_operating_age(OperatingAge operating_age)
Definition: bme68x_bsec2.h:55
void set_algorithm_output(AlgorithmOutput algorithm_output)
Definition: bme68x_bsec2.h:54
void set_bsec2_configuration(const uint8_t *data, const uint32_t len)
Definition: bme68x_bsec2.h:64
void set_iaq_sensor(sensor::Sensor *sensor)
Definition: bme68x_bsec2.h:76
float calc_sensor_sample_rate_(SampleRate sample_rate)
struct bme68x_heatr_conf bme68x_heatr_conf_
Definition: bme68x_bsec2.h:114
void set_co2_equivalent_sensor(sensor::Sensor *sensor)
Definition: bme68x_bsec2.h:79
void set_iaq_accuracy_text_sensor(text_sensor::TextSensor *sensor)
Definition: bme68x_bsec2.h:83
void set_temperature_sensor(sensor::Sensor *sensor)
Definition: bme68x_bsec2.h:72
text_sensor::TextSensor * iaq_accuracy_text_sensor_
Definition: bme68x_bsec2.h:155
void set_pressure_sensor(sensor::Sensor *sensor)
Definition: bme68x_bsec2.h:73
void set_humidity_sample_rate(SampleRate sample_rate)
Definition: bme68x_bsec2.h:62
std::string size_t len
Definition: helpers.h:293
void set_state_save_interval(uint32_t interval)
Definition: bme68x_bsec2.h:69
void set_sample_rate(SampleRate sample_rate)
Definition: bme68x_bsec2.h:59
void queue_push_(std::function< void()> &&f)
Definition: bme68x_bsec2.h:107
void set_breath_voc_equivalent_sensor(sensor::Sensor *sensor)
Definition: bme68x_bsec2.h:80
void set_humidity_sensor(sensor::Sensor *sensor)
Definition: bme68x_bsec2.h:74
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base-class for all sensors.
Definition: sensor.h:57
void read_(int64_t trigger_time_ns)
std::queue< std::function< void()> > queue_
Definition: bme68x_bsec2.h:122
void set_temperature_sample_rate(SampleRate sample_rate)
Definition: bme68x_bsec2.h:60
esphome::sensor::Sensor * sensor
Definition: statsd.h:38