view FXAnalyse.c @ 61:37075b60408b

Fix N2 measurement after cleanup. Further simplification
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Thu, 18 Oct 2012 12:24:31 +0200
parents cd7b5903f168
children 53dbce5f0745
line wrap: on
line source

#include <utility.h>
#include <ansi_c.h>
#include <cvirte.h>		
#include <userint.h>
#include <formatio.h>
#include <string.h>
#include <future.h>

#include "YLCStuff.h"
#include "FXAnalyse.h" 
#include "Plot.h"
#include "Allan.h"
#include "DDSBes.h"
#include "DDS4xAD9912.h"
#include "DDS_Fox.h" 
#include "muParserDLL.h"

#define FXLINELENGTH 123
#define LOGFILEPATH "C:\\Femto\\Software\\FXQE80"

#define DEDRIFT_DDS_FREQUENCY 70000000

// panel handling stuff
static PanelHandle MainPanel ;
static PanelHandle CalcN1Panel ;
static PanelHandle CalcN2Panel ;
static PanelHandle CalcN3Panel ;
static PanelHandle EstimateN3Panel;

char LogFileName[MAX_PATHNAME_LEN];
char ExtraMathFileName[MAX_PATHNAME_LEN];

double utc = 0;
double mjd = 0;

double Ch1, Ch2, Ch3, Ch4;
double Math1, Math2, Math3, Math4, Math5;
double N1, N2, N3, N4;

Plot_Data PlotCh1, PlotCh2, PlotCh3, PlotCh4, PlotMath1, PlotMath2, PlotMath3, PlotMath4, PlotMath5 ;  
Allan_Data AllanCh1, AllanCh2, AllanCh3, AllanCh4, AllanMath1, AllanMath2, AllanMath3, AllanMath4, AllanMath5 ; 

// 1xAD9956 DDS box
DDSParameter DDS1xAD9956;
// 4xAD9912 DDS box
DDS4xAD9912_Data DDS4xAD9912;

muParserHandle_t MathParser1, MathParser2, MathParser3, MathParser4, MathParser5;

int Acquiring = FALSE;

long OldLogFilePtr = 0;
double Ndiv = 8.0;

enum {
	N_MEASUREMENT_NONE,
	N_MEASUREMENT_INIT,
	N_MEASUREMENT_SLOPE,
	N_MEASUREMENT_ADJUST_FREQ_PLUS,
	N_MEASUREMENT_FREP_PLUS,
	N_MEASUREMENT_ADJUST_FREQ_MINUS,
	N_MEASUREMENT_FREP_MINUS,
};

int settling = 0;

int Measuring_1 = FALSE;
int Measuring_2 = FALSE;											  
int Measuring_3 = FALSE;

double FrequDDS1=110000000.0, FrequDDS2=15300000.0, FrequDDS3=150400000.0, FrequDDS4=110000000.0;
double Slope_1=0.0,Slope_2=0.0,Slope_3=0.0,Beatslope_2=0.0; 
double SlopeTime1=40.0, SlopeTime2=40.0; SlopeTime3=40.0;

double N_1=0.0, N_2=0.0, N_3=0.0;
double DeltaT_1=20.0, DeltakHz_1=500.0, t1_1=0.0, t2_1=0.0, t3_1=0.0, Frepplus_1=0.0, Frepminus_1=0.0;
double DeltaT_2=20.0, DeltakHz_2=500.0, t1_2=0.0, t2_2=0.0, t3_2=0.0, Frepplus_2=0.0, Frepminus_2=0.0;
double DeltaT_3=20.0, DeltakHz_3=500.0, t1_3=0.0, t2_3=0.0, t3_3=0.0, Frepplus_3=0.0, Frepminus_3=0.0;

int n_1=0, n_2=0, n_3=0;
double Frequ_slope_1=0.0,Moy_slope_1=0.0,Slope_slope_1=0.0,Frequ_slope_2=0.0,Moy_slope_2=0.0,Slope_slope_2=0.0,Frequ_slope_3=0.0,Moy_slope_3=0.0,Slope_slope_3=0.0;  
int N_slope_1=0,N_slope_2=0,N_slope_3=0;
double  Beat_slope_2=0.0 ,Moy_Beatslope_2=0.0,Slope_Beatslope_2=0.0; 
double Ch4_slope=0.0,Moy_Ch4slope_1=0.0,Slope_Ch4slope_1=0.0,Ch4Slope=0.0;

double FrequencyDDSBesInit = 0.0;
double FrequencyDDS3Init = 0.0;

double DeltaDDS3=0.0,Delta10K_Plus=0.0,Delta10K_Minus=0.0;
double Nu1=0.0, Nu2= 200000-147000+282143746.557455e6;  

double Step1=800000.0,Step2=800000.0;

double Ch4Plus=0.0,Ch4Minus=0.0;

double Frequencystep1=10000.0, tbegin1=0.0, Frepbefore1=0.0, Frequency1=0.0;
double Frequencystep2=10.0, tbegin2=0.0, Frepbefore2=0.0, Ch2before=0.0, Frequency2=0.0;
double Frequencystep3=100000.0, tbegin3=0.0, Frepbefore3=0.0, Frequency3=0.0;

volatile bool Getsign1=FALSE,Getsign2=FALSE,Getsign3=FALSE;
double Signe1=1.0,Signe2=1.0,Signe3=0.0;

// slope cancelling

int SlopeMeasuring = FALSE;
int StopSlopeCancellingOnUnlocked = TRUE;
double SlopeMath2 = 0.0; // currently applied frequency dedrifiting slope
double TimetoSlope = 60.0;
double SlopeMeasuringTimeBegin = 0.0;

double Slope_Math2slope=0.0,Math2_slope=0.0,Moy_Math2slope=0.0;
int N_Math2slope=0.0;

double LimitToDelock=5.0;
double limitotakoff=70.0;

int ratio=10; //Recentre la frequence tous les ratios

int FrequCorrec = FALSE, KeepFrequ = TRUE, KeepSlope = TRUE;
int Nratio = -1;
double MoyMath2 = 0.0, CenterFrequencyCh2 = 0.0;
int CenterFrequencyCh2ToDetermine = FALSE;

enum {
	SLOPE_REFERENCE_MICROWAVE = 0,
	SLOPE_REFERENCE_HG_CAVITY = 1,
};

int invertSlopeSign = 0;
int slopeReference = SLOPE_REFERENCE_MICROWAVE;

int KeepCentering = FALSE; 
double Timetorecenter275K = 3600.0 * 10;
double Timetorecenter10K = 3600.0 * 3;
double CenteringTimeBegin275K = 0.0;
double CenteringTimeBegin10K = 0.0;


int DDSFox_Set(DDSParameter *DDS, double Frequency, double Sweeprate)
{
	DDS->Frequency = Frequency; 
	DDS->SweepRate = Sweeprate;
	
	DDSFox_SetFreq(DDS);
	DDSFox_SetDf(DDS);
	
	return 0;
}


muParserHandle_t initMathParser() 
{
	muParserHandle_t parser = mupCreate();
	mupDefineOprtChars(parser, "abcdefghijklmnopqrstuvwxyzµ"
                        	   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                        	   "+-*^/?<>=#!$%&|~'_");
	mupDefineVar(parser, "UTC", &utc);
	mupDefineVar(parser, "MJD", &mjd);
	mupDefineVar(parser, "Ch1", &Ch1);
	mupDefineVar(parser, "Ch2", &Ch2);
	mupDefineVar(parser, "Ch3", &Ch3);
	mupDefineVar(parser, "Ch4", &Ch4);
	mupDefineVar(parser, "DDS1", &(DDS4xAD9912.Frequency1));   
	mupDefineVar(parser, "DDS2", &(DDS4xAD9912.Frequency2));
	mupDefineVar(parser, "DDS3", &(DDS4xAD9912.Frequency3));  
	mupDefineVar(parser, "DDS4", &(DDS4xAD9912.Frequency4));  
	mupDefineVar(parser, "N1", &N1);
	mupDefineVar(parser, "N2", &N2);
	mupDefineVar(parser, "N3", &N3);
	mupDefineVar(parser, "Nu1", &Nu1);
	mupDefineVar(parser, "Nu2", &Nu2);
	mupDefineVar(parser, "DeltaDDS3", &DeltaDDS3);
	mupDefineVar(parser, "Signe1", &Signe1);
	mupDefineVar(parser, "Signe2", &Signe2); 
	mupDefineVar(parser, "Ndiv", &Ndiv); 
	mupDefinePostfixOprt(parser, "P", &Peta,  1);
	mupDefinePostfixOprt(parser, "T", &Tera,  1);
	mupDefinePostfixOprt(parser, "G", &Giga,  1);
	mupDefinePostfixOprt(parser, "M", &Mega,  1);
	mupDefinePostfixOprt(parser, "k", &kilo,  1);
	mupDefinePostfixOprt(parser, "m", &milli, 1);
	mupDefinePostfixOprt(parser, "u", &micro, 1);
	mupDefinePostfixOprt(parser, "µ", &micro, 1);
	mupDefinePostfixOprt(parser, "n", &nano,  1);
	mupDefinePostfixOprt(parser, "p", &pico,  1);
	mupDefinePostfixOprt(parser, "f", &femto, 1);
	
	return parser;
}


int main (int argc, char *argv[])
{
	double frequency;
	char expr[1024];
	
	if (InitCVIRTE (0, argv, 0) == 0)
		return -1;
	if ((MainPanel = LoadPanel (0, "FXAnalyse.uir", PANEL)) < 0)
		return -1;
	if ((CalcN1Panel = LoadPanel (0, "FXAnalyse.uir", CALCN1)) < 0)
		return -1;
	if ((CalcN2Panel = LoadPanel (0, "FXAnalyse.uir", CALCN2)) < 0)
		return -1;
	if ((CalcN3Panel = LoadPanel (0, "FXAnalyse.uir", CALCN3)) < 0)
		return -1;
	if ((EstimateN3Panel = LoadPanel (MainPanel, "FXAnalyse.uir", ESTIMATEN3)) < 0)
		return -1;
	
	DisplayPanel (MainPanel);
	
	// initialize 4x AD9959 DDS box
	DDS4xAD9912_Reset(&DDS4xAD9912);
	GetCtrlVal(MainPanel, PANEL_DDS1, &frequency);
	DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, frequency);
	GetCtrlVal(MainPanel, PANEL_DDS2, &frequency);
	DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, frequency);
	GetCtrlVal(MainPanel, PANEL_DDS3, &frequency);
	DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, frequency);
	GetCtrlVal(MainPanel, PANEL_DDS4, &frequency);
	DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, frequency);
	
	// initialyze 1x AD9956 DDS box
	strcpy(DDS1xAD9956.ip, "145.238.205.58"); 
	DDS1xAD9956.Port = 6665;
	DDS1xAD9956.Profil = 7; // configuration profile: single freq=0, sweep=7
	DDS1xAD9956.Clock = 200000000;
	DDS1xAD9956.Frequency = DEDRIFT_DDS_FREQUENCY; 
	DDS1xAD9956.Delta_T = 0.01;
	DDS1xAD9956.SweepRate = 0.0;
	DDS_Initialize(&DDS1xAD9956);
	
	Ch1 = Ch2 = Ch3 = Ch4 = 0.0;
	
	GetCtrlVal(MainPanel, PANEL_N1CHOICE, &N1);
	GetCtrlVal(MainPanel, PANEL_N2CHOICE, &N2);
	GetCtrlVal(MainPanel, PANEL_N3CHOICE, &N3);
	
	MathParser1 = initMathParser();
	GetCtrlVal(MainPanel, PANEL_MATHSTRING1, expr);
	mupSetExpr(MathParser1, expr);
	
	MathParser2 = initMathParser();
	mupDefineVar(MathParser2, "Math1", &Math1);
	mupDefineVar(MathParser2, "DDS", &(DDS4xAD9912.Frequency1));
	GetCtrlVal(MainPanel, PANEL_MATHSTRING2, expr);
	mupSetExpr(MathParser2, expr);
	
	MathParser3 = initMathParser();
	mupDefineVar(MathParser3, "Math1", &Math1);
	mupDefineVar(MathParser3, "Math2", &Math2);
	mupDefineVar(MathParser3, "DDS", &(DDS4xAD9912.Frequency2));
	GetCtrlVal(MainPanel, PANEL_MATHSTRING3, expr);
	mupSetExpr(MathParser3, expr);
	
	MathParser4 = initMathParser();
	mupDefineVar(MathParser4, "Math1", &Math1);
	mupDefineVar(MathParser4, "Math2", &Math2);
	mupDefineVar(MathParser4, "Math3", &Math3);
	GetCtrlVal(MainPanel, PANEL_MATHSTRING4, expr);
	mupSetExpr(MathParser4, expr);
	
	MathParser5 = initMathParser();
	mupDefineVar(MathParser5, "Math1", &Math1);
	mupDefineVar(MathParser5, "Math2", &Math2);
	mupDefineVar(MathParser5, "Math3", &Math3);
	mupDefineVar(MathParser5, "Math4", &Math4);
	GetCtrlVal(MainPanel, PANEL_MATHSTRING5, expr);
	mupSetExpr(MathParser5, expr);
	
	RunUserInterface();
	
	DiscardPanel(MainPanel);
	return 0;
}


