diff FXAnalyse.c @ 142:fd085d61e4ca

Rework data logging
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Wed, 22 Jan 2014 14:45:23 +0100
parents 3f7eef731ccc
children 09a4548e1436
line wrap: on
line diff
--- a/FXAnalyse.c	Wed Jan 22 14:45:22 2014 +0100
+++ b/FXAnalyse.c	Wed Jan 22 14:45:23 2014 +0100
@@ -42,10 +42,11 @@
 int CVICALLBACK FileDataProvider (void *functionData);
 int CVICALLBACK KKDataProvider (void *functionData);
 
+
 // select which data provider to use
 #define DataProvider KKDataProvider
 
-
+struct event event;
 double utc;
 double Ch1, Ch2, Ch3, Ch4;
 double Math1, Math2, Math3, Math4, Math5;
@@ -285,7 +286,63 @@
 };
 
 
-char * thousands(char *buffer, int size, char *fmt, double val)
+// data loggging
+struct datafile {
+	char *name;
+	double *data;
+	int nchan;
+	int control;
+	int write;
+};
+
+
+struct datafile datafiles[] = {
+	{ "Raw", event.data, 4, PANEL_SAVE_RAW, FALSE },
+	{ "DDS", DDS4xAD9912.frequency, 4, PANEL_SAVE_DDS, FALSE },
+	{ "Lo", &Math2, 1, PANEL_SAVE_LO, FALSE },
+	{ "Hg", &Math3, 1, PANEL_SAVE_HG, FALSE },
+	{ "Sr", &Math4, 1, PANEL_SAVE_SR, FALSE },
+	{ "Ex", &Math5, 1, PANEL_SAVE_EXTRA, FALSE },
+	{ NULL, }
+};
+
+
+static void write_data(const char *folder, const char *name, const char *id, 
+		const char *timestr, double utc, double *v, int nchan)
+{
+	char line[1024];
+	char filename[FILENAME_MAX];
+	
+	// construct filename in the form folder\\id-name.txt
+	snprintf(filename, sizeof(filename), "%s\\%s-%s.txt", folder, id, name);
+	
+	int fd = OpenFile(filename, VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
+	switch (nchan)
+	{
+		case 1:
+			Fmt(line, "%s\t%f[p3]\t%f[p3]", 
+				timestr, utc, v[0]);
+			break;
+		case 4:
+			Fmt(line, "%s\t%f[p3]\t%f[p3]\t%f[p3]\t%f[p3]\t%f[p3]",
+				timestr, utc, v[0], v[1], v[2], v[3]);
+			break;
+		default:
+			strcpy(line, "?");
+	}
+	WriteLine(fd, line, -1);
+	CloseFile(fd);
+}
+
+
+static inline void datafile_append(struct datafile *d, char *id, char *timestr)
+{
+	if (d->write)
+		write_data(DATAFOLDER, d->name, id, timestr, utc, d->data, d->nchan);
+}
+
+
+static const char * thousands(char *buffer, int size, char *fmt, double val)
 {
 	// compute how many separators we need
 	#pragma DisableFunctionRuntimeChecking log10
@@ -346,8 +403,12 @@
 	SetCtrlVal(LoggingPanel, LOGGING_LOGGING, msg);
 }
 
+
+int Sr_datalogger_enabled = FALSE;
+
+
 // Sr data logger
-int sendLogger(const char* id, double utc, double data)
+int Sr_datalogger_send(const char* id, double utc, double data)
 {
 	static unsigned int handle = 0;
 	char buffer[1024];
@@ -365,12 +426,13 @@
 	if (ClientTCPWrite(handle, buffer, strlen(buffer) + 1, 0) < 0) {
 		// try to reconnect and resend
 		handle = 0;
-		sendLogger(id, utc, data);
+		Sr_datalogger_send(id, utc, data);
 	}
 	
 	return 0;
 }
 
+
 muParserHandle_t initMathParser() 
 {
 	muParserHandle_t parser = mupCreate();
@@ -382,10 +444,10 @@
 	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, "DDS1", &(DDS4xAD9912.frequency[0]));
+	mupDefineVar(parser, "DDS2", &(DDS4xAD9912.frequency[1]));
+	mupDefineVar(parser, "DDS3", &(DDS4xAD9912.frequency[2]));  
+	mupDefineVar(parser, "DDS4", &(DDS4xAD9912.frequency[3]));  
 	mupDefineVar(parser, "N1", &N1);
 	mupDefineVar(parser, "N2", &N2);
 	mupDefineVar(parser, "N3", &N3);
@@ -412,36 +474,6 @@
 }
 
 
