vscode를 실행후 blink 예제를 실행한다.
vscode 왼쪽에 EXPLORER 창을 보면 blink_example_main.c 파일을 더블클릭하여 열어준다.
/* Blink 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 "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "led_strip.h"
#include "sdkconfig.h"
//필요 파일들을 포함 시켜 준다.
static const char *TAG = "example";
//프로젝트 이름은 example이다.
/* Use project configuration menu (idf.py menuconfig) to choose the GPIO to blink,
or you can edit the following line and set a number here.
*/
#define BLINK_GPIO CONFIG_BLINK_GPIO //blink신호를 주기위한 포트가 몇번인지 셋팅
//CONFIG_BLINK_GPIO에 마우스 왼쪽 버튼을 더블 클릭후 키보드 단축키 F12을 누르면
//선언 된곳으로 이동하여 무엇으로 선언되었는지 확인 할수 있다. 여기서 CONFIG_BLINK_GPIO는 8로 선언 되어 있다.
static uint8_t s_led_state = 0; //LED 상태를 알고 점멸 시키기 우해 static(지워지지 않는) uint8_t (0부터 255까지 정수만 가지는 변수로 선언)
//변수 이름은 s_led_state로 선언하고 초기값은 0으로 한다.
#ifdef CONFIG_BLINK_LED_RMT //#ifdef는 만약 CONFIG_BLINK_LED_RMT가 선언 되어 있으면 프로그램 컴파일시
//아래 #elif CONFIG_BLINK_LED_RMT까지 컴파일 하라는 부분. 여기 예제는 선언 되어 있음.
static led_strip_handle_t led_strip;
static void blink_led(void) //s_led_state 값에 따라 led 변화가 있게 하는 부분
{
/* If the addressable LED is enabled */
if (s_led_state) {
/* Set the LED pixel using RGB from 0 (0%) to 255 (100%) for each color */
led_strip_set_pixel(led_strip, 0, 16, 16, 16);
/* Refresh the strip to send data */
led_strip_refresh(led_strip);
} else {
/* Set all LED off to clear all pixels */
led_strip_clear(led_strip);
}
}
static void configure_led(void) //led가 연결된 포트 셋팅
{
ESP_LOGI(TAG, "Example configured to blink addressable LED!");
/* LED strip initialization with the GPIO and pixels number*/
led_strip_config_t strip_config = {
.strip_gpio_num = BLINK_GPIO,
.max_leds = 1, // at least one LED on board
};
led_strip_rmt_config_t rmt_config = {
.resolution_hz = 10 * 1000 * 1000, // 10MHz
};
ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip));
/* Set all LED off to clear all pixels */
led_strip_clear(led_strip);
}
#elif CONFIG_BLINK_LED_GPIO //#elif는 c언어에서 else if를 뜻함. CONFIG_BLINK_LED_GPIO가 선언 되어 있으면 #endif까지 컴파일에 포함 하라는 이야기임.
//여기 예제는 선언 안되어 있어서 아래는 컴파일 안하고 포함 안됨.
static void blink_led(void)
{
/* Set the GPIO level according to the state (LOW or HIGH)*/
gpio_set_level(BLINK_GPIO, s_led_state); //BLINK_GPIO로 할당된 포트를 s_led_state 변수게 값이 0이면 0을 내보내고 1이면 1을 내보내게 하는부분.
}
static void configure_led(void)
{
ESP_LOGI(TAG, "Example configured to blink GPIO LED!"); //출력창에 Example configured to blink GPIO LED! 문자를 출력.
gpio_reset_pin(BLINK_GPIO); //BLINK_GPIO(8) 핀을 LOW로 리셋시키는 부분.
/* Set the GPIO as a push/pull output */
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT); //BLINK_GPIO(8) 핀을 그냥 출력 핀으로 설정함.
}
#endif
void app_main(void)
{
/* Configure the peripheral according to the LED type */
configure_led(); //led제어를 위해 포트를 셋팅 하는 부분
while (1) {
ESP_LOGI(TAG, "Turning the LED %s!", s_led_state == true ? "ON" : "OFF"); //현재 LED상태값을 PC 모니터로 출력하는 부분
blink_led(); //여기서 s_led_state 값에 따른 출력을 내보내주는 펑션을 불러오는 부분.
/* Toggle the LED state */
s_led_state = !s_led_state; //s_led_state 값이 16진수로 0x00이면 0Xff로 반전 하는 부분
vTaskDelay(CONFIG_BLINK_PERIOD / portTICK_PERIOD_MS); //led가 동작 하는것을 확인하기 위해 지연 시간을 준다.
//이게 없으면 계속 켜져 있는것처럼 보이고 컴퓨터 디버깅 창에도 메시지가 계속 나온다.
}
}
필요하면 확인하세요.