void OnCloseViewPanel(int panel){
	
	if (panel==PlotCh1.PlotPanel) 		{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_FREQ1PLOT, FALSE) ; } ; 
	if (panel==PlotCh2.PlotPanel) 		{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_FREQ2PLOT, FALSE) ; } ; 
	if (panel==PlotCh3.PlotPanel) 		{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_FREQ3PLOT, FALSE) ; } ; 
	if (panel==PlotCh4.PlotPanel) 		{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_FREQ4PLOT, FALSE) ; } ;
	if (panel==PlotMath1.PlotPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_MATH1PLOT, FALSE) ; } ; 
	if (panel==PlotMath2.PlotPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_MATH2PLOT, FALSE) ; } ;
	if (panel==PlotMath3.PlotPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_MATH3PLOT, FALSE) ; } ; 
	if (panel==PlotMath4.PlotPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_MATH4PLOT, FALSE) ; } ;
	if (panel==PlotMath5.PlotPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_MATH5PLOT, FALSE) ; } ;
	
	if (panel==AllanCh1.AllanPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_FREQ1ALLAN, FALSE) ; } ; 
	if (panel==AllanCh2.AllanPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_FREQ2ALLAN, FALSE) ; } ; 
	if (panel==AllanCh3.AllanPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_FREQ3ALLAN, FALSE) ; } ; 
	if (panel==AllanCh4.AllanPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_FREQ4ALLAN, FALSE) ; } ; 
	if (panel==AllanMath1.AllanPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_MATH1ALLAN, FALSE) ; } ; 
	if (panel==AllanMath2.AllanPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_MATH2ALLAN, FALSE) ; } ; 
	if (panel==AllanMath3.AllanPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_MATH3ALLAN, FALSE) ; } ; 
	if (panel==AllanMath4.AllanPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_MATH4ALLAN, FALSE) ; } ; 
	if (panel==AllanMath5.AllanPanel) 	{ SetCtrlVal(MainPanel,PANEL_CHECKBOX_MATH5ALLAN, FALSE) ; } ;  
	
	return ;
}

int CVICALLBACK QuitCallback (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	switch (event)
	{
		case EVENT_COMMIT:
			QuitUserInterface(0);
			mupRelease(MathParser1);
			mupRelease(MathParser2); 
			mupRelease(MathParser3);
			mupRelease(MathParser4);
			mupRelease(MathParser5);
			break;
	}
	return 0;
}

int  CVICALLBACK CB_OnEventMain(int panel, int event, void *callbackData, int eventData1, int eventData2)
{
	int ActiveControl ;
	int StepIndex ;
	double Step ;
	
	switch (event) {
		case EVENT_KEYPRESS:
			switch (eventData1)	// ie the code of the key which has been stroke 
				{
				case VAL_RIGHT_ARROW_VKEY :
					ActiveControl = GetActiveCtrl(panel);
					if (ActiveControl==PANEL_DDS1 || ActiveControl==PANEL_DDS1STEP) {
						GetCtrlIndex(MainPanel, PANEL_DDS1STEP, &StepIndex);
						if (StepIndex<14){
							SetCtrlIndex(MainPanel, PANEL_DDS1STEP, ++StepIndex) ;
							GetCtrlVal(MainPanel, PANEL_DDS1STEP, &Step);
							SetCtrlAttribute(MainPanel, PANEL_DDS1, ATTR_INCR_VALUE, Step) ;
						};
					};
					if (ActiveControl==PANEL_DDS2 || ActiveControl==PANEL_DDS2STEP) {
						GetCtrlIndex(MainPanel, PANEL_DDS2STEP, &StepIndex);
						if (StepIndex<14){
							SetCtrlIndex(MainPanel, PANEL_DDS2STEP, ++StepIndex) ;
							GetCtrlVal(MainPanel, PANEL_DDS2STEP, &Step);
							SetCtrlAttribute(MainPanel, PANEL_DDS2, ATTR_INCR_VALUE, Step) ;
						};
					};
					if (ActiveControl==PANEL_DDS3|| ActiveControl==PANEL_DDS3STEP) {
						GetCtrlIndex(MainPanel, PANEL_DDS3STEP, &StepIndex);
						if (StepIndex<14){
							SetCtrlIndex(MainPanel, PANEL_DDS3STEP, ++StepIndex) ;
							GetCtrlVal(MainPanel, PANEL_DDS3STEP, &Step);
							SetCtrlAttribute(MainPanel, PANEL_DDS3, ATTR_INCR_VALUE, Step) ;
						};
					};
					if (ActiveControl==PANEL_DDS4|| ActiveControl==PANEL_DDS4STEP) {
						GetCtrlIndex(MainPanel, PANEL_DDS4STEP, &StepIndex);
						if (StepIndex<14){
							SetCtrlIndex(MainPanel, PANEL_DDS4STEP, ++StepIndex) ;
							GetCtrlVal(MainPanel, PANEL_DDS4STEP, &Step);
							SetCtrlAttribute(MainPanel, PANEL_DDS4, ATTR_INCR_VALUE, Step) ;
						};
					};
					break;
				case VAL_LEFT_ARROW_VKEY :
					ActiveControl = GetActiveCtrl(panel);
					if (ActiveControl==PANEL_DDS1 || ActiveControl==PANEL_DDS1STEP) {
						GetCtrlIndex(MainPanel, PANEL_DDS1STEP, &StepIndex);
						if (StepIndex>0){
							SetCtrlIndex(MainPanel, PANEL_DDS1STEP, --StepIndex) ;
							GetCtrlVal(MainPanel, PANEL_DDS1STEP, &Step);
							SetCtrlAttribute(MainPanel, PANEL_DDS1, ATTR_INCR_VALUE, Step) ;
						};
					};
					if (ActiveControl==PANEL_DDS2 || ActiveControl==PANEL_DDS2STEP) {
						GetCtrlIndex(MainPanel, PANEL_DDS2STEP, &StepIndex);
						if (StepIndex>0){
							SetCtrlIndex(MainPanel, PANEL_DDS2STEP, --StepIndex) ;
							GetCtrlVal(MainPanel, PANEL_DDS2STEP, &Step);
							SetCtrlAttribute(MainPanel, PANEL_DDS2, ATTR_INCR_VALUE, Step) ;
						};
					};
					if (ActiveControl==PANEL_DDS3 || ActiveControl==PANEL_DDS3STEP) {
						GetCtrlIndex(MainPanel, PANEL_DDS3STEP, &StepIndex);
						if (StepIndex>0){
							SetCtrlIndex(MainPanel, PANEL_DDS3STEP, --StepIndex) ;
							GetCtrlVal(MainPanel, PANEL_DDS3STEP, &Step);
							SetCtrlAttribute(MainPanel, PANEL_DDS3, ATTR_INCR_VALUE, Step) ;
						};
					};
					if (ActiveControl==PANEL_DDS4 || ActiveControl==PANEL_DDS4STEP) {
						GetCtrlIndex(MainPanel, PANEL_DDS4STEP, &StepIndex);
						if (StepIndex>0){
							SetCtrlIndex(MainPanel, PANEL_DDS4STEP, --StepIndex) ;
							GetCtrlVal(MainPanel, PANEL_DDS4STEP, &Step);
							SetCtrlAttribute(MainPanel, PANEL_DDS4, ATTR_INCR_VALUE, Step) ;
						};
					};
					break;
				case VAL_F2_VKEY :
					SetActiveCtrl(MainPanel, PANEL_DDS1);
					break;
				case VAL_F3_VKEY :
					SetActiveCtrl(MainPanel, PANEL_DDS2);
					break;
				case VAL_F4_VKEY :
					SetActiveCtrl(MainPanel, PANEL_DDS3);
					break;
				case VAL_F5_VKEY :
					SetActiveCtrl(MainPanel, PANEL_DDS4);
					break;
				};
			break;
			
	}
	return 0;
}


void CurrentFileName(char *fname)
{
	char day[3], month[3], year[3];
	char *date = DateStr();
	Scan(date, "%s>%s[w2]-%s[w2]-20%s[w2]", month, day, year);
	Fmt(fname, "%s<%s\\%s%s%s_Frequ.txt", LOGFILEPATH, year, month, day);
}


int CVICALLBACK CB_OnStart (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	switch (event)
	{
		case EVENT_COMMIT:
			if (Acquiring) {
				PlotCh1.IndexPoint = 0;
				PlotCh2.IndexPoint = 0;
				PlotCh3.IndexPoint = 0;
				PlotCh4.IndexPoint = 0;
				PlotMath1.IndexPoint = 0;
				PlotMath2.IndexPoint = 0; 
				PlotMath3.IndexPoint = 0; 
				PlotMath4.IndexPoint = 0; 
				PlotMath5.IndexPoint = 0;
				Allan_Reset(&AllanCh1);
				Allan_Reset(&AllanCh2);
				Allan_Reset(&AllanCh3);
				Allan_Reset(&AllanCh4);
				Allan_Reset(&AllanMath1);
				Allan_Reset(&AllanMath2);
				Allan_Reset(&AllanMath3);
				Allan_Reset(&AllanMath4);
				Allan_Reset(&AllanMath5);
			}
			Acquiring = TRUE;
			SetCtrlAttribute(MainPanel, PANEL_STARTBUTTON, ATTR_LABEL_TEXT, "__RESET"); 
			
			CurrentFileName(LogFileName);
			GetFileInfo(LogFileName, &OldLogFilePtr);
			OldLogFilePtr -= OldLogFilePtr%FXLINELENGTH + FXLINELENGTH - 2;
			break;
		}
	return 0;
}

