코드 정리

- 주석 영문으로 변경
- Allman 스타일로 통일
This commit is contained in:
2026-04-16 12:01:51 +09:00
parent c98d9ae14e
commit 2861cb9815
35 changed files with 2406 additions and 2557 deletions

View File

@@ -1,11 +1,11 @@
/*******************************************************************************
* @file led_control.c
* @brief LED 직접 제어 드라이버 (BSP 미사용)
* @brief Direct LED control driver (no BSP)
* @date 2026-03-30
*
* app_timer 1개로 2색 LED(녹색/주황)의 복합 blink 패턴 구현
* 단순 on/off 상태는 타이머 없이 즉시 GPIO 제어
* 복합 패턴(에러 등)은 phase 기반 state machine으로 처리
* Implements dual-color LED (green/orange) blink patterns with a single app_timer.
* Simple on/off states use immediate GPIO control without a timer.
* Complex patterns (e.g. error) are handled by a phase-based state machine.
******************************************************************************/
#include "led_control.h"
@@ -13,20 +13,20 @@
#include "app_timer.h"
/*==============================================================================
* 내부 상수
* Internal constants
*============================================================================*/
#define MS_TO_TICKS(ms) APP_TIMER_TICKS(ms)
/*==============================================================================
* 색상
* Colors
*============================================================================*/
#define COLOR_NONE 0
#define COLOR_GREEN 1
#define COLOR_ORANGE 2
/*==============================================================================
* 에러 패턴 상수 (No.7)
* 3Hz 깜빡 3회 = 166ms on + 166ms off × 3 = ~1, 이후 꺼짐 1
* Error pattern constants (No.7)
* 3Hz blink x3 = 166ms on + 166ms off x 3 = ~1s, then off for 1s
*============================================================================*/
#define ERROR_BLINK_ON_MS 166
#define ERROR_BLINK_OFF_MS 166
@@ -34,42 +34,42 @@
#define ERROR_PAUSE_MS 1000
/*==============================================================================
* 패턴 테이블 (단순 blink)
* Pattern table (for simple blink)
*============================================================================*/
typedef struct
{
uint32_t on_ms; /* LED 켜짐 시간 (ms) */
uint32_t off_ms; /* LED 꺼짐 시간 (ms) */
uint8_t color; /* COLOR_GREEN 또는 COLOR_ORANGE */
bool repeat; /* true: 무한 반복, false: 1회 후 OFF */
uint32_t on_ms; /* LED on duration (ms) */
uint32_t off_ms; /* LED off duration (ms) */
uint8_t color; /* COLOR_GREEN or COLOR_ORANGE */
bool repeat; /* true: repeat forever, false: once then OFF */
} led_pattern_t;
static const led_pattern_t m_patterns[LED_STATE_COUNT] =
{
[LED_STATE_OFF] = { 0, 0, COLOR_NONE, false },
[LED_STATE_POWER_ON] = { 2000, 0, COLOR_GREEN, false }, /* 초록 점등 2초 → 유지 */
[LED_STATE_POWER_OFF] = { 2000, 0, COLOR_GREEN, false }, /* 초록 점등 2초 → OFF */
[LED_STATE_ADVERTISING] = { 500, 500, COLOR_GREEN, true }, /* 초록 점멸 1초 */
[LED_STATE_DETACH_WARNING] = { 1000, 3000, COLOR_GREEN, true }, /* 초록 1 on / 3 off */
[LED_STATE_ALIGN_SEARCHING] = { 1000, 1000, COLOR_ORANGE, true }, /* 주황 점멸 1초 */
[LED_STATE_ALIGN_COMPLETE] = { 3000, 1000, COLOR_GREEN, true }, /* 초록 3 on / 1 off */
[LED_STATE_ERROR] = { 0, 0, COLOR_ORANGE, true } /* 별도 state machine */
[LED_STATE_POWER_ON] = { 2000, 0, COLOR_GREEN, false }, /* Green on 2s, stay on */
[LED_STATE_POWER_OFF] = { 2000, 0, COLOR_GREEN, false }, /* Green on 2s, then OFF */
[LED_STATE_ADVERTISING] = { 500, 500, COLOR_GREEN, true }, /* Green blink 1s interval */
[LED_STATE_DETACH_WARNING] = { 1000, 3000, COLOR_GREEN, true }, /* Green 1s on / 3s off */
[LED_STATE_ALIGN_SEARCHING] = { 1000, 1000, COLOR_ORANGE, true }, /* Orange blink 1s interval */
[LED_STATE_ALIGN_COMPLETE] = { 3000, 1000, COLOR_GREEN, true }, /* Green 3s on / 1s off */
[LED_STATE_ERROR] = { 0, 0, COLOR_ORANGE, true } /* Separate state machine */
};
/*==============================================================================
* 모듈 변수
* Module variables
*============================================================================*/
APP_TIMER_DEF(m_led_timer);
static led_state_t m_current_state = LED_STATE_OFF;
static bool m_phase_on; /* true: LED 켜진 구간, false: 꺼진 구간 */
static bool m_phase_on; /* true: LED on phase, false: off phase */
/* 에러 패턴 전용 */
static uint8_t m_error_blink_cnt; /* 현재까지 깜빡인 횟수 */
/* Error pattern only */
static uint8_t m_error_blink_cnt; /* Blink count so far */
static uint8_t m_error_phase; /* 0: blink-on, 1: blink-off, 2: pause */
/*==============================================================================
* GPIO 헬퍼
* GPIO helpers
*============================================================================*/
static inline void led_green_on(void)
@@ -122,7 +122,7 @@ static void led_color_on(uint8_t color)
}
/*==============================================================================
* 타이머 시작 헬퍼
* Timer start helper
*============================================================================*/
static void timer_start_ms(uint32_t ms)
{
@@ -131,7 +131,7 @@ static void timer_start_ms(uint32_t ms)
}
/*==============================================================================
* 에러 패턴 state machine (No.7)
* Error pattern state machine (No.7)
* phase 0: LED ON (166ms) → phase 1
* phase 1: LED OFF (166ms) → cnt++ → cnt<3 ? phase 0 : phase 2
* phase 2: PAUSE (1000ms) → cnt=0, phase 0
@@ -148,30 +148,30 @@ static void error_pattern_tick(void)
{
switch (m_error_phase)
{
case 0: /* ON 구간 끝 → OFF */
case 0: /* ON phase done -> OFF */
led_all_off();
m_error_phase = 1;
timer_start_ms(ERROR_BLINK_OFF_MS);
break;
case 1: /* OFF 구간 끝 */
case 1: /* OFF phase done */
m_error_blink_cnt++;
if (m_error_blink_cnt < ERROR_BLINK_COUNT)
{
/* 다시 ON */
/* Back to ON */
m_error_phase = 0;
led_color_on(COLOR_ORANGE);
timer_start_ms(ERROR_BLINK_ON_MS);
}
else
{
/* 3회 완료 → pause */
/* 3 blinks done -> pause */
m_error_phase = 2;
timer_start_ms(ERROR_PAUSE_MS);
}
break;
case 2: /* pause 끝 → 처음부터 */
case 2: /* Pause done -> restart */
m_error_blink_cnt = 0;
m_error_phase = 0;
led_color_on(COLOR_ORANGE);
@@ -184,13 +184,13 @@ static void error_pattern_tick(void)
}
/*==============================================================================
* 타이머 콜백
* Timer callback
*============================================================================*/
static void led_timer_handler(void * p_context)
{
(void)p_context;
/* 에러 상태는 별도 처리 */
/* Error state handled separately */
if (m_current_state == LED_STATE_ERROR)
{
error_pattern_tick();
@@ -201,7 +201,7 @@ static void led_timer_handler(void * p_context)
if (m_phase_on)
{
/* ON→OFF 전환 */
/* ON -> OFF transition */
led_all_off();
m_phase_on = false;
@@ -211,40 +211,40 @@ static void led_timer_handler(void * p_context)
}
else if (!p->repeat)
{
/* 1회성: off_ms == 0 이면 그냥 유지 (POWER_ON) 또는 끄기 (POWER_OFF) */
/* One-shot: if off_ms == 0, stay on (POWER_ON) or turn off (POWER_OFF) */
if (m_current_state == LED_STATE_POWER_OFF)
{
led_all_off();
m_current_state = LED_STATE_OFF;
}
/* POWER_ON: 점등 유지 상태 → 타이머 x */
/* POWER_ON: stay lit, no timer */
}
}
else
{
/* OFF ON 전환 */
/* OFF -> ON transition */
if (p->repeat)
{
led_color_on(p->color);
m_phase_on = true;
timer_start_ms(p->on_ms);
}
/* repeat == false && off_ms > 0 인 경우는 현재 없음 */
/* No current case where repeat == false && off_ms > 0 */
}
}
/*==============================================================================
* 공개 함수
* Public functions
*============================================================================*/
void led_init(void)
{
/* GPIO 출력 설정 */
/* Configure GPIO outputs */
nrf_gpio_cfg_output(LED_PIN_GREEN);
nrf_gpio_cfg_output(LED_PIN_ORANGE);
led_all_off();
/* 타이머 생성 (single-shot) */
/* Create timer (single-shot) */
app_timer_create(&m_led_timer, APP_TIMER_MODE_SINGLE_SHOT, led_timer_handler);
m_current_state = LED_STATE_OFF;
@@ -254,7 +254,7 @@ void led_set_state(led_state_t state)
{
if (state >= LED_STATE_COUNT) return;
/* 이전 패턴 중단 */
/* Stop previous pattern */
app_timer_stop(m_led_timer);
led_all_off();
@@ -266,15 +266,15 @@ void led_set_state(led_state_t state)
switch (state)
{
case LED_STATE_OFF:
/* 이미 all off */
/* Already all off */
break;
/* 에러 패턴: 별도 state machine */
/* Error pattern: separate state machine */
case LED_STATE_ERROR:
error_pattern_start();
break;
/* 그 외: on 구간부터 시작 */
/* All others: start from ON phase */
default:
led_color_on(p->color);
m_phase_on = true;

View File

@@ -1,10 +1,10 @@
/*******************************************************************************
* @file led_control.h
* @brief LED 직접 제어 드라이버 (BSP 미사용)
* @brief Direct LED control driver (no BSP)
* @date 2026-03-30
*
* 녹색(P0.12) + 주황(P0.29) 2색 LED를 app_timer 기반으로 제어
* 각 상태별 on/off 시간, 색상, 반복 패턴을 테이블로 관리
* Controls dual-color LED green(P0.12) + orange(P0.29) via app_timer.
* On/off timing, color, and repeat pattern managed per state in a table.
******************************************************************************/
#ifndef LED_CONTROL_H__
@@ -14,39 +14,39 @@
#include <stdbool.h>
/*==============================================================================
* LED 핀 정의
* LED pin definitions
*============================================================================*/
#define LED_PIN_GREEN NRF_GPIO_PIN_MAP(0, 12) /* 녹색 LED */
#define LED_PIN_ORANGE NRF_GPIO_PIN_MAP(0, 29) /* 주황 LED */
#define LED_ACTIVE_LOW 1 /* 1 = Active Low (GPIO LOW에서 LED 켜짐) */
#define LED_PIN_GREEN NRF_GPIO_PIN_MAP(0, 12) /* Green LED */
#define LED_PIN_ORANGE NRF_GPIO_PIN_MAP(0, 29) /* Orange LED */
#define LED_ACTIVE_LOW 1 /* 1 = Active Low (LED on when GPIO LOW) */
/*==============================================================================
* LED 상태 열거형
* LED state enumeration
*============================================================================*/
typedef enum
{
LED_STATE_OFF = 0, /* 모든 LED 소등 */
LED_STATE_POWER_ON, /* 1: 전원 ON - 초록 소등 → 녹색 점등 2 */
LED_STATE_POWER_OFF, /* 2: 전원 OFF - 초록 점등 2초 → 소등 */
LED_STATE_ADVERTISING, /* 3: 블루투스 스캐닝 - 초록 점멸 1초 간격(반복) */
LED_STATE_DETACH_WARNING, /* 4: 정상 작동중(미부착 시) - 초록 점등 1초 / 소등 3초 (반복) */
LED_STATE_ALIGN_SEARCHING, /* 5: 정렬모드(탐지중) - 주황 점멸 1초 간격 (반복) */
LED_STATE_ALIGN_COMPLETE, /* 6: 정렬모드(탐지 완료) - 초록 점등 3초 / 소등 1초 */
LED_STATE_ERROR, /* 7: 오류 발생 - 주황 3Hz 깜빡 3회 / 꺼짐 1 (반복) */
LED_STATE_COUNT /* 배열 크기 */
LED_STATE_OFF = 0, /* All LEDs off */
LED_STATE_POWER_ON, /* 1: Power ON - green on 2s */
LED_STATE_POWER_OFF, /* 2: Power OFF - green on 2s then off */
LED_STATE_ADVERTISING, /* 3: BLE advertising - green blink 1s interval (repeat) */
LED_STATE_DETACH_WARNING, /* 4: Normal operation (detached) - green 1s on / 3s off (repeat) */
LED_STATE_ALIGN_SEARCHING, /* 5: Alignment mode (searching) - orange blink 1s interval (repeat) */
LED_STATE_ALIGN_COMPLETE, /* 6: Alignment mode (complete) - green 3s on / 1s off */
LED_STATE_ERROR, /* 7: Error - orange 3Hz blink x3 / off 1s (repeat) */
LED_STATE_COUNT /* Array size */
} led_state_t;
/*==============================================================================
* 공개 함수
* Public functions
*============================================================================*/
/** @brief LED GPIO 초기화 + 타이머 생성 - main()에서 1회 호출 */
/** @brief Initialize LED GPIO + create timer - call once from main() */
void led_init(void);
/** @brief LED 상태 변경 - 이전 패턴을 즉시 중단하고 새 패턴 시작 */
/** @brief Change LED state - immediately stops previous pattern and starts new one */
void led_set_state(led_state_t state);
/** @brief 현재 LED 상태 조회 */
/** @brief Get current LED state */
led_state_t led_get_state(void);
#endif /* LED_CONTROL_H__ */