//Listing 6.3 Interrupts example
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
void IRAM_ATTR button_isr_handler(void* arg);
#define BUTTON_PIN 4 // Button connected to GPIO4
void app_main() {
// Configuring GPIO as input
gpio_set_direction(BUTTON_PIN, GPIO_MODE_INPUT);
gpio_set_pull_mode(BUTTON_PIN, GPIO_PULLUP_ONLY);
// Setting interrupt on FALLING edge (button press)
gpio_set_intr_type(BUTTON_PIN, GPIO_INTR_NEGEDGE);
// Install ISR service (interrupt handling)
gpio_install_isr_service(0);
// Attaching ISR handler to BUTTON_PIN
gpio_isr_handler_add(BUTTON_PIN, button_isr_handler, NULL);
while (1) {
printf("Main loop running...\n");
vTaskDelay(pdMS_TO_TICKS(500)); // loop continues running
}
}
// Interrupt Service Routine (ISR)
void IRAM_ATTR button_isr_handler(void* arg) {
printf("Interrupt: Button Pressed!\n");
}