int CVICALLBACK CB_OnStop (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	switch (event) {
		case EVENT_COMMIT:
			Acquiring = FALSE ;
			SetCtrlAttribute(MainPanel, PANEL_STARTBUTTON, ATTR_LABEL_TEXT, "__START");
			break;
	}
	return 0;
}

int CVICALLBACK CB_OnTimer (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	int LogFile, FileOpt, FileHg, FileExtraMath;
	long LogFileSize;
	char LineBuffer[FXLINELENGTH+10] = "\r\n_1 ";
	
	char TimeTag[] = "100103 000000.000";   // K+K time tag meaning here 2010 january the 3rd at 00:00:00.000
	char Date[] = "03/01/2010" ;
	char Time[] = "00:00:00.000" ;
	char Year[] = "2010";
	char ShortYear[] = "10";  // the last 2 digits of calendar year only
	char Month[] = "01";
	char Day[] = "03";
	char Hour[] = "00";
	char Min[] = "00" ;
	char Sec[] = "00.000";
	struct tm LocalTime ;
	time_t utcTime;
	char * ReportString = "dd.mm.yy\t00:00:00\t3481610838.000\t0000000000000.00000000";

	int BoxChecked = FALSE; 
	
	switch (event) {
		case EVENT_TIMER_TICK:
			if (! Acquiring)
				break;
			
			GetFileInfo(LogFileName, &LogFileSize);
					
			if (LogFileSize > OldLogFilePtr+2*FXLINELENGTH-2) {  // if a complete newline has been written
						
				SuspendTimerCallbacks();
				
				// Open Log file and get to the beginning of newly completed line
				LogFile = OpenFile(LogFileName, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
				OldLogFilePtr += FXLINELENGTH;
				SetFilePtr(LogFile, OldLogFilePtr, 0);
				
				// return the last complete string from the log file and scan it for date and time information 
				
				// first, the time tag, and store it in various formats
				ReadFile(LogFile, TimeTag, 17);
						
				CopyBytes(Date,0,TimeTag,4,2);
				CopyBytes(Date,3,TimeTag,2,2);   
				CopyBytes(Date,8,TimeTag,0,2);
				CopyBytes(Time,0,TimeTag,7,2);
				CopyBytes(Time,3,TimeTag,9,2);
				CopyBytes(Time,6,TimeTag,11,6);
				SetCtrlVal(MainPanel, PANEL_DATE, Date);
				SetCtrlVal(MainPanel, PANEL_TIME, Time); 
				
				CopyBytes(Year,2,TimeTag,0,2); // first 2 bytes of year string remains "20"
				CopyBytes(ShortYear,0,TimeTag,0,2);
				CopyBytes(Month,0,TimeTag,2,2);
				CopyBytes(Day,0,TimeTag,4,2);
				CopyBytes(Hour,0,TimeTag,7,2);
				CopyBytes(Min,0,TimeTag,9,2);
				CopyBytes(Sec,0,TimeTag,11,6);
				Fmt(&LocalTime.tm_year, "%d<%s", Year);
				Fmt(&LocalTime.tm_mon,  "%d<%s", Month);
				Fmt(&LocalTime.tm_mday, "%d<%s", Day);
				Fmt(&LocalTime.tm_hour, "%d<%s", Hour);
				Fmt(&LocalTime.tm_min,  "%d<%s", Min);
				Fmt(&LocalTime.tm_sec,  "%d<%s", "00");    // special case to handle non integer number of UTC seconds
				LocalTime.tm_hour += 0;
				LocalTime.tm_min  -= 0;  
				LocalTime.tm_sec  -= 0;  
				LocalTime.tm_mon  -= 1;  	// january is month 0 for tm struct
				LocalTime.tm_year -= 1900; // year is number of years since 1900 for tm struct
				LocalTime.tm_isdst = -1;  // daylight saving flag MUST be set to -1 (unallocated is bugging and +1 is making 1 hour error in summer)
				utcTime = mktime (&LocalTime);
				utc = (double) utcTime + strtod(Sec,NULL);
				mjd=utc/86400.;		//nb de jours depuis l'origine d'UTC (01/01/1900 à 00h00 GMT)
				mjd+=15020;			//date MJD de la date origine d'UTC
				SetCtrlVal(MainPanel, PANEL_UTC, utc);
				SetCtrlVal(MainPanel, PANEL_MJD, mjd);
				
				// scan the line for counters's channels information
				
				ReadLine(LogFile, LineBuffer, FXLINELENGTH+9);
				CloseFile(LogFile);
				
				Scan(LineBuffer, "%f%f%f%f", &Ch1, &Ch2, &Ch3, &Ch4);
				Ch1 = 1000*Ch1;
				Ch2 = 1000*Ch2;
				Ch3 = 1000*Ch3;
				Ch4 = 1000*Ch4;
						
				SetCtrlVal(MainPanel, PANEL_FREQ1, Ch1);
				SetCtrlVal(MainPanel, PANEL_FREQ2, Ch2);
				SetCtrlVal(MainPanel, PANEL_FREQ3, Ch3);  
				SetCtrlVal(MainPanel, PANEL_FREQ4, Ch4);

				SetCtrlVal(MainPanel, PANEL_CENTERFREQUENCY, CenterFrequencyCh2);
				
				// Treat data
				
				Math1 = mupEval(MathParser1);
				SetCtrlVal(MainPanel,PANEL_MATH1, Math1);
				
				Math2 = mupEval(MathParser2);
				SetCtrlVal(MainPanel,PANEL_MATH2, Math2);
				
				Math3 = mupEval(MathParser3);
				SetCtrlVal(MainPanel,PANEL_MATH3, Math3);
				
				Math4 = mupEval(MathParser4);
				SetCtrlVal(MainPanel,PANEL_MATH4, Math4);
				
				Math5 = mupEval(MathParser5);
				SetCtrlVal(MainPanel,PANEL_MATH5, Math5);
				
				// Plot Data and calculus if required
				
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ1PLOT, &BoxChecked);
				if (BoxChecked) {
					Plot_AddFrequency(&PlotCh1, Ch1);
				}
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ1ALLAN, &BoxChecked);
				if (BoxChecked) {
					Allan_AddFrequency(&AllanCh1, Ch1);
				}

				GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ2PLOT, &BoxChecked);
				if (BoxChecked) {
					Plot_AddFrequency(&PlotCh2, Ch2);
				}
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ2ALLAN, &BoxChecked);
				if (BoxChecked) {
					Allan_AddFrequency(&AllanCh2, Ch2);
				}
				
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ3PLOT, &BoxChecked);
				if (BoxChecked) {
					Plot_AddFrequency(&PlotCh3, Ch3);
				}
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ3ALLAN, &BoxChecked);
				if (BoxChecked) {
					Allan_AddFrequency(&AllanCh3, Ch3);
				}
				
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ4PLOT, &BoxChecked);
				if (BoxChecked) {
					Plot_AddFrequency(&PlotCh4, Ch4);
				}
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ4ALLAN, &BoxChecked);
				if (BoxChecked) {
					Allan_AddFrequency(&AllanCh4, Ch4);
				}
				
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH1PLOT, &BoxChecked);
				if (BoxChecked) {
					Plot_AddFrequency(&PlotMath1, Math1);
				}
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH1ALLAN, &BoxChecked);
				if (BoxChecked) {
					Allan_AddFrequency(&AllanMath1, Math1);
				}
				
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2PLOT, &BoxChecked);
				if (BoxChecked) {
					Plot_AddFrequency(&PlotMath2, Math2);
				}
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2ALLAN, &BoxChecked);
				if (BoxChecked) {
					Allan_AddFrequency(&AllanMath2, Math2);
				}
				
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3PLOT, &BoxChecked);
				if (BoxChecked) {
					Plot_AddFrequency(&PlotMath3, Math3);
				}
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3ALLAN, &BoxChecked);
				if (BoxChecked) {
					Allan_AddFrequency(&AllanMath3, Math3);
				}
				
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH4PLOT, &BoxChecked);
				if (BoxChecked) {
					Plot_AddFrequency(&PlotMath4, Math4);
				}
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH4ALLAN, &BoxChecked);
				if (BoxChecked) {
					Allan_AddFrequency(&AllanMath4, Math4);
				}
				
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH5PLOT, &BoxChecked);
				if (BoxChecked) {
					Plot_AddFrequency(&PlotMath5, Math5);
				}
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH5ALLAN, &BoxChecked);
				if (BoxChecked) {
					Allan_AddFrequency(&AllanMath5, Math5);
				}
				
				// Calcul de N
				
				switch (Measuring_1) {
					
					case N_MEASUREMENT_NONE:
						// not measuring
						break;
					
					case N_MEASUREMENT_INIT:
						// initialization step
						
						// set DDS1 to nominal frequency
						SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1);
						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
						
						GetCtrlVal(MainPanel, PANEL_DDS2, &FrequencyDDSBesInit);
						t2_1 = t3_1 = 0.0;
						t1_1 = utc;
						Frequ_slope_1 = Math1;
						Moy_slope_1 = Frequ_slope_1;
						Ch4_slope = Ch4;
						Moy_Ch4slope_1 = Ch4_slope;
						N_slope_1 = 1;
						
						// next step
						Measuring_1 += 1;
						break;
						
					case N_MEASUREMENT_SLOPE:
						// slope measurement
						
						N_slope_1 = N_slope_1 + 1; 
						Frequ_slope_1 = Math1;
						Ch4_slope = Ch4; 
						Moy_slope_1 = ((N_slope_1-1)*Moy_slope_1 + Frequ_slope_1)/N_slope_1;
						Moy_Ch4slope_1 = ((N_slope_1-1)*Moy_Ch4slope_1 + Ch4_slope)/N_slope_1;
						Slope_slope_1 = (Slope_slope_1*(N_slope_1-2) + 6*(Frequ_slope_1-Moy_slope_1)/N_slope_1)/(N_slope_1+1);
						Slope_Ch4slope_1 = (Slope_Ch4slope_1*(N_slope_1-2) + 6*(Ch4_slope-Moy_Ch4slope_1)/N_slope_1)/(N_slope_1+1);
						
						if ((utc - t1_1) > SlopeTime1) {
							Slope_1 = Slope_slope_1;
							Ch4Slope =  Slope_Ch4slope_1;
							SetCtrlVal(CalcN1Panel, CALCN1_SLOPE, Slope_1);
							
							N_slope_1 = 0;
							Frequ_slope_1 = 0.0;
							Moy_slope_1 = 0.0;
							Slope_slope_1 = 0.0;
							Ch4_slope = 0.0;
							Moy_Ch4slope_1 = 0.0;
							Slope_Ch4slope_1 = 0.0;
							
							// frep positive step
							DDS4xAD9912_FrequencyRampe(&DDS4xAD9912,1, FrequDDS1,(FrequDDS1+DeltakHz_1*1000), Step1/Ndiv);
							SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1+DeltakHz_1*1000));
							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, (FrequDDS1+DeltakHz_1*1000));
							
							// allow counter to settle
							settling = 3;
							
							// next step
							Measuring_1 += 1;
						}
						break;
						
					case N_MEASUREMENT_ADJUST_FREQ_PLUS:
					case N_MEASUREMENT_ADJUST_FREQ_MINUS:
						// adjust DDS frequency to keep beatnote within the bandpass filter
						
						if (settling > 0) {
							settling--;
							break;
						}
						
						double fDDS2;
						GetCtrlVal(MainPanel, PANEL_DDS2, &fDDS2);
						fDDS2 += 275000 - Ch4;
						SetCtrlVal(MainPanel, PANEL_DDS2, fDDS2);
						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, fDDS2);
						
						// allow counter to settle
						settling = 3;
						
						// next step
						Measuring_1 += 1;
						break;								
						
					case N_MEASUREMENT_FREP_PLUS:
						// frep positive step
						
						if (settling > 0) {
							settling--;
							break;
						}
						
						if (t2_1 == 0.0)
							t2_1 = utc;
						
						Frepplus_1 = Frepplus_1 + Math1 - Slope_1 * (utc - t2_1);
						Ch4Plus = Ch4Plus + Ch4 - Ch4Slope * (utc - t2_1);
						n_1 += 1;
						
						if ((utc - t2_1) > DeltaT_1) {
							Frepplus_1 = Frepplus_1 / n_1;
							Ch4Plus = Ch4Plus / n_1; 
							n_1 = 0;
							
							// frep negative step
							DDS4xAD9912_FrequencyRampe(&DDS4xAD9912,1, (FrequDDS1+DeltakHz_1*1000),(FrequDDS1-DeltakHz_1*1000), Step1/Ndiv);
							SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1-DeltakHz_1*1000));  
							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, (FrequDDS1-DeltakHz_1*1000));
							
							// allow counter to settle
							settling = 3;
							
							// next step
							Measuring_1 += 1;
						}
						break;
						
					case N_MEASUREMENT_FREP_MINUS:
						// frep negative step
						
						if (settling > 0) {
							settling--;
							break;
						}
						
						if (t3_1 == 0.0)
							t3_1 = utc;
						
						Frepminus_1 = Frepminus_1 + Math1 - Slope_1 * (utc - t3_1);
						Ch4Minus = Ch4Minus + Ch4 - Ch4Slope * (utc - t3_1);
						n_1 += 1;
						
						if ((utc - t3_1) > DeltaT_1) {
							Frepminus_1 = Frepminus_1 / n_1;
							Ch4Minus = Ch4Minus / n_1;
							n_1 = 0;
							
							// compute N1
							N_1 = Signe1 * (2*Ndiv * DeltakHz_1 * 1000)/(Frepminus_1 - Frepplus_1 - Slope_1 * (t3_1 - t2_1));
							SetCtrlVal(CalcN1Panel, CALCN1_N, N_1);
						
							t1_1 = 0.0;
							t2_1 = 0.0;
							t3_1 = 0.0;
							Frepminus_1 = 0.0;
							Frepplus_1 = 0.0;
							
							// back to nominal frep
							DDS4xAD9912_FrequencyRampe(&DDS4xAD9912, 1, FrequDDS1-DeltakHz_1*1000,FrequDDS1, Step1/Ndiv );
							SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1);
							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
							SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit);
							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, FrequencyDDSBesInit);
							
							// done
							Measuring_1 = N_MEASUREMENT_NONE;
						}
						break;
				}

				switch (Measuring_2) {

					case N_MEASUREMENT_NONE:
						// not measuring
						break;
					
					case N_MEASUREMENT_INIT:
						// initialization step
						
						// set DDS1 to nominal frequency
						SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1);
						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
						
						GetCtrlVal(MainPanel, PANEL_DDS2, &FrequencyDDSBesInit);
						GetCtrlVal(MainPanel, PANEL_DDS3, &FrequencyDDS3Init);
						t1_2 = utc;
						Frequ_slope_2 = Math1;
						Beat_slope_2 = Ch2;
						Moy_slope_2 = Frequ_slope_2;
						Moy_Beatslope_2 = Beat_slope_2;
						N_slope_2 = 1;
						Nu1 = N1 * (250000000 + Math1);
						
						// next step
						Measuring_2 += 1;
						break;
						
					case N_MEASUREMENT_SLOPE:
						// slope measurement
						
						N_slope_2 = N_slope_2 + 1; 
						Frequ_slope_2 = Math1;
						Beat_slope_2 = Ch2;  
						Moy_slope_2 = ((N_slope_2-1)*Moy_slope_2 + Frequ_slope_2)/N_slope_2;
						Moy_Beatslope_2 = ((N_slope_2-1)*Moy_Beatslope_2 + Beat_slope_2)/N_slope_2;
						Slope_slope_2 = (Slope_slope_2*(N_slope_2-2) + 6*(Frequ_slope_2-Moy_slope_2)/N_slope_2)/(N_slope_2+1);
						Slope_Beatslope_2 = (Slope_Beatslope_2*(N_slope_2-2) + 6*(Beat_slope_2-Moy_Beatslope_2)/N_slope_2)/(N_slope_2+1);

						if ((utc - t1_2) > SlopeTime2) {
							Slope_2 = Slope_slope_2;
							Beatslope_2 = Slope_Beatslope_2;
							SetCtrlVal(CalcN2Panel, CALCN2_SLOPE, Beatslope_2);

							N_slope_2 = 0;
							Frequ_slope_2 = 0.0;
							Moy_slope_2 = 0.0;
							Slope_slope_2 = 0.0;
							Moy_Beatslope_2 = 0.0;
							Slope_Beatslope_2 = 0.0;
							Beat_slope_2 = 0.0;
							
							// frep positive step
							double fDDS1 = FrequDDS1 + DeltakHz_2 * 1000;
							printf("fDDS1 = %g\n", fDDS1);
							DDS4xAD9912_FrequencyRampe(&DDS4xAD9912,1, FrequDDS1, fDDS1, Step2/Ndiv);
							SetCtrlVal(MainPanel, PANEL_DDS1, fDDS1);
							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, fDDS1);
							// adjust DDS3 to keep beatnote within the bandpass filter. prediction
							DeltaDDS3 = -DeltakHz_2*1000*(-Signe1/Signe2)*Ndiv*(Nu2)/(Nu1) - Beatslope_2*(utc-t1_2);
							printf("deltaDDS3 = %g\n", DeltaDDS3);
							SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3Init + DeltaDDS3);
							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequencyDDS3Init + DeltaDDS3);
						
							// allow counter to settle
							settling = 3;
							
							// next step
							Measuring_2 += 1;
						}
						break;

					case N_MEASUREMENT_ADJUST_FREQ_PLUS:
					case N_MEASUREMENT_ADJUST_FREQ_MINUS:
						// adjust DDS frequency to keep beatnote within the bandpass filter
						
						if (settling > 0) {
							settling--;
							break;
						}
						
						double fDDS2 = DDS4xAD9912.Frequency2 + 275000 - Ch4;
						SetCtrlVal(MainPanel, PANEL_DDS2, fDDS2);
						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, fDDS2);
						
						double fDDS3 = DDS4xAD9912.Frequency3 + 10000 - Ch2;
						DeltaDDS3 = DeltaDDS3 + 10000 - Ch2;
						SetCtrlVal(MainPanel, PANEL_DDS3, fDDS3);
						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, fDDS3);

						// allow counter to settle
						settling = 3;

						// next step
						Measuring_2 += 1;
						break;
						
					case N_MEASUREMENT_FREP_PLUS:
						// frep positive step
						
						if (settling > 0) {
							settling--;
							break;
						}

						if (t2_1 == 0.0)
							t2_1 = utc;

						Frepplus_2 = Frepplus_2 + Math1 + 250000000 - Slope_2 * (utc - t2_2);
						Delta10K_Plus = Delta10K_Plus + 10000 - (Ch2 - Beatslope_2 * (utc - t2_2));
						n_2 += 1;
						
						if ((utc - t2_2) > DeltaT_2) {
							Frepplus_2 = Frepplus_2 / n_2;
							Delta10K_Plus = Delta10K_Plus / n_2;
							n_2 = 0;

							// negative frequency step
							DDS4xAD9912_FrequencyRampe(&DDS4xAD9912,1, (FrequDDS1+DeltakHz_2*1000),(FrequDDS1-DeltakHz_2*1000), Step2/Ndiv);
							SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1-DeltakHz_2*1000));
							DDS4xAD9912_SetFrequency(&DDS4xAD9912,1, (FrequDDS1-DeltakHz_2*1000));
							double fDDS3 = FrequencyDDS3Init+DeltakHz_2*1000*(-Signe1/Signe2)*Ndiv*(Nu2)/(Nu1);
							DeltaDDS3 = fDDS3 - DDS4xAD9912.Frequency3;
							SetCtrlVal(MainPanel, PANEL_DDS3, fDDS3);
							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, fDDS3);

							// allow counter to settle
							settling = 3;
							
							// next step
							Measuring_2 += 1;
						}
						break;
						
					case N_MEASUREMENT_FREP_MINUS:
						// frep negative step
						
						if (settling > 0) {
							settling--;
							break;
						}
						
						if (t3_1 == 0.0)
							t3_1 = utc;

						Frepminus_2 = Frepminus_2 + Math1 + 250000000 - Slope_2 * (utc - t3_2);
						Delta10K_Minus=  Delta10K_Minus + 10000 - (Ch2 - Beatslope_2 * (utc - t3_2));
						n_2 += 1;

						if ((utc -t3_2) > DeltaT_2) {
							Frepminus_2 = Frepminus_2 / n_2;
							Delta10K_Minus = Delta10K_Minus / n_2;
							n_2 = 0;

							// compute N2
							N_2 = (Signe2)*(-DeltaDDS3+Delta10K_Plus-Delta10K_Minus-Beatslope_2*(t3_2-t2_2) )/(Frepminus_2-Frepplus_2-Slope_2*(t3_2-t2_2));
							SetCtrlVal(CalcN2Panel, CALCN2_N, N_2);
							
							// back to nominal frequency
							DDS4xAD9912_FrequencyRampe (&DDS4xAD9912, 1, FrequDDS1-DeltakHz_2*1000,FrequDDS1, Step2/Ndiv );
							SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1);
							DDS4xAD9912_SetFrequency(&DDS4xAD9912,1,FrequDDS1);
							Delay(0.1); 
							SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit);  
							DDS4xAD9912_SetFrequency (&DDS4xAD9912, 2, FrequencyDDSBesInit );
							Delay(0.1); 
							SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3Init-Beatslope_2*(utc-t1_2));
							DDS4xAD9912_SetFrequency (&DDS4xAD9912, 3, FrequencyDDS3Init-Beatslope_2*(utc-t1_2) );
							
							t1_2 = 0.0;
							t2_2 = 0.0;
							t3_2 = 0.0;
							Frepminus_2 = 0.0;
							Frepplus_2 = 0.0;
							Delta10K_Minus = 0.0;
							Delta10K_Plus = 0.0;

							// done
							Measuring_2 = N_MEASUREMENT_NONE;
						}
						break;
				}
				
				switch (Measuring_3) {
					
					case N_MEASUREMENT_NONE:
						// not measuring N3
						break;
						
					case N_MEASUREMENT_INIT:
						// init
						
						SetCtrlVal(MainPanel, PANEL_DDS4, FrequDDS4);
						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, FrequDDS4);
						settling = 3;
						
						t1_3 = utc;
						N_slope_3 = 0;
						// record current DDS3 frequency
						GetCtrlVal(MainPanel, PANEL_DDS3, &FrequencyDDS3Init);
						
						// next step
						Measuring_3 += 1;
						break;
						
					case N_MEASUREMENT_SLOPE:
						// slope measurement
						
						if (settling > 0) {
							settling--;
							break;
						}
						
						N_slope_3++;
						Frequ_slope_3 = Ch2;
						Moy_slope_3 = ((N_slope_3-1)*Moy_slope_3 + Frequ_slope_3)/N_slope_3;
						Slope_slope_3 = (Slope_slope_3*(N_slope_3-2) + 6*(Frequ_slope_3-Moy_slope_3)/N_slope_3)/(N_slope_3+1);
						
						if (utc - t1_3 > SlopeTime3) {
							// slope measurement
							Slope_3 = Slope_slope_3;
							
							t2_3 = utc;
							N_slope_3 = 0;
							Frequ_slope_3 = 0.0;
							Moy_slope_3 = 0.0;
							Slope_slope_3 = 0.0;
							
							// frep positive step
							SetCtrlVal(MainPanel, PANEL_DDS4, FrequDDS4 + DeltakHz_3 * 1000);
							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, FrequDDS4 + DeltakHz_3 * 1000);
							// compensate with DDS3 to keep measured beatnote in counter box range
							double fDDS3 = FrequencyDDS3Init + N3/N1 * Ndiv * DeltakHz_3 * 1000;
							SetCtrlVal(MainPanel, PANEL_DDS3, fDDS3);
							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, fDDS3);
							
							// allow counter to settle
							settling = 3;
							
							// next step
							Measuring_3 += 1;
						}
						break;

					case N_MEASUREMENT_ADJUST_FREQ_PLUS:
					case N_MEASUREMENT_ADJUST_FREQ_MINUS:
						// adjust DDS frequency to keep beatnote within the bandpass filter

						// next step
						Measuring_3 += 1;
						break;						
						
					case N_MEASUREMENT_FREP_PLUS:
						// frep positive step
						
						if (settling > 0) {
							settling--;
							break;
						}
						
						n_3++;
						Frepplus_3 += Ch2 - Slope_3 * (utc - t2_3);
						
						if (utc - t2_3 > DeltaT_3) {
							// positive step measurement
							Frepplus_3 = Frepplus_3 / n_3;
							
							n_3 = 0;
							t3_3 = utc;
							
							// frep negative step
							SetCtrlVal(MainPanel, PANEL_DDS4, FrequDDS4 - DeltakHz_3 * 1000);
							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, FrequDDS4 - DeltakHz_3 * 1000);
							// compensate with DDS3 to keep measured beatnote in counter box range
							double fDDS3 = FrequencyDDS3Init - N3/N1 * Ndiv * DeltakHz_3 * 1000;
							SetCtrlVal(MainPanel, PANEL_DDS3, fDDS3);
							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, fDDS3);
							
							// allow counter to settle
							settling = 3;
							
							// next step
							Measuring_3 += 1;
						}
						break;
						
						
					case N_MEASUREMENT_FREP_MINUS:
						// frep negative step
						
						if (settling > 0) {
							settling--;
							break;
						}
						
						n_3++;
						Frepminus_3 += Ch2 - Slope_3 * (utc - t3_3);
						
						if (utc - t3_3 > DeltaT_3) {
							// positive step measurement
							Frepminus_3 = Frepminus_3 / n_3;
							
							// compute N3
							N_3 = 1000.0 * DeltakHz_3 / (Frepplus_3 - Frepminus_3 + (2 * N3/N1 * Ndiv * DeltakHz_3 * 1000));
							SetCtrlVal(CalcN3Panel, CALCN3_N, N_3);
							
							t1_3=0.0;
							t2_3=0.0;
							t3_3=0.0;
							n_3 = 0;
							Frepminus_3 = 0.0;
							Frepplus_3 = 0.0;
							
							// back to nominal frep
							SetCtrlVal(MainPanel, PANEL_DDS4, FrequDDS4);
							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, FrequDDS4);
							// back to initial DDS3 frequency
							SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3Init);
							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequencyDDS3Init);
							
							// done
							Measuring_3 = N_MEASUREMENT_NONE;
						}
						break;
				}
				
				// Calcul du signe de fb
				
				if (Getsign1 == TRUE) {
					if (utc > tbegin1+2) {
						if (Math1 > Frepbefore1)
							Signe1 = -1.0;
						else
							Signe1 = +1.0;
						SetCtrlVal(MainPanel, PANEL_DDS1, Frequency1); 
						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, Frequency1);
						Getsign1 = FALSE;
						SetCtrlVal(MainPanel, PANEL_SIGN1, Signe1);
					}
				}
				if (Getsign2 == TRUE) {
					if (utc > tbegin2+2){
						if (Math1 > Frepbefore2) { 
							if (Ch2 > Ch2before)
								Signe2 = +1.0;
							else
								Signe2 = -1.0;
						} else {
							if (Ch2 > Ch2before)
								Signe2 = -1.0;
							else 
								Signe2 = +1.0;
						}
						SetCtrlVal(MainPanel, PANEL_DDS1, Frequency2); 
						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, Frequency2);
						Getsign2 = FALSE;
						SetCtrlVal(MainPanel, PANEL_SIGN2, Signe2);
					}
				}
				if (Getsign3 == TRUE) {
					if (utc > tbegin3+2){
						if (Ch3 > Frepbefore3)
							Signe3 = -1.0;
						else
							Signe3 = +1.0;
						SetCtrlVal(MainPanel, PANEL_DDS3, Frequency3); 
						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, Frequency3);
						Getsign3 = FALSE;
						SetCtrlVal(MainPanel, PANEL_SIGN3, Signe3);
					}
				}
				
				// slope measurement 
				if (SlopeMeasuring == TRUE)
				{
					double currentFreq = 0.0;
					
					// select reference
					switch (slopeReference) {
					case SLOPE_REFERENCE_MICROWAVE:
						currentFreq = Math2;
						break;
					case SLOPE_REFERENCE_HG_CAVITY:
						currentFreq = Ch2 * 1062.5 / 1542.2;
						break;
					}
					
					// stop slope cancelling if the comb is not locked
					if (StopSlopeCancellingOnUnlocked & (fabs(currentFreq - Math2_slope) < limitotakoff)) {
						
						double frequency = DEDRIFT_DDS_FREQUENCY;
						if (KeepFrequ)
							frequency = DDSFox_ReadFreq(&DDS1xAD9956);
						
						if (! KeepSlope)
							SlopeMath2 = 0.0;
						
						SetCtrlVal(MainPanel, PANEL_SLOPE_APPLIED, SlopeMath2);
						DDSFox_Set(&DDS1xAD9956, frequency, SlopeMath2);
						
						Slope_Math2slope = 0.0;
						SlopeMeasuring = FALSE;
						SetCtrlVal(MainPanel, PANEL_MEASURE_SLOPE, 0);
					}
					
					// update slope measurement
					N_Math2slope = N_Math2slope + 1;
					Math2_slope = currentFreq;
					Moy_Math2slope = ((N_Math2slope-1)*Moy_Math2slope + Math2_slope)/N_Math2slope;
					Slope_Math2slope = (Slope_Math2slope*(N_Math2slope-2) + 6*(Math2_slope-Moy_Math2slope)/N_Math2slope)/(N_Math2slope+1);
					// update indicator
					SetCtrlVal(MainPanel, PANEL_SLOPE_MEASURED, 0);
					
					// update applied slope
					if ((utc - SlopeMeasuringTimeBegin) > TimetoSlope) {
						
						if (invertSlopeSign)
							SlopeMath2 = SlopeMath2 - Slope_Math2slope;
						else
							SlopeMath2 = SlopeMath2 + Slope_Math2slope;
						
						if (FrequCorrec) {
							// proportional correction
							
							Nratio += 1;
							if (Nratio >= 1) {
								MoyMath2 = MoyMath2 + Moy_Math2slope;
							}
							if (Nratio == 1 && CenterFrequencyCh2ToDetermine == TRUE) {
								CenterFrequencyCh2 = MoyMath2;
								CenterFrequencyCh2ToDetermine = FALSE;
							}
							if (Nratio == ratio) {
								double correction = (MoyMath2/ratio-CenterFrequencyCh2)/TimetoSlope;
								SlopeMath2 = SlopeMath2 + correction;
								Nratio = 0;
								MoyMath2 = 0.0;
							}
						}
						
						double frequency = DDSFox_ReadFreq(&DDS1xAD9956);
						SetCtrlVal(MainPanel, PANEL_SLOPE_APPLIED, SlopeMath2);
						DDSFox_Set(&DDS1xAD9956, frequency, SlopeMath2);
						
						N_Math2slope = 0.0;
						Math2_slope = 0.0;
						Moy_Math2slope = 0.0;
						Slope_Math2slope = 0.0;
						SlopeMeasuringTimeBegin = utc;
					}
				}
				
				// re-centering
				if (KeepCentering) {
					if ((utc - CenteringTimeBegin275K) > Timetorecenter275K && CenteringTimeBegin275K > 10) {
						double frequency;
						GetCtrlVal(MainPanel, PANEL_DDS2, &frequency);
						// adjust DDS2 frequency to keep Ch4 reading around 275000
						frequency = frequency + 275000 - Ch4;
						SetCtrlVal(MainPanel, PANEL_DDS2, frequency);
						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, frequency);
						CenteringTimeBegin275K = utc;
					}
					
					if ((utc - CenteringTimeBegin10K) > Timetorecenter10K && CenteringTimeBegin10K > 10) {
						double frequency;
						GetCtrlVal(MainPanel, PANEL_DDS3, &frequency);
						// adjust DDS3 frequency to keep Ch2 reading around 10000
						frequency = frequency + 10000 - Ch2;
						SetCtrlVal(MainPanel, PANEL_DDS3, frequency);
						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, frequency);
						CenteringTimeBegin10K = utc;
					}
				}
				
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2AUTOSAV, &BoxChecked);		   // AutoSave OL
				if (BoxChecked) {
					SetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2SAVE, TRUE);   // so that it will try to write it (at next block) if it seems reasonnable, even though it was off before
				}
				
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2SAVE, &BoxChecked);			   // Save OL (Math2)
				if (BoxChecked) {
					FileOpt = OpenFile("z:\\MeasuresFifi1\\OptCavity.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
					Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p3]", Date, Time, utc, Math2);
					WriteLine(FileOpt, ReportString, -1);
					CloseFile(FileOpt);
					FileOpt = OpenFile("C:\\Femto\\Results\\OptCavity.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);   // a local backup for debugging
					Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p3]", Date, Time, utc, Math2);
					WriteLine(FileOpt, ReportString, -1);
					CloseFile(FileOpt);
				}
				
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3AUTOSAV, &BoxChecked);		   // AutoSave Hg (Math3)
				if (BoxChecked) {
					SetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3SAVE, TRUE);   // so that it will try to write it (at next block) if it seems reasonnable, even though it was off before
				}
				
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3SAVE, &BoxChecked);			   // Save Hg
				
				if (BoxChecked) {
					FileHg = OpenFile("z:\\MeasuresFifi1\\HgCavity.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
					Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p3]", Date, Time, utc, Math3);
					WriteLine(FileHg, ReportString, -1);
					CloseFile(FileHg);
					FileHg = OpenFile("C:\\Femto\\Results\\HgCavity.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
					Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p3]", Date, Time, utc, Math3);
					WriteLine(FileHg, ReportString, -1);
					CloseFile(FileHg);
				}
				
				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH5SAVE, &BoxChecked);				// Save ExtraMath (Math5)
				if (BoxChecked) {
					FileExtraMath = OpenFile(ExtraMathFileName, VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
					Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p7]", Date, Time, utc, Math5);
					WriteLine(FileExtraMath, ReportString, -1);
					CloseFile(FileExtraMath);
				}
				
				// Special case to handle change of day at next second
				if ( LocalTime.tm_hour==23 && LocalTime.tm_min==59 && strtod(Sec,NULL)>=58 ) { 
					Acquiring = FALSE;
					do {
						Delay(5.1);
						CurrentFileName(LogFileName);
					} while (!GetFileInfo(LogFileName, &OldLogFilePtr));
					Acquiring = TRUE;
					OldLogFilePtr = 2;
				}
				
				ResumeTimerCallbacks(); 
				
			}		
			break;
	}
	return 0;
}


