ESPHome  2024.10.2
bme680_bsec.h
Go to the documentation of this file.
1 #pragma once
2 
8 #include "esphome/core/defines.h"
9 #include <map>
10 
11 #ifdef USE_BSEC
12 #include <bsec.h>
13 #endif
14 
15 namespace esphome {
16 namespace bme680_bsec {
17 #ifdef USE_BSEC
18 
19 enum IAQMode {
22 };
23 
27 };
28 
29 enum SampleRate {
33 };
34 
35 #define BME680_BSEC_SAMPLE_RATE_LOG(r) (r == SAMPLE_RATE_DEFAULT ? "Default" : (r == SAMPLE_RATE_ULP ? "ULP" : "LP"))
36 
38  public:
39  void set_device_id(const std::string &devid) { this->device_id_.assign(devid); }
40  void set_temperature_offset(float offset) { this->temperature_offset_ = offset; }
41  void set_iaq_mode(IAQMode iaq_mode) { this->iaq_mode_ = iaq_mode; }
42  void set_state_save_interval(uint32_t interval) { this->state_save_interval_ms_ = interval; }
43  void set_supply_voltage(SupplyVoltage supply_voltage) { this->supply_voltage_ = supply_voltage; }
44 
45  void set_sample_rate(SampleRate sample_rate) { this->sample_rate_ = sample_rate; }
46  void set_temperature_sample_rate(SampleRate sample_rate) { this->temperature_sample_rate_ = sample_rate; }
47  void set_pressure_sample_rate(SampleRate sample_rate) { this->pressure_sample_rate_ = sample_rate; }
48  void set_humidity_sample_rate(SampleRate sample_rate) { this->humidity_sample_rate_ = sample_rate; }
49 
59 
60  static std::vector<BME680BSECComponent *> instances;
61  static int8_t read_bytes_wrapper(uint8_t devid, uint8_t a_register, uint8_t *data, uint16_t len);
62  static int8_t write_bytes_wrapper(uint8_t devid, uint8_t a_register, uint8_t *data, uint16_t len);
63  static void delay_ms(uint32_t period);
64 
65  void setup() override;
66  void dump_config() override;
67  float get_setup_priority() const override;
68  void loop() override;
69 
70  protected:
71  void set_config_();
72  float calc_sensor_sample_rate_(SampleRate sample_rate);
73  void update_subscription_();
74 
75  void run_();
76  void read_();
77  void publish_(const bsec_output_t *outputs, uint8_t num_outputs);
78  int64_t get_time_ns_();
79 
80  void publish_sensor_(sensor::Sensor *sensor, float value, bool change_only = false);
81  void publish_sensor_(text_sensor::TextSensor *sensor, const std::string &value);
82 
83  void snapshot_state_(); // Fetch the current BSEC library state and save it in the bsec_state_data_ member (volatile
84  // memory)
85  void restore_state_(); // Push the state contained in the bsec_state_data_ member (volatile memory) to the BSEC
86  // library
87  int reinit_bsec_lib_(); // Prepare the BSEC library to be used again after this object returns active
88  // (as the library may have been used by other objects)
89  void load_state_(); // Initialize the ESP preferences object; retrieve the BSEC library state from the ESP
90  // preferences (storage); then save it in the bsec_state_data_ member (volatile memory) and
91  // push it to the BSEC library
92  void save_state_(
93  uint8_t accuracy); // Save the bsec_state_data_ member (volatile memory) to the ESP preferences (storage)
94 
95  void queue_push_(std::function<void()> &&f) { this->queue_.push(std::move(f)); }
96 
97  static uint8_t work_buffer_[BSEC_MAX_WORKBUFFER_SIZE];
98  struct bme680_dev bme680_;
99  bsec_library_return_t bsec_status_{BSEC_OK};
100  int8_t bme680_status_{BME680_OK};
101 
102  int64_t last_time_ms_{0};
104  int64_t next_call_ns_{0};
105 
106  std::queue<std::function<void()>> queue_;
107 
109  uint8_t bsec_state_data_[BSEC_MAX_STATE_BLOB_SIZE]; // This is the current snapshot of the BSEC library state
111  uint32_t state_save_interval_ms_{21600000}; // 6 hours - 4 times a day
112  uint32_t last_state_save_ms_ = 0;
113  bsec_bme_settings_t bme680_settings_;
114 
115  std::string device_id_;
119 
120  SampleRate sample_rate_{SAMPLE_RATE_LP}; // Core/gas sample rate
124 
134 };
135 #endif
136 } // namespace bme680_bsec
137 } // namespace esphome
void set_co2_equivalent_sensor(sensor::Sensor *sensor)
Definition: bme680_bsec.h:57
text_sensor::TextSensor * iaq_accuracy_text_sensor_
Definition: bme680_bsec.h:130
void set_supply_voltage(SupplyVoltage supply_voltage)
Definition: bme680_bsec.h:43
void set_pressure_sample_rate(SampleRate sample_rate)
Definition: bme680_bsec.h:47
void queue_push_(std::function< void()> &&f)
Definition: bme680_bsec.h:95
void publish_(const bsec_output_t *outputs, uint8_t num_outputs)
static void delay_ms(uint32_t period)
float calc_sensor_sample_rate_(SampleRate sample_rate)
Definition: bme680_bsec.cpp:85
void set_gas_resistance_sensor(sensor::Sensor *sensor)
Definition: bme680_bsec.h:53
void set_humidity_sensor(sensor::Sensor *sensor)
Definition: bme680_bsec.h:52
void set_pressure_sensor(sensor::Sensor *sensor)
Definition: bme680_bsec.h:51
static int8_t read_bytes_wrapper(uint8_t devid, uint8_t a_register, uint8_t *data, uint16_t len)
void set_iaq_accuracy_text_sensor(text_sensor::TextSensor *sensor)
Definition: bme680_bsec.h:55
uint8_t bsec_state_data_[BSEC_MAX_STATE_BLOB_SIZE]
Definition: bme680_bsec.h:109
void set_state_save_interval(uint32_t interval)
Definition: bme680_bsec.h:42
static std::vector< BME680BSECComponent * > instances
Definition: bme680_bsec.h:60
void set_temperature_sensor(sensor::Sensor *sensor)
Definition: bme680_bsec.h:50
void set_breath_voc_equivalent_sensor(sensor::Sensor *sensor)
Definition: bme680_bsec.h:58
void set_temperature_sample_rate(SampleRate sample_rate)
Definition: bme680_bsec.h:46
void set_iaq_sensor(sensor::Sensor *sensor)
Definition: bme680_bsec.h:54
std::string size_t len
Definition: helpers.h:292
static int8_t write_bytes_wrapper(uint8_t devid, uint8_t a_register, uint8_t *data, uint16_t len)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
static uint8_t work_buffer_[BSEC_MAX_WORKBUFFER_SIZE]
Definition: bme680_bsec.h:97
void set_sample_rate(SampleRate sample_rate)
Definition: bme680_bsec.h:45
Base-class for all sensors.
Definition: sensor.h:57
void publish_sensor_(sensor::Sensor *sensor, float value, bool change_only=false)
std::queue< std::function< void()> > queue_
Definition: bme680_bsec.h:106
void set_humidity_sample_rate(SampleRate sample_rate)
Definition: bme680_bsec.h:48
This Class provides the methods to read/write bytes from/to an i2c device.
Definition: i2c.h:133
esphome::sensor::Sensor * sensor
Definition: statsd.h:37
void set_iaq_accuracy_sensor(sensor::Sensor *sensor)
Definition: bme680_bsec.h:56
void set_device_id(const std::string &devid)
Definition: bme680_bsec.h:39