main/task_bmp280.c

changeset 1
1c9894662795
child 2
3462a53e548f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/task_bmp280.c	Mon Mar 27 22:13:21 2023 +0200
@@ -0,0 +1,84 @@
+/**
+ * @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;
+
+    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->bmp280.valid = false;
+    bmp280_state->bmp280.fake = false;
+    bmp280_state->bmp280.address = 0;
+    bmp280_state->bmp280.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");
+    while (1) {
+
+	uxBits = xEventGroupWaitBits(xEventGroupBMP280, TASK_BMP280_REQUEST_TB, pdFALSE, pdFALSE, portMAX_DELAY );
+
+	if (uxBits & TASK_BMP280_REQUEST_TB) {
+
+	    ESP_LOGI(TAG, "Requested BMP280 readings");
+
+	    if (bmp280_read_float(&bmp280_dev, &temperature, &pressure, &humidity) != ESP_OK) {
+		ESP_LOGI(TAG, "Temperature/pressure reading failed");
+	    } else {
+		ESP_LOGI(TAG, "Pressure: %.2f Pa, Temperature: %.2f C", pressure, temperature);
+	    }
+	    xEventGroupClearBits(xEventGroupBMP280, TASK_BMP280_REQUEST_TB);
+	    xEventGroupSetBits(xEventGroupBMP280, TASK_BMP280_REQUEST_DONE);
+#if 1
+//	    ESP_LOGI(TAG, "Battery    raw: %4d, atten: %d  %.3f volt, error: %d", i2c_reading, atten, i2c_state->Batt_voltage / 1000.0, i2c_state->Batt_error);
+#endif
+	}
+    }
+}

mercurial