int CVICALLBACK CB_OnFreqPlot (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	int BoxChecked ;
	Plot_Data * pPlot = NULL;
	char PlotTitle[] = "Ch? Frequency Plot" ;
	double PlotMin = 10e6 ;
	double PlotMax = 65e6 ;
	
	switch (event) {
		case EVENT_COMMIT:
		
			GetCtrlVal(MainPanel, control, &BoxChecked);
			switch (control) {
				case PANEL_CHECKBOX_FREQ1PLOT :
					pPlot = &PlotCh1 ;
					Fmt(PlotTitle, "Ch1 Frequency Plot") ;
					PlotMin = 54.999e6 ; PlotMax = 55.001e6 ;
					break ;
				case PANEL_CHECKBOX_FREQ2PLOT :
					pPlot = &PlotCh2 ;
					Fmt(PlotTitle, "Ch2 Frequency Plot") ;
					PlotMin = 8.0e3 ; PlotMax = 12.0e3 ;
					break ;
				case PANEL_CHECKBOX_FREQ3PLOT :
					pPlot = &PlotCh3 ;
					Fmt(PlotTitle, "Ch3 Frequency Plot") ;
					PlotMin = 8.0e3 ; PlotMax = 12.0e3 ;
					break ;
				case PANEL_CHECKBOX_FREQ4PLOT :
					pPlot = &PlotCh4 ;
					Fmt(PlotTitle, "Ch4 Frequency Plot") ;
					break ;
				case PANEL_CHECKBOX_MATH1PLOT :
					pPlot = &PlotMath1 ;
					Fmt(PlotTitle, "Math1 Plot") ;
					PlotMin = 765.0e6 ; PlotMax = 775.0e6 ;
					break ;
				case PANEL_CHECKBOX_MATH2PLOT :
					pPlot = &PlotMath2 ;
					Fmt(PlotTitle, "Math2 Plot") ;
					PlotMin = -1.0e9 ; PlotMax = 1.0e9 ;
					break ;	
				case PANEL_CHECKBOX_MATH3PLOT :
					pPlot = &PlotMath3 ;
					Fmt(PlotTitle, "Math3 Plot") ;
					PlotMin = -1.0e9 ; PlotMax = 1.0e9 ;
					break ;	
				case PANEL_CHECKBOX_MATH4PLOT :
					pPlot = &PlotMath4 ;
					Fmt(PlotTitle, "Math4 Plot") ;
					PlotMin = -1.0e9 ; PlotMax = 1.0e9 ;
					break ;	
				case PANEL_CHECKBOX_MATH5PLOT :
					pPlot = &PlotMath5 ;
					Fmt(PlotTitle, "Math5 Plot") ;
					PlotMin = -1.0e9 ; PlotMax = 1.0e9 ;
					break ;
			}
			
			if (BoxChecked)
				Plot_InitPanel(pPlot, PlotTitle, PlotMin, PlotMax, &OnCloseViewPanel);
			else
				Plot_ClosePanel(pPlot);
			break;
	}
	return 0;
}


