i2c fixes + server improvements
This commit is contained in:
@@ -1,29 +1,52 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <bmp280.h>
|
||||
#include <esp_log.h>
|
||||
#include "ygg_bmp280.h"
|
||||
#include "ygg_state.h"
|
||||
#include "ygg_lock.h"
|
||||
|
||||
static const char *TAG = "ygg_bmp280";
|
||||
|
||||
void ygg_bmp280_init(bmp280_t *dev) {
|
||||
ESP_ERROR_CHECK(i2cdev_init());
|
||||
// UWAGA: i2cdev_init() + ygg_lock_init() robi app_main() raz przed
|
||||
// wszystkimi initami. Tutaj już nie tworzymy magistrali.
|
||||
bmp280_params_t params;
|
||||
bmp280_init_default_params(¶ms);
|
||||
memset(dev, 0, sizeof(bmp280_t));
|
||||
|
||||
ESP_ERROR_CHECK(bmp280_init_desc(dev, BMP280_I2C_ADDRESS_0, 0, I2C_MASTER_SDA, I2C_MASTER_SCL));
|
||||
// bmp280_init_desc() ustawia 1MHz - za szybko na pająka + dzieloną
|
||||
// magistralę z OLED. Zjeżdżamy do 100kHz przed pierwszym użyciem
|
||||
// (device jest dopinane do busa leniwie przy pierwszej transakcji).
|
||||
dev->i2c_dev.cfg.master.clk_speed = YGG_I2C_FREQ_HZ;
|
||||
ESP_ERROR_CHECK(bmp280_init(dev, ¶ms));
|
||||
|
||||
bool bme280p = dev->id == BME280_CHIP_ID;
|
||||
printf("BMP280: found %s\n", bme280p ? "BME280" : "BMP280");
|
||||
}
|
||||
|
||||
void ygg_bmp280_read_sensor(bmp280_t *dev) {
|
||||
float pressure, temperature, humidity;
|
||||
esp_err_t ygg_bmp280_read_sensor(bmp280_t *dev) {
|
||||
float pressure = 0, temperature = 0, humidity = 0;
|
||||
bool bme280p = dev->id == BME280_CHIP_ID;
|
||||
|
||||
if (bmp280_read_float(dev, &temperature, &pressure, &humidity) != ESP_OK)
|
||||
printf("Temperature/pressure reading failed\n");
|
||||
// Cały odczyt pod wspólnym mutexem I2C - OLED (esp_lcd) używa
|
||||
// tego samego busa bez wiedzy o mutexach i2cdev.
|
||||
// Bez tego flush LVGL potrafi wejść w środek odczytu -> timeout.
|
||||
if (!ygg_i2c_lock_take(pdMS_TO_TICKS(1000))) {
|
||||
ESP_LOGW(TAG, "I2C busy, skipping readout");
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
esp_err_t err = bmp280_read_float(dev, &temperature, &pressure, &humidity);
|
||||
ygg_i2c_lock_give();
|
||||
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Temperature/pressure reading failed: %s - keeping last good readout",
|
||||
esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
|
||||
// Aktualizuj globalny stan TYLKO przy sukcesie.
|
||||
CURR_READOUT.temperature = temperature;
|
||||
CURR_READOUT.pressure = pressure;
|
||||
CURR_READOUT.humidity = humidity;
|
||||
@@ -32,4 +55,5 @@ void ygg_bmp280_read_sensor(bmp280_t *dev) {
|
||||
printf(", Humidity: %.2f\n", humidity);
|
||||
else
|
||||
printf("\n");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user