#ifndef _PWM_H_
#define _PWM_H_

#include "apt32f102.h"
#include "apt32f102_ept.h"

#define PWM_OUT_VAL_MAX     3000
#define PWM_OUT_VAL_MIN     0
#define BRIGHTNESS_MAX      100
#define BRIGHTNESS_MIN      0

#define BRIGHTNESS_UP_LIMIT         100
#define BRIGHTNESS_DOWN_LIMIT       0

typedef enum
{
    PWM_OUT_CH1 = 0x00,
    PWM_OUT_CH2,
    PWM_OUT_CH3,
    PWM_OUT_CH4,
    PWM_OUT_CH_MAX,
}PWM_CHN_e;

typedef enum{
	PWMA_Decreasing = 0,		//PWM调节方向 - 递减
	PWMA_Ascending,				//PWM调节方向 - 递增
	PWMA_Nochange,				//PWM调节方向 - 不调节
}PWM_ADJUST_ORIENTATION;

typedef struct{
	U8_T irqEnable;					//PWM控制中断使能状态
	U8_T allBrightness;				//全局调光
	U8_T allBrightnessUpLimit;		//全局调光上限百分比
	U8_T allBrightnessDownLimit;	//全局调光下限百分比
	
	U8_T brightnessCurr[PWM_OUT_CH_MAX];			//当前PWM 百分比亮度
	U8_T switchState[PWM_OUT_CH_MAX];
	U8_T controlFlag[PWM_OUT_CH_MAX];
	U8_T addOrDecFlag[PWM_OUT_CH_MAX];
	
	U16_T allPwmUpLimit;							//全局可调上限
	U16_T allPwmDownLimit;							//全局可调下限
	U16_T pwmValCurr[PWM_OUT_CH_MAX];             	//当前PWM
    U16_T pwmValTarget[PWM_OUT_CH_MAX];           	//目标PWM
	U16_T gradualTime[PWM_OUT_CH_MAX];				//调节时间	, 单位：ms

    float pwmStep[PWM_OUT_CH_MAX];            		//步进值
	float pwmFloatValCurr[PWM_OUT_CH_MAX];			//当前PWM - 浮点类型

}PWM_CONTROL_T;

typedef struct{
	U8_T enable[PWM_OUT_CH_MAX];					//自动调节使能状态 0x01:使能，0x00:未使能
	U8_T adjMode[PWM_OUT_CH_MAX];					//0x00:调节到顶端停止	0x01:循环调节	0x02:停止调节
	U8_T adjDir[PWM_OUT_CH_MAX];					//0x00:递减	0x01:递增	0x02:与上次命令相反
	float pwmStep[PWM_OUT_CH_MAX];					//PWM步进值
	U32_T pwmStepTime[PWM_OUT_CH_MAX];				//步进时间 ， 单位：ms
	U32_T pwmStepTick[PWM_OUT_CH_MAX];				//步进时间戳 ， 单位：ms
}PWM_AUTO_ADJUST_T;

extern PWM_CONTROL_T g_pwm;
extern PWM_AUTO_ADJUST_T g_pwmAutoAdj;

void PWM_Init(void);

void PWM_SetUpLimitVal(U8_T val);
void PWM_SetDownLimitVal(U8_T val);
void Pwm_SetAllBrightness(U8_T val);

void PWM_SetOutDuty(U8_T index,U16_T val);
void PWM_SetCHGradualTime(U8_T index, U16_T val);
void Pwm_SetOnOffState(U8_T index, U8_T state);
void PWM_SetOutBrightness(U8_T index, U8_T brightness);
void PWM_SetAutoAdjust(U8_T index,U8_T mode,U8_T adj,U16_T adj_time);
void PWM_Timer_Enable(void);
void PWM_Timer_Disable(void);
void PWM_Timer_1ms_Task(void);

#endif