int CVICALLBACK CB_OnAllanPlot (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	int BoxChecked ;
	Allan_Data * pAllan = NULL;
	char AllanTitle[] = "Ch? Allan Deviation  " ;
	double Normalizer = 300e12 ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			
			GetCtrlVal(MainPanel, control, &BoxChecked);
			switch (control) {
				case PANEL_CHECKBOX_FREQ1ALLAN :
					pAllan = &AllanCh1 ;
					Fmt(AllanTitle, "Ch1 Allan Deviation") ;
					Normalizer = 1.84e12 ;
					break ;
				case PANEL_CHECKBOX_FREQ2ALLAN :
					pAllan = &AllanCh2 ;
					Fmt(AllanTitle, "Ch2 Allan Deviation") ;
					Normalizer = 10.e3 ; 
					break ;
				case PANEL_CHECKBOX_FREQ3ALLAN :
					pAllan = &AllanCh3 ;
					Fmt(AllanTitle, "Ch3 Allan Deviation") ;
					Normalizer = 429.228e12 ; 
					break ;
				case PANEL_CHECKBOX_FREQ4ALLAN :
					pAllan = &AllanCh4 ;
					Fmt(AllanTitle, "Ch4 Allan Deviation") ;
					Normalizer = 275.0e3 ; 
					break ;
				case PANEL_CHECKBOX_MATH1ALLAN :
					pAllan = &AllanMath1 ;
					Fmt(AllanTitle, "Math1 Allan Deviation") ;
					Normalizer = 250.0e6 ;
					break ;
				case PANEL_CHECKBOX_MATH2ALLAN :
					pAllan = &AllanMath2 ;
					Fmt(AllanTitle, "Math2 Allan Deviation") ;
					Normalizer = 194.395e12 ;
					break ;
				case PANEL_CHECKBOX_MATH3ALLAN :
					pAllan = &AllanMath3 ;
					Fmt(AllanTitle, "Math3 Allan Deviation") ;
					Normalizer = 282.143e12 ;
					break ;
				case PANEL_CHECKBOX_MATH4ALLAN :
					pAllan = &AllanMath4 ;
					Fmt(AllanTitle, "Math4 Allan Deviation") ;
					Normalizer = 429.228e12 ;
					break ;
				case PANEL_CHECKBOX_MATH5ALLAN :
					pAllan = &AllanMath5 ;
					Fmt(AllanTitle, "Math5 Allan Deviation") ;
					Normalizer = 429.228e12 ;
					break ;
			}			
			
			if (BoxChecked) {
				Allan_InitPanel(pAllan, AllanTitle, Normalizer, &OnCloseViewPanel) ;	
				}
			else {
				Allan_ClosePanel(pAllan) ;
				} ;
			break;
			
		case EVENT_RIGHT_CLICK:

			break;
			
		}
	return 0;
}

