#ifndef __LED_H__
#define __LED_H__

#include "../common/def.h"

extern uint8_t ledPinNum;
extern uint8_t ledState;
extern uint8_t ledDelay;

void ledInit();

void ledOn();
void ledOff();
void ledToggle();

void ledLeftShift(uint8_t toggle, uint8_t d);
void ledRightShift(uint8_t toggle, uint8_t d);
void ledFlower(uint8_t toggle, uint8_t d);

#endif /* __LED_H__ */
// led.c
#include "led.h"

uint8_t ledPinNum;
uint8_t ledState;
uint8_t ledDelay;

// LED 출력 설정 초기화 함수
void ledInit() {        // 매개변수로 포인터변수 선언
    LED_DDR = 0xff;             // LED Port Output 설정
    ledPinNum = 0;
    ledState = 0;
    ledDelay = 0;
}

// 전체 LED On 함수
void ledOn() {
    for (uint8_t i = 0; i < 8; i++) {
        LED_PORT |= (1 << i);
    }
}

// 전체 LED Off 함수
void ledOff() {
    for (uint8_t i = 0; i < 8; i++) {
        LED_PORT &= ~(1 << i);
    }
}

// 전체 LED On, Off 전환 함수
void ledToggle() {
    uint8_t i;

    if (ledState == 0) {
        for (i = 0; i < 8; i++) {
            LED_PORT |= (1 << i);
        }
        ledState = 1;
    }
    else if (ledState == 1) {
        for (i = 0; i < 8; i++) {
            LED_PORT &= ~(1 << i);
        }
        ledState = 0;
    }
}

// LED 왼쪽 이동하며 On 함수
// toggle set 깜빡이며 이동, d 값에 따라 delay 시간 조정
void ledLeftShift(uint8_t toggle, uint8_t d) {
    switch (toggle) {
        case 0:
        if (ledPinNum <=0) {
            LED_PORT |= (1 << 0);
            LED_PORT &= ~(1 << 7);
        }
        else {
            LED_PORT |= (1 << ledPinNum);
            LED_PORT &= ~(1 << (ledPinNum - 1));
        }

        if (ledDelay >= d) {
            if (ledPinNum >= 7) {
                ledPinNum = 0;
            }
            else {
                ledPinNum++;
            }
            ledDelay = 0;
        }
        else {
            ledDelay++;
        }
        break;
    
        case 1:
        if (ledState == 0) {
            LED_PORT |= (1 << ledPinNum);
            if (ledDelay >= d) {
                ledState = 1;
                ledDelay = 0;
            }
            else {
                ledDelay++;
            }
        }

        else if (ledState == 1) {
            LED_PORT &= ~(1 << ledPinNum);
            if (ledDelay >= d) {
                ledState = 0;
                if (ledPinNum >= 7) {
                    ledPinNum = 0;
                }
                else {
                    ledPinNum++;
                }
                ledDelay = 0;
            }
            else {
                ledDelay++;
            }
        }
        break;
    }
}

// LED 오른쪽 이동하며 On 함수
// toggle set 깜빡이며 이동, d 값에 따라 delay 시간 조정
void ledRightShift(uint8_t toggle, uint8_t d) {
    switch (toggle) {
        case 0:
        if (ledPinNum >= 7) {
            LED_PORT |= (1 << 7);
            LED_PORT &= ~(1 << 0);
        }
        else {
            LED_PORT |= (1 << ledPinNum);
            LED_PORT &= ~(1 << (ledPinNum + 1));
        }

        if (ledDelay >= d) {
            if (ledPinNum <= 0) {
                ledPinNum = 7;
            }
            else {
                ledPinNum--;
            }
            ledDelay = 0;
        }
        else {
            ledDelay++;
        }
        break;
    
        case 1:
        if (ledState == 0) {
            LED_PORT |= (1 << ledPinNum);
            if (ledDelay >= d) {
                ledState = 1;
                ledDelay = 0;
            }
            else {
                ledDelay++;
            }
        }

        else if (ledState == 1) {
            LED_PORT &= ~(1 << ledPinNum);
            if (ledDelay >= d) {
                ledState = 0;
                if (ledPinNum <= 0) {
                    ledPinNum = 7;
                }
                else {
                    ledPinNum--;
                }
                ledDelay = 0;
            }
            else {
                ledDelay++;
            }
        }
        break;
    }

}

// LED 좌우에서 동시에 이동하며 On 함수
// toggle set 깜빡이며 이동, d 값에 따라 delay 시간 조정
void ledFlower(uint8_t toggle, uint8_t d) {
    switch (toggle) {
        case 0:
        if (ledPinNum <=0) {
            LED_PORT |= (1 << 0) | (1 << 7);
        }
        else {
            LED_PORT |= (1 << ledPinNum) | (1 << (7 - ledPinNum));;
            LED_PORT &= ~((1 << (ledPinNum - 1)) | (1 << (8 - ledPinNum)));
        }
        
        if (ledDelay >= d) {
            if (ledPinNum >= 7) {
                ledPinNum = 1;
            }
            else if (ledPinNum == 3) {
                ledPinNum = 5;
            }
            else {
                ledPinNum++;
            }
            ledDelay = 0;
        }
        else {
            ledDelay++;
        }
        break;

        case 1:
        if (ledState == 0) {
            LED_PORT |= (1 << ledPinNum) | (1 << (7 - ledPinNum));
            if (ledDelay >= d) {
                ledState = 1;
                ledDelay = 0;
            }
            else {
                ledDelay++;
            }
        }
        else if (ledState == 1) {
            LED_PORT &= ~((1 << ledPinNum) | (1 << (7 - ledPinNum)));
            if (ledDelay >= d) {
                ledState = 0;
                if (ledPinNum >= 7) {
                    ledPinNum = 0;
                }
                else {
                    ledPinNum++;
                }
                ledDelay = 0;
            }
            else {
                ledDelay++;
            }
        }
        break;
    }
}