#include <stdio.h>
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "driver/gpio.h"
#define led_pin GPIO_NUM_2
#define button_pin GPIO_NUM_4
TimerHandle_t offTimer;
void offcallback(TimerHandle_t timer){
gpio_set_level(led_pin,0);
}
static void IRAM_ATTR button_isr(void *args){
static uint32_t last_intr_time = 0;
uint32_t now = xTaskGetTickCountFromISR();
if(now - last_intr_time > pdMS_TO_TICKS(200)){
BaseType_t hp_task_woken = pdFALSE;
xTimerResetFromISR(offTimer,&hp_task_woken);
gpio_set_level(led_pin,1);
if(hp_task_woken){
portYIELD_FROM_ISR();
}
}
last_intr_time = now;
}
void app_main() {
gpio_reset_pin(led_pin);
gpio_set_direction(led_pin,GPIO_MODE_OUTPUT);
gpio_config_t btn_config = {
.pin_bit_mask = (1ULL<<button_pin),
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,
.intr_type = GPIO_INTR_NEGEDGE,
};
gpio_config(&btn_config);
offTimer = xTimerCreate("AutoOffTimer",
pdMS_TO_TICKS(5000),
pdFALSE,
(void*)0,
offcallback);
gpio_install_isr_service(0);
gpio_isr_handler_add(button_pin,button_isr,NULL);
printf("System started, press to Light up");
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4