int  CVICALLBACK CB_ChangeDDSOut (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
	double frequency ;
	
	switch (event) {
		case EVENT_COMMIT:
			GetCtrlVal(MainPanel, control, &frequency);
			
			switch (control) {
				case PANEL_DDS1:
					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, frequency);
					break;
				case PANEL_DDS2:
					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, frequency);
					break;
				case PANEL_DDS3:
					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, frequency);
					break;
				case PANEL_DDS4:
					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, frequency);
					break;
			}
	}
	return 0;
}

int  CVICALLBACK CB_ChangeDDSStep (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
	double Step ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			GetCtrlVal(MainPanel, control, &Step);
			if (control==PANEL_DDS1STEP)   { SetCtrlAttribute(panel, PANEL_DDS1,   ATTR_INCR_VALUE, Step); }   
			if (control==PANEL_DDS2STEP)   { SetCtrlAttribute(panel, PANEL_DDS2,   ATTR_INCR_VALUE, Step); }
			if (control==PANEL_DDS3STEP)   { SetCtrlAttribute(panel, PANEL_DDS3,   ATTR_INCR_VALUE, Step); }   
			if (control==PANEL_DDS4STEP)   { SetCtrlAttribute(panel, PANEL_DDS4,   ATTR_INCR_VALUE, Step); } 
			break;
		case EVENT_RIGHT_CLICK:

			break;
			
		}
	return 0;
}

int CVICALLBACK CB_ChangeMath (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	int len;
	char *string;
		
	switch (event)
	{
		case EVENT_COMMIT:
			GetCtrlAttribute(panel, control, ATTR_STRING_TEXT_LENGTH, &len);
			string = (char *)malloc(sizeof(char) * (len + 1));
			GetCtrlVal(panel, control, string);
			switch (control) {
				case PANEL_MATHSTRING1:
					mupSetExpr(MathParser1, string);
					break;
				case PANEL_MATHSTRING2:
					mupSetExpr(MathParser2, string);
					break;
				case PANEL_MATHSTRING3:
					mupSetExpr(MathParser3, string);
					break;
				case PANEL_MATHSTRING4:
					mupSetExpr(MathParser4, string);
					break;
				case PANEL_MATHSTRING5:
					mupSetExpr(MathParser5, string);
					break;
			}
			free(string);
			break;
	}
	return 0;
}

int CVICALLBACK CB_ChangeN (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{

	switch (event)
		{
		case EVENT_COMMIT:
			if (control==PANEL_N1CHOICE) GetCtrlVal(MainPanel, control, &N1) ;
			if (control==PANEL_N2CHOICE) GetCtrlVal(MainPanel, control, &N2) ;
			if (control==PANEL_N3CHOICE) GetCtrlVal(MainPanel, control, &N3) ;
			break;
		}
	return 0;
}

int CVICALLBACK CB_OnPlus10k (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	double Frequency ;
	
	switch (event)
	{
		case EVENT_COMMIT:
			switch (control)
			{
				case PANEL_PLUS10KDDS1:
					GetCtrlVal(MainPanel, PANEL_DDS1, &Frequency);
					Frequency += 10000.0;
					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, Frequency);	
					SetCtrlVal(MainPanel, PANEL_DDS1, Frequency);
					break;
				case PANEL_PLUS10KDDS2:
					GetCtrlVal(MainPanel, PANEL_DDS2, &Frequency);
					Frequency += 10000.0;
					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, Frequency);	
					SetCtrlVal(MainPanel, PANEL_DDS2, Frequency);
					break;
				case PANEL_PLUS10KDDS3:
					GetCtrlVal(MainPanel, PANEL_DDS3, &Frequency);
					Frequency += 10000.0;
					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, Frequency);	
					SetCtrlVal(MainPanel, PANEL_DDS3, Frequency);
					break;
				case PANEL_PLUS10KDDS4:
					GetCtrlVal(MainPanel, PANEL_DDS4, &Frequency);
					Frequency += 10000.0;
					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, Frequency);	
					SetCtrlVal(MainPanel, PANEL_DDS4, Frequency);
					break;
			}
			break;
	}
	return 0;
}


