#ifndef _MATH_CALC_
#define _MATH_CALC_



#define  SAMPLECARD_SAIL1674    0
#define  SAMPLECARD_PCL711B     1
#define  SAMPLECARD_AC1082      2
#define  SAMPLECARD_ADT650      3
#define  SAMPLECARD_AD976_12    4
#define  SAMPLECARD_AD976_16    5
#define  SAMPLECARD_PCL818HD    6
#define  SAMPLECARD_PCI1713     7
#define  SAMPLECARD_NEWA012     8
#define  SAMPLECARD_MAX11046_12       9
#define  SAMPLECARD_MAX11046_16      10
#define  SAMPLECARD_MAX11046_FIFO_12 11
#define  SAMPLECARD_MAX11046_FIFO_16 12
#define  SAMPLECARD_AD7616_12        13
#define  SAMPLECARD_AD7616_16        14
#define  SAMPLECARD_AD7616_FIFO_12   15
#define  SAMPLECARD_AD7616_FIFO_16   16

#define  SAMPLECARD_EMULATION   20

#define  DACARD_PCL711B        0
#define  DACARD_AD669          1
#define  DACARD_AD7616_AD669   2

#define  CHANNEL0_TYPE   0x30
#define  CHANNEL1_TYPE   0x70
#define  CHANNEL2_TYPE   0xB0
#define  COUNTER_LOOP_COUNT   40




#define MAX11046_SAMPLERATE_128K  0x00u
#define MAX11046_SAMPLERATE_64K   0x01u
#define MAX11046_SAMPLERATE_32K   0x02u
#define MAX11046_SAMPLERATE_16K   0x03u
#define MAX11046_SAMPLERATE_8K    0x04u
#define MAX11046_SAMPLERATE_4K    0x05u
#define MAX11046_SAMPLERATE_2K    0x06u
#define MAX11046_SAMPLERATE_1K    0x07u
#define MAX11046_SAMPLERATE_500   0x08u
#define MAX11046_SAMPLERATE_250   0x09u
#define MAX11046_SAMPLERATE_125   0x0Au





#define MATH_CALC_ARRAY_COUNT    20
#define CARD_836_BASE            0x240
#define CARD_836_DIO             (CARD_836_BASE+16)
#define CARD_836_DIO_LOW         CARD_836_DIO
#define CARD_836_DIO_HIGH        (CARD_836_DIO_LOW+1)
#define CARD_836_COUNT0          0
#define CARD_836_COUNT1          1
#define CARD_836_COUNT2          2
#define CARD_836_COUNT3          3
#define CARD_836_COUNT4          4
#define CARD_836_COUNT5          5
#define CARD_836_FREQ_NULL       0x0
#define CARD_836_FREQ_1_65K      0x1
#define CARD_836_FREQ_10K_650K   0x2
#define CARD_836_FREQ_100K_6500K 0x3

#define CARD_836_Fre_Max         200


class CMathCalc
{
private:
	static  BOOL  m_bDot;
private:

	static  BOOL  m_bSampleDebug;
/*
	static	BOOL  m_bDebug;
	static	int   m_nDebugDotCount;
	static	int   m_cxDebug[];
*/
private:
	float m_fDotAmpX[2];
	float m_fDotAmpY[2];
	float m_fDotVolX[2];
	float m_fDotVolY[2];
	int   m_nSpeedDelayTime;
	int   m_nSampleDelay;
	//int   m_nSynchronizationPort;
	//int   m_nSynchronizationBit;
	int   m_nZhaPhLocation[MATH_CALC_ARRAY_COUNT];
	int   m_nSynchronizationLocation[MATH_CALC_ARRAY_COUNT];
private:
	double m_dbCyclePeriod;
private:
	WORD  (*m_pStandand)[STANDAND_DATA_COUNT];
public:
	static void SetSampleDebug(BOOL bDebug){m_bSampleDebug=bDebug;}
	static BOOL GetSampleDebug(){return(m_bSampleDebug);}
public:
	static void SetDot(BOOL bDot){m_bDot=bDot;}
public:
	//static BOOL   GetDebug(){return(m_bDebug);}
	//static void   SetDebug(BOOL bDebug){m_bDebug=bDebug;}
public:
	void   SetCyclePeriod(double dbValue){m_dbCyclePeriod=dbValue;}
	double GetCyclePeriod(){return(m_dbCyclePeriod);}
public:
	void   SetSpeedDelayTime(int nTime){m_nSpeedDelayTime=nTime;}
	int    GetSpeedDelayTime(){return(m_nSpeedDelayTime);}
	void   SetSampleDelay(int nTime){m_nSampleDelay=nTime;}
	int    GetSampleDelay(){return(m_nSampleDelay);}
public:
	void  SetStandandPoint(WORD (*pStandand)[STANDAND_DATA_COUNT]){m_pStandand=pStandand;}
public:
	void  SetSynchronizationLocation(int nIndex,int nValue){m_nSynchronizationLocation[nIndex]=nValue;}
	//void  SetSynchronizationPortAndBit(int nPort,int nBit);
	void  SetAmpAndVol(int bAmpSytle,int nVolStyle);
public:
	CMathCalc();
public:
	//static void  WordToFloat(float *pfWave,WORD *pwWave,int nCount);
	//static void  FloatToWord(WORD *pwWave,float *pfWave,int nCount);
	//static void  ClearWord(WORD *pwWave,int nCount);
	//static void  ClearFloat(float *pfWave,int nCount);

public:
	void  SetDotAmpXY(float fX1,float fY1,float fX2,float fY2);
	void  SetDotVolXY(float fX1,float fY1,float fX2,float fY2);
public:
	//Calc Common
	int   GetMax(WORD *pnWave,int nCount);
	int   GetMin(WORD  *pnWave,int nCount);
	//Calc Freq
	WORD  GetCounter(int nChannel);
	BOOL  InitFreq(int nChannel);
	void  BeginFreq();
	void  PurseFreq(int nChannel,int *pnTimes,WORD *pwOldCounter);
	float EndFreq(int nChannel,int nTimes);

