#ifndef _APP_SWITCHCTRL_H_
#define _APP_SWITCHCTRL_H_

#include "stdint.h"
#include "BLV_Dev_Action.h"
#include "BLV_All_Dev_Init.h"

#define Key_BUFF_Size			32
#define KeyHoldJudgeTime        400
#define SWI_KEY_LONG_PERIOD    	200
#define SWIOUTUPEXIST     		0x00   //开关反馈灯上报标志

typedef enum{
	RS_SWITCH_CH1 = 0x00,
	RS_SWITCH_CH2,
	RS_SWITCH_CH3,
	RS_SWITCH_CH4,
	RS_SWITCH_CH5,
	RS_SWITCH_CH6,
	RS_SWITCH_CH7,
	RS_SWITCH_CH8,
	RS_SWITCH_CH9,
	RS_SWITCH_CH10,
	RS_SWITCH_CH11,
	RS_SWITCH_CH12,
	RS_SWITCH_CH13,
	RS_SWITCH_CH14,
	RS_SWITCH_CH15,
	RS_SWITCH_CH16,
	RS_SWITCH_CH17,
	RS_SWITCH_CH18,
	RS_SWITCH_CH19,
	RS_SWITCH_CH20,
	RS_SWITCH_CH21,
	RS_SWITCH_CH22,
	RS_SWITCH_CH23,
	RS_SWITCH_CH24,
	RS_SWITCH_CH25,
	RS_SWITCH_CH26,
	RS_SWITCH_CH27,
	RS_SWITCH_CH28,
	RS_SWITCH_CH29,
	RS_SWITCH_CH30,
	RS_SWITCH_CH_MAX,
}RS485_SWITCH_NUM_E;

typedef enum
{ 
	KeyNoAction 	= 0x00,     //按键无动作
	KeyPress   		= 0x01,  	//按键按下    短按	
	KeyRelease  	= 0x02,  	//按键松开    释放	
	KeyHold    		= 0x03,  	//按键保持    长按
	RotaryLight     = 0x04,     //旋转调亮度
	RotaryVol       = 0x05,     //旋转调音量
	RotaryTemp      = 0x06,     //旋转调温度
	RotaryCCT       = 0x07,     //旋转调色温
	RotaryOther     = 0x08,     //旋转调其他
}keyState_Typedef;        //输入按键状态枚举


typedef struct 
{
	uint8_t  delayCountNow;   //当前按键按下计次
	uint8_t  keyThisState;    //按键的当前电平状态  
	uint8_t  keyState;      //点按  长按  和松开
}Swikey_Typedef;

typedef struct
{
	BLV_COMM_RECORD_G comm_record;	//通讯记录
	
	uint8_t DevReadBuf[Key_BUFF_Size]; 				//按键数组，每个字节存按键的状态
	uint8_t DevReadBuf_last[Key_BUFF_Size]; 		//按键数组，每个字节存按键的状态	
		
	uint32_t KeyBitValue;     //按键按位键值  32个位可以表示32个按键，从低位开始存储 OCY开关，1表示有长按 0表示无长按

	uint8_t DevSendBuf[Key_BUFF_Size]; 			//发送数组，每个字节存指示灯的状态
	uint8_t DevSendBuf_last[Key_BUFF_Size]; 		//发送数组，每个字节存指示灯的上一个状态

	uint8_t A9IORepeatFlag;     //A9IO重发标志，当没有接收到键值时，重发标志置一，Sn号不增加。当接收到键值时，重发标志清0，Sn号增加

	uint8_t SwtInputValidNum;     //开关有效的输出数量
	uint8_t SwtOutputValidNum;    //开关有效的输出数量
	
	uint8_t SwtRelayLedCtrlFlag;    //开关指示灯控制标志
	uint8_t SwtRelayLedCtrlCnt;     //指示灯控制计数
	
	uint8_t KeyCntChangFlag;        //按键变化计数
	uint8_t SwtCycleStep;          //开关控制状态机变量，用于设置开关控制开关

	uint8_t DevSendCnt;     	//设备发送计数   计数达到重发次数都没有回复，判为离线
	uint8_t DevOffline;       	//设备离线标志 1，设备离线   0，设备在线
	uint8_t DevOfflineLast;   	//设备离线标志 1，设备离线   0，设备在线
	
	uint8_t SwitchSn;           //开关Sn号
	
	/*主动设备不需要询问时间戳*/
	//uint32_t inquire_tick;		//询问时间戳
	
	uint8_t MultiValidNo[Key_BUFF_Size];      //多联开关有效联数        2024-05-23
	uint8_t MultiNumber[Key_BUFF_Size];       //多联开关当前执行下标    2024-05-23
	
	uint16_t RotaryValue[5];         //旋钮上报的值
	uint8_t  RotaryValue_Flag;    //旋钮值变化标记
	uint8_t  RotaryBL;            //旋钮背光状态
	uint8_t  RotaryBL_Last;       //旋钮背光上次状态
	uint8_t  RotaryBL_Flag;       //旋钮背光变化标记
    uint8_t  RotaryCCTValue_Flag; //旋钮色温值变化标记,2025-07-10,YYW
	
	uint8_t  RL_Upper_limit;        //亮度调节上限
	uint8_t  RL_Upper_limit_Last;   //亮度调节上限
	uint8_t  RL_Lower_limit;        //亮度调节下限
	uint8_t  RL_Lower_limit_Last;   //亮度调节下限
	
	uint8_t  RL_Limit_Flag;
	
	uint32_t Rotary_PageEn;         //旋钮页面启用标记
	uint32_t Rotary_PageEnLast;   
	uint8_t  Rotary_PageFlag;       //旋钮页面启用发送标记
	
}__attribute__ ((__packed__))RS485_SWI_INFO;						//485开关的数据与函数

void BLW_RS485_Switch_Init(uint16_t DataLen, uint8_t *Data);
void BLW_RS485_Switch_For_Logic_Init(LOGICFILE_DEVICE_INFO *dev_info,uint8_t *data,uint16_t data_len);
uint8_t Get_Switch_Online_Status(uint32_t devaddr);
uint8_t Get_Rotary_Switch_Online_Status(uint32_t devaddr);

#endif


