changeset 52:30146cc6cd77

Cleanup. Remove some C abominations
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Wed, 10 Oct 2012 18:16:02 +0200
parents fbb172892253
children 57e2e88aadb9
files FXAnalyse.c
diffstat 1 files changed, 125 insertions(+), 157 deletions(-) [+]
line wrap: on
line diff
--- a/FXAnalyse.c	Wed Oct 10 14:52:04 2012 +0200
+++ b/FXAnalyse.c	Wed Oct 10 18:16:02 2012 +0200
@@ -131,11 +131,11 @@
 int invertSlopeSign = 0;
 int slopeReference = SLOPE_REFERENCE_MICROWAVE;
 
-volatile bool KeepCentering=FALSE; 
-double Timetorecenter275K=3600.0*10; 
-double Timetorecenter10K=3600.0*3;  
-double CenteringTimeBegin275K=0.0;
-double CenteringTimeBegin10K=0.0; 
+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)
@@ -150,56 +150,54 @@
 }
 
 
-void initMathParser(muParserHandle_t * pMathParser) 
+muParserHandle_t initMathParser() 
 {
-	(* pMathParser) = mupCreate() ;		// Math parser number 1, normally used for frep
-	mupDefineOprtChars( * pMathParser, "abcdefghijklmnopqrstuvwxyzµ"
-                              		   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-                            		   "+-*^/?<>=#!$%&|~'_");
-	mupDefineVar( * pMathParser, "UTC", &utc);
-	mupDefineVar( * pMathParser, "MJD", &mjd);
-	mupDefineVar( * pMathParser, "Ch1", &Ch1);
-	mupDefineVar( * pMathParser, "Ch2", &Ch2);
-	mupDefineVar( * pMathParser, "Ch3", &Ch3);
-	mupDefineVar( * pMathParser, "Ch4", &Ch4);
-	mupDefineVar( * pMathParser, "DDS1", &(DDS4xAD9912.Frequency1));   
-	mupDefineVar( * pMathParser, "DDS2", &(DDS4xAD9912.Frequency2));
-	mupDefineVar( * pMathParser, "DDS3", &(DDS4xAD9912.Frequency3));  
-	mupDefineVar( * pMathParser, "DDS4", &(DDS4xAD9912.Frequency4));  
-	mupDefineVar( * pMathParser, "N1", &N1);
-	mupDefineVar( * pMathParser, "N2", &N2);
-	mupDefineVar( * pMathParser, "N3", &N3);
-	mupDefineVar( * pMathParser, "Nu1", &Nu1);
-	mupDefineVar( * pMathParser, "Nu2", &Nu2);
-	mupDefineVar( * pMathParser, "DeltaDDS3", &DeltaDDS3);
-	mupDefineVar( * pMathParser, "Signe1", &Signe1);
-	mupDefineVar( * pMathParser, "Signe2", &Signe2); 
-	mupDefineVar( * pMathParser, "Ndiv", &Ndiv); 
-	mupDefinePostfixOprt( * pMathParser, "P", &Peta , 1);
-	mupDefinePostfixOprt( * pMathParser, "T", &Tera , 1);
-	mupDefinePostfixOprt( * pMathParser, "G", &Giga , 1);
-	mupDefinePostfixOprt( * pMathParser, "M", &Mega , 1);
-	mupDefinePostfixOprt( * pMathParser, "k", &kilo , 1);
-	mupDefinePostfixOprt( * pMathParser, "m", &milli, 1);
-	mupDefinePostfixOprt( * pMathParser, "u", &micro, 1);
-	mupDefinePostfixOprt( * pMathParser, "µ", &micro, 1);
-	mupDefinePostfixOprt( * pMathParser, "n_1", &nano,  1);
-	mupDefinePostfixOprt( * pMathParser, "p", &pico,  1);
-	mupDefinePostfixOprt( * pMathParser, "f", &femto, 1);
+	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 initfreqDDS ;
-	char * MathString1 = "                                                             " ;
-	char * MathString2 = "                                                             " ;
-	char * MathString3 = "                                                             " ;
-	char * MathString4 = "                                                             " ;  
-	char * MathString5 = "                                                             " ;
+	double frequency;
+	char expr[1024];
 	
 	if (InitCVIRTE (0, argv, 0) == 0)
-		return -1;	/* out of memory */
+		return -1;
 	if ((MainPanel = LoadPanel (0, "FXAnalyse.uir", PANEL)) < 0)
 		return -1;
 	if ((CalcN1Panel = LoadPanel (0, "FXAnalyse.uir", CALCN1)) < 0)
@@ -214,15 +212,15 @@
 	DisplayPanel (MainPanel);
 	
 	// initialize 4x AD9959 DDS box
-	DDS4xAD9912_Reset(&DDS4xAD9912) ;
-	GetCtrlVal(MainPanel, PANEL_DDS1, &initfreqDDS) ;
-	DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, initfreqDDS) ;
-	GetCtrlVal(MainPanel, PANEL_DDS2, &initfreqDDS) ;
-	DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, initfreqDDS) ;
-	GetCtrlVal(MainPanel, PANEL_DDS3, &initfreqDDS) ;
-	DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, initfreqDDS) ;
-	GetCtrlVal(MainPanel, PANEL_DDS4, &initfreqDDS) ;
-	DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, initfreqDDS) ;
+	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"); 
@@ -234,52 +232,47 @@
 	DDS1xAD9956.SweepRate = 0.0;
 	DDS_Initialize(&DDS1xAD9956);
 	
