#ifndef BLV_BUS_DEV_C12DIMMINGFUN_H
#define BLV_BUS_DEV_C12DIMMINGFUN_H

#include "BLV_485_Protocol.h"

#define C12DIMMING_TYPE			0x03		//C12调光设备类型 - 这设备类型只用于BLV_BUS协议通讯使用
//#define DEV_C12DIMMING_Type		0x18		//用于设备列表中的设备类型

#define C12_SET_LIGHT_CMD     		0x21     //百分比设定亮度命令
#define C12_SET_ONOFF_CMD     		0x24     //设定开关命令
#define C12_SET_WHOLE_CMD     			0x23     //设定全局命令
#define C12_SET_LIGHT_UPDOWN_CMD     0x22     //亮度相对调节命令

#define C12_READ_LIGHT_CMD     0x20     //读取亮度命令
#define C12_READ_LIGHT_ONOFF_CMD     0x20     //读取亮度开关命令

/*回复命令*/
#define C12_READ_LIGHT_ONOFF_REPLY_CMD  0x30     //读取亮度开关命令回复
#define C12_SET_LIGHT_REPLY_CMD     		0x31     //百分比设定亮度命令回复
#define C12_SET_ONOFF_REPLY_CMD     		0x34     //设定开关命令回复
#define C12_SET_WHOLE_REPLY_CMD     		0x33     //设定全局命令回复
#define C12_SET_LIGHT_REPLY_UPDOWN_CMD   0x32     //亮度相对调节命令回复
//#define C12_READ_ONOFF_CMD     0x25     //读取开关命令

/*递增递减调节（相对调节）*/
#define BUS_C12DIMMING_UPDOWN_MODE_SINGLE  0x00    //(单次调节)亮度调节到顶端后停止
#define BUS_C12DIMMING_UPDOWN_MODE_LOOP  0x01     //(循环调节)亮度调节到顶端后反向调节
#define BUS_C12DIMMING_UPDOWN_MODE_STOP  0x02     //停止调节

#define BUS_C12DIMMING_UPDOWN_DIR_DOWN  	0x00    //递减调节
#define BUS_C12DIMMING_UPDOWN_DIR_UP  		0x01     //递增调节
#define BUS_C12DIMMING_UPDOWN_DIR_TOGGLE  0x02     //和上次命令反向相反

/*开关操作*/
#define BUS_C12DIMMING_CHx_ON  0x01     //打开回路
#define BUS_C12DIMMING_CHx_OFF  0x00     //关闭回路

/*全局参数*/
#define BUS_C12DIMMING_WHOLE_LIGHT  (1<<7)     				//全局亮度
#define BUS_C12DIMMING_WHOLE_UPPER_LIMIT  (1<<6)     //全局可调上限
#define BUS_C12DIMMING_WHOLE_LOWER_LIMIT  (1<<5)     //全局可调下限

typedef enum{
	C12Dimming_CH1,
	C12Dimming_CH2,
	C12Dimming_CH3,
	C12Dimming_CH4,
	C12Dimming_CH5,
	C12Dimming_CH6,
	C12Dimming_CH7,
	C12Dimming_CH8,
	C12Dimming_CH9,
	C12Dimming_CH10,
	C12Dimming_CH11,
	C12Dimming_CH12,
	C12Dimming_CH_MAX,
	
}BUS_C12DIMMING_NUM_E;

