/**
 ******************************************************************************
 * @file    main.c
 * @author  MCU Application Team
 * @brief   Main program body
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) Puya Semiconductor Co.
 * All rights reserved.</center></h2>
 *
 * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under BSD 3-Clause license,
 * the "License"; You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                        opensource.org/licenses/BSD-3-Clause
 *
 ******************************************************************************
 */
/* Includes ------------------------------------------------------------------*/
#include "led_drivers.h"
#if APP_LED_ENABLE
static uint8_t led_run;
static uint8_t led_seg;
static uint8_t led_com;
static uint32_t LED_PORTA;
static uint32_t LED_PORTB;
static uint32_t LED_PORTF;
uint8_t led_data[LED_COUNT];
static GPIO_TypeDef *LED_PORT[LED_COUNT];
static uint16_t LED_PIN[LED_COUNT];
/**
 * @brief 数码管IO
 */
const uint8_t LED_TBL[8] = {	LED0_GPIO_PIN, LED1_GPIO_PIN, LED2_GPIO_PIN, LED3_GPIO_PIN, 
								LED4_GPIO_PIN, LED5_GPIO_PIN, LED6_GPIO_PIN, LED7_GPIO_PIN};
/********************************************************
**	函数名	void LED_Init(void)
**	描述	LED IO口初始化，
**	传入	：无
**	返回	：无
*********************************************************/
void LED_Init(void)
{
	uint8_t i;
	GPIO_TypeDef *GPIOx;
	uint32_t PINx;
    led_com = 0;
	LED_PORTA = 0;
	LED_PORTB = 0;
	LED_PORTF = 0;
    for (i = 0; i < LED_COUNT; i++)
    {
		GPIO_Init(LED_TBL[i],OUTPUT|OPENDRAIN);
		GPIO_ClearBit(LED_TBL[i]);
		GPIOx = GPIO_PORT(LED_TBL[i]);
		PINx = GPIO_PIN(LED_TBL[i]);
		/*	用于控制所有LED IO状态	*/
		if(GPIOx == GPIOA)
			LED_PORTA |= PINx;
		else if(GPIOx == GPIOB)
			LED_PORTB |= PINx;
		else if(GPIOx == GPIOF)
			LED_PORTF |= PINx;
		/*	用于控制单个LED IO状态	*/
		LED_PORT[i] = GPIOx;
		LED_PIN[i] = PINx;
    }
	LED_Default();
	led_run = 1;
}
/********************************************************
**	函数名	void LED_Sleep(void)
**	描述	数码管休眠，设置IO口状态
**	传入	：无
**	返回	：无
*********************************************************/
void LED_Sleep(void)
{
	led_run = 0;
	/*	所有LED开漏输出1，IO状态呈高阻态	*/
	LL_GPIO_SetPinOutputType(GPIOA, LED_PORTA, LL_GPIO_OUTPUT_OPENDRAIN);
	LL_GPIO_SetPinOutputType(GPIOB, LED_PORTB, LL_GPIO_OUTPUT_OPENDRAIN);
	LL_GPIO_SetPinOutputType(GPIOF, LED_PORTF, LL_GPIO_OUTPUT_OPENDRAIN);
	GPIOA->ODR |= LED_PORTA;
	GPIOB->ODR |= LED_PORTB;
	GPIOF->ODR |= LED_PORTF;
}
/********************************************************
**	函数名	void LED_Wake(void)
**	描述	LED退出休眠，继续显示
**	传入	：无
**	返回	：无
*********************************************************/
void LED_Wake(void)
{
	led_run = 1;
}
/********************************************************
**	函数名	void LED_Scan(void)
**	描述	LED扫描
**	传入	：无
**	返回	：无
*********************************************************/
void LED_Scan(void)
{
#if (TK_LED_ENABLE == 0)
	if(led_run == 0)
		return;
	/*	所有LED开漏输出1，IO状态呈高阻态	*/
	LL_GPIO_SetPinOutputType(GPIOA, LED_PORTA, LL_GPIO_OUTPUT_OPENDRAIN);
	LL_GPIO_SetPinOutputType(GPIOB, LED_PORTB, LL_GPIO_OUTPUT_OPENDRAIN);
	LL_GPIO_SetPinOutputType(GPIOF, LED_PORTF, LL_GPIO_OUTPUT_OPENDRAIN);
	GPIOA->ODR |= LED_PORTA;
	GPIOB->ODR |= LED_PORTB;
	GPIOF->ODR |= LED_PORTF;
	/*	逐个IO点亮LED	*/
	led_seg++;
	if(led_seg >= LED_COUNT)
	{
		led_seg = 0;
		led_com++;
		if(led_com >= LED_COUNT)
			led_com = 0;
		/*	当扫到自己时转为扫下一个	*/
		if(led_seg == led_com)
		{
			led_seg++;
			if(led_seg >= LED_COUNT)
				led_seg = 0;
		}
	}
	/*	当前IO口推挽输出1	*/
	LL_GPIO_SetPinOutputType(LED_PORT[led_com], LED_PIN[led_com], LL_GPIO_OUTPUT_PUSHPULL);
	/*	需要点亮时拉低LED	*/
	if(led_data[led_com] & (1 << led_seg))
	{
		LED_PORT[led_seg]->BRR = LED_PIN[led_seg];
	}
#endif
}
#if (TK_LED_ENABLE == 1)
void TK_LED_Scan(uint8_t State)
{
	uint8_t data, i;
	if (smg_run == 0)
        return;
	switch(State)
	{
		/*	关闭LED显示	*/
		case TK_LED_OFF:
			/*	所有LED开漏输出1，IO状态呈高阻态	*/
			LL_GPIO_SetPinOutputType(GPIOA, LED_PORTA, LL_GPIO_OUTPUT_OPENDRAIN);
			LL_GPIO_SetPinOutputType(GPIOB, LED_PORTB, LL_GPIO_OUTPUT_OPENDRAIN);
			LL_GPIO_SetPinOutputType(GPIOF, LED_PORTF, LL_GPIO_OUTPUT_OPENDRAIN);
			GPIOA->ODR |= LED_PORTA;
			GPIOB->ODR |= LED_PORTB;
			GPIOF->ODR |= LED_PORTF;
		break;
		/*	打开LED显示	*/
		case TK_LED_ON:
			/*	当前IO口推挽输出1	*/
			LL_GPIO_SetPinOutputType(LED_PORT[led_com], LED_PIN[led_com], LL_GPIO_OUTPUT_PUSHPULL);
			/*	需要点亮时拉低LED	*/
			if(led_data[led_com] & (1 << led_seg))
			{
				LED_PORT[led_seg]->BRR = LED_PIN[led_seg];
			}
		break;
		/*	显示下一个数据	*/
		case TK_LED_COL:
			/*	逐个IO点亮LED	*/
			led_seg++;
			if(led_seg >= LED_COUNT)
			{
				led_seg = 0;
				led_com++;
				if(led_com >= LED_COUNT)
					led_com = 0;
				/*	当扫到自己时转为扫下一个	*/
				if(led_seg == led_com)
				{
					led_seg++;
					if(led_seg >= LED_COUNT)
						led_seg = 0;
				}
			}
		break;
	} 
}
#endif

#endif