-	// Initialize the 5 (separate) math parsers for frep, Nu_Hg, Nu_Sr, N3 and ExtraMath
-
-	Ch1=0.0 ; Ch2=0.0 ; Ch3=0.0 ; Ch4=0.0 ;
-	GetCtrlVal(MainPanel, PANEL_N1CHOICE, &N1) ;
-	GetCtrlVal(MainPanel, PANEL_N2CHOICE, &N2) ;
-
-	GetCtrlVal(MainPanel, PANEL_N3CHOICE, &N3) ;
+	Ch1 = Ch2 = Ch3 = Ch4 = 0.0;
+	
+	GetCtrlVal(MainPanel, PANEL_N1CHOICE, &N1);
+	GetCtrlVal(MainPanel, PANEL_N2CHOICE, &N2);
+	GetCtrlVal(MainPanel, PANEL_N3CHOICE, &N3);
 	
-	initMathParser(&MathParser1);
-	GetCtrlVal(MainPanel, PANEL_MATHSTRING1, MathString1);
-	mupSetExpr(MathParser1, MathString1);
+	MathParser1 = initMathParser();
+	GetCtrlVal(MainPanel, PANEL_MATHSTRING1, expr);
+	mupSetExpr(MathParser1, expr);
 	
-	initMathParser(&MathParser2) ;
-	mupDefineVar( MathParser2, "Math1", &Math1);
-	mupDefineVar( MathParser2, "DDS", &(DDS4xAD9912.Frequency1));
-	GetCtrlVal(MainPanel, PANEL_MATHSTRING2, MathString2) ;
-	mupSetExpr(MathParser2, MathString2);
+	MathParser2 = initMathParser();
+	mupDefineVar(MathParser2, "Math1", &Math1);
+	mupDefineVar(MathParser2, "DDS", &(DDS4xAD9912.Frequency1));
+	GetCtrlVal(MainPanel, PANEL_MATHSTRING2, expr);
+	mupSetExpr(MathParser2, expr);
 	
-	initMathParser(&MathParser3) ;
-	mupDefineVar( MathParser3, "Math1", &Math1);
-	mupDefineVar( MathParser3, "Math2", &Math2);
-	mupDefineVar( MathParser3, "DDS", &(DDS4xAD9912.Frequency2));
-	GetCtrlVal(MainPanel, PANEL_MATHSTRING3, MathString3) ;
-	mupSetExpr(MathParser3, MathString3);
-	
-	initMathParser(&MathParser4) ;
-	mupDefineVar( MathParser4, "Math1", &Math1);
-	mupDefineVar( MathParser4, "Math2", &Math2);
-	mupDefineVar( MathParser4, "Math3", &Math3);
-	GetCtrlVal(MainPanel, PANEL_MATHSTRING4, MathString4) ;
-	mupSetExpr(MathParser4, MathString4);
+	MathParser3 = initMathParser();
+	mupDefineVar(MathParser3, "Math1", &Math1);
+	mupDefineVar(MathParser3, "Math2", &Math2);
+	mupDefineVar(MathParser3, "DDS", &(DDS4xAD9912.Frequency2));
+	GetCtrlVal(MainPanel, PANEL_MATHSTRING3, expr);
+	mupSetExpr(MathParser3, expr);
 	
-	initMathParser(&MathParser5) ;
-	mupDefineVar( MathParser5, "Math1", &Math1);
-	mupDefineVar( MathParser5, "Math2", &Math2);
-	mupDefineVar( MathParser5, "Math3", &Math3);
-	mupDefineVar( MathParser5, "Math4", &Math4);
-	GetCtrlVal(MainPanel, PANEL_MATHSTRING5, MathString5) ;
-	mupSetExpr(MathParser5, MathString5);
+	MathParser4 = initMathParser();
+	mupDefineVar(MathParser4, "Math1", &Math1);
+	mupDefineVar(MathParser4, "Math2", &Math2);
+	mupDefineVar(MathParser4, "Math3", &Math3);
+	GetCtrlVal(MainPanel, PANEL_MATHSTRING4, expr);
+	mupSetExpr(MathParser4, expr);
 	
-	
-	RunUserInterface ();
+	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);
 	
-	// Stops everything at the end
+	RunUserInterface();
 	
-	DiscardPanel (MainPanel);
+	DiscardPanel(MainPanel);
 	return 0;
 }
 
