/**
******************************************************************************************************************************************************************************************
*@file                       dm_button_gpio.c
*@author         
*@data                       
*@version                    v1.0
*@par Copyright(c):
*
*@par History:
*     version:author,date,desc
*     v1.0   :create this file
*
*******************************************************************************************************************************************************************************************
*/
#include <stdint.h>
#include <stdbool.h>
#include "cpu.h"
#include "user_gpio.h"
#include "cpu_timer.h"
#include "user_timer.h"
#include "rgb_gpio.h"
#include "cpu_deepsleep.h"
#include "app_trace.h"
#include "sdk_include.h"
void led_rgb_light_off(UINT8 event,void *param);
void led_rgb_light_on(UINT8 event,void *param);

static rgb_env_t rgb_env;
struct fsm_timer *led_close_timer = NULL;
uint8_t pwm_on = 0;

void rgb_led_init(void)
{
    memset(&rgb_env, 0, sizeof(rgb_env_t));
	led_close_timer = NULL;
}

static void rgb_gpio_into_default_state(void)
{
	user_gpio_out(rgb_env.rgb_param.r_pin, DISABLE);
	user_gpio_out(rgb_env.rgb_param.g_pin, DISABLE);
	user_gpio_out(rgb_env.rgb_param.b_pin, DISABLE);
}


static void rgb_close_timer(UINT8 event,void *param)
{
    RGB_DEBUG("close timer !\n\r");
    rgb_gpio_into_default_state();
	timer_enable_ctrl_cmd(TIMER1, TIMER_STOP);
    cpu_prevent_sleep_clear(TIMER1_ONGOING);
}

static void rgb_open_timer(UINT8 event,void *param)
{
	RGB_DEBUG("open timer !\n\r");
	timer_enable_ctrl_cmd(TIMER1, TIMER_START);
	cpu_prevent_sleep_set(TIMER1_ONGOING);
}

//单色光
static void user_rgb_on(uint8_t type,uint8_t on,uint8_t fresh)
{
	static uint8_t last_on = 0xFF;
	if(!fresh && (last_on == on))
		return;
	
	last_on 		= on;
	if(FALSE == on){
		rgb_gpio_into_default_state();
		return;
	}
	
	RGB_DEBUG("r %d g  %d b %d ",
		rgb_env.rgb_param.r_pin,
		rgb_env.rgb_param.g_pin,
		rgb_env.rgb_param.b_pin);

	switch(rgb_env.step){
		case 0:
			RGB_DEBUG("A 0 \n");
			user_gpio_out(rgb_env.rgb_param.r_pin, ENABLE);
			user_gpio_out(rgb_env.rgb_param.g_pin, ENABLE);
			user_gpio_out(rgb_env.rgb_param.b_pin, ENABLE);			
			break;
		case 1:
			RGB_DEBUG("A 1 \n");
			user_gpio_out(rgb_env.rgb_param.r_pin, ENABLE);
			user_gpio_out(rgb_env.rgb_param.g_pin, DISABLE);
			user_gpio_out(rgb_env.rgb_param.b_pin, DISABLE);
			break;
		case 2:
			RGB_DEBUG("A 2 \n");
			user_gpio_out(rgb_env.rgb_param.r_pin, ENABLE);
			user_gpio_out(rgb_env.rgb_param.g_pin, DISABLE);
			user_gpio_out(rgb_env.rgb_param.b_pin, ENABLE);
			break;
		case 3:
			RGB_DEBUG("A 3 \n");
			user_gpio_out(rgb_env.rgb_param.r_pin, ENABLE);
			user_gpio_out(rgb_env.rgb_param.g_pin, ENABLE);
			user_gpio_out(rgb_env.rgb_param.b_pin, DISABLE);	
			break;
		case 4:
			RGB_DEBUG("A 4 \n");
			user_gpio_out(rgb_env.rgb_param.r_pin, DISABLE);
			user_gpio_out(rgb_env.rgb_param.g_pin, DISABLE);
			user_gpio_out(rgb_env.rgb_param.b_pin, ENABLE);
			break;
		case 5:
			RGB_DEBUG("A 5 \n");
			user_gpio_out(rgb_env.rgb_param.r_pin, DISABLE);
			user_gpio_out(rgb_env.rgb_param.g_pin, ENABLE);
			user_gpio_out(rgb_env.rgb_param.b_pin, ENABLE);
			break;
		case 6:
			RGB_DEBUG("A 6 \n");
			user_gpio_out(rgb_env.rgb_param.r_pin, DISABLE);
			user_gpio_out(rgb_env.rgb_param.g_pin, ENABLE);
			user_gpio_out(rgb_env.rgb_param.b_pin, DISABLE);
			break;
	}
}


