#ifndef  _ARMATURE_H_
#define  _ARMATURE_H_
//------------------------------------------------------------------------
#include <Register.h>
#include <Graph.h>

#define    ARMATURE_SINGLE_BAR_ADD1      0


//#define MANUFA_RELAY_ORMON      0
//#define MANUFA_RELAY_NAIS       1
//#define MANUFA_RELAY_NEC        2
//#define MANUFA_RELAY_WANGLI     3
//#define MANUFA_RELAY_SONGCHUANG 4
//#define MANUFA_RELAY_SLOWEST    5




#define  SURGE_GRAPHWAVE_COUNT 1000


//#define  MAX_YAMING_COUNT               24
#define  MAX_SHORT_SUPPORT_COUNT        24
//#define  NEED_YAMING_TRIGGER
//#define  NEED_YAMING_DOUBLE
//#define  NEED_YAMING_PHASE


//#define  NEED_ARMATURE_LIMIT_12 12
//#define  NEED_ARMATURE_LIMIT_24 24
//#define  NEED_ARMATURE_LIMIT_32 32
#define  NEED_ARMATURE_LIMIT_36 36
//#define  NEED_ARMATURE_LIMIT_40 40
//#define  NEED_ARMATURE_LIMIT_48 48
//#define  NEED_ARMATURE_LIMIT_54 54
//#define  NEED_ARMATURE_LIMIT_64 64


#if defined(MAX_YAMING_COUNT) || defined(NEED_YAMING_COUNT2) || defined(MAX_SHORT_SUPPORT_COUNT)
	#undef NEED_ARMATURE_LIMIT_12
	#undef NEED_ARMATURE_LIMIT_24
	#undef NEED_ARMATURE_LIMIT_32
	#undef NEED_ARMATURE_LIMIT_36
	#undef NEED_ARMATURE_LIMIT_40
	#undef NEED_ARMATURE_LIMIT_48
	#undef NEED_ARMATURE_LIMIT_54
	#undef NEED_ARMATURE_LIMIT_64
#endif




#if defined(NEED_ARMATURE_LIMIT_12)
	#define  MAX_ARMATURE_SUPPORT_COUNT     12
#elif defined(NEED_ARMATURE_LIMIT_24)
	#define  MAX_ARMATURE_SUPPORT_COUNT     24
#elif defined(NEED_ARMATURE_LIMIT_32)
	#define  MAX_ARMATURE_SUPPORT_COUNT     32
	#elif defined(NEED_ARMATURE_LIMIT_36)
	#define  MAX_ARMATURE_SUPPORT_COUNT     36
#elif defined(NEED_ARMATURE_LIMIT_40)
	#define  MAX_ARMATURE_SUPPORT_COUNT     40
#elif defined(NEED_ARMATURE_LIMIT_48)
	#define  MAX_ARMATURE_SUPPORT_COUNT     48
#elif defined(NEED_ARMATURE_LIMIT_54)
	#define  MAX_ARMATURE_SUPPORT_COUNT     54
#elif defined(NEED_ARMATURE_LIMIT_64)
	#define  MAX_ARMATURE_SUPPORT_COUNT     64
#elif defined(MAX_SHORT_SUPPORT_COUNT)
	#define  MAX_ARMATURE_SUPPORT_COUNT     24
#elif defined(MAX_YAMING_COUNT) || defined(NEED_YAMING_COUNT2)
	#define  MAX_ARMATURE_SUPPORT_COUNT     24
#else
	#define  MAX_ARMATURE_SUPPORT_COUNT     64

#endif

#if (defined(MAX_YAMING_COUNT) || defined(NEED_YAMING_COUNT2)) && !defined(MAX_SHORT_SUPPORT_COUNT)
	#error "Need define MAX_SHORT_SUPPORT_COUNT Macro!"
#endif



#define STANDAND_BLOCK_SIZE 320L //240L