int CVICALLBACK CB_OnMinus10k (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	double Frequency;
	
	switch (event)
	{
		case EVENT_COMMIT:
			switch (control)
			{
				case PANEL_MINUS10KDDS1:
					GetCtrlVal(MainPanel, PANEL_DDS1, &Frequency);
					Frequency -= 10000.0;
					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, Frequency);
					SetCtrlVal(MainPanel, PANEL_DDS1, Frequency);
					break;
				case PANEL_MINUS10KDDS2:
					GetCtrlVal(MainPanel, PANEL_DDS2, &Frequency);
					Frequency -= 10000.0;
					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, Frequency);	
					SetCtrlVal(MainPanel, PANEL_DDS2, Frequency);
					break;
				case PANEL_MINUS10KDDS3:
					GetCtrlVal(MainPanel, PANEL_DDS3, &Frequency);
					Frequency -= 10000.0;
					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, Frequency);	
					SetCtrlVal(MainPanel, PANEL_DDS3, Frequency);
					break;
				case PANEL_MINUS10KDDS4:
					GetCtrlVal(MainPanel, PANEL_DDS4, &Frequency);
					Frequency -= 10000.0;
					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, Frequency);
					SetCtrlVal(MainPanel, PANEL_DDS4, Frequency);
					break;
			}
			break;
	}
	return 0;
}



int CVICALLBACK CB_OnSaveOpt (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	bool BoxChecked = FALSE ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			
			GetCtrlVal(MainPanel, control, &BoxChecked);
			
			if (BoxChecked) {
				SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH2AUTOSAV, ATTR_DIMMED, FALSE) ;
				}
			
			if (!BoxChecked) {
				SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH2AUTOSAV, ATTR_DIMMED, TRUE) ;
				}
			
			break;
		}
	return 0;
}



 
int CVICALLBACK CB_OnSaveHg (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	bool BoxChecked = FALSE ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			
			GetCtrlVal(MainPanel, control, &BoxChecked);
			
			if (BoxChecked) {
				SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH3AUTOSAV, ATTR_DIMMED, FALSE) ;
				}
			
			if (!BoxChecked) {
				SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH3AUTOSAV, ATTR_DIMMED, TRUE) ;
				}
			
			break;
		}
	return 0;
}

int CVICALLBACK CB_OnAutoSaveHg (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	bool BoxChecked = FALSE ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			
			GetCtrlVal(MainPanel, control, &BoxChecked);
			
			if (BoxChecked) {
				SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH2SAVE, ATTR_DIMMED, TRUE) ;
				}
			
			if (!BoxChecked) {
				SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH2SAVE, ATTR_DIMMED, FALSE) ;
				}

			break;
		}
	return 0;
}

int CVICALLBACK CB_OnSaveSr (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	bool BoxChecked = FALSE ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			
			GetCtrlVal(MainPanel, control, &BoxChecked);
			
			if (BoxChecked) {
				SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH3AUTOSAV, ATTR_DIMMED, FALSE) ;
				}
			
			if (!BoxChecked) {
				SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH3AUTOSAV, ATTR_DIMMED, TRUE) ;        
				}
			break;
		}
	return 0;
}

int CVICALLBACK CB_OnAutoSaveSr (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	bool BoxChecked = FALSE ; 
	
	switch (event)
		{
		case EVENT_COMMIT:

			GetCtrlVal(MainPanel, control, &BoxChecked);
			
			if (BoxChecked) {
				SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH3SAVE, ATTR_DIMMED, TRUE) ;
				}
			
			if (!BoxChecked) {
				SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH3SAVE, ATTR_DIMMED, FALSE) ;
				}
			
			break;
		}
	return 0;
}


int CVICALLBACK CB_OnAcceptN (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	switch (event)
	{
		case EVENT_COMMIT:
			if (panel == CalcN1Panel) {
				N1 = round(N_1);
				SetCtrlVal(MainPanel, PANEL_N1CHOICE, N1);
			}
			if (panel == CalcN2Panel) {
				N2 = round(N_2);
				SetCtrlVal(MainPanel, PANEL_N2CHOICE, N2);
			}
			if (panel == CalcN3Panel) {
				N3 = round(N_3);
				SetCtrlVal(MainPanel, PANEL_N3CHOICE, N3);
			} 
			break;
	}
	return 0;
}


int CVICALLBACK OnChooseSaveFile (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	switch (event)
		{
		case EVENT_COMMIT:

			FileSelectPopup("D:\\Manip Femto", "*.txt", "*.txt", "Selection of the ExtraMath saving file", VAL_SAVE_BUTTON, 0, 0, 0, FALSE, ExtraMathFileName) ;
			SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH5SAVE, ATTR_DIMMED, FALSE) ;
			
			break;
		}
	return 0;
}


int CVICALLBACK CB_OnNCalculus (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{

	int PanelIsVisible, PanelTop, PanelLeft, PanelWidth ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			
			if (control==PANEL_N1CALCULUS){
				GetPanelAttribute (CalcN1Panel, ATTR_VISIBLE , &PanelIsVisible);
				if (PanelIsVisible) {
				}
				else {
					GetPanelAttribute (MainPanel, ATTR_TOP,   &PanelTop) ;
					GetPanelAttribute (MainPanel, ATTR_LEFT,  &PanelLeft) ;
					GetPanelAttribute (MainPanel, ATTR_WIDTH, &PanelWidth) ;
					SetPanelAttribute (CalcN1Panel, ATTR_TOP, PanelTop+170 ) ;
					SetPanelAttribute (CalcN1Panel, ATTR_LEFT, PanelLeft+PanelWidth+4 ) ;
					DisplayPanel(CalcN1Panel) ;
				} 
				}
			if (control==PANEL_N2CALCULUS){
				GetPanelAttribute (CalcN2Panel, ATTR_VISIBLE , &PanelIsVisible);
				if (PanelIsVisible) {
				}
				else {
					GetPanelAttribute (MainPanel, ATTR_TOP,   &PanelTop) ;
					GetPanelAttribute (MainPanel, ATTR_LEFT,  &PanelLeft) ;
					GetPanelAttribute (MainPanel, ATTR_WIDTH, &PanelWidth) ;
					SetPanelAttribute (CalcN2Panel, ATTR_TOP, PanelTop+310 ) ;
					SetPanelAttribute (CalcN2Panel, ATTR_LEFT, PanelLeft+PanelWidth+4 ) ;
					DisplayPanel(CalcN2Panel) ;
				} 
				}
			if (control==PANEL_N3CALCULUS){
				GetPanelAttribute (CalcN3Panel, ATTR_VISIBLE , &PanelIsVisible);
				if (PanelIsVisible) {
				}
				else {
					GetPanelAttribute (MainPanel, ATTR_TOP,   &PanelTop) ;
					GetPanelAttribute (MainPanel, ATTR_LEFT,  &PanelLeft) ;
					GetPanelAttribute (MainPanel, ATTR_WIDTH, &PanelWidth) ;
					SetPanelAttribute (CalcN3Panel, ATTR_TOP, PanelTop+480 ) ;
					SetPanelAttribute (CalcN3Panel, ATTR_LEFT, PanelLeft+PanelWidth+4 ) ;
					DisplayPanel(CalcN3Panel) ;
				} 
				}
			break;
		}
	
	return 0;
}


int CVICALLBACK CB_OnStartNCalculus (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	
	switch (event)
	{
		case EVENT_COMMIT:
			if (panel == CalcN1Panel)
				Measuring_1 = TRUE;
			if (panel == CalcN2Panel)
				Measuring_2 = TRUE;
			if (panel == CalcN3Panel)
				Measuring_3 = TRUE;
		break;
	}
	
	return 0;
}



int CVICALLBACK CB_OnNStop (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	int PanelIsVisible;
	
	switch (event)
	{
		case EVENT_COMMIT:
			if (panel == CalcN1Panel) {
				GetPanelAttribute (CalcN1Panel, ATTR_VISIBLE, &PanelIsVisible);
				if (PanelIsVisible)
					HidePanel(CalcN1Panel);
				
				Measuring_1 = FALSE;
				Frepminus_1=0.0;
				Frepplus_1=0.0;
			  	t1_1=0.0;
			 	t2_1=0.0;
			 	t3_1=0.0; 
				N_slope_1=0;
				Frequ_slope_1=0.0;
				Moy_slope_1=0.0;
				Slope_slope_1 =0.0;
				Ch4_slope=0.0;
				Moy_Ch4slope_1=0.0;
				Slope_Ch4slope_1=0.0;
					
				SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1) ;
				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
				SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit) ;
				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, FrequencyDDSBesInit); 
			}	
			if (panel == CalcN2Panel) {
				GetPanelAttribute (CalcN2Panel, ATTR_VISIBLE, &PanelIsVisible);
				if (PanelIsVisible)
					HidePanel(CalcN2Panel);
				
				Measuring_2=FALSE;
				Frepminus_2=0.0;
				Delta10K_Minus=0.0;
				Frepplus_2=0.0;
				Delta10K_Plus=0.0; 
				DeltaDDS3=0.0; 
			 	t1_2=0.0;
			  	t2_2=0.0;
			  	t3_2=0.0; 
				N_slope_2=0;
				Frequ_slope_2=0.0;
				Moy_slope_2=0.0;
				Beat_slope_2=0.0;
				Moy_Beatslope_2=0.0;
				Slope_Beatslope_2 =0.0;
				Slope_slope_2 =0.0;
				
				SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1) ;
				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
				SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit) ;
				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, FrequencyDDSBesInit);
				SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3Init) ;
				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequencyDDS3Init); 
			}
			if (panel == CalcN3Panel) {		
				GetPanelAttribute (CalcN3Panel, ATTR_VISIBLE, &PanelIsVisible);
				if (PanelIsVisible)										  
					HidePanel(CalcN3Panel);
				
				Measuring_3 = FALSE;
				Frepminus_3 = 0.0;
				Frepplus_3 = 0.0;
			   	t1_3 = 0.0;
			  	t2_3 = 0.0;
			   	t3_3 = 0.0; 
				N_slope_3 = 0;
				Frequ_slope_3 = 0.0;
				Moy_slope_3 = 0.0;
				Slope_slope_3 = 0.0;
			}
		break;
	}
	return 0;
}


int  CVICALLBACK CB_OnIntegrationTime (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
	double Time ;
	
	switch (event) {
		case EVENT_COMMIT:
			if (panel == CalcN1Panel) {
				GetCtrlVal(CalcN1Panel, CALCN1_INTEGRATIONTIME, &Time) ;
				DeltaT_1=Time;
			}
			if (panel == CalcN2Panel) {
				GetCtrlVal(CalcN2Panel, CALCN2_INTEGRATIONTIME, &Time) ;
				DeltaT_2=Time;
			}
			if (panel == CalcN3Panel) {
				GetCtrlVal(CalcN3Panel, CALCN3_INTEGRATIONTIME, &Time) ;
				DeltaT_3=Time;
			}
			break;
	}
	return 0;
}


int  CVICALLBACK CB_OnDeltaFreq (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
	double Frequ ;
	
	switch (event)
	{
		case EVENT_COMMIT:
			if (panel == CalcN1Panel) {
				GetCtrlVal(CalcN1Panel, CALCN1_DELTAFREQ, &Frequ);
				DeltakHz_1 = Frequ;
			}
			if (panel == CalcN2Panel) {
				GetCtrlVal(CalcN2Panel, CALCN2_DELTAFREQ, &Frequ) ;
				DeltakHz_2=Frequ;
			}
			if (panel == CalcN3Panel) {
				GetCtrlVal(CalcN3Panel, CALCN3_DELTAFREQ, &Frequ) ;
				DeltakHz_3=Frequ;
			}
			break;
	}
	return 0;
}