// 100Hz
timer_init_type_def TIMER_init;
void rgb_gpio_timer_callback(void)
{
// 周期为40ms
	rgb_env.duty_counter ++;
	if(rgb_env.duty_counter <= 50){
		if(rgb_env.duty_time <= 96)
			rgb_env.duty_time += 2;
	}else if(rgb_env.duty_counter <= 100){
		if(rgb_env.duty_time >= 4)
			rgb_env.duty_time -= 2;
	}
	
	if(rgb_env.duty_counter == 100)
	{
		rgb_env.duty_counter = 0;
		
		rgb_env.step ++;
		rgb_env.sw_step ++;
		
		if(rgb_env.step >= 7){
			rgb_env.step = 0;
			rgb_env.duty_time = 2;
		}
		
		if(rgb_env.sw_step >= PWM_SHOW_TIME){
			RGB_DEBUG("pwm stop \n");
			rgb_pwm_stop();
			return;
		}

		switch(rgb_env.step){// 绿 蓝 红 青 黄 紫 白
			case 0:
				user_pwm_config(rgb_env.rgb_param.r_pin,DISABLE);	
				user_pwm_config(rgb_env.rgb_param.g_pin,ENABLE);	// 绿
				user_pwm_config(rgb_env.rgb_param.b_pin,DISABLE);
				break;
			case 1:
				user_pwm_config(rgb_env.rgb_param.r_pin,DISABLE);
				user_pwm_config(rgb_env.rgb_param.g_pin,DISABLE);
				user_pwm_config(rgb_env.rgb_param.b_pin,ENABLE);	// 蓝
				break;
			case 2:
				user_pwm_config(rgb_env.rgb_param.r_pin,ENABLE);	// 红
				user_pwm_config(rgb_env.rgb_param.g_pin,DISABLE);
				user_pwm_config(rgb_env.rgb_param.b_pin,DISABLE);
				break;
			case 3:
				user_pwm_config(rgb_env.rgb_param.r_pin,DISABLE);
				user_pwm_config(rgb_env.rgb_param.g_pin,ENABLE);	// 绿
				user_pwm_config(rgb_env.rgb_param.b_pin,ENABLE);	// 蓝
				break;
			case 4:
				user_pwm_config(rgb_env.rgb_param.r_pin,ENABLE);	// 红
				user_pwm_config(rgb_env.rgb_param.g_pin,ENABLE);	// 绿
				user_pwm_config(rgb_env.rgb_param.b_pin,DISABLE);
				break;
			case 5:	
				user_pwm_config(rgb_env.rgb_param.r_pin,ENABLE);	// 红
				user_pwm_config(rgb_env.rgb_param.g_pin,DISABLE);
				user_pwm_config(rgb_env.rgb_param.b_pin,ENABLE);	// 蓝
				break;
			case 6:
				user_pwm_config(rgb_env.rgb_param.r_pin,ENABLE);	// 红
				user_pwm_config(rgb_env.rgb_param.g_pin,ENABLE);	// 绿
				user_pwm_config(rgb_env.rgb_param.b_pin,ENABLE);	// 蓝
				break;
		}

	}

	TIMER_init.TIMER_PWM_Hz = 10666;								//配置PWM时钟16000000 / 1500Hz
	TIMER_init.TIMER_PWM_duty_cycle = rgb_env.duty_time;			//配置PWM占空比0
	breath_pwm_start(0,&TIMER_init);								//PWM初始化：时钟，占空比配置
}

void rgb_pwm_start(void)
{	
	RGB_DEBUG("rgb pwm start ");
	timer_del_byp_free(&led_close_timer);
	rgb_open_timer(0,NULL);
	
	user_rgb_on(rgb_env.step,FALSE,TRUE);	
	rgb_env.step 			= 0;
	rgb_env.sw_step 		= 0;
	rgb_env.duty_time 		= 2;
	rgb_env.duty_counter 	= 0;
	
	user_pwm_config(rgb_env.rgb_param.r_pin,DISABLE);
	user_pwm_config(rgb_env.rgb_param.g_pin,ENABLE);
	user_pwm_config(rgb_env.rgb_param.b_pin,DISABLE);
	TIMER_init.TIMER_PWM_Hz = 10666;								//配置PWM时钟16000000 / 1500Hz
	TIMER_init.TIMER_PWM_duty_cycle = rgb_env.duty_time;			//配置PWM占空比0
	pwm_start(0,&TIMER_init);								//PWM初始化：时钟，占空比配置
	pwm_on = 1;
}			

void rgb_pwm_stop(void)
{	
	if(pwm_on == 0)
		return;

	RGB_DEBUG("rgb pwm sop \n");
	
	timer_del_byp_free(&led_close_timer);
	rgb_close_timer(0,NULL);
	pwm_stop(0);
	user_pwm_config(rgb_env.rgb_param.r_pin,ENABLE);
	user_pwm_config(rgb_env.rgb_param.g_pin,DISABLE);
	user_pwm_config(rgb_env.rgb_param.b_pin,DISABLE);
	led_rgb_light_off(0,NULL);
	pwm_on = 0;
}			

