diff FXAnalyse.c @ 134:bd28161e5ac2

Major code cleanup
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Wed, 22 Jan 2014 12:29:28 +0100
parents 7540703b8473
children 77539f2597b1
line wrap: on
line diff
--- a/FXAnalyse.c	Wed Jan 22 12:10:17 2014 +0100
+++ b/FXAnalyse.c	Wed Jan 22 12:29:28 2014 +0100
@@ -51,8 +51,114 @@
 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 ; 
+
+// panels
+static int MainPanel;
+static int CalcNPanel;
+static int EstimateN3Panel;
+static int LoggingPanel;
+
+
+struct adev {
+	Allan_Data allan;
+	double *data;
+	const char *title;
+	double normalization;
+	int control;
+};
+
+
+#define ADEV_INIT(__channel, __normalization)	\
+	{											\
+		.data = & ## __channel,					\
+		.title = #__channel,					\
+		.normalization = __normalization,		\
+		.control = PANEL_ADEV_ ## __channel		\
+	}
+
+
+int adev_toggle(struct adev *adev)
+{
+	if (adev->allan.active)
+		Allan_ClosePanel(&(adev->allan));
+	else
+		Allan_InitPanel(&(adev->allan), adev->title,
+			adev->normalization, MainPanel, adev->control);
+	return adev->allan.active;
+}
+
+
+void adev_update(struct adev *adev)
+{
+	if (adev->allan.active)
+		Allan_AddFrequency(&(adev->allan), *(adev->data));
+}
+
+
+struct adev adevs[] = {
+	ADEV_INIT(Ch1, 1.84e12),
+	ADEV_INIT(Ch2, 10.0e3),
+	ADEV_INIT(Ch3, 429.228e12),
+	ADEV_INIT(Ch4, 275.0e3),
+	ADEV_INIT(Math1, 250.0e6),
+	ADEV_INIT(Math2, 194.395e12),
+	ADEV_INIT(Math3, 282.143e12),
+	ADEV_INIT(Math4, 429.228e12),
+	ADEV_INIT(Math5, 1.0),
+	{ NULL }
+};
+
+
+struct plot {
+	Plot_Data plot;
+	double *data;
+	const char *title;
+	double min;
+	double max;
+	int control;
+};
+
+
+#define PLOT_INIT(__channel, __min, __max) 		\
+	{											\
+		.data = & ## __channel, 				\
+		.title = #__channel,					\
+		.min = __min,							\
+		.max = __max,							\
+		.control = PANEL_PLOT_ ## __channel		\
+	}
+
+
+int plot_toggle(struct plot *plot)
+{
+	if (plot->plot.active)
+		Plot_ClosePanel(&(plot->plot));
+	else
+		Plot_InitPanel(&(plot->plot), plot->title,
+			plot->min, plot->max, MainPanel, plot->control);
+	return plot->plot.active;
+}
+
+
+void plot_update(struct plot *plot)
+{
+	if (plot->plot.active)
+		Plot_AddFrequency(&(plot->plot), *(plot->data));
+}
+
+struct plot plots[] = {
+	PLOT_INIT(Ch1, 54.999e6, 55.001e6),
+	PLOT_INIT(Ch2, 0.0, 0.0),
+	PLOT_INIT(Ch3, 0.0, 0.0),
+	PLOT_INIT(Ch4, 0.0, 0.0),
+	PLOT_INIT(Math1, 0.0, 0.0),
+	PLOT_INIT(Math2, 0.0, 0.0),
+	PLOT_INIT(Math3, 0.0, 0.0),
+	PLOT_INIT(Math4, 0.0, 0.0),
+	PLOT_INIT(Math5, 0.0, 0.0),
+	{ NULL }
+};
+
 
 // 1xAD9956 DDS box
 DDSParameter DDS1xAD9956;
@@ -179,18 +285,12 @@
 };
 
 
-// panels
-static int MainPanel;
-static int CalcNPanel;
-static int EstimateN3Panel;
-static int LoggingPanel;
-
-
 char * thousands(char *buffer, int size, char *fmt, double val)
 {
 	// compute how many separators we need
 	#pragma DisableFunctionRuntimeChecking log10
-	int nsep = log10(fabs(val)) / 3;
+	int nsep = log10(fabs(val));
+	nsep = (nsep > 0 ? nsep / 3 : 0);
 	// format value
 	int len = snprintf(buffer, size, fmt, val);
 	char *src = buffer + len;
@@ -431,31 +531,6 @@
 }
 
 
-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)
 {
@@ -635,7 +710,6 @@
 {
 	struct event event;
 	int read;
-	int BoxChecked = FALSE;
 	
 	switch (ev) {
 		case EVENT_TSQ_ITEMS_IN_QUEUE:
@@ -677,88 +751,13 @@
 				SetCtrlVal(MainPanel,PANEL_MATH4, thousands(buffer, sizeof(buffer), "%.3f", Math4));
 				SetCtrlVal(MainPanel,PANEL_MATH5, thousands(buffer, sizeof(buffer), "%.3f", Math5));
 				
-				// plot
-				
-				GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ1PLOT, &BoxChecked);
-				if (BoxChecked) {
-					Plot_AddFrequency(&PlotCh1, Ch1);
-				}
-				GetCtrlVal(MainPanel, PANEL_CHECKBOX_FREQ1ALLAN, &BoxChecked);
-				if (BoxChecked) {
-					Allan_AddFrequency(&AllanCh1, Ch1);
-				}
+				// update timeseries plots
+				for (struct plot *plot = plots; plot->data; plot++)
+					plot_update(plot);
 
-				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);
-				}
+				// update allan deviation plots
+				for (struct adev *adev = adevs; adev->data; adev++)
+					adev_update(adev);
 				
 				// Calcul de N
 				
@@ -1478,67 +1477,13 @@
 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) {
+	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 ;
+			for (struct plot *plot = plots; plot->data; plot++) {
+				if (plot->control == control)
+					plot_toggle(plot);
 			}
-			
-			if (BoxChecked)
-				Plot_InitPanel(pPlot, PlotTitle, PlotMin, PlotMax, &OnCloseViewPanel);
-			else
-				Plot_ClosePanel(pPlot);
 			break;
 	}
 	return 0;
@@ -1548,77 +1493,15 @@
 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) ;
-				} ;
+			for (struct adev *adev = adevs; adev->data; adev++) {
+				if (adev->control == control)
+					adev_toggle(adev);
+			}
 			break;
-			
-		case EVENT_RIGHT_CLICK:
-
-			break;
-			
-		}
+	}
 	return 0;
 }