main/task_bmp280.c

changeset 2
3462a53e548f
parent 1
1c9894662795
equal deleted inserted replaced
1:1c9894662795 2:3462a53e548f
11 11
12 SemaphoreHandle_t xSemaphoreBMP280 = NULL; ///< Semaphore BMP280 task 12 SemaphoreHandle_t xSemaphoreBMP280 = NULL; ///< Semaphore BMP280 task
13 EventGroupHandle_t xEventGroupBMP280; ///< Events BMP280 task 13 EventGroupHandle_t xEventGroupBMP280; ///< Events BMP280 task
14 BMP280_State *bmp280_state; ///< Public state for other tasks 14 BMP280_State *bmp280_state; ///< Public state for other tasks
15 15
16 extern bmp280_params_t bmp280_params; 16 extern bmp280_params_t bmp280_params;
17 extern bmp280_t bmp280_dev; 17 extern bmp280_t bmp280_dev;
18 18
19 const int TASK_BMP280_REQUEST_DONE = BIT0; ///< All requests are done. 19 const int TASK_BMP280_REQUEST_DONE = BIT0; ///< All requests are done.
20 const int TASK_BMP280_REQUEST_TB = BIT1; ///< Request Temperature and Barometer 20 const int TASK_BMP280_REQUEST_TB = BIT1; ///< Request Temperature and Barometer
21 21
22 22
42 * Task to read BMP280 sensor on request. 42 * Task to read BMP280 sensor on request.
43 */ 43 */
44 void task_bmp280(void *pvParameter) 44 void task_bmp280(void *pvParameter)
45 { 45 {
46 float pressure, temperature, humidity; 46 float pressure, temperature, humidity;
47 int error = 0;
47 48
48 ESP_LOGI(TAG, "Starting task BMP280 sda=%d scl=%d", CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL); 49 ESP_LOGI(TAG, "Starting task BMP280 sda=%d scl=%d", CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL);
49 bmp280_state = malloc(sizeof(BMP280_State)); 50 bmp280_state = malloc(sizeof(BMP280_State));
50 51
51 bmp280_state->bmp280.valid = false; 52 bmp280_state->valid = false;
52 bmp280_state->bmp280.fake = false; 53 bmp280_state->fake = (bmp280_dev.i2c_dev.addr == 0) ? true:false;
53 bmp280_state->bmp280.address = 0; 54 bmp280_state->address = bmp280_dev.i2c_dev.addr;
54 bmp280_state->bmp280.error = BMP280_ERR_NONE; 55 bmp280_state->error = BMP280_ERR_NONE;
55 56
56 /* event handler and event group for this task */ 57 /* event handler and event group for this task */
57 xEventGroupBMP280 = xEventGroupCreate(); 58 xEventGroupBMP280 = xEventGroupCreate();
58 EventBits_t uxBits; 59 EventBits_t uxBits;
59 60
60 /* 61 /*
61 * Task loop forever. 62 * Task loop forever.
62 */ 63 */
63 ESP_LOGI(TAG, "Starting loop BMP280 sensor"); 64 ESP_LOGI(TAG, "Starting loop BMP280 sensor 0x%02x %d", bmp280_state->address, bmp280_state->fake);
64 while (1) { 65 while (1) {
65 66
66 uxBits = xEventGroupWaitBits(xEventGroupBMP280, TASK_BMP280_REQUEST_TB, pdFALSE, pdFALSE, portMAX_DELAY ); 67 uxBits = xEventGroupWaitBits(xEventGroupBMP280, TASK_BMP280_REQUEST_TB, pdFALSE, pdFALSE, portMAX_DELAY );
67 68
68 if (uxBits & TASK_BMP280_REQUEST_TB) { 69 if (uxBits & TASK_BMP280_REQUEST_TB) {
69 70
70 ESP_LOGI(TAG, "Requested BMP280 readings"); 71 if (! bmp280_state->fake) {
72 /* Real sensor is present */
73 error = bmp280_read_float(&bmp280_dev, &temperature, &pressure, &humidity);
74 if (xSemaphoreTake(xSemaphoreBMP280, 25) == pdTRUE) {
75 if (error == ESP_OK) {
76 bmp280_state->error = BMP280_ERR_NONE;
77 bmp280_state->valid = true;
78 bmp280_state->temperature = temperature;
79 bmp280_state->pressure = pressure;
80 bmp280_state->humidity = humidity;
81 } else {
82 bmp280_state->error = BMP280_ERR_READ;
83 bmp280_state->valid = false;
84 bmp280_state->temperature = 0;
85 bmp280_state->pressure = 0;
86 bmp280_state->humidity = 0;
87 }
88 xSemaphoreGive(xSemaphoreBMP280);
89 }
90 } else {
91 /* Use fake values */
92 if (xSemaphoreTake(xSemaphoreBMP280, 25) == pdTRUE) {
93 bmp280_state->error = BMP280_ERR_NONE;
94 bmp280_state->valid = true;
95 bmp280_state->temperature = 21.23;
96 bmp280_state->pressure = 101360;
97 bmp280_state->humidity = 0;
98 xSemaphoreGive(xSemaphoreBMP280);
99 }
100 }
71 101
72 if (bmp280_read_float(&bmp280_dev, &temperature, &pressure, &humidity) != ESP_OK) {
73 ESP_LOGI(TAG, "Temperature/pressure reading failed");
74 } else {
75 ESP_LOGI(TAG, "Pressure: %.2f Pa, Temperature: %.2f C", pressure, temperature);
76 }
77 xEventGroupClearBits(xEventGroupBMP280, TASK_BMP280_REQUEST_TB); 102 xEventGroupClearBits(xEventGroupBMP280, TASK_BMP280_REQUEST_TB);
78 xEventGroupSetBits(xEventGroupBMP280, TASK_BMP280_REQUEST_DONE); 103 xEventGroupSetBits(xEventGroupBMP280, TASK_BMP280_REQUEST_DONE);
79 #if 1 104 #if 1
80 // 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); 105 ESP_LOGI(TAG, " TB: %.3f C, %.1f hPa, error: %d", bmp280_state->temperature, bmp280_state->pressure / 100, bmp280_state->error);
81 #endif 106 #endif
82 } 107 }
83 } 108 }
84 } 109 }

mercurial