	float StartFreq(int nChannel,int nCircle,BOOL bDelay=TRUE);

	//Calc FG
	void  GetFGPeriod(int *pnWave,int nCount,int nIndex,int *pnStart,int *pnEnd,int *pnMiddle,int nValidDot=20);
	int   GetFGMax(unsigned int *pnWave,int nStart,int nEnd,int nMiddle,BOOL *pbHigh=NULL);
	int   GetFGMin(unsigned int  *pnWave,int nStart,int nEnd,int nMiddle,BOOL *pbHigh=NULL);
	float GetZhanPh(int *pnData,int nCount,int nMax);
	//Calc Sin
	void  GetSinPeriod(int *pnWave,int nCount,int nIndex,int *pnStart,int *pnEnd,int *pnMiddle);
	//Line Coordinate
	//float GetLineCoordinateXY(float fX,float *pBufX,float *pBufY,int nCount);
	//static float GetLineCoordinateXY(float fX1,float fY1,float fX2,float fY2,float fValue);
	//Sample
	//void  SynchronizationWaitting();
	static void  SynchronizationWaitting(BOOL bSurge, BOOL bWaitBeforeHigh);



	void  SampleData_1_Cha_Synchronization(int nBase,int nChannel,WORD *pwData,int nCalcCount,BOOL bHigh=FALSE);
	void  SampleData_1_Cha_Nonsynchronization(int nBase,int nChannel,WORD *pwData,int nCalcCount,BOOL bHigh=FALSE);
	void  SampleData_2_Cha_Synchronization(int nBase,int nChannel,WORD *pwData,int nCalcCount);
	void  SampleData_2_Cha_Nonsynchronization(int nBase,int nChannel,WORD *pwData,int nCalcCount);
	void  SampleDataSurge(WORD *pwData,int nCount);

	//Transrate
	//void   TransrateIntergrateWave(WORD *pwData,int nCount,double dbZoom=1.0);
	//Calc SampleData
	//float  Modify_Average(WORD *pwData,int nLength);
	float  Modify_1_Cha_Average(WORD *pwData,WORD wFlags,int nCalcCount,BOOL bAmpModify,BOOL bVolModify);
	float  Modify_2_Cha_Average(WORD *pwData,WORD wFlags,int nCalcCount,BOOL bAmpModify,BOOL bVolModify);
	float  Modify_1_Cha_Synchronization(WORD *pwData,WORD wFlags,int nCount,BOOL bAmpModify,BOOL bVolModify);
	float  Modify_1_Cha_Nonsynchronization(WORD *pwData,WORD wFlags,int nCount,BOOL bAmpModify,BOOL bVolModify);
	float  Modify_2_Cha_Synchronization(WORD *pwData,WORD wFlags,int nCount,BOOL bAmpModify,BOOL bVolModify);
	float  Modify_2_Cha_Nonsynchronization(WORD *pwData,WORD wFlags,int nCount,BOOL bAmpModify,BOOL bVolModify);
	//float  ModifySurgeCross(WORD *pwSourceData,WORD *pwTargetData,int nCount,int nIndex,BOOL bAverage);

