main/task_bmp280.c

Sun, 16 Apr 2023 12:27:12 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 16 Apr 2023 12:27:12 +0200
changeset 30
7448b8dd4288
parent 2
3462a53e548f
permissions
-rw-r--r--

Preparations for BLE GATT. Added extra time after INA219 is powered on before measurement. Reduced LEDC frequency to 60 Hz, that makes the LED lights less nervous. Hardware mod on output 4, now needs external pulldown resistor.

/**
 * @file task_bmp280.c
 * @brief The FreeRTOS task to query the BMP280 sensor.
 */


#include "config.h"


static const char		*TAG = "task_bmp280";

SemaphoreHandle_t		xSemaphoreBMP280 = NULL;	///< Semaphore BMP280 task
EventGroupHandle_t		xEventGroupBMP280;		///< Events BMP280 task
BMP280_State			*bmp280_state;			///< Public state for other tasks

extern bmp280_params_t		bmp280_params;
extern bmp280_t			bmp280_dev;

const int TASK_BMP280_REQUEST_DONE = BIT0;			///< All requests are done.
const int TASK_BMP280_REQUEST_TB = BIT1;			///< Request Temperature and Barometer



void request_bmp280(void)
{
    xEventGroupClearBits(xEventGroupBMP280, TASK_BMP280_REQUEST_DONE);
    xEventGroupSetBits(xEventGroupBMP280, TASK_BMP280_REQUEST_TB);
}



bool ready_bmp280(void)
{
    if (xEventGroupGetBits(xEventGroupBMP280) & TASK_BMP280_REQUEST_DONE)
	return true;
    return false;
}



/*
 * Task to read BMP280 sensor on request.
 */
void task_bmp280(void *pvParameter)
{
    float pressure, temperature, humidity;
    int		error = 0;

    ESP_LOGI(TAG, "Starting task BMP280 sda=%d scl=%d", CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL);
    bmp280_state = malloc(sizeof(BMP280_State));
    
    bmp280_state->valid = false;
    bmp280_state->fake = (bmp280_dev.i2c_dev.addr == 0) ? true:false;
    bmp280_state->address = bmp280_dev.i2c_dev.addr;
    bmp280_state->error = BMP280_ERR_NONE;

    /* event handler and event group for this task */
    xEventGroupBMP280 = xEventGroupCreate();
    EventBits_t uxBits;

    /*
     * Task loop forever.
     */
    ESP_LOGI(TAG, "Starting loop BMP280 sensor 0x%02x %d", bmp280_state->address, bmp280_state->fake);
    while (1) {

	uxBits = xEventGroupWaitBits(xEventGroupBMP280, TASK_BMP280_REQUEST_TB, pdFALSE, pdFALSE, portMAX_DELAY );

	if (uxBits & TASK_BMP280_REQUEST_TB) {

	    if (! bmp280_state->fake) {
		/* Real sensor is present */
		error = bmp280_read_float(&bmp280_dev, &temperature, &pressure, &humidity);
		if (xSemaphoreTake(xSemaphoreBMP280, 25) == pdTRUE) {
		    if (error == ESP_OK) {
			bmp280_state->error = BMP280_ERR_NONE;
			bmp280_state->valid = true;
			bmp280_state->temperature = temperature;
			bmp280_state->pressure = pressure;
			bmp280_state->humidity = humidity;
		    } else {
			bmp280_state->error = BMP280_ERR_READ;
			bmp280_state->valid = false;
			bmp280_state->temperature = 0;
			bmp280_state->pressure = 0;
			bmp280_state->humidity = 0;
		    }
		    xSemaphoreGive(xSemaphoreBMP280);
		}
	    } else {
		/* Use fake values */
		if (xSemaphoreTake(xSemaphoreBMP280, 25) == pdTRUE) {
		    bmp280_state->error = BMP280_ERR_NONE;
                    bmp280_state->valid = true;
		    bmp280_state->temperature = 21.23;
                    bmp280_state->pressure = 101360;
                    bmp280_state->humidity = 0;
		    xSemaphoreGive(xSemaphoreBMP280);
		}
	    }

	    xEventGroupClearBits(xEventGroupBMP280, TASK_BMP280_REQUEST_TB);
	    xEventGroupSetBits(xEventGroupBMP280, TASK_BMP280_REQUEST_DONE);
#if 1
	    ESP_LOGI(TAG, "  TB: %.3f C, %.1f hPa, error: %d", bmp280_state->temperature, bmp280_state->pressure / 100, bmp280_state->error);
#endif
	}
    }
}

mercurial