Skip to content

Commit

Permalink
examples: Use FreeRTOS timer instead of esp_timer for periodic tasks
Browse files Browse the repository at this point in the history
esp_timer is generally recommended when high precision is required.
However, since that is not the case for periodic reporting of temperature
values, replaced it with freeRTOS Timer.
  • Loading branch information
shahpiyushv committed Mar 22, 2021
1 parent 009c6d7 commit 8854443
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
15 changes: 7 additions & 8 deletions examples/multi_device/main/app_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/

#include <sdkconfig.h>
#include <freertos/FreeRTOS.h>
#include <freertos/timers.h>

#include <iot_button.h>
#include <esp_rmaker_core.h>
Expand All @@ -33,7 +35,7 @@

static bool g_power_state = DEFAULT_SWITCH_POWER;
static float g_temperature = DEFAULT_TEMPERATURE;
static esp_timer_handle_t sensor_timer;
static TimerHandle_t sensor_timer;

static void app_sensor_update(void *priv)
{
Expand All @@ -57,13 +59,10 @@ float app_get_current_temperature()
esp_err_t app_sensor_init(void)
{
g_temperature = DEFAULT_TEMPERATURE;
esp_timer_create_args_t sensor_timer_conf = {
.callback = app_sensor_update,
.dispatch_method = ESP_TIMER_TASK,
.name = "app_sensor_update_tm"
};
if (esp_timer_create(&sensor_timer_conf, &sensor_timer) == ESP_OK) {
esp_timer_start_periodic(sensor_timer, REPORTING_PERIOD * 1000000U);
sensor_timer = xTimerCreate("app_sensor_update_tm", (REPORTING_PERIOD * 1000) / portTICK_PERIOD_MS,
pdTRUE, NULL, app_sensor_update);
if (sensor_timer) {
xTimerStart(sensor_timer, 0);
return ESP_OK;
}
return ESP_FAIL;
Expand Down
18 changes: 9 additions & 9 deletions examples/temperature_sensor/main/app_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
CONDITIONS OF ANY KIND, either express or implied.
*/

#include <freertos/FreeRTOS.h>
#include <freertos/timers.h>
#include <sdkconfig.h>
#include <esp_rmaker_core.h>
#include <esp_rmaker_standard_types.h>
Expand All @@ -22,7 +24,7 @@
/* This is the GPIO on which the power will be set */
#define OUTPUT_GPIO 19

static esp_timer_handle_t sensor_timer;
static TimerHandle_t sensor_timer;

#define DEFAULT_SATURATION 100
#define DEFAULT_BRIGHTNESS 50
Expand All @@ -35,7 +37,7 @@ static uint16_t g_saturation = DEFAULT_SATURATION;
static uint16_t g_value = DEFAULT_BRIGHTNESS;
static float g_temperature;

static void app_sensor_update(void *priv)
static void app_sensor_update(TimerHandle_t handle)
{
static float delta = 0.5;
g_temperature += delta;
Expand All @@ -62,14 +64,12 @@ esp_err_t app_sensor_init(void)
if (err != ESP_OK) {
return err;
}

g_temperature = DEFAULT_TEMPERATURE;
esp_timer_create_args_t sensor_timer_conf = {
.callback = app_sensor_update,
.dispatch_method = ESP_TIMER_TASK,
.name = "app_sensor_update_tm"
};
if (esp_timer_create(&sensor_timer_conf, &sensor_timer) == ESP_OK) {
esp_timer_start_periodic(sensor_timer, REPORTING_PERIOD * 1000000U);
sensor_timer = xTimerCreate("app_sensor_update_tm", (REPORTING_PERIOD * 1000) / portTICK_PERIOD_MS,
pdTRUE, NULL, app_sensor_update);
if (sensor_timer) {
xTimerStart(sensor_timer, 0);
g_hue = (100 - g_temperature) * 2;
ws2812_led_set_hsv(g_hue, g_saturation, g_value);
return ESP_OK;
Expand Down

0 comments on commit 8854443

Please sign in to comment.