#pragma once
#include "IrisFiberSpectrometerBase.h"
#include <QtSerialPort/QSerialPort>

#define CRC16_INIT         0xFFFF
#define CRC16_POLYNOMIAL   0xa001

const uint8_t GET_ADDRESS[]           = {0x01,0x03,0x00,0x01,0x00,0x01};
const uint8_t GET_BANDRATE[]          = {0x01,0x03,0x00,0x02,0x00,0x01};
const uint8_t GET_INTEGRAL_TIME[]     = {0x01,0x03,0x00,0x06,0x00,0x01};
const uint8_t GET_AVERAGE_NUMBER[]    = {0x01,0x03,0x00,0x07,0x00,0x01};
const uint8_t GET_WAVELENTH_AT_BAND[] = {0x01,0x03,0x00,0x10,0x00,0x02};
const uint8_t GET_VALUE_AT_BAND[]     = {0x01,0x03,0x00,0x30,0x00,0x02};
const uint8_t GET_SETTING_OF_LAMP[]   = {0x01,0x03,0x00,0x04,0x00,0x01};
const uint8_t GET_WAVELENTH_COEFF[]   = {0x01,0x03,0x00,0x20,0x00,0x08};
const uint8_t GET_ALL_DN[]            = {0x01,0x03,0x01,0x00,0x10,0x00};
const uint8_t GET_SERIAL_NUMBER[]     = {0x01,0x03,0x00,0x40,0x00,0x00};
const uint8_t GET_PRODUCT_NAME[]      = {0x01,0x03,0x00,0x50,0x00,0x00};

const uint8_t SET_ADDRESS[]           = {0x01,0x06,0x00,0x01};
const uint8_t SET_BandRATE[]          = {0x01,0x06,0x00,0x02};
const uint8_t SET_INTEGRAL_TIME[]     = {0x01,0x06,0x00,0x06};
const uint8_t SET_AVERAGE_NUMBER[]    = {0x01,0x06,0x00,0x07};
const uint8_t SET_WORK_MODE[]         = {0x01,0x06,0x00,0x01};

const uint8_t SET_WAVELENTH_COEFF[]   = {0x01,0x10,0x00,0x20,0x00,0x08,0x16};

class IS11_Ctrl_Qt :public CIrisFSBase
{
	Q_OBJECT  
public:
	IS11_Ctrl_Qt(QObject* parent = nullptr);
	virtual ~IS11_Ctrl_Qt();
public:
	//do not call
	//int ReInit();
	//设置波特率
	//int SetBaudRate(int iBaud);
	//初始化设备
	int Initialize(bool bIsUSBMode, std::string ucPortNumber, std::string strDeviceName);

	//关闭设备
	void Close();

	//单次测试采集 用以确认设备像素数
	//int SingleShot(int& iPixels);

	//单次数据采集
	int SingleShot(DataFrame& dfData);

	//单次暗背景采集
	//int SingleShotDark(ATPDataFrame &dfData);

	//int SingleShotDeducted(ATPDataFrame &dfData);
	//设置曝光时间
	int SetExposureTime(int iExposureTimeInMS);

	//获取曝光时间设置
	int GetExposureTime(int& iExposureTimeInMS);

	//int GetWaveLength(float *pfWaveLength);

	//获取设备信息
	int GetDeviceInfo(DeviceInfo& Info);

	//获取设备特征数据
	int GetDeviceAttribute(DeviceAttribute& Attr);

	//int GetDeviceListInfo(); //use type name to enum

	//设置温度
	int SetDeviceTemperature(float fTemperature);

	//获取温度
	int GetDeviceTemperature(float& fTemperature);

	//自动曝光
	int PerformAutoExposure(float fMinScaleFactor, float fMaxScaleFactor, float& fPredictedExposureTime);

#ifdef _DEBUG
public:
#else // 
private:
#endif
	//port
	int m_iBaudRate;
	QSerialPort* m_pSerialPort;

	//ATP
	DeviceInfo m_diDeviceInfo;
	DeviceAttribute m_daDeviceAttr;

	//Attr
	int m_iExposureTime;

#ifdef _DEBUG
public:
#else //  
private:
#endif
	//unsigned short crc16(const uint8_t *pbdata, size_t sz);
	unsigned short CalCRC16(const uint8_t *pbData, size_t szLength);
	void Conv_LTB(char * pcData, int iLength);

	int Write_IS11(char * pcCMD, size_t szCMDLength);
	int Read_IS11(QByteArray &qbaRecv);

	

	int SendData_CMD03(char * pcCMD, size_t szCMDLength);
	int RecvData_CMD03(QByteArray &qbaRecv);
	int RecvData_CMD03(DataFrame& dfData);
	//int RecvData_CMD03(float& fWavelength);
	//int RecvData_CMD03(DeviceAttribute& Attr);

	int SendData_CMD06(char *pcCMD, size_t szCMDLength, char *pcValue, size_t szValueLenth);
	int RecvData_CMD06(QByteArray &qbaRecv);

	int SendData_CMD10(char * pcCMD, size_t szCMDLength);
	int RecvData_CMD10(QByteArray &qbaRecv);

	int ParseHdr(QByteArray &qbaData,char cHdrType);

	
};