//Armature Style
#define ARMATURE_STYLE_C_N_CCW  0  //常电阻+普通绕线+逆时针绕线
#define ARMATURE_STYLE_C_N_CW   1  //常电阻+普通绕线+顺时针绕线
#define ARMATURE_STYLE_C_P_CCW  2  //常电阻+峰谷绕线+逆时针绕线
#define ARMATURE_STYLE_C_P_CW   3  //常电阻+峰谷绕线+顺时针绕线
#define ARMATURE_STYLE_S_N_CCW  4  //单飞叉+普通绕线+逆时针绕线
#define ARMATURE_STYLE_S_N_CW   5  //单飞叉+普通绕线+顺时针绕线
#define ARMATURE_STYLE_S_P_CCW  6  //单飞叉+峰谷绕线+逆时针绕线
#define ARMATURE_STYLE_S_P_CW   7  //单飞叉+峰谷绕线+顺时针绕线
#define ARMATURE_STYLE_D_N_CCW  8  //双飞叉+普通绕线+逆时针绕线
#define ARMATURE_STYLE_D_N_CW   9  //双飞叉+普通绕线+顺时针绕线
#define ARMATURE_STYLE_D_P_CCW  10 //双飞叉+峰谷绕线+逆时针绕线
#define ARMATURE_STYLE_D_P_CW   11 //双飞叉+峰谷绕线+顺时针绕线

#define  ARMATURE_STYLE_EX_NORMAL        0x0000 //Normal
#define  ARMATURE_STYLE_EX_SHORT         0x0001 //Short-1
#define  ARMATURE_STYLE_EX_PANE          0x0002 //Pane
#define  ARMATURE_STYLE_EX_SHORT2        0x0003 //Short-2
#define  ARMATURE_STYLE_EX_SHORT3        0x0004 //Short-3
#define  ARMATURE_STYLE_EX_180           0x0005 //180
#define  ARMATURE_STYLE_EX_SHORT3_W      0x0006 //Reserved//Short-3-W
#define  ARMATURE_STYLE_EX_PANE2         0x0007 //Pane2
#define  ARMATURE_STYLE_EX_SHORT_T180    0x0008 //Short   + 180Ex
#define  ARMATURE_STYLE_EX_SHORT2_T180   0x0009 //Short2  + 180Ex
#define  ARMATURE_STYLE_EX_SHORT3_T180   0x000A //Short3  + 180Ex
#define  ARMATURE_STYLE_EX_BBARx         0x000B //BBar-1 + BBar-X
#define  ARMATURE_STYLE_EX_SHORT4        0x000C //Normal+Jiya2
#define  ARMATURE_STYLE_EX_SHORT5        0x000D //Normal+Jiya3
#define  ARMATURE_STYLE_EX_PANE3         0x000E //Pane3+ZhangJie


#define  ARMATURE_STYLE_CONSTANT        0x0001 //Constant
#define  ARMATURE_STYLE_SINGLE_FLYING   0x0002 //Single Flying
#define  ARMATURE_STYLE_DOUBLE_FLYING   0x0004 //Double Flying
#define  ARMATURE_ADD_PEAK              0x0008//Peak Or Gain
#define  ARMATURE_CLOCK                 0x0010//Clock Or Anti-Clock



#define  STATOR_STYLE_SERIAL_FLYING   0x0001 //Serial
#define  STATOR_STYLE_SINGLE_FLYING   0x0002 //Single
#define  STATOR_STYLE_THREE_FLYING    0x0004 //Three
#define  STATOR_STYLE_T               0x0008 //T while Single
#define  STATOR_STYLE_O               0x0010 //O while Three


#define ARMATURE_2003_VER       0
#define ARMATURE_2004_VER       1
#define ARMATURE_2005_VER1      2
#define ARMATURE_2005_VER2      3
#define ARMATURE_2005_VER3      4
#define ARMATURE_2005_VER4      5
#define ARMATURE_2006_CLEVA     6
#define ARMATURE_2006_VER2      7
#define ARMATURE_2006_JHN       8
#define ARMATURE_2006_VER4      9
#define ARMATURE_2006_4067     10
#define ARMATURE_2010_CX       11


#define OVER_DAMP         0
#define CRITICAL_DAMP     1
#define LACK_DAMP         2
#define NONE_DAMP         3

#define ARMATURE_METHOD_COIL   0
#define ARMATURE_METHOD_BARBAR 1
#define ARMATURE_METHOD_OTHER  2


#define SURGE_JUDGE_PERIOD_SONG_HIGH  1
#define SURGE_JUDGE_PERIOD_SONG_MID   2
#define SURGE_JUDGE_PERIOD_SONG_LOW   3
#define SURGE_JUDGE_PERIOD_NORMAL     4
#define SURGE_JUDGE_PERIOD_JING_LOW   5
#define SURGE_JUDGE_PERIOD_JING_MID   6
#define SURGE_JUDGE_PERIOD_JING_HIGH  7

//#if defined(MAX_YAMING_COUNT) || defined(NEED_YAMING_COUNT2)
//	#define VALUE_SURGE_OR_YAMING VALUE_STYLE_YAMING
//#elif defined(ARMATURE_210_B)
//	#define VALUE_SURGE_OR_YAMING VALUE_STYLE_SURGE_EX
//#else
//	#define VALUE_SURGE_OR_YAMING VALUE_STYLE_SURGE
//#endif