typedef struct{
	BLV_COMM_RECORD_G comm_record;						//通讯记录
	
	uint8_t light[C12Dimming_CH_MAX];       //当前亮度值
	uint8_t last_light[C12Dimming_CH_MAX];   //上一次亮度值
	uint16_t on_off;												//当前开关状态
	uint16_t last_on_off;									//上一次开关状态
	uint8_t whole_light;       					//当前全局亮度
	uint8_t whole_upper_limit;       				//当前全局上限
	uint8_t whole_lower_limit;              //当前全局下限
	uint8_t light_updown_mode;          //当前相对调节模式
	uint8_t light_updown_dir;          //当前相对调节方向
	uint8_t light_updown_num;          //当前相对调节步进值
	
	uint16_t light_Control_Flag;						//百分比亮度控制标志位
	uint16_t on_off_Control_Flag;						//开关控制标志位
	uint8_t whole_Control_Flag;             //全局控制位： bit7：全局亮度、bit6：全局可调上限、 bit5：全局可调下限
	uint16_t light_updown_Control_Flag;      //亮度相对调节控制位
	
	uint16_t last_light_Control_Flag;						//上次百分比亮度控制标志位
	uint16_t last_on_off_Control_Flag;						//上次开关控制标志位
	uint8_t last_whole_Control_Flag;             //上次全局控制位： bit7：全局亮度、bit6：全局可调上限、 bit5：全局可调下限
	uint16_t last_light_updown_Control_Flag;      //上次亮度相对调节控制位
	
	uint8_t fade_time;                      //渐变时间
	uint8_t set_light[C12Dimming_CH_MAX];   //设定亮度值
	uint8_t set_onoff;                       //设定开关状态
	uint8_t set_whole_light;       					//设定全局亮度
	uint8_t set_whole_upper_limit;       		//设定全局可调上限
	uint8_t set_whole_lower_limit;       		//设定全局可调下限
	uint8_t set_light_updown_mode;          //设定相对调节模式
	uint8_t set_light_updown_dir;          //设定相对调节方向
	uint8_t set_light_updown_num;          //设定相对调节步进值
	
	uint8_t LedUpDown[C12Dimming_CH_MAX];   //循环调光
	uint8_t Send_Type;									//发送数据类型
	uint8_t DevSendCnt;									//设备发送计数   计数达到重发次数都没有回复，判为离线
	uint8_t DevOffline;       							//设备离线标志 1，设备离线   0，设备在线
	uint8_t DevOfflineLast;								//设备上一次离线标志 1，设备离线   0，设备在线
	uint8_t DevSendSN;									//发送SN号
	
	uint32_t 	inquire_tick;		//询问时间戳
}__attribute__ ((__packed__)) BUS_C12DIMMING_INFO;


void BLV_BUS_DEV_C12Dimming_Data_Init(void);
uint8_t BLV_BUS_C12Dimming_Cycle_Call(uint32_t dev_addr);
uint8_t BLV_BUS_C12Dimming_Data_Processing(uint32_t dev_addr,uint32_t data_addr,uint16_t len);
void BUS_C12Dimming_Light_Control_Datasend(Device_Public_Information_G *BUS_Public, BUS_C12DIMMING_INFO *Dev_Info);
void BUS_C12Dimming_OnOff_Control_Datasend(Device_Public_Information_G *BUS_Public, BUS_C12DIMMING_INFO *Dev_Info);
void BUS_C12Dimming_Whole_Control_Datasend(Device_Public_Information_G *BUS_Public, BUS_C12DIMMING_INFO *Dev_Info);
void BUS_C12Dimming_UpDown_Control_Datasend(Device_Public_Information_G *BUS_Public, BUS_C12DIMMING_INFO *Dev_Info);
void BUS_C12Dimming_Inquire_Datasend(Device_Public_Information_G *BUS_Public, BUS_C12DIMMING_INFO *Dev_Info);

void BUS_C12Dimming_Control_CHx_OnOff(uint32_t dev_addr,BUS_C12DIMMING_NUM_E loop,uint8_t start);  //控制C12调光单路开关
void BUS_C12Dimming_Control_OnOff(uint32_t dev_addr, uint16_t Control_Flag, uint8_t start);  //控制C12调光多个回路开关

void BUS_C12Dimming_Control_CHx_Light(uint32_t dev_addr, BUS_C12DIMMING_NUM_E loop, uint8_t light, uint8_t fadeTime);  //控制C12调光单路亮度
void BUS_C12Dimming_Control_Light(uint32_t dev_addr, uint16_t Control_Flag, uint8_t light, uint8_t fadeTime, uint8_t onoff);  //控制C12调光多个回路亮度

/*可以同时控制C12调光多个回路不同亮度*/
void BUS_C12Dimming_Control_Light_Currency(uint32_t dev_addr, uint16_t Control_Flag, uint8_t* light, uint8_t fadeTime, uint8_t onoff);

void BUS_C12Dimming_Control_Whole(uint32_t dev_addr, uint8_t mask, uint8_t light, uint8_t upper_limit, uint8_t lower_limit); //设置C12调光全局参数

void BUS_C12Dimming_Control_CHx_UpDown(uint32_t dev_addr, BUS_C12DIMMING_NUM_E loop, uint8_t mode, uint8_t dir, uint8_t num); //控制C12调光单路递增递减调节
void BUS_C12Dimming_Control_UpDown(uint32_t dev_addr, uint16_t Control_Flag, uint8_t mode, uint8_t dir, uint8_t num);  ////控制C12调光多路递增递减调节
void BLV_C12Dimming_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t loop,uint16_t start);
uint16_t BLV_C12Dimming_Read_State(uint32_t devaddr,uint16_t loop);


#endif
