/* Interrupt Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#define LED_RED GPIO_NUM_2
#define BTN_RED GPIO_NUM_13
#define BTN_UP GPIO_NUM_21
#define BTN_DN GPIO_NUM_5
#define BTN_L GPIO_NUM_19
#define BTN_R GPIO_NUM_18
gpio_num_t buttons[4] = {BTN_UP, BTN_DN, BTN_L, BTN_R};
#define DELAY_TIME 200
volatile uint8_t button_pressed = 0;
static void gpio_isr_handler(void* arg) {
static TickType_t last_press = 0;
TickType_t this_press = xTaskGetTickCountFromISR()
if (gpio_get_level(*((gpio_num_t *) arg)) == false
&& this_press > last_press + pdMS_TO_TICKS(100)) {
button_pressed = *((uint8_t *) arg);
}
last_press = this_press;
}
void button_config()
{
gpio_install_isr_service(0);
printf("configuring buttons\n");
for (int i = 0; i < 4; i++) {
gpio_config_t button_conf = {
.pin_bit_mask = 1 << buttons[i],
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_ANYEDGE // Interrupt on button press
};
ESP_ERROR_CHECK(gpio_config(&button_conf));
ESP_ERROR_CHECK(gpio_isr_handler_add(buttons[i], gpio_isr_handler, (void *) (buttons + i)));
}
printf("config complete\n");
}
void led_config()
{
gpio_reset_pin(LED_RED);
gpio_set_direction(LED_RED, GPIO_MODE_OUTPUT);
}
extern "C" void app_main()
{
uint8_t led_value = 0;
button_config();
led_config();
while (1) {
if (button_pressed) {
printf("%d \n", button_pressed);
button_pressed = false;
led_value = !led_value;
gpio_set_level(LED_RED, led_value);
}
vTaskDelay(DELAY_TIME / portTICK_PERIOD_MS);
}
}