//------------------------------------------------------------------------
class  CArmatureLearning:public CGraphWave
{
private:
	BOOL   m_bAutoScale;
	int    m_nStartPole;
	BOOL   m_bClock;

private:
	int    m_nStyle;
//	int    m_nStyleEx;
	int    m_nBar;

	float  m_sfWave[MAX_ARMATURE_SUPPORT_COUNT];
	float  m_sfLowLimits[MAX_ARMATURE_SUPPORT_COUNT];
	float  m_sfHighLimits[MAX_ARMATURE_SUPPORT_COUNT];
	int    m_nAround;

public:
	int    GetStartPole(){return(m_nStartPole);}
	void   SetStartPole(int nStartPole){m_nStartPole = nStartPole;}

	void   SetClock(BOOL bClock){m_bClock = bClock;}

	void   ShowCircleCoordinate();


	void   SetAutoScale(BOOL bValue){m_bAutoScale=bValue;}
	BOOL   GetAutoScale(){return(m_bAutoScale);}

        BOOL   GetAround(){return(m_nAround);}
        void   SetAround(BOOL nAround){m_nAround = nAround;}

public:
	void   InitLimit();
public:
	void   SetGraphForeColor(int nColor){m_nGraphForeColor=nColor;}
	void   SetGraphBackColor(int nColor){m_nGraphBackColor=nColor;}
	void   SetGraphLineColor(int nColor){m_nGraphLineColor=nColor;}

	//void   SetTextColor(int nColor){m_nTextColorX=nColor;}

	int    GetGraphForeColor(){return(m_nGraphForeColor);}
	int    GetGraphBackColor(){return(m_nGraphBackColor);}
	int    GetGraphLineColor(){return(m_nGraphLineColor);}
	//int    GetTextColor(){return(m_nTextColor);}

public:
//	void   SetStyleEx(int nStyleEx){m_nStyleEx = nStyleEx;}
	void   SetStyle(int nStyle){m_nStyle=nStyle;}
	int    GetStyle(){return(m_nStyle);}
	void   SetBarCount(int nBar){m_nBar=nBar;}
	int    GetBarCount(){return(m_nBar);}
	void   SetWave(float *pfWave,int nCount);
	void   SetWave(int nIndex,float fBar);
	float* GetWave(){return(m_sfWave);}

	void   SetLowLimits(int nIndex,float fValue);
	void   SetLowLimits(float *pfValue,int nCount);
	void   SetHighLimits(int nIndex,float fValue);
	void   SetHighLimits(float *pfValue,int nCount);
public:
	void   DrawWave(BOOL bXorMode = TRUE) ;
	void   ShowFrame();
public:
	CArmatureLearning();
};


class CCalculate
{
private:
	int    m_nPin;
	DWORD  m_dwStyle;
	int    m_nStyleEx;

	static int    m_nBaseIndex;
	float  m_fI;
	float  m_fIc;
	float  m_fTemp;
	float  m_fsData[MAX_ARMATURE_SUPPORT_COUNT+4][MAX_ARMATURE_SUPPORT_COUNT];
private:
	int    m_nMinBar;
	int    m_nMaxBar;
	int    m_nBar;
static  BOOL   m_nAround;

	BOOL   m_bWeldBar;
public:
	float  m_fSampleVolMin;
	float  m_fSampleVolMax;
public:
	float GetSampleVolMin(){return (m_fSampleVolMin);}
	float GetSampleVolMax(){return (m_fSampleVolMax);}
	void  PushSampleVol(float fVol, BOOL bInit);
public:
	BOOL   GetWeldBar(){return(m_bWeldBar);}
	void   SetWeldBar(BOOL bWeldBar){m_bWeldBar = bWeldBar;}
	BOOL   GetAround(){return(m_nAround);}
        void   SetAround(BOOL nAround){m_nAround = nAround;}

private:
	void   (*m_pDrawFc)(WORD *pwData,int nCount,int nMode,int nColor, BOOL bCover);
	void   (*m_pSampleFc)(int nBase,int nChannel,WORD* pwData,int nCount,BOOL bHigh);
	void   (*m_pCircleFc)(int nBar,BOOL bOpen);
	float  (*m_pSampleConstCurrentFc)();

