#include<stdio.h>
#include"freertos/FreeRTOS.h"
#include "freertos/task.h"
#include"driver/gpio.h"
#include"esp_log.h"
#include "sdkconfig.h"
#include <inttypes.h>
#define LED 2
#define Push_Button 4
bool gpio_value = false;//default state to be
static void IRAM_ATTR gpio_isr_handler(void *arg) //handler function of the code after isr
{
/*
Good to DO - Implement the bouncing algorithm
to eliminate the Spurious Interrupt trigger
*/
/*Disable the interrupt*/
gpio_intr_disable(Push_Button);//Disable the Interrupt on the LED Pin
gpio_isr_handler_remove(Push_Button); //Remove the Interrupt service routine on the Push Button
/*Button is pressed. Toggle the LED */
gpio_set_level(LED, gpio_value); //Set the level of the LED pin
/*Change state of the LED*/
gpio_value = !gpio_value; //Reset the level of the LED pin
/*Re-Enable the Interrupt*/
gpio_isr_handler_add(Push_Button, gpio_isr_handler, NULL); //Enable the interrupt routine on the push button
gpio_intr_enable(Push_Button); //Enable the Interrupt on the Push button
}
void app_main(void)
{
/*Reset the pin*/
gpio_reset_pin(LED); //Reset the LED pin
gpio_reset_pin(Push_Button); //Reset the PushButton Pin
/*Set the GPIOs to Output mode*/
gpio_set_direction(LED, GPIO_MODE_OUTPUT); //Set the GPIO MODE of the LED as output
gpio_set_direction(Push_Button, GPIO_MODE_INPUT); //Set the GPIO MODE of the Push button as input
/*Enable Pullup for Input PIN*/
gpio_pullup_en(Push_Button); //Enable Pullup
/*Disable Pulldown for Input Pin*/
gpio_pulldown_dis(Push_Button);
/*Configure Raising Edge detection Interrupt for Input PIn*/
gpio_set_intr_type(Push_Button, GPIO_INTR_POSEDGE); //Set the interrupt on the GPIO as positive edge
//Whenever press then release the button->it rises->set interrupt to positive edge which is the period that signal is changing from LOW to HIGH
/*install gpio isr service to default values*/
gpio_install_isr_service(0);
/*Attach the ISR to the GPIO interrupt*/
gpio_isr_handler_add(Push_Button, gpio_isr_handler,NULL);
/*Enable the Interrupt*/
gpio_intr_enable(Push_Button);
while(true){
if(gpio_get_level(LED))
printf("ON");
vTaskDelay(100/portTICK_PERIOD_MS);
if(!gpio_get_level(LED))
printf("OFF\n");
vTaskDelay(100/portTICK_PERIOD_MS);
}
}