ESPHome 2025.5.0
Loading...
Searching...
No Matches
bme680_bsec.h
Go to the documentation of this file.
1#pragma once
2
9#include <map>
10
11#ifdef USE_BSEC
12#include <bsec.h>
13#endif
14
15namespace esphome {
16namespace bme680_bsec {
17#ifdef USE_BSEC
18
23
28
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
51 void set_pressure_sensor(sensor::Sensor *sensor) { this->pressure_sensor_ = sensor; }
52 void set_humidity_sensor(sensor::Sensor *sensor) { this->humidity_sensor_ = sensor; }
54 void set_iaq_sensor(sensor::Sensor *sensor) { this->iaq_sensor_ = sensor; }
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);
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
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_device_id(const std::string &devid)
Definition bme680_bsec.h:39
void set_pressure_sensor(sensor::Sensor *sensor)
Definition bme680_bsec.h:51
std::queue< std::function< void()> > queue_
void set_state_save_interval(uint32_t interval)
Definition bme680_bsec.h:42
void set_co2_equivalent_sensor(sensor::Sensor *sensor)
Definition bme680_bsec.h:57
void set_iaq_accuracy_sensor(sensor::Sensor *sensor)
Definition bme680_bsec.h:56
void set_breath_voc_equivalent_sensor(sensor::Sensor *sensor)
Definition bme680_bsec.h:58
static std::vector< BME680BSECComponent * > instances
Definition bme680_bsec.h:60
void set_humidity_sensor(sensor::Sensor *sensor)
Definition bme680_bsec.h:52
void set_pressure_sample_rate(SampleRate sample_rate)
Definition bme680_bsec.h:47
void set_humidity_sample_rate(SampleRate sample_rate)
Definition bme680_bsec.h:48
static int8_t read_bytes_wrapper(uint8_t devid, uint8_t a_register, uint8_t *data, uint16_t len)
void set_supply_voltage(SupplyVoltage supply_voltage)
Definition bme680_bsec.h:43
static uint8_t work_buffer_[BSEC_MAX_WORKBUFFER_SIZE]
Definition bme680_bsec.h:97
void queue_push_(std::function< void()> &&f)
Definition bme680_bsec.h:95
float calc_sensor_sample_rate_(SampleRate sample_rate)
static int8_t write_bytes_wrapper(uint8_t devid, uint8_t a_register, uint8_t *data, uint16_t len)
uint8_t bsec_state_data_[BSEC_MAX_STATE_BLOB_SIZE]
void set_iaq_sensor(sensor::Sensor *sensor)
Definition bme680_bsec.h:54
void publish_(const bsec_output_t *outputs, uint8_t num_outputs)
void publish_sensor_(sensor::Sensor *sensor, float value, bool change_only=false)
text_sensor::TextSensor * iaq_accuracy_text_sensor_
void set_sample_rate(SampleRate sample_rate)
Definition bme680_bsec.h:45
void set_temperature_sample_rate(SampleRate sample_rate)
Definition bme680_bsec.h:46
void set_gas_resistance_sensor(sensor::Sensor *sensor)
Definition bme680_bsec.h:53
void set_iaq_accuracy_text_sensor(text_sensor::TextSensor *sensor)
Definition bme680_bsec.h:55
void set_temperature_sensor(sensor::Sensor *sensor)
Definition bme680_bsec.h:50
static void delay_ms(uint32_t period)
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:133
Base-class for all sensors.
Definition sensor.h:57
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:301