ESPHome  2022.12.8
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 
24 enum SampleRate {
28 };
29 
30 #define BME680_BSEC_SAMPLE_RATE_LOG(r) (r == SAMPLE_RATE_DEFAULT ? "Default" : (r == SAMPLE_RATE_ULP ? "ULP" : "LP"))
31 
33  public:
34  void set_temperature_offset(float offset) { this->temperature_offset_ = offset; }
35  void set_iaq_mode(IAQMode iaq_mode) { this->iaq_mode_ = iaq_mode; }
36  void set_state_save_interval(uint32_t interval) { this->state_save_interval_ms_ = interval; }
37 
38  void set_sample_rate(SampleRate sample_rate) { this->sample_rate_ = sample_rate; }
39  void set_temperature_sample_rate(SampleRate sample_rate) { this->temperature_sample_rate_ = sample_rate; }
40  void set_pressure_sample_rate(SampleRate sample_rate) { this->pressure_sample_rate_ = sample_rate; }
41  void set_humidity_sample_rate(SampleRate sample_rate) { this->humidity_sample_rate_ = sample_rate; }
42 
43  void set_temperature_sensor(sensor::Sensor *sensor) { this->temperature_sensor_ = sensor; }
44  void set_pressure_sensor(sensor::Sensor *sensor) { this->pressure_sensor_ = sensor; }
45  void set_humidity_sensor(sensor::Sensor *sensor) { this->humidity_sensor_ = sensor; }
47  void set_iaq_sensor(sensor::Sensor *sensor) { this->iaq_sensor_ = sensor; }
52 
54  static int8_t read_bytes_wrapper(uint8_t address, uint8_t a_register, uint8_t *data, uint16_t len);
55  static int8_t write_bytes_wrapper(uint8_t address, uint8_t a_register, uint8_t *data, uint16_t len);
56  static void delay_ms(uint32_t period);
57 
58  void setup() override;
59  void dump_config() override;
60  float get_setup_priority() const override;
61  void loop() override;
62 
63  protected:
64  void set_config_(const uint8_t *config);
65  float calc_sensor_sample_rate_(SampleRate sample_rate);
66  void update_subscription_();
67 
68  void run_();
69  void read_(int64_t trigger_time_ns, bsec_bme_settings_t bme680_settings);
70  void publish_(const bsec_output_t *outputs, uint8_t num_outputs);
71  int64_t get_time_ns_();
72 
73  void publish_sensor_(sensor::Sensor *sensor, float value, bool change_only = false);
74  void publish_sensor_(text_sensor::TextSensor *sensor, const std::string &value);
75 
76  void load_state_();
77  void save_state_(uint8_t accuracy);
78 
79  void queue_push_(std::function<void()> &&f) { this->queue_.push(std::move(f)); }
80 
81  struct bme680_dev bme680_;
82  bsec_library_return_t bsec_status_{BSEC_OK};
83  int8_t bme680_status_{BME680_OK};
84 
85  int64_t last_time_ms_{0};
87  int64_t next_call_ns_{0};
88 
89  std::queue<std::function<void()>> queue_;
90 
92  uint32_t state_save_interval_ms_{21600000}; // 6 hours - 4 times a day
93  uint32_t last_state_save_ms_ = 0;
94 
97 
98  SampleRate sample_rate_{SAMPLE_RATE_LP}; // Core/gas sample rate
102 
112 };
113 #endif
114 } // namespace bme680_bsec
115 } // namespace esphome
void set_co2_equivalent_sensor(sensor::Sensor *sensor)
Definition: bme680_bsec.h:50
text_sensor::TextSensor * iaq_accuracy_text_sensor_
Definition: bme680_bsec.h:108
static int8_t write_bytes_wrapper(uint8_t address, uint8_t a_register, uint8_t *data, uint16_t len)
void set_pressure_sample_rate(SampleRate sample_rate)
Definition: bme680_bsec.h:40
void queue_push_(std::function< void()> &&f)
Definition: bme680_bsec.h:79
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:63
void set_gas_resistance_sensor(sensor::Sensor *sensor)
Definition: bme680_bsec.h:46
void set_humidity_sensor(sensor::Sensor *sensor)
Definition: bme680_bsec.h:45
static BME680BSECComponent * instance
Definition: bme680_bsec.h:53
void set_pressure_sensor(sensor::Sensor *sensor)
Definition: bme680_bsec.h:44
void set_iaq_accuracy_text_sensor(text_sensor::TextSensor *sensor)
Definition: bme680_bsec.h:48
void set_state_save_interval(uint32_t interval)
Definition: bme680_bsec.h:36
void set_config_(const uint8_t *config)
Definition: bme680_bsec.cpp:58
static int8_t read_bytes_wrapper(uint8_t address, uint8_t a_register, uint8_t *data, uint16_t len)
void set_temperature_sensor(sensor::Sensor *sensor)
Definition: bme680_bsec.h:43
void set_breath_voc_equivalent_sensor(sensor::Sensor *sensor)
Definition: bme680_bsec.h:51
void read_(int64_t trigger_time_ns, bsec_bme_settings_t bme680_settings)
void set_temperature_sample_rate(SampleRate sample_rate)
Definition: bme680_bsec.h:39
void set_iaq_sensor(sensor::Sensor *sensor)
Definition: bme680_bsec.h:47
std::string size_t len
Definition: helpers.h:281
Definition: a4988.cpp:4
void set_sample_rate(SampleRate sample_rate)
Definition: bme680_bsec.h:38
Base-class for all sensors.
Definition: sensor.h:50
void publish_sensor_(sensor::Sensor *sensor, float value, bool change_only=false)
std::queue< std::function< void()> > queue_
Definition: bme680_bsec.h:89
void set_humidity_sample_rate(SampleRate sample_rate)
Definition: bme680_bsec.h:41
void set_iaq_accuracy_sensor(sensor::Sensor *sensor)
Definition: bme680_bsec.h:49