#include "bsp_led_ui.h"
/* ѣLED1- PA9 LED2- PA10 LED3- PA11 LED4- PA12*/
/*  */

/* LEDƵ4״̬ */
/* LED_UI[0] -> PA9״̬LED_UI[1] -> PA10״̬....Դ*/
uint8_t LED_UI[4] = {1,0,0,0};  //ʼֻн1

void led_ui_disp(uint8_t *LED_UI)
{
    // ڼ¼ǰ LED ״̬ʱ
    uint8_t temp = 0x00;
    // ʹþ̬¼֮ǰ LED ״̬ıжǷҪʾ
    static uint8_t temp_before = 0xff;  // ʼΪ 0һν뺯

    // ֻ LED_UI ǰ4ԪأӦ PA9-PA12
    for (int i = 0; i < 4; i++) 
    {
        // ǰԪصֵõ temp ĶӦλ
        if (LED_UI[i]) 
            { //  LED_UI[i] Ϊ ()
            temp |= (1 << i); //  temp ĶӦλΪ 1
            } 
        else 
            { //  ucLed[i] Ϊ ()
            temp &= ~(1 << i); //  temp ĶӦλΪ 0
            }
    }

    // ǰ״̬֮ǰ״̬ͬʱ򣬲Ÿʾ
    if (temp != temp_before) 
    {
        // ʹ BSRR ĴЧú
        // ص
        GPIOA->BSRR = (0x0F << (9 + 16));  //  PA9-PA12 õͣ+16 Ϊ˷ BSRR ĵ 16 λ (BRR)

        // Ҫõ͵
        GPIOA->BSRR = ((~temp & 0x0F) << 9);         //  temp ֵ PA9-PA12


        temp_before = temp;    // ¼¼ľ״̬
    }


}

/* ƣled_ui_proc() */
/* ãLED ʾ */
/*           ÿεled_ui_proc()ʱLED  LED_UI еֵǹر */
void led_ui_proc(void)
{

    //OLEDʾ߼дж߼
    LED_UI[0] = (oled_disp_mode == 0);
    LED_UI[1] = (oled_disp_mode == 1);
    LED_UI[2] = (oled_disp_mode == 2);
    LED_UI[3] = (oled_disp_mode == 3);
    
    //ʾǰ Led_Pos λõ LED ״̬
    led_ui_disp(LED_UI);
}
