#include "hal_led.h"
#include "bsp_gpio.h"
#include "nrf_gpio.h"
#include "usr_config.h"
#include "bsp_time.h"
#include "system.h"
#include "hal_ble_client.h"
#include "bsp_pwm.h"
#include "nrf_delay.h"

#define LED_ENABLE						0
#define LED_DISABLE						1

#define LED_DEFAULT_NUMBER		4

#define ONE_LED_LIGHT_TIME		(3*4*8*4*0.125)		//us

//高位先发，按照GRB的顺序发送数据
//50us以上reset
//#define WS_L								0,0x8000,0x8000,0x8000
//#define WS_H								0,0,0,0x8000

static pwm_values_common_t led_color_seq_values[3][4*8]				= {0};
static nrf_pwm_sequence_t *led_color_seq 											= NULL;
static const uint16_t WS_H[4] 																= {0,0,0,0x8000};
static const uint16_t WS_L[4] 																= {0,0x8000,0x8000,0x8000};

struct WS_t{
	uint8_t  onoff; 
	uint32_t color;		//颜色
};
static struct WS_t m_wsled[LED_NUM_OF_LED]									=	{0};


/**
	@brief 设置灯的时序
	@param color - [in] 灯的颜色
	@return 无
*/
static void WS2812_DisplayDot(uint32_t col)
{
	uint32_t col_r = (col & 0x00FF0000) >> 16;
	uint32_t col_g = (col & 0x0000FF00) >> 8;
	uint32_t col_b = col & 0x000000FF;
	
	uint32_t t = 0x80;
	uint32_t index = 0;
	for(int i=0;i<8;i++)
	{
		index = i*4;
		if(col_g & t){
			led_color_seq_values[0][index+0] = WS_H[0];
			led_color_seq_values[0][index+1] = WS_H[1];
			led_color_seq_values[0][index+2] = WS_H[2];
			led_color_seq_values[0][index+3] = WS_H[3];
		}else{
			led_color_seq_values[0][index+0] = WS_L[0];
			led_color_seq_values[0][index+1] = WS_L[1];
			led_color_seq_values[0][index+2] = WS_L[2];
			led_color_seq_values[0][index+3] = WS_L[3];
		}
		index = i*4;
		if(col_r & t){
			led_color_seq_values[1][index+0] = WS_H[0];
			led_color_seq_values[1][index+1] = WS_H[1];
			led_color_seq_values[1][index+2] = WS_H[2];
			led_color_seq_values[1][index+3] = WS_H[3];
		}else{
			led_color_seq_values[1][index+0] = WS_L[0];
			led_color_seq_values[1][index+1] = WS_L[1];
			led_color_seq_values[1][index+2] = WS_L[2];
			led_color_seq_values[1][index+3] = WS_L[3];
		}
		index = i*4;
		if(col_b & t){
			led_color_seq_values[2][index+0] = WS_H[0];
			led_color_seq_values[2][index+1] = WS_H[1];
			led_color_seq_values[2][index+2] = WS_H[2];
			led_color_seq_values[2][index+3] = WS_H[3];
		}else{
			led_color_seq_values[2][index+0] = WS_L[0];
			led_color_seq_values[2][index+1] = WS_L[1];
			led_color_seq_values[2][index+2] = WS_L[2];
			led_color_seq_values[2][index+3] = WS_L[3];
		}
		t = t>>1;
	}		

}

static uint8_t reflash =0 ;

//强制关闭LED
void LED_Close_Enforce(void){
	 DEBUG_LOG("!!!!!!LED_Close_Enforce\n");
	 nrf_gpio_pin_write(PIN_LED_ENABLE,LED_DISABLE); 
	 reflash =0 ;
}

void LED_SetColor(uint8_t n,uint32_t color)
{ 
	if(n>=LED_NUM_OF_LED) return;
	m_wsled[n].color = color;
	reflash = 1;
	DEBUG_LOG("WS2812_SetColor[%d]:0x%4X\n",n,color);
}

void LED_Stop(uint8_t n)
{
	if(n>=LED_NUM_OF_LED) return;
	if(0 == m_wsled[n].onoff) return;
	DEBUG_LOG("LED_Stop:%d\n",n);
	m_wsled[n].onoff = 0;
	reflash =1;
}

void LED_Start(uint8_t n,uint32_t color)
{ 
	if(n>=LED_NUM_OF_LED) return;
	if(1 == m_wsled[n].onoff && m_wsled[n].color == color) return;
	m_wsled[n].onoff = 1;
	m_wsled[n].color = color;
	reflash =1;
	DEBUG_LOG("LED_Start:%d,%X\n",n,color);
}

void LED_Process(void)
{
  static uint8_t state =0;
	uint8_t i =0;
	switch(state){
		case 0:
					if(reflash){reflash=0;
						for(i=LED_NUM_OF_LED-1;i>0;i--){ //显示优先级最高的灯
							if(m_wsled[i].onoff>0){ 
								DEBUG_LOG("!!!!!hal_WS2812_Process->play[%d]\n",i);
								WS2812_DisplayDot(m_wsled[i].color);
								break;
							}
						}
						if(i==0) {
							nrf_gpio_pin_write(PIN_LED_ENABLE,LED_DISABLE); //无灯显示,关闭电源
							DEBUG_LOG("!!!!LED_DISABLE\n");
						}
						else{
							nrf_gpio_pin_write(PIN_LED_ENABLE,LED_ENABLE); //打开灯电源
							state = 1;
						}
					}
					break;
		case 1:
			  DEBUG_LOG("!!!!!!SetSimplePwmPlayBack\n",i);
			  SetSimplePwmPlayBack(led_color_seq, LED_DEFAULT_NUMBER, PWM_FLAG_STOP);
			  state =0;
		    break;
		default:state = 0;break;
	}
}



#if DEBUG_LEDRGB

void WS2812_Test(void)
{
	static uint8_t flag = 0;
	if(flag==0){ flag = 1;
		LED_Start(WS2812_TEST,WS2812_COLOR_ORANGE);
	}else{ flag = 0;
		LED_Stop(WS2812_TEST);
	}
}

#endif

void cb_LED_Wakeup(uint32_t t)
{
	Pwm_Initialize();
//	DEBUG_LOG("!!!!cb_LED_Wakeup\n");
}

void cb_LED_Sleep(uint32_t t)
{
	nrf_gpio_pin_write(PIN_LED_ENABLE,LED_DISABLE);
	Pwm_UnInitialize();
//	DEBUG_LOG("!!!!cb_LED_Sleep\n");
}

void LED_Init(void)
{	
	SetPwm_BaseClock(NRF_PWM_CLK_8MHz);
	SetPwm_Channels(PIN_LED_CONTROL,NRF_DRV_PWM_PIN_NOT_USED,NRF_DRV_PWM_PIN_NOT_USED,NRF_DRV_PWM_PIN_NOT_USED);
	SetPwm_DutyCycleThreshold(3);
	Pwm_Initialize();
	
	led_color_seq = Pwm_SetComSequence(led_color_seq_values[0], PWM_SEQUENCE_VALUES_LEN(led_color_seq_values),0,0);
	
	nrf_gpio_cfg_output(PIN_LED_ENABLE); 	nrf_gpio_pin_write(PIN_LED_ENABLE,LED_ENABLE);
	Process_Start(5,"LED_Process",LED_Process);
	
	#if DEBUG_LEDRGB
	Process_Start(2000,"WS2812_Test",WS2812_Test);
	#endif
	
	Sleep_Regist(cb_LED_Sleep);
	Wakeup_Regist(cb_LED_Wakeup);
}