	int    m_nOpenRelayTime;
	int    m_nCloseRelayTime;
	int    m_nChannelRelayTime;
	BOOL   m_bDebug;
private:  //2005-10-17
static	int    m_nMethod;
public:
	void   SetMethod(int nMethod){m_nMethod = nMethod;}
	int    GetMethod(){return(m_nMethod);}
	void   SetBar(int nBar);
	int    GetBar(){return(m_nBar);}
	void   SetStyle(DWORD dwStyle){m_dwStyle = dwStyle;}
	void   SetStyleEx(int nStyleEx){m_nStyleEx = nStyleEx;}

	DWORD  GetStyle(){return(m_dwStyle);}
	int    GetMinBar(){return(m_nMinBar);}
	int    GetMaxBar(){return(m_nMaxBar);}
	void   SetPin(int nPin);
	int    GetPin(){return(m_nPin);}
	void   SetBase(int nBase){m_nBaseIndex = nBase;}
	int    GetBase(){return(m_nBaseIndex);}
	void   GetRange(int* pnMin,int *pnMax){*pnMin = m_nMinBar;*pnMax = m_nMaxBar;}

	void   SetI(float fI) {m_fI = fI;}
	void   SetIc(float fIc) {m_fIc = fIc;}
	float  GetI(){return(m_fI);}
	float  GetIc(){return(m_fIc);}

	float  GetTemp(){return(m_fTemp);}
	BOOL   SetData(int nRow,int nCol,float fData);
	float  GetData(int nRow,int nCol);


	void   SetSample3Data(int nRow,int nChannel,float fData);
	void   ClearWave(BOOL bClear);
	void   Display();
	void   DebugData1(char* pStrFile);
	void   DebugData2(char* pStrFile);
	void   DebugData3(char* pStrFile);

public:
	float Modify_Average(WORD *pwData,int nCount);
	float Modify_Internal(WORD *pwData,int nCount,float fX1,float fY1,float fX2,float fY2);

	float GetLineCoordinateXY(float fX1,float fY1,float fX2,float fY2,float fValue);

	void  SetDrawFc(void (*pFc)(WORD *pwData,int nCount,int nMode,int nColor, BOOL bCover)){m_pDrawFc = pFc;}
	void  SetSampleFc(void (*pFc)(int nBase,int nChannel,WORD* pwData,int nCount,BOOL bHigh)){m_pSampleFc = pFc;}
	void  SetCircleFc(void (*pFc)(int nBar,BOOL bOpen)){m_pCircleFc = pFc;}
	void  SetConstCurrentFc(float (*pFc)()){m_pSampleConstCurrentFc = pFc;}

	void  SetOpenRelayTime(int nTime){m_nOpenRelayTime=nTime;}
	void  SetCloseRelayTime(int nTime){m_nCloseRelayTime=nTime;}
	void  SetChannelRelayTime(int nTime){m_nChannelRelayTime=nTime;}
	void  SetDebug(BOOL bDebug){m_bDebug = bDebug;}

	static void  KernelCalcForAfter(float * pfResis, float* pfWeld, int nBar, int nStyle, int nStartPole);
	void  CalcForAfter(float * pfTargetBase, float* pfTargetWeld, BOOL bTemp);


       BOOL  SampleData_Bar4067(CRegister* pTestRegister,WORD* pwSourceData,WORD* pwTargetData,
			int nCount,float *pfAdjust,int nBase,int nChannel,BOOL bHigh);

       BOOL  SampleData_Coil4067(CRegister* pTestRegister,WORD* pwSourceData,WORD* pwTargetData,
		int nCount,float *pfAdjust,int nBase,int nChannel,BOOL bHigh);

       BOOL  SampleData_Coil4067Ex(CRegister* pTestRegister,WORD* pwSourceData,WORD* pwTargetData,
		 int nCount,float *pfAdjust,int nBase,int nChannel,BOOL bHigh);

	//Coil Method
	BOOL  SampleData_Coil1(CRegister* pTestRegister,WORD* pwSourceData,WORD* pwTargetData,
		int nCount,float *pfAdjust,int nBase,int nChannel,BOOL bHigh);
	BOOL  SampleData_Coil2(CRegister* pTestRegister,WORD* pwSourceData,WORD* pwTargetData,
		int nCount,float *pfAdjust,int nBase,int nChannel,BOOL bHigh);
	BOOL   Calc1(float *pfTargetBase,float*pfTargetWeld,BOOL bTemp=FALSE,float fTemp = 20.0f);

	//BBar Kernel
	BOOL  SampleData_Bar1_Kernel(CRegister* pTestRegister,WORD* pwSourceData,WORD* pwTargetData,
		int nCount, float *pfAdjust,
		int nBase,int nChannel,BOOL bHigh, int nMethod);