-void writeData(const char *folder, const char *name, const char *id, 
-		const char *timestr, double utc, double value)
-{
-	char line[1024];
-	char filename[FILENAME_MAX];
-	
-	// construct filename in the form folder\\id-name.txt
-	snprintf(filename, sizeof(filename), "%s\\%s-%s.txt", folder, id, name);
-	
-	int fd = OpenFile(filename, VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
-	Fmt(line, "%s\t%f[p3]\t%f[p3]", timestr, utc, value);
-	WriteLine(fd, line, -1);
-	CloseFile(fd);
-}
-
-void writeData4(const char *folder, const char *name, const char *id, 
-		const char *timestr, double utc, double v1, double v2, double v3, double v4)
-{
-	char line[1024];
-	char filename[FILENAME_MAX];
-	
-	// construct filename in the form folder\\id-name.txt
-	snprintf(filename, sizeof(filename), "%s\\%s-%s.txt", folder, id, name);
-	
-	int fd = OpenFile(filename, VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
-	Fmt(line, "%s\t%f[p3]\t%f[p3]\t%f[p3]\t%f[p3]\t%f[p3]", timestr, utc, v1, v2, v3, v4);
-	WriteLine(fd, line, -1);
-	CloseFile(fd);
-}
-
 void CVICALLBACK DataAvailableCB (CmtTSQHandle queueHandle, unsigned int event,
 		int value, void *callbackData);
 
@@ -489,14 +521,14 @@
 	
 	MathParser2 = initMathParser();
 	mupDefineVar(MathParser2, "Math1", &Math1);
-	mupDefineVar(MathParser2, "DDS", &(DDS4xAD9912.Frequency1));
+	mupDefineVar(MathParser2, "DDS", &(DDS4xAD9912.frequency[0]));
 	GetCtrlVal(MainPanel, PANEL_MATHSTRING2, expr);
 	mupSetExpr(MathParser2, expr);
 	
 	MathParser3 = initMathParser();
 	mupDefineVar(MathParser3, "Math1", &Math1);
 	mupDefineVar(MathParser3, "Math2", &Math2);
-	mupDefineVar(MathParser3, "DDS", &(DDS4xAD9912.Frequency2));
+	mupDefineVar(MathParser3, "DDS", &(DDS4xAD9912.frequency[1]));
 	GetCtrlVal(MainPanel, PANEL_MATHSTRING3, expr);
 	mupSetExpr(MathParser3, expr);
 	
@@ -703,7 +735,6 @@
 void CVICALLBACK DataAvailableCB (CmtTSQHandle queueHandle, unsigned int ev,
 		int value, void *callbackData)
 {
-	struct event event;
 	int read;
 	
 	switch (ev) {
@@ -944,7 +975,7 @@
 							
 							// adjust DDS3 to keep beatnote within the bandpass filter. prediction
 							double fDDS3 = FrequencyDDS3Init - DeltakHz_2*1000*(-Sign1/Sign2)*Ndiv*(Nu2)/(Nu1) - Beatslope_2*(utc-t1_2);
-							DeltaDDS3 = fDDS3 - DDS4xAD9912.Frequency3;
+							DeltaDDS3 = fDDS3 - DDS4xAD9912_GetFrequency(&DDS4xAD9912, 3);
 							printf("deltaDDS3 = %g\n", DeltaDDS3);
 							SetCtrlVal(MainPanel, PANEL_DDS3, fDDS3);
 							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, fDDS3);
@@ -966,11 +997,11 @@
 							break;
 						}
 						
-						double fDDS2 = DDS4xAD9912.Frequency2 + 275000 - Ch4;
+						double fDDS2 = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 2) + 275000 - Ch4;
 						SetCtrlVal(MainPanel, PANEL_DDS2, fDDS2);
 						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, fDDS2);
 						
-						double fDDS3 = DDS4xAD9912.Frequency3 + 10000 - Ch2;
+						double fDDS3 = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 3) + 10000 - Ch2;
 						DeltaDDS3 = DeltaDDS3 + 10000 - Ch2;
 						SetCtrlVal(MainPanel, PANEL_DDS3, fDDS3);
 						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, fDDS3);
