main/task_temp.c

changeset 32
84e54b14e7db
equal deleted inserted replaced
31:ec5c7794dcd6 32:84e54b14e7db
1 /**
2 * @file task_temp.c
3 * @brief The FreeRTOS task to query the temperature sensor.
4 */
5
6
7 #include "config.h"
8
9
10 static const char *TAG = "task_temp";
11
12 SemaphoreHandle_t xSemaphoreTEMP = NULL; ///< Semaphore TEMP task
13 EventGroupHandle_t xEventGroupTEMP; ///< Events TEMP task
14 TEMP_State *temp_state; ///< Public state for other tasks
15
16 const int TASK_TEMP_REQUEST_DONE = BIT0; ///< All requests are done.
17 const int TASK_TEMP_REQUEST_TEMP = BIT1; ///< Request Temperature and Barometer
18
19
20
21 void request_temp(void)
22 {
23 xEventGroupClearBits(xEventGroupTEMP, TASK_TEMP_REQUEST_DONE);
24 xEventGroupSetBits(xEventGroupTEMP, TASK_TEMP_REQUEST_TEMP);
25 }
26
27
28
29 bool ready_temp(void)
30 {
31 if (xEventGroupGetBits(xEventGroupTEMP) & TASK_TEMP_REQUEST_DONE)
32 return true;
33 return false;
34 }
35
36
37 /*
38 * Task to read the chip temperature sensor on request.
39 */
40 void task_temp(void *pvParameter)
41 {
42 float tsens_value;
43 int error = 0;
44
45 ESP_LOGI(TAG, "Starting task temperature");
46 temp_state = malloc(sizeof(TEMP_State));
47
48 temp_state->valid = false;
49 temp_state->error = TEMP_ERR_NONE;
50
51 temperature_sensor_handle_t temp_sensor = NULL;
52 temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(-10, 80);
53 ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor_config, &temp_sensor));
54
55 /* event handler and event group for this task */
56 xEventGroupTEMP = xEventGroupCreate();
57 EventBits_t uxBits;
58
59 /*
60 * Task loop forever.
61 */
62 while (1) {
63
64 uxBits = xEventGroupWaitBits(xEventGroupTEMP, TASK_TEMP_REQUEST_TEMP, pdFALSE, pdFALSE, portMAX_DELAY );
65 if (uxBits & TASK_TEMP_REQUEST_TEMP) {
66
67 ESP_ERROR_CHECK(temperature_sensor_enable(temp_sensor));
68 error = temperature_sensor_get_celsius(temp_sensor, &tsens_value);
69 ESP_ERROR_CHECK(temperature_sensor_disable(temp_sensor));
70
71 if (xSemaphoreTake(xSemaphoreTEMP, 25) == pdTRUE) {
72 if (error == ESP_OK) {
73 temp_state->error = TEMP_ERR_NONE;
74 temp_state->valid = true;
75 temp_state->temperature = tsens_value;
76 } else {
77 temp_state->error = TEMP_ERR_READ;
78 temp_state->valid = false;
79 temp_state->temperature = 0;
80 }
81 xSemaphoreGive(xSemaphoreTEMP);
82 }
83
84 xEventGroupClearBits(xEventGroupTEMP, TASK_TEMP_REQUEST_TEMP);
85 xEventGroupSetBits(xEventGroupTEMP, TASK_TEMP_REQUEST_DONE);
86 #if 0
87 ESP_LOGI(TAG, " TEMP: %.3f C, error: %d", temp_state->temperature, temp_state->error);
88 #endif
89 }
90 }
91 }

mercurial