#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include "esp_event.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
ESP_EVENT_DECLARE_BASE(TIMER_EVENTS); // declaration of the timer events family
enum { // declaration of the specific events under the timer event family
TIMER_EVENT_STARTED, // raised when the timer is first started
TIMER_EVENT_EXPIRY, // raised when a period of the timer has elapsed
TIMER_EVENT_STOPPED // raised when the timer has been stopped
};
/* Event source periodic timer related definitions */
ESP_EVENT_DEFINE_BASE(TIMER_EVENTS);
void timer_started_handler(void *event_handler_arg, esp_event_base_t event_base,
int32_t event_id, void *event_data) {
static int count = 0;
count++;
printf("event_id: %ld , count : %d\n", event_id,count);
}
void app_main(void) {
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_ERROR_CHECK(
esp_event_handler_instance_register(TIMER_EVENTS, TIMER_EVENT_STARTED, timer_started_handler, NULL, NULL));
ESP_ERROR_CHECK(
esp_event_handler_instance_register(TIMER_EVENTS, TIMER_EVENT_STOPPED, timer_started_handler, NULL, NULL));
while (1) {
ESP_ERROR_CHECK(
esp_event_post(TIMER_EVENTS, TIMER_EVENT_STARTED, NULL, 0, portMAX_DELAY));
sleep(1);
ESP_ERROR_CHECK(
esp_event_post(TIMER_EVENTS, TIMER_EVENT_STOPPED, NULL, 0, portMAX_DELAY));
sleep(1);
}
// while (true) {
// printf("Hello from app_main!\n");
// sleep(1);
// }
}
esp32 event loop handle 代码
于 2023-05-01 19:06:02 首次发布