87 lines
2.8 KiB
C++
87 lines
2.8 KiB
C++
// main.cpp
|
|
#include <iostream>
|
|
#include <cstdint>
|
|
#include <thread>
|
|
#include <chrono>
|
|
#include "bme68x.h"
|
|
#include "common.h"
|
|
|
|
int main()
|
|
{
|
|
struct bme68x_dev dev;
|
|
|
|
// 1) Initialize the I²C interface
|
|
if (bme68x_interface_init(&dev, BME68X_I2C_INTF) != BME68X_OK) {
|
|
std::cerr << "ERROR: I2C interface init failed\n";
|
|
return -1;
|
|
}
|
|
|
|
// 2) Probe & reset the sensor
|
|
if (bme68x_init(&dev) != BME68X_OK) {
|
|
std::cerr << "ERROR: Sensor init failed\n";
|
|
bme68x_interface_deinit(&dev);
|
|
return -1;
|
|
}
|
|
// 3) Configure T/H/P oversampling + 1 s standby (ODR) for continuous reads
|
|
struct bme68x_conf conf = {};
|
|
conf.os_temp = BME68X_OS_16X;
|
|
conf.os_pres = BME68X_OS_4X;
|
|
conf.os_hum = BME68X_OS_4X;
|
|
conf.filter = BME68X_FILTER_SIZE_3;
|
|
conf.odr = BME68X_ODR_1000_MS; // 1 000 ms standby :contentReference[oaicite:0]{index=0}
|
|
int8_t rslt=0;
|
|
rslt = bme68x_set_conf(&conf, &dev);
|
|
if (rslt != BME68X_OK) {
|
|
std::cerr << "ERROR: set_conf failed (" << int(rslt) << ")\n";
|
|
bme68x_interface_deinit(&dev);
|
|
return -1;
|
|
}
|
|
|
|
struct bme68x_heatr_conf heatr_conf={};
|
|
heatr_conf.enable=BME68X_DISABLE;
|
|
rslt = bme68x_set_heatr_conf(BME68X_SLEEP_MODE,&heatr_conf, &dev);
|
|
if (rslt != BME68X_OK) {
|
|
std::cerr << "ERROR: set_heatr_conf failed (" << int(rslt) << ")\n";
|
|
bme68x_interface_deinit(&dev);
|
|
return -1;
|
|
}
|
|
// 4) Start continuous (sequential) mode
|
|
rslt = bme68x_set_op_mode(BME68X_SEQUENTIAL_MODE, &dev);
|
|
if (rslt != BME68X_OK) {
|
|
std::cerr << "ERROR: set_op_mode failed (" << int(rslt) << ")\n";
|
|
bme68x_interface_deinit(&dev);
|
|
return -1;
|
|
}
|
|
|
|
std::cout << "Continuous mode: reading every 1 s...\n";
|
|
|
|
// 5) Loop forever: wait standby interval, then read latest data
|
|
while (true) {
|
|
// wait 1 s (standby time) before next batch
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
|
|
|
struct bme68x_data data[3];
|
|
uint8_t n_fields = 0;
|
|
|
|
rslt = bme68x_get_data(BME68X_SEQUENTIAL_MODE, data, &n_fields, &dev);
|
|
if (rslt != BME68X_OK) {
|
|
std::cerr << "WARN: get_data error (" << int(rslt) << ")\n";
|
|
continue;
|
|
}
|
|
|
|
// print all new fields (typically 3 values per cycle)
|
|
for (uint8_t i = 0; i < n_fields; i++) {
|
|
std::cout
|
|
<< "T=" << (data[i].temperature * (9.0f/5.0f))+32.0f << " °F, "
|
|
<< "P=" << data[i].pressure/1000.0f << " kPa, "
|
|
<< "H=" << data[i].humidity << " %RH, "
|
|
<< "G=" << data[i].gas_resistance << " Ω\n";
|
|
}
|
|
std::cout << "-------------------------\n";
|
|
}
|
|
|
|
// (never reached, but good practice)
|
|
bme68x_interface_deinit(&dev);
|
|
return 0;
|
|
}
|