	//Draw Wave
	//static void  DrawWave(int nX,int nY,int nWidth,int /*nHeight*/,float fScaleX,float fScaleY,WORD wFlags,WORD *pwWaveData,int nCount,BOOL bPurge,int nForeColor,int nBackColor=CYAN);
	//static void  DrawWave(int nX,int nY,int nWidth,int /*nHeight*/,float fScaleX,float fScaleY,WORD wFlags,float *pwWaveData,int nCount,BOOL bPurge,int nForeColor,int nBackColor=CYAN);
	//static void  TransrateSurgeTo2048(WORD *pwSurge,WORD *pwTarget,int nCount);
public:
	//836HD
	//BOOL  DigFilterStart(int nBase,int nCounter,float fFilterMaxFreq);
	//float CounterFreqRead(int nBase,int nCounter,int nFreqStyle,float fFilterMaxFreq);
	//float CounterSpeedRead(int nBase,int nCounter,int nFreqStyle,float fFilterMaxFreq);

public:

       //AD1804
       BOOL SampleAC1082_1Ch(int nBase,int nChannel,WORD* pwData,int nCount,BOOL bAd16, BOOL bHigh);


        //AD976
        //BOOL AD976_1Ch_Phase(int nBase,int nChannel,
        //        WORD *pwData, WORD* pwRect, int nCount,
	//        int nRectPort, int nRectBit, BOOL bAd16, BOOL bHigh);

        //BOOL SampleAD976_1Ch(int nBase, int nChannel, WORD* pwData, int nCount, BOOL bAd16, BOOL bHigh);
	BOOL SampleAD976_2Ch(int nBase, int nChannel, WORD* pwData, int nCount, BOOL bAd16);


	//Sail1674
	//BOOL SampleSail1674_1Ch_Phase(int nBase,int nChannel,WORD *pwWave,
	//	WORD* pwRect, int nCount, int nPort, int nBit,BOOL bHigh=FALSE);

	//BOOL SampleSAIL1674_1Ch(int nBase,int nChannel,WORD* pwData,int nCount,BOOL bHigh);
	BOOL SampleSAIL1674_2Ch(int nBase,int nChannel,WORD* pwData,int nCount);
	//Surge
	void  SampleDataSurgeEx(WORD *pwData,int nCount,int nOscillatory,int nDivideFreq,
		BOOL bInternalTrigger, BOOL bSign = FALSE, BOOL bYamingTrigger = FALSE, int nRotorTime = 100);

	float ModifySurgeCross(WORD *pwData, WORD *pwDataSt, int nCount, int nStart, int nEnd, WORD wDC, BOOL bAverage, int nSurgeSimilarity);
	float ModifySurgeArea (WORD *pwData, WORD *pwDataSt, int nCount, int nStart, int nEnd, WORD wDC, BOOL bAverage, int nSurgeSimilarity);
	float ModifySurgePhase(int nZeroIndex, WORD *pwData, WORD *pwDataSt, int nCount, WORD wDC);

	float ModifySurgeEx(WORD *pwData,int nCount,int nIndex,BOOL bAverage);
};
//void  HglgDrawWave(int nX,int nY,int nWidth,int /*nHeight*/,float fScaleX,float fScaleY,WORD wFlags,WORD *pwWaveData,int nCount,BOOL bPurge,int nForeColor,int nBackColor=CYAN);

