#include "hal_ws2812.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 "nrf_delay.h"




#define COLOR_RED		0x00000F00
#define COLOR_ORANGE	0x00070F00
#define COLOR_YELLOW	0x000F0F00
#define COLOR_GREEN		0x000F0000
#define COLOR_CYAN		0x000F000F
#define COLOR_BLUE		0x0000000F
#define COLOR_PURPLE	0x00000F0F

#define COLOR_WHITE		0x000F0F0F
#define COLOR_BLACK		0x00000000


uint32_t COL_TAB[] = {COLOR_RED,COLOR_ORANGE,COLOR_YELLOW,COLOR_GREEN,COLOR_CYAN,COLOR_BLUE,COLOR_PURPLE,COLOR_WHITE};

#define LED_NUM		25
uint32_t led_color[LED_NUM];
uint32_t led_color2[LED_NUM];
uint32_t led_color3[LED_NUM];

void WS2812_DisplayDot(uint32_t col)
{
	uint32_t t = 0x00800000;
	for(int i=0;i<24;i++){
		if(col&t){WS_H;} else{WS_L;}
		t = t>>1;
	}
}

void WS2812_DisplayN(uint32_t* p,uint32_t n)
{
	for(int i=0;i<n;i++){
		WS2812_DisplayDot(p[i]);
	}
	nrf_gpio_pin_write(PIN_SEL,0);
}

static uint8_t flag = 0;
void WS2812_Process(void* t)
//void WS2812_Process(void)
{
	static uint32_t tim=0;
	if(flag>0) return;
	if(TIME_GetTicks()-tim>=10){ tim = TIME_GetTicks();
		WS2812_DisplayN(led_color,LED_NUM);
		WS2812_DisplayN(led_color2,LED_NUM);
		WS2812_DisplayN(led_color3,2);
		WS2812_DisplayN(led_color,LED_NUM);
		WS2812_DisplayN(led_color2,LED_NUM);
	}
		
}

uint8_t randTab[] = {2,17,1,24,15,17,24,0,3,9,15,17,20,16,2,18,6,15,1,5,8,12,16,7,2,14,13,14,22,14,8,23,10,20,7,3,11,8,8,13,21,25,16,5,0,16,16,15,23,8,2,19,8,17,6,14,2,9,19,14,2,4,11,13,15,18,0,2,22,12,11,5,23,4,19,20,12,11,13,3,5,11,13,8,8,13,2,18,14,23,2,10,18,2,12,6,14,23,1,22};
void WS2812_Test(void)
{
	static uint32_t rand = 0;
	static uint8_t dex = 0;
	flag = 1;
	rand = randTab[dex%100];
	for(int i=0;i<LED_NUM;i++){
		if(i<=rand){
			led_color[i] = COL_TAB[(i)%8];
			led_color2[i] = COL_TAB[(i)%8];
		}else{
			led_color[i] = COLOR_BLACK;
			led_color2[i] = COLOR_BLACK;
		}
		led_color3[i] = COLOR_BLACK;
	}
	dex++;
	flag = 0;
}


void WS2812_Init(void)
{	
	nrf_gpio_cfg_output(PIN_SEL);		nrf_gpio_pin_write(PIN_SEL,0);
	memset(led_color,0,sizeof(led_color));
	TIME_Regist(WS2812_Process);
	Process_Start(100,WS2812_Test);
	DEBUG_LOG("WS2812_Init============================\n",0);
}