	BOOL  SampleData_BarB(CRegister* pTestRegister,WORD* pwSourceData,WORD* pwTargetData,
		int nCount, float *pfAdjust,
		int nBase,int nChannel,BOOL bHigh);

	BOOL  SampleData_BarW(CRegister* pTestRegister,WORD* pwSourceData,WORD* pwTargetData,
		int nCount, float *pfAdjust,
		int nBase,int nChannel,BOOL bHigh);

	//BBar Method
	BOOL  SampleData_Bar1(CRegister* pTestRegister,WORD* pwSourceData,WORD* pwTargetData,
		int nCount, float *pfAdjust,
		int nBase,int nChannel,BOOL bHigh);

	//BBar Method- SELECTCHANNEL_BOARD
	BOOL  SampleData_Bar2(CRegister* pTestRegister,WORD* pwSourceData,WORD* pwTargetData,
		int nCount, float *pfAdjust,
		int nBase,int nChannel,BOOL bHigh);
	BOOL   Calc2(float *pfTargetBase,float*pfTargetWeld,BOOL bTemp=FALSE,float fTemp = 20.0f);

	//Old Firstsail
//	BOOL  SampleData_Coil2(CRegister* pTestRegister,WORD* pwSourceData,WORD* pwTargetData,
//		int nCount,float *pfAdjust,int nBase,int nChannel,BOOL bHigh);
//	BOOL   Calc3(float *pfTargetBase,float*pfTargetWeld,BOOL bTemp=FALSE,float fTemp=20.0f);


	BOOL  SampleData_Coil4(CRegister* pTestRegister, WORD* pwSourceData,
		WORD* pwTargetData, int nCount, float *pfAdjust,
		int nBase,int nChannel,BOOL bHigh);

	//BOOL   Calc4(float *pfTargetBase,float*pfTargetWeld,BOOL bTemp=FALSE,float fTemp=20.0f);
	BOOL  SampleData_A210(CRegister* pTestRegister,WORD* pwSourceData,WORD* pwTargetData,
		int nCount,float *pfAdjust,int nBase,int nChannel,BOOL bHigh);
	BOOL Calc_A210_Bar1(float *pfTargetBase, float* pfTargetWeld, BOOL bTemp,float fTemp);
	BOOL Calc_A210_Coil1(float *pfTargetBase, float* pfTargetWeld, BOOL bTemp,float fTemp);

	BOOL  SampleData_Debug(WORD* pwSourceData,WORD* pwTargetData,
		int nCount,float *pfAdjust,int nBase,int nChannel,BOOL bHigh,
		int nPole1, int nPole2, int nCh1, int nCh2);

//	int    SortArmatureData(float *pfData,int nCount);
	static int    FindStartPole(float *pfData,int nCount, int nStyle);
	static void   AntiArmatureData(float *pfData,int nCount);
	float  GetVoltageEmulation(float* pfDataBuffer,int nBar,float fI,int nPole,int nIndex);
	void   LoopSample3Data(float *pfDataBuffer,int nBar,BOOL bClock);

public:
	static void   LoopArmatureData(int nIndex ,float *pfData,int nCount);

public:
	CCalculate();
};

class CSurgeStandand:public CDialog
{
public:
	BOOL  m_bStandand;
	BOOL  m_bWave;
	//BOOL  m_bStart;
	//BOOL  m_bTest;


	//BOOL   m_bSaveResistance;
public:
	void OnTime();
	//void OnDraw();
	//void   SetSaveResistance(BOOL bSave){m_bSaveResistance = bSave;}
	//BOOL   GetSaveResistance(){return(m_bSaveResistance);}
public:
	void DrawAverageNumber();
	int  GetAlreadyTestCount();
	BOOL IsEmpty();
	void DrawStandand();
public:
	void FAR KeyF2();
    void FAR KeyF3();
public:
	void DrawSurgeWave(int nIndex , WORD *pwData,BOOL bFinish);
public:
	CSurgeStandand();//{m_bStandand=m_bWave=m_bStart=m_bTest=FALSE;};
};


void FAR NewSurgeStandand();
BYTE FAR CalcChannelByte(int nChannel);
void FAR Execute_SelectChannelOff();
void FAR Execute_SelectChannelOn(int nChannel1, int nChannel2);

void TransrateBBarToCoil_20180119(float* pfRb, float* pfRc, int nBar, int nSteps);

//------------------------------------------------------------------------
#endif