main/co2meter.c

changeset 38
46e2c385e9de
parent 37
358bbd5b608e
child 47
1ab1f4a8c328
equal deleted inserted replaced
37:358bbd5b608e 38:46e2c385e9de
8 static const char *TAG = "co2meter"; 8 static const char *TAG = "co2meter";
9 9
10 10
11 const esp_app_desc_t *app_desc = NULL; ///< Application description 11 const esp_app_desc_t *app_desc = NULL; ///< Application description
12 int Main_Loop1 = ML1_INIT; ///< Loop 1 init 12 int Main_Loop1 = ML1_INIT; ///< Loop 1 init
13 bool System_TimeOk = false; ///< System time status
14 time_t now; ///< Current time
15 struct tm timeinfo; ///< Current time structure
16 char strftime_buf[64]; ///< Time buffer
17 int num_sensors = 0; ///< Detected DS18B20 sensors 13 int num_sensors = 0; ///< Detected DS18B20 sensors
18 static RTC_DATA_ATTR struct timeval sleep_enter_time;
19 static TaskHandle_t xTaskDS18B20 = NULL; 14 static TaskHandle_t xTaskDS18B20 = NULL;
20 static TaskHandle_t xTaskADC = NULL; 15 static TaskHandle_t xTaskADC = NULL;
21 static TaskHandle_t xTaskWifi = NULL; 16 static TaskHandle_t xTaskWifi = NULL;
22 static TaskHandle_t xTaskMQTT = NULL; 17 static TaskHandle_t xTaskMQTT = NULL;
23 static TaskHandle_t xTaskUser = NULL; 18 static TaskHandle_t xTaskUser = NULL;
35 30
36 31
37 32
38 void app_main() 33 void app_main()
39 { 34 {
40 struct timeval now;
41 gettimeofday(&now, NULL);
42 int sleep_time_ms = (now.tv_sec - sleep_enter_time.tv_sec) * 1000 + (now.tv_usec - sleep_enter_time.tv_usec) / 1000;
43 esp_err_t ret; 35 esp_err_t ret;
44 36
45 Main_Loop1 = ML1_INIT; 37 Main_Loop1 = ML1_INIT;
46 app_desc = esp_ota_get_app_description(); 38 app_desc = esp_ota_get_app_description();
47 /* event handler and event group for the user interface */ 39 /*
40 * event handler and event group for the user interface
41 */
48 xEventGroupUser = xEventGroupCreate(); 42 xEventGroupUser = xEventGroupCreate();
49 43 user_cold();
50 switch (esp_sleep_get_wakeup_cause()) {
51 case ESP_SLEEP_WAKEUP_EXT1: {
52 ESP_LOGI(TAG, "Starting from deep sleep, Rotary switch pressed");
53 user_wakeup();
54 break;
55 }
56 case ESP_SLEEP_WAKEUP_TIMER: {
57 ESP_LOGI(TAG, "Starting from deep sleep, timer wakeup after %dms", sleep_time_ms);
58 break;
59 }
60 case ESP_SLEEP_WAKEUP_UNDEFINED:
61 default:
62 ESP_LOGI(TAG, "Starting from hard reset");
63 user_cold();
64 }
65
66 const int wakeup_time_sec = 55;
67 ESP_LOGI(TAG, "Enabling timer wakeup, %ds", wakeup_time_sec);
68 esp_sleep_enable_timer_wakeup(wakeup_time_sec * 1000000);
69 const uint64_t ext_wakeup_pin_1_mask = 1ULL << ROT_ENC_SW_GPIO;
70
71 ESP_LOGI(TAG, "Enabling EXT1 wakeup on pin GPIO%d", ROT_ENC_SW_GPIO);
72 esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask, ESP_EXT1_WAKEUP_ALL_LOW);
73 44
74 /* 45 /*
75 * Initialize NVS 46 * Initialize NVS
76 */ 47 */
77 ret = nvs_flash_init(); 48 ret = nvs_flash_init();
294 user_refresh(); 265 user_refresh();
295 } 266 }
296 break; 267 break;
297 268
298 case ML1_DONE: 269 case ML1_DONE:
270 /* Wait here until the timer resets the loop */
299 break; 271 break;
300 } 272 }
301
302 if (Main_Loop1 == ML1_DONE && ! user_busy())
303 break;
304
305 vTaskDelay(10 / portTICK_PERIOD_MS); 273 vTaskDelay(10 / portTICK_PERIOD_MS);
306 } 274 }
307 275
308 ESP_LOGI(TAG, "Entering deep sleep");
309 gettimeofday(&sleep_enter_time, NULL);
310 esp_deep_sleep_start();
311
312 // ESP_LOGI(TAG, "Do nothing loop");
313 // vTaskDelay(55000 / portTICK_PERIOD_MS);
314
315 Main_Loop1 = ML1_INIT; 276 Main_Loop1 = ML1_INIT;
316 } 277 }
317 } 278 }
318 279

mercurial