/*
#define  BASE_PCL_711B       0x220
//-----------------------------------------------
#define  COUNTER0_PCL_711B         (BASE_PCL_711B+0x00)
#define  COUNTER1_PCL_711B         (BASE_PCL_711B+0x01)
#define  COUNTER2_PCL_711B         (BASE_PCL_711B+0x02)
#define  COUNTER_CONTROL_PCL_711B  (BASE_PCL_711B+0x03)
//-----------------------------------------------
#define  AD_L_PCL_711B       (BASE_PCL_711B+0x04)
#define  AD_H_PCL_711B       (BASE_PCL_711B+0x05)
//-----------------------------------------------
#define  DI_L_PCL_711B       (BASE_PCL_711B+0x06)
#define  DI_H_PCL_711B       (BASE_PCL_711B+0x07)
//-----------------------------------------------
#define  CLEAR_IRQ_PCL_711B  (BASE_PCL_711B+0x08)
//-----------------------------------------------
#define  GAIN_PCL_711B       (BASE_PCL_711B+0x09)
//-----------------------------------------------
#define  CHANNEL_PCL_711B    (BASE_PCL_711B+0x0A)
//-----------------------------------------------
#define  MODE_PCL_711B       (BASE_PCL_711B+0x0B)
//-----------------------------------------------
#define  SOFTWARE_PCL_711B   (BASE_PCL_711B+0x0C)
//-----------------------------------------------
#define  DO_L_PCL_711B       (BASE_PCL_711B+0x0D)
#define  DO_H_PCL_711B       (BASE_PCL_711B+0x0E)
//-----------------------------------------------
#define  GAIN_5V01_PCL_711B         0x01
#define  GAIN_5V02_PCL_711B         0x02
#define  GAIN_5V04_PCL_711B         0x04
#define  GAIN_5V08_PCL_711B         0x08
#define  GAIN_5V16_PCL_711B         0x10
//-----------------------------------------------
#define  BINRARY(a,b,c,d,e,f,g,h)      ((BYTE)(a<<7)+\
					(BYTE)(b<<6)+\
					(BYTE)(c<<5)+\
					(BYTE)(d<<4)+\
					(BYTE)(e<<3)+\
					(BYTE)(f<<2)+\
					(BYTE)(g<<1)+\
					(BYTE)(h<<0))
//-----------------------------------------------
#define  Rt_Zero_LOW   0x00
#define  Rt_Zero_High  0x01
//-----------------------------------------------
#define SHIFT8(a)   (a<<8)
#define SHIFT16(a)   (a<<16)
#define LOBYTE(a)   ((BYTE)(a&0xFF))
#define HIBYTE(a)   ((((DWORD)a)>>8)&0xFF)
#define LOWORD(a)   ((WORD)(a&0xFFFFL))
#define HIWORD(a)   ((((DWORD)a)>>16)&0xFFFFL)
//-----------------------------------------------
void PCL_711B_Sample_TimerNonIRQ(WORD *pData,int nCount,int nChannel=0,DWORD dwSample=5000lu,int nGainType=GAIN_5V01_PCL_711B,BOOL bWait=TRUE);
void PCL_711B_Emulate(WORD *pData,int nCount,float fAmp,float fFreq,DWORD dwSampleRate=5000lu);
//-----------------------------------------------
void  DrawWave(int nX,int nY,int nWidth,int nHeight,float fScaleX,float fScaleY,WORD wFlags,float *pwWaveData,int nCount,BOOL bPurge,int nForeColor,int nBackColor);
//-----------------------------------------------
void WindowHM(WORD *pwData,int nCount);
void WindowHN(WORD *pwData,int nCount);
void WindowBox(WORD *pwData,int nCount);
void fft(float *xreal,float *ximag,int n,int inv);
int  GetMaxAmpLocation(float *pfReal,float *pfImag,int nCount);
int  GetPeakCount(float *pfReal,float *pfImag,int nCount);
int  GetLocationPeak(float *pfReal,float *pfImag,int nCount,int nIndex);

void FAR GetMin2Coordinate(float *pfDataX,float *pfDataY,int nCount,float *pfK,float *pfB,float *pfR2);
void FAR Line(int,int,int,int,int);

void FAR EmulateSampleSurge(WORD *pwSource,int nCount);
BOOL FAR InitFreq(int nChannel);
float GetTemperatureFromRt(float fRt);
float GetAntiLine(float x1,float y1,float x2,float y2,float fR);

BOOL FAR EmulationSurge9225(WORD* pwData,int nCount,int nBase,int nOscillatory,int nDivideFreq,BOOL bInternalTrigger);

BOOL New_PCL_711B_Single_Software(int nBase,int nChannel,WORD* pwData,int nCount);

void PCL711B_Emulate(WORD *pData,int nCount,float fAmp,float fFreq,DWORD dwSampleRate);
void PCL711B_Sample_Timer(int nBase,WORD  *pwData,int nCount,int nChannel,DWORD dwSampleRate,int nGainType=SAIL_GAIN_5V01_PCL711B);
void PCL711B_Sample_Timer(int nBase,float *pfData,int nCount,int nChannel,DWORD dwSampleRate,int nGainType=SAIL_GAIN_5V01_PCL711B);
*/
float GetTemperatureFromRt(float fRt);
void PCL711B_Sample_Timer(int nBase,WORD *pwData,int nCount,int nChannel,BOOL bNotInit,DWORD dwSampleRate,int nGainType=SAIL_GAIN_5V01_PCL711B);

int AD_AC1082_Cha (int nChannel);

BOOL PublicSampleData_Ch1(int nBase,int nChannel, WORD* pwData,int nCount,BOOL bHigh);
BOOL PublicSampleData_Ch2(int nBase,int nChannel, WORD* pwData,int nCount);
BOOL PublicSampleData_Ch1_Phase(int nBase,int nChannel,
	WORD *pwData, WORD* pwRect, int nCount,
	int nRectPort, int nRectBit, BOOL bHigh);


BOOL FAR JudgeFS300_20A();
BOOL FAR MachineSelfTest(BOOL bSurge, BOOL bWaitBeforeHigh);
void FAR DisplaySelfTestInformation(int nFormat);
BOOL GetSelfTestInformation();
void FumuEx(double dbA, double dbB, double dbC, float* pfRoot1, float* pfRoot2);

float ModifySurgeAreaEx(WORD *pwData,int nCount,int nIndex,BOOL bAverage,WORD wDC);
float ModifySurgePole(WORD *pwData,int nCount,int nIndex,BOOL bAverage);

float ModifySurgePeriod(float fR);


#endif