Finished the BMP280 task.

Tue, 28 Mar 2023 11:25:46 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 28 Mar 2023 11:25:46 +0200
changeset 2
3462a53e548f
parent 1
1c9894662795
child 3
e5d91caa6ab4

Finished the BMP280 task.

main/task_bmp280.c file | annotate | diff | comparison | revisions
main/task_bmp280.h file | annotate | diff | comparison | revisions
--- a/main/task_bmp280.c	Mon Mar 27 22:13:21 2023 +0200
+++ b/main/task_bmp280.c	Tue Mar 28 11:25:46 2023 +0200
@@ -13,8 +13,8 @@
 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;
+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
@@ -44,14 +44,15 @@
 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->bmp280.valid = false;
-    bmp280_state->bmp280.fake = false;
-    bmp280_state->bmp280.address = 0;
-    bmp280_state->bmp280.error = BMP280_ERR_NONE;
+    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();
@@ -60,24 +61,48 @@
     /*
      * Task loop forever.
      */
-    ESP_LOGI(TAG, "Starting loop BMP280 sensor");
+    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) {
 
-	    ESP_LOGI(TAG, "Requested BMP280 readings");
+	    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);
+		}
+	    }
 
-	    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);
+	    ESP_LOGI(TAG, "  TB: %.3f C, %.1f hPa, error: %d", bmp280_state->temperature, bmp280_state->pressure / 100, bmp280_state->error);
 #endif
 	}
     }
--- a/main/task_bmp280.h	Mon Mar 27 22:13:21 2023 +0200
+++ b/main/task_bmp280.h	Tue Mar 28 11:25:46 2023 +0200
@@ -12,13 +12,13 @@
  * Error codes in this task
  */
 #define	BMP280_ERR_NONE			0	///< No errors
-//#define	I2C_ERR_READ			1	///< Generic read error
+#define BMP280_ERR_READ			1
 
 
 /**
- * @brief BMP280 sensor
+ * @brief Structure containing the variables for the BMP280 task.
  */
-typedef struct strBMP280 {
+typedef struct {
     bool		valid;			///< Valid measurement
     bool		fake;			///< Fake measurement
     uint8_t		address;		///< Device i2c address
@@ -26,14 +26,6 @@
     float		humidity;
     float		pressure;		///< Pressure in hPa
     int			error;			///< Error result
-} bmp280_tt;
-
-
-/**
- * @brief Structure containing the variables for the BMP280 task.
- */
-typedef struct {
-    bmp280_tt		bmp280;			///< Sensor results
 } BMP280_State;
 
 

mercurial