#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h" // Include ESP logging header
// Define the GPIO pin for the LED
#define LED_GPIO_PIN GPIO_NUM_2
// Define a log tag for this module
static const char *TAG = "LED_CONTROLLER";
// LED Control Class
class LEDController {
public:
// Constructor: Initialize the LED GPIO pin
LEDController(gpio_num_t gpio_pin) : _gpio_pin(gpio_pin) {
// Configure the GPIO pin as output
gpio_pad_select_gpio(_gpio_pin);
gpio_set_direction(_gpio_pin, GPIO_MODE_OUTPUT);
ESP_LOGI(TAG, "LED initialized on GPIO %d", _gpio_pin);
}
// Turn the LED on
void turnOn() {
gpio_set_level(_gpio_pin, 1);
ESP_LOGI(TAG, "LED turned ON");
}
// Turn the LED off
void turnOff() {
gpio_set_level(_gpio_pin, 0);
ESP_LOGI(TAG, "LED turned OFF");
}
// Make the LED blink with a specified interval (in milliseconds)
void blink(int interval_ms) {
turnOn(); // Turn the LED on
ESP_LOGI(TAG, "LED is blinking ON");
vTaskDelay(interval_ms / portTICK_PERIOD_MS); // Delay for the specified interval
turnOff(); // Turn the LED off
ESP_LOGI(TAG, "LED is blinking OFF");
vTaskDelay(interval_ms / portTICK_PERIOD_MS); // Delay again
}
private:
gpio_num_t _gpio_pin; // Store the GPIO pin number for the LED
};
// Main program
extern "C" void app_main(void) {
// Create an LED controller object for GPIO 2
LEDController led(LED_GPIO_PIN);
// Turn the LED on for 2 seconds
ESP_LOGI(TAG, "Turning LED on for 2 seconds...");
led.turnOn(); // Turn on the LED
vTaskDelay(2000 / portTICK_PERIOD_MS); // Wait for 2 seconds
// Turn the LED off for 2 seconds
ESP_LOGI(TAG, "Turning LED off for 2 seconds...");
led.turnOff(); // Turn off the LED
vTaskDelay(2000 / portTICK_PERIOD_MS); // Wait for 2 seconds
// Blink the LED 5 times with a 500ms interval
ESP_LOGI(TAG, "Blinking LED 5 times with 500ms interval...");
for (int i = 0; i < 5; ++i) {
led.blink(500); // Blink every 500 milliseconds
}
// Turn the LED on again for 3 seconds
ESP_LOGI(TAG, "Turning LED on again for 3 seconds...");
led.turnOn(); // Turn on the LED
vTaskDelay(3000 / portTICK_PERIOD_MS); // Keep it on for 3 seconds
// Finally, turn the LED off
ESP_LOGI(TAG, "Turning LED off finally...");
led.turnOff(); // Turn off the LED
}