@@ -315,7 +308,7 @@
 	switch (event)
 	{
 		case EVENT_COMMIT:
-			QuitUserInterface (0);
+			QuitUserInterface(0);
 			mupRelease(MathParser1);
 			mupRelease(MathParser2); 
 			mupRelease(MathParser3);
@@ -475,16 +468,12 @@
 int CVICALLBACK CB_OnStop (int panel, int control, int event,
 		void *callbackData, int eventData1, int eventData2)
 {
-	switch (event)
-		{
+	switch (event) {
 		case EVENT_COMMIT:
 			Acquiring = FALSE ;
 			SetCtrlAttribute(MainPanel, PANEL_STARTBUTTON, ATTR_LABEL_TEXT, "__START");
 			break;
-		case EVENT_RIGHT_CLICK:
-
-			break;
-		}
+	}
 	return 0;
 }
 
@@ -510,12 +499,6 @@
 	char * ReportString = "dd.mm.yy\t00:00:00\t3481610838.000\t0000000000000.00000000";
 
 	int BoxChecked = FALSE; 
-
-	double FoxFrequ=0.0;
-	
-	double FrequencyToChange=0.0;
-	double DeltaCh2=0.0;
-	double DeltaCh4=0.0;  
 	
 	switch (event)
 	{
@@ -1240,37 +1223,31 @@
 						break;
 					}
 					
-					if  (utc-SlopeMeasuringTimeBegin > TimetoSlope)
-					{
+					if ((utc - SlopeMeasuringTimeBegin) > TimetoSlope) {
+						
 						Nratio = Nratio + 1;
-						
-						if (Nratio >= 1) {
+						if Nratio >= 1
 							MoyMath2 = MoyMath2 + Moy_Math2slope;
-						}
 						
-						if (invertSlopeSign) {
+						if (invertSlopeSign)
 							SlopeMath2 = SlopeMath2 - Slope_Math2slope;
-						} else {
+						else
 							SlopeMath2 = SlopeMath2 + Slope_Math2slope;
-						}
 						
 						N_Math2slope = 0.0;
 						Math2_slope = 0.0;
 						Moy_Math2slope = 0.0;
 						Slope_Math2slope = 0.0;
 						
-						if (Nratio == 1 && CenterFrequencyCh2ToDetermine == TRUE)
-						{
+						if (Nratio == 1 && CenterFrequencyCh2ToDetermine == TRUE) {
 							CenterFrequencyCh2 = MoyMath2;
 							CenterFrequencyCh2ToDetermine = FALSE;
 						}
 						
 						OnSlopeCancelling = TRUE;
 						
-						if (Nratio == ratio)
-						{
-							if (FrequCorrec == TRUE)
-							{
+						if (Nratio == ratio) {
+							if (FrequCorrec == TRUE) {
 								SlopeCorrection = (MoyMath2/ratio-CenterFrequencyCh2)/TimetoSlope;
 								SlopeMath2 = SlopeMath2 + SlopeCorrection;
 							}
@@ -1278,9 +1255,9 @@
 							MoyMath2 = 0.0;
 						}
 						
-						FoxFrequ = DDSFox_ReadFreq(&DDS1xAD9956);
+						double frequency = DDSFox_ReadFreq(&DDS1xAD9956);
 						SetCtrlVal(MainPanel, PANEL_SLOPETOCANCEL, SlopeMath2);
-						DDSFox_Set(&DDS1xAD9956, FoxFrequ, SlopeMath2);
+						DDSFox_Set(&DDS1xAD9956, frequency, SlopeMath2);
 						
 						nstabilisationSlopeMeasuring = 0;
 						SlopeMeasuringTimeBegin = utc;
@@ -1338,27 +1315,26 @@
 				}
 				
 				// re-centering
-				if (KeepCentering) 
-				{
-					
-					DeltaCh4=275000-Ch4;
-					DeltaCh2=10000-Ch2;
-					
-					if (utc- CenteringTimeBegin275K > Timetorecenter275K && CenteringTimeBegin275K>10)
-					{
-						GetCtrlVal(MainPanel, PANEL_DDS2, &FrequencyToChange);
-						SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyToChange+DeltaCh4);
-						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, FrequencyToChange+DeltaCh4);
-						CenteringTimeBegin275K=utc;
+				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)
-							{
-								GetCtrlVal(MainPanel, PANEL_DDS3, &FrequencyToChange);
-								SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyToChange+DeltaCh2); 
-								DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequencyToChange+DeltaCh2);
-								CenteringTimeBegin10K=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
@@ -1433,8 +1409,7 @@
 	double PlotMin = 10e6 ;
 	double PlotMax = 65e6 ;
 	
-	switch (event)
-		{
+	switch (event) {
 		case EVENT_COMMIT:
 		
 			GetCtrlVal(MainPanel, control, &BoxChecked);
@@ -1485,19 +1460,12 @@
 					break ;
 			}
 			
-			if (BoxChecked) {
-				Plot_InitPanel(pPlot, PlotTitle, PlotMin, PlotMax, &OnCloseViewPanel) ;	
-				}
-			else {
-				Plot_ClosePanel(pPlot) ;
-				} ;
+			if (BoxChecked)
+				Plot_InitPanel(pPlot, PlotTitle, PlotMin, PlotMax, &OnCloseViewPanel);
+			else
+				Plot_ClosePanel(pPlot);
 			break;
-			
-		case EVENT_RIGHT_CLICK:
-
-			break;
-			
-		}
+	}
 	return 0;
 }