// Code segment 3.1
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
// Function declaration
int readPinState(int pin);
// Button and LED pin definitions
#define BUTTON_PIN 4
#define LED_PIN 2
void app_main() {
// Configure button pin as input
gpio_set_direction(BUTTON_PIN, GPIO_MODE_INPUT);
// Configure LED pin as output
gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);
while (1) {
// read the button pin
int btnVal = readPinState(BUTTON_PIN);
// display the button state.
// Not pressed while state = 1, Pressed while state = 0
if (btnVal == 1) { // not pressed
vTaskDelay(pdMS_TO_TICKS(200));
printf("Button not pressed. ");
printf("Button state = %d\n", btnVal);
gpio_set_level(LED_PIN, 0); // Turn off LED
}
else { // pressed
vTaskDelay(pdMS_TO_TICKS(200));
printf("Button pressed. ");
printf("Button state = %d\n", btnVal);
gpio_set_level(LED_PIN, 1); // Turn on LED
}
}
}
/* Function Name - readPinState
* Description - read the state of a pin
* Return type - integer
* Parameters - pin - a GPIO pin
*/
int readPinState(int pin) {
return gpio_get_level(pin);
}