int  CVICALLBACK CB_OnFindSign (int panel, int control, int event, 
		void *callbackData, int eventData1, int eventData2)
{
	
	switch (event) {
		case EVENT_COMMIT:
			switch (control)
			{
				case PANEL_FINDSIGN1:
					tbegin1 = utc;
					Frepbefore1 = Math1;
					GetCtrlVal(MainPanel, PANEL_DDS1, &Frequency1) ;
					SetCtrlVal(MainPanel, PANEL_DDS1, Frequency1+Frequencystep1) ;  
					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, Frequency1+Frequencystep1);
					Getsign1 = TRUE;
					break;
				case PANEL_FINDSIGN2:
					tbegin2 = utc;
					Frepbefore2 = Math1;
					Ch2before = Ch2;
					GetCtrlVal(MainPanel, PANEL_DDS1, &Frequency2) ;
					SetCtrlVal(MainPanel, PANEL_DDS1, Frequency2+Frequencystep2) ;
					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, Frequency2+Frequencystep2);
					Getsign2 = TRUE;
					break;
				case PANEL_FINDSIGN3:
					tbegin3 = utc;
					Frepbefore3 = Math1;
					GetCtrlVal(MainPanel, PANEL_DDS4, &Frequency3) ;
					SetCtrlVal(MainPanel, PANEL_DDS4, Frequency3+Frequencystep3) ; 
					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, Frequency3+Frequencystep3);
					Getsign3 = TRUE;
					break;
			}
			break;
	}
	return 0;
}


int  CVICALLBACK CB_OnFind275K (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
	double frequency;
	
	switch (event)
	{
		case EVENT_COMMIT:
			switch (control)
			{
				case PANEL_FIND275K2:
					GetCtrlVal(MainPanel, PANEL_DDS2, &frequency);
					frequency = frequency + 275000 - Ch4;
					SetCtrlVal(MainPanel, PANEL_DDS2, frequency) ;  
					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, frequency);
					break;
					 
				case PANEL_FIND10K3:
					GetCtrlVal(MainPanel, PANEL_DDS3, &frequency);
					frequency = frequency + 10000 - Ch2;
					SetCtrlVal(MainPanel, PANEL_DDS3, frequency);
					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, frequency);
					break;
			}
			break;
	}
	return 0;
}


int  CVICALLBACK CB_OnSlopeTime (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
	double Time ;
	
	switch (event)
	{
		case EVENT_COMMIT:
			if (panel == CalcN1Panel) {
				GetCtrlVal(CalcN1Panel, CALCN1_SLOPETIME, &Time);
				SlopeTime1=Time;
			}
			if (panel == CalcN2Panel) {
				GetCtrlVal(CalcN2Panel, CALCN2_SLOPETIME, &Time);
				SlopeTime2=Time;
			}
			if (panel == CalcN3Panel) {
				GetCtrlVal(CalcN3Panel, CALCN3_SLOPETIME, &Time);
				SlopeTime3=Time;
			}
			break;
	}
	return 0;
}


 int  CVICALLBACK CB_OnChangeNdiv (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
	double NewN ;
	
	switch (event)
		{
		case EVENT_COMMIT:
			   if (control==PANEL_CHANGENDIV){ 
				GetCtrlVal(MainPanel, PANEL_CHANGENDIV, &NewN) ;
				Ndiv=NewN;
				FrequDDS1=880000000.0/Ndiv;
				DeltakHz_1=1800.0*8/Ndiv;
				DeltakHz_2=1500.0*8/Ndiv;
				SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1) ;
				DDS4xAD9912_SetFrequency(&DDS4xAD9912,1,FrequDDS1);
				SetCtrlVal(CalcN1Panel, CALCN1_DELTAFREQ, DeltakHz_1) ;
				SetCtrlVal(CalcN2Panel, CALCN2_DELTAFREQ, DeltakHz_2) ; 
			   }

			break;
			
		}
	return 0;
}


 int  CVICALLBACK CB_MeasureSlope (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
	double frequency;
	int active;
	
	switch (event)
	{
		case EVENT_COMMIT:
				   
			GetCtrlVal(panel, control, &active);
				   
			if (active) {
				
				SlopeMeasuringTimeBegin = utc;
				SlopeMeasuring = TRUE;
				
				frequency = DDSFox_ReadFreq(&DDS1xAD9956);
				GetCtrlVal(panel, PANEL_SLOPE_APPLIED, &SlopeMath2);
				DDSFox_Set(&DDS1xAD9956, frequency, SlopeMath2);
				
				Nratio = -1;
				CenterFrequencyCh2ToDetermine = TRUE ;
				CenterFrequencyCh2 = 0.0;
				
			} else {
				
				frequency = DEDRIFT_DDS_FREQUENCY;
				if (KeepFrequ)
					frequency = DDSFox_ReadFreq(&DDS1xAD9956);
				
				if (! KeepSlope)
					SlopeMath2 = 0.0;
				
				SetCtrlVal(panel, PANEL_SLOPE_APPLIED, SlopeMath2);
				DDSFox_Set(&DDS1xAD9956, frequency, SlopeMath2);
				
				SlopeMeasuring = FALSE; 
				N_Math2slope = 0.0;
				Math2_slope = 0.0;
				Moy_Math2slope = 0.0;
				Slope_Math2slope = 0.0;
				MoyMath2 = 0.0; 
				Slope_Math2slope = 0.0;
				CenterFrequencyCh2 = 0.0;
				Nratio = -1;
			}
			break;
	}
	return 0;
}

 
int  CVICALLBACK CB_OnResetSlope (int panel, int control, int event, 
		void *callbackData, int eventData1, int eventData2)
{

	switch (event) {
		case EVENT_COMMIT:
			CenterFrequencyCh2 = 0.0;
			MoyMath2 = 0.0;
			Nratio = -1;
			CenterFrequencyCh2ToDetermine = TRUE ;
			SlopeMath2 = 0.0; 
			SetCtrlVal(panel, PANEL_SLOPE_APPLIED, SlopeMath2);
			DDSFox_Set(&DDS1xAD9956, DEDRIFT_DDS_FREQUENCY, SlopeMath2);
			break;
	}
	return 0;
}
  
  
int  CVICALLBACK CB_ChangeSlopeTime (int panel, int control, int event, 
		void *callbackData, int eventData1, int eventData2)
{

	switch (event) {
		case EVENT_COMMIT:
			GetCtrlVal(MainPanel, PANEL_SLOPETIME, &TimetoSlope); 
			break;
	}
	return 0;
}


int  CVICALLBACK CB_OnCROX (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
	switch (event) {
		case EVENT_COMMIT:
			switch (control) {
				
				case PANEL_CHECKBOX_CORRFREQU:
					// enable frequency correction
				   	GetCtrlVal(MainPanel, PANEL_CHECKBOX_CORRFREQU, &FrequCorrec);
					break;
			
				case PANEL_CHECKBOX_KEEP:
					// keep current dedrifting frequency when dedrifting is disabled
				   	GetCtrlVal(MainPanel, PANEL_CHECKBOX_KEEP, &KeepFrequ);
					break;
					
				case PANEL_CHECKBOX_KEEPSLOPE:
					// keep current dedrifting slope when dedrifting is disabled
				   	GetCtrlVal(MainPanel, PANEL_CHECKBOX_KEEPSLOPE, &KeepSlope);
					break;
			}
 			break;
	}
	return 0;
}



int  CVICALLBACK CB_OnReCentering (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
		bool BoxChecked = FALSE; 

	switch (event)
		{
		case EVENT_COMMIT:
			
			if (control== PANEL_CHECKBOX_RECENTER)  
			{
				   	GetCtrlVal(MainPanel, PANEL_CHECKBOX_RECENTER, &BoxChecked) ;
						if (BoxChecked) {
							KeepCentering=TRUE ;
							CenteringTimeBegin275K=utc;
							CenteringTimeBegin10K=utc;
							}
						else {KeepCentering=FALSE ;}
							
			}
			
			
 			break;
			
		}
	return 0;
}


int  CVICALLBACK CB_OnStopSlopeCancellingOnUnlocked (int panel, int control, int event, 
		 void *callbackData, int eventData1, int eventData2)
{
	switch (event)
	{
		case EVENT_COMMIT:
			GetCtrlVal(MainPanel, PANEL_CHECKBOX_STOPIFAUTODE, &StopSlopeCancellingOnUnlocked);
			break;
	}
	return 0;
}

int CVICALLBACK CB_OnSlopeReference (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	switch (event)
	{
		case EVENT_COMMIT:
			GetCtrlVal(MainPanel, PANEL_SLOPE_REFERENCE, &slopeReference);
			break;
	}
	return 0;
}


int CVICALLBACK CB_OnEstimateN (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	int visible;
	double wl;
	
	switch (event)
	{
		case EVENT_COMMIT:
			/* be prepaid to support more N estimates for different beat notes */
			switch (control)
			{
				case PANEL_ESTIMATE_N3:
					GetPanelAttribute(EstimateN3Panel, ATTR_VISIBLE , &visible);
					if (! visible) {
						DisplayPanel(EstimateN3Panel);
					}
					/* set current frep */
					SetCtrlVal(EstimateN3Panel, ESTIMATEN3_FREP, 250e6 + Math1);
					/* default wavelenght for Sr cavity */
					GetCtrlVal(EstimateN3Panel, ESTIMATEN3_WAVELENGTH, &wl);
					if (wl == 0.0)
						SetCtrlVal(EstimateN3Panel, ESTIMATEN3_WAVELENGTH, 698.50);
					/* reset N3 estimate */
					SetCtrlVal(EstimateN3Panel, ESTIMATEN3_N, 0.0);
					break;
			}
			break;
	}
	
	return 0;
}			

int CVICALLBACK CB_OnNEstimateCancel (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	int visible;
	
	switch (event)
	{
		case EVENT_COMMIT:
			GetPanelAttribute(panel, ATTR_VISIBLE, &visible);
			if (visible)
				HidePanel(panel);
			break;
	}
	return 0;
}

int CVICALLBACK CB_OnNEstimateSet (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	switch (event)
	{
		case EVENT_COMMIT:
			GetCtrlVal(panel, ESTIMATEN3_N, &N3);
			SetCtrlVal(MainPanel, PANEL_N3CHOICE, N3);
			HidePanel(panel);
			break;
	}
	return 0;
}

int CVICALLBACK CB_OnNEstimate (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	double frep, wl, N;
	
	switch (event)
	{
		case EVENT_COMMIT:
			GetCtrlVal(panel, ESTIMATEN3_FREP, &frep);
			GetCtrlVal(panel, ESTIMATEN3_WAVELENGTH, &wl);
			N = round(299792458.0 / wl / 1e-9 / frep);
			SetCtrlVal(panel, ESTIMATEN3_N, N);
			break;
	}
	return 0;
}

int CVICALLBACK CB_SetSlope (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	double frequency;
	
	switch (event)
	{
		case EVENT_COMMIT:
			GetCtrlVal(panel, control, &SlopeMath2);
			frequency = DDSFox_ReadFreq(&DDS1xAD9956);
			DDSFox_Set(&DDS1xAD9956, frequency, SlopeMath2);
			break;
	}
	return 0;
}

int CVICALLBACK CB_InvertSlopeSign (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{
	switch (event)
	{
		case EVENT_COMMIT:
			GetCtrlVal(panel, control, &invertSlopeSign);
			break;
	}
	return 0;
}