void rgb_config(rgb_conf_t *p_conf)
{
	timer_config_t   rgb_timer_param;
	memcpy(&rgb_env.rgb_param, p_conf, sizeof(rgb_conf_t));
	rgb_env.step      							= 0;

    /// timer init
    rgb_timer_param.timer_idx 					= IDX_TIMER1;
	rgb_timer_param.timer_param.timer_period 	= p_conf->scan_interval;
	timer_config(TIMER1, &rgb_timer_param, rgb_gpio_timer_callback);
	timer_enable_ctrl_cmd(TIMER1, TIMER_STOP);
}

void led_rgb_light_off(UINT8 event,void *param)
{
	timer_del_byp_free(&led_close_timer);
	user_rgb_on(rgb_env.step,FALSE,TRUE);	
}

void led_rgb_light_on(UINT8 event,void *param)
{
	RGB_DEBUG("rgb on step %d ",rgb_env.step);

	user_rgb_on(rgb_env.step,TRUE,TRUE);
	timer_del_byp_free(&led_close_timer);
	led_close_timer = FsmAddTimerCx(15000, 0, led_rgb_light_off, NULL);
}

//PWM测试：同串口选择PWM通道，配置PWM时钟、占空比
//注意：只有TIMER0，TIMER1可以产生PWM，TIMER2，TIMER3不能产生。
int pwm_config(uint8_t PWMx,uint16_t pwmHz,uint32_t duty_cycle)
{	
	gpio_init(GPIOA_Pin1,GPIO_Mode_Output);
	gpio_init(GPIOA_Pin2,GPIO_Mode_Output);
	gpio_remap_config(GPIOA_Pin1,GPIO_Remap_PWM0);
	gpio_remap_config(GPIOA_Pin2,GPIO_Remap_PWM1);

	//初始化关闭TIMER
	timer_cmd(TIMER0, DISABLE);
	timer_cmd(TIMER1, DISABLE);
	TIMER_init.TIMER_PWM_Hz = 16000000 / pwmHz;   	//配置PWM时钟50Hz
	TIMER_init.TIMER_PWM_duty_cycle = duty_cycle;    		//配置PWM占空比0
	timer_pwm_init(PWMx,&TIMER_init);						//PWM初始化：时钟，占空比配置
	timer_cmd(TIMER0, ENABLE);//使能TIMER，产生PWM波形
	timer_cmd(TIMER1, ENABLE);
	return 0;
}

uint8_t is_led_stop(void)
{
	if(kb_info.led_stat == 0){
		return TRUE;
	}else if(kb_info.led_stat == 1){
		if(led_close_timer){
			return FALSE;
		}
	}else if(kb_info.led_stat == 2){
		if(rgb_env.sw_step < PWM_SHOW_TIME){
			return FALSE;
		}
	}
	return TRUE;
}

void led_switch_type(uint8_t process)
{
	rgb_env.run_counter = 0;
	switch(process){
		case 0:		//普通按键
			if(kb_info.led_stat == 1){
				if(led_close_timer){
					timer_del_byp_free(&led_close_timer);
					led_close_timer = FsmAddTimerCx(15000, 0, led_rgb_light_off, NULL);
					return;
				}
			}else if(kb_info.led_stat == 2){
				if(rgb_env.sw_step < PWM_SHOW_TIME){
					rgb_env.sw_step = 0;
					return;
				}
			}
			break;
		case 1:		//切换类型
			rgb_env.step = 0;
			if(kb_info.led_stat == 0){
				led_rgb_light_off(0,NULL);
			}
			
			if((kb_info.led_stat == 1) || (kb_info.led_stat == 2))
			{
				if(is_led_stop())
				{
					break;
				}
			}
			kb_info.led_stat ++;
			if(kb_info.led_stat == 3)
				kb_info.led_stat = 0;
			flash_erase(HOST_TYPE_FLASH,0x1000);
			flash_write(HOST_TYPE_FLASH,sizeof(kb_info),(uint8_t *)(&kb_info));
			break;
		case 2:		//切换颜色
			if(kb_info.led_stat == 1)
			{
				if(is_led_stop())
				{
					break;
				}
				rgb_env.step++;
				if(rgb_env.step >= 7)
					rgb_env.step = 0;
			}else if(kb_info.led_stat == 2){
				if(rgb_env.sw_step < PWM_SHOW_TIME){
					rgb_env.sw_step = 0;
					return;
				}
			}
			break;
	}
	switch(kb_info.led_stat)
	{
		case 0:
			rgb_pwm_stop();
			break;
		case 1:
			led_rgb_light_on(0,NULL);
			break;
		case 2:
			rgb_pwm_start();
			break;
	}
}

