GPIO Interrupts (Advice please)
Posted: Sun May 19, 2019 5:39 am
Hi all,
I'm working with GPIO Interrupts (its my first time) and I really need advice on my code.
The thing is simple: To detect a push button with an interrupt.
The code set the interrupt and when detect the button pressed, disable the interrupt for a time.
Its ok? Is there a better way to do it? Is there a good place to read about it?
Thanks in advance!
I'm working with GPIO Interrupts (its my first time) and I really need advice on my code.
The thing is simple: To detect a push button with an interrupt.
The code set the interrupt and when detect the button pressed, disable the interrupt for a time.
Its ok? Is there a better way to do it? Is there a good place to read about it?
Thanks in advance!
Code: Select all
#include <stdio.h>
#include "esp_timer.h"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#define BLINK_LED 32 //Just a led blinking
#define BUTTON_LED_1 25 //This Led light for a second when button is pressed
#define PUSH_BUTTON_1 27
#define ESP_INTR_FLAG_DEFAULT 0
static int b_presionado_1 = 0; //a flag: 1 = button pressed, 0 = button released
static int b_letInt_1 = 0; //a flag: 0 = dont let the interrupt happen, 1 = let it
void triggered_1_isr(void* instance)
{
b_presionado_1 = 1;
b_letInt_1 = 0;
gpio_intr_disable(BUTTON_LED_1);
}
void app_main(void)
{
unsigned long tiempo;
unsigned long temporizador_1;
unsigned long temporizador_2;
int estadoLed;
gpio_pad_select_gpio(BLINK_LED);
gpio_set_direction(BLINK_LED, GPIO_MODE_OUTPUT);
gpio_pad_select_gpio(BUTTON_LED_1);
gpio_set_direction(BUTTON_LED_1, GPIO_MODE_OUTPUT);
gpio_pad_select_gpio(PUSH_BUTTON_1);
gpio_set_direction(PUSH_BUTTON_1, GPIO_MODE_INPUT);
gpio_pullup_en(PUSH_BUTTON_1);
gpio_pulldown_dis(PUSH_BUTTON_1);
gpio_set_intr_type(PUSH_BUTTON_1, GPIO_INTR_NEGEDGE);
gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
gpio_isr_handler_add(PUSH_BUTTON_1, triggered_1_isr, (void*) PUSH_BUTTON_1);
gpio_intr_enable(PUSH_BUTTON_1);
b_letInt_1 = 1;
tiempo = esp_timer_get_time();
temporizador_1 = esp_timer_get_time();
gpio_set_level(BLINK_LED, 1);
estadoLed = 0;
while (true)
{
//-------------------------- Keep a led blinking every 0,5s ---------------------------
if(esp_timer_get_time() - tiempo > 500000 && estadoLed == 0)
{
tiempo = esp_timer_get_time();
gpio_set_level(BLINK_LED, 0);
estadoLed = 1;
}
if(esp_timer_get_time() - tiempo > 500000 && estadoLed == 1)
{
tiempo = esp_timer_get_time();
gpio_set_level(BLINK_LED, 1);
estadoLed = 0;
}
//--------------------------------------------------------------------------------------
//If button is press light a led and start a timer
if(b_presionado_1)
{
temporizador_1 = esp_timer_get_time();
b_presionado_1 = 0;
gpio_set_level(BUTTON_LED_1, 0);
}
//Check the timer for the button led. If greater than 1s, then lights off and let new interrupts
if(!b_letInt_1)
{
if(esp_timer_get_time() - temporizador_1 > 1000000)
{
gpio_set_level(BUTTON_LED_1, 1);
b_letInt_1 = 1;
gpio_intr_enable(PUSH_BUTTON_1);
}
}
}
}