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 } |