esp32-warm-water
esp32 based project for the control of a heating element based on temperature
timer.c
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2021 wolffshots
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * A note on the copyright line: https://www.copyright.gov/title17/92chap4.html#408
25  */
26 
32 #include "timer.h"
33 
34 #include <stdio.h>
35 #include <string.h>
36 #include <unistd.h>
37 
38 #include "esp_log.h"
39 #include "esp_sleep.h"
40 #include "sdkconfig.h"
41 
42 static const char *TAG = CONFIG_TIMER_LOG_TAG;
51 void general_timer_init(esp_timer_handle_t timer, void(timer_callback)(void *arg), bool periodic, int period_in_millis, char *timer_name)
52 {
53  const esp_timer_create_args_t timer_args = {
54  .callback = timer_callback, // function to call when timer is reached
55  .name = timer_name}; // name is optional, but may help identify the timer when debugging
56  ESP_ERROR_CHECK(esp_timer_create(&timer_args, &timer)); // create the timer but don't start it
57  if (periodic) // check if periodic flag
58  {
59  ESP_ERROR_CHECK(esp_timer_start_periodic(timer, 1000 * period_in_millis)); // start the timer periodically for a given period
60  }
61  else
62  {
63  ESP_ERROR_CHECK(esp_timer_start_once(timer, 1000 * period_in_millis)); // start the timer once for a given period
64  }
65  ESP_LOGI(TAG, "started timer: %s, time since boot: %lld us", timer_name, esp_timer_get_time()); // log out that the timers have started
66 }
71 void general_timer_deinit(esp_timer_handle_t timer)
72 {
73  ESP_ERROR_CHECK(esp_timer_stop(timer)); // stop timer
74  ESP_ERROR_CHECK(esp_timer_delete(timer)); // free up resources
75  ESP_LOGI(TAG, "stopped and deleted timer"); // log out that the timer has stopped
76 }
void general_timer_deinit(esp_timer_handle_t timer)
Definition: timer.c:71
void general_timer_init(esp_timer_handle_t timer, void(timer_callback)(void *arg), bool periodic, int period_in_millis, char *timer_name)
Definition: timer.c:51
defininitions for wrapper component which assists in setting up timers
const char * TAG
Definition: wifi_sta.c:42