@@ -1010,7 +1041,7 @@
 							
 							// adjust DDS3 to keep beatnote within the bandpass filter. prediction
 							double fDDS3 = FrequencyDDS3Init + DeltakHz_2*1000*(-Sign1/Sign2)*Ndiv*(Nu2)/(Nu1);
-							DeltaDDS3 = fDDS3 - DDS4xAD9912.Frequency3;
+							DeltaDDS3 = fDDS3 - DDS4xAD9912_GetFrequency(&DDS4xAD9912, 3);
 							SetCtrlVal(MainPanel, PANEL_DDS3, fDDS3);
 							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, fDDS3);
 
@@ -1088,8 +1119,8 @@
 						f_beat_Sr_plus = f_beat_Sr_minus = 0.0;
 						
 						// record current DDS frequencies
-						FrequencyDDSBesInit = DDS4xAD9912.Frequency2;
-						FrequencyDDS3Init = DDS4xAD9912.Frequency3;
+						FrequencyDDSBesInit = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 2);
+						FrequencyDDS3Init = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 3);
 						
 						// next step
 						Measuring_3 += 1;
@@ -1137,7 +1168,7 @@
 						}
 						
 						// adjust DDS frequency to keep 55 MHz tracker oscillator locked
-						double fDDS2 = DDS4xAD9912.Frequency2 + 275000 - Ch4;
+						double fDDS2 = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 2) + 275000 - Ch4;
 						SetCtrlVal(MainPanel, PANEL_DDS2, fDDS2);
 						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, fDDS2);
 						
@@ -1414,53 +1445,13 @@
 				char id[7];
 				strftime(id, sizeof(id), "%y%m%d", ltime);
 				
-				int save;
-				
-				// write counter data to disk
-				GetCtrlVal(MainPanel, PANEL_SAVE_RAW_DATA, &save);
-				if (save) {
-					writeData4(DATAFOLDER, "Raw", id, timestr, utc, Ch1, Ch2, Ch3, Ch4);
-				}
-				
-				// write Lo frequency (Math2) to disk
-				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2SAVE, &save);
-				if (save) {
-					writeData(DATAFOLDER, "Lo", id, timestr, utc, Math2);
-				}
-				
-				// write Hg frequency (Math3) to disk
-				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3SAVE, &save);
-				if (save) {
-					writeData(DATAFOLDER, "Hg", id, timestr, utc, Math3);
-					writeData("C:\\Femto\\Results", "Hg", id, timestr, utc, Math3);
-				}
-				
-				// write Sr frequency (Math4) to disk
-				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH4SAVE, &save);
-				if (save) {
-					writeData(DATAFOLDER, "Sr", id, timestr, utc, Math4);
-				}
+				// write datafiles
+				for (struct datafile *d = datafiles; d->data; d++)
+					datafile_append(d, id, timestr);
 				
 				// send Sr frequency (Math4) to Sr data logger
-				GetCtrlVal(MainPanel, PANEL_SR_LOGGER, &save);
-				if (save) {
-					sendLogger("FEMTO2", utc, Math4);
-				}
-				
-				
-				// write ExtraMath (Math5) to disk
-				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH5SAVE, &save);
-				if (save) {
-					writeData(DATAFOLDER, "Ex", id, timestr, utc, Math5);
-				}
-				
-				// write DDS freqs to disk
-				GetCtrlVal(MainPanel, PANEL_SAVE_DDS_FREQS, &save);
-				if (save) {
-					writeData4(DATAFOLDER, "DDS", id, timestr, utc,
-						DDS4xAD9912.Frequency1, DDS4xAD9912.Frequency2,
-						DDS4xAD9912.Frequency3, DDS4xAD9912.Frequency4);
-				}
+				if (Sr_datalogger_enabled)
+					Sr_datalogger_send("FEMTO2", utc, Math4);
 				
 			}		
 			break;
@@ -2236,6 +2227,22 @@
 	return 0;
 }
 
+int CVICALLBACK CB_SaveData (int panel, int control, int event,
+		void *callbackData, int eventData1, int eventData2)
+{
+	switch (event)
+	{
+		case EVENT_COMMIT:
+			for (struct datafile *d = datafiles; d->data; d++) {
+				if (d->control == control)
+					GetCtrlVal(panel, control, &(d->write));
+			}
+			break;
+	}
+	return 0;
+}
+
+
 int CVICALLBACK CB_RecenterInterval (int panel, int control, int event,
 		void *callbackData, int eventData1, int eventData2)
 {