changeset 91:4102fe614df2

Fix timestamping. Cleanup data providers
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Fri, 22 Mar 2013 16:32:15 +0100
parents c9aec93005a4
children 33f0b6206864
files FXAnalyse.c data-provider.h fake-data-provider.c file-data-provider.c future.c future.h kk-data-provider.c
diffstat 7 files changed, 124 insertions(+), 103 deletions(-) [+]
line wrap: on
line diff
--- a/FXAnalyse.c	Fri Mar 22 18:49:58 2013 +0100
+++ b/FXAnalyse.c	Fri Mar 22 16:32:15 2013 +0100
@@ -18,11 +18,14 @@
 
 #define DEDRIFT_DDS_FREQUENCY 70000000
 
-// panels
-static int MainPanel;
-static int CalcNPanel;
-static int EstimateN3Panel;
-static int LoggingPanel;
+// number of channels read
+#define NCHAN 4
+
+// data acquisition event
+struct event {
+	struct timeval time;
+	double data[NCHAN];
+};
 
 // data acquisition status
 int acquiring;
@@ -145,6 +148,13 @@
 double CenteringTimeBegin10K = 0.0;
 
 
+// panels
+static int MainPanel;
+static int CalcNPanel;
+static int EstimateN3Panel;
+static int LoggingPanel;
+
+
 struct stat {
 	int samples;
 	double mean;
@@ -259,10 +269,6 @@
 }
 
 
-#define NCHAN 4
-#define DATA_EVENT_SIZE (NCHAN + 1)
-#define DATA_QUEUE_SIZE (128 * DATA_EVENT_SIZE)
-
 void CVICALLBACK DataAvailableCB (CmtTSQHandle queueHandle, unsigned int event,
 		int value, void *callbackData);
 
@@ -337,10 +343,10 @@
 	mupSetExpr(MathParser5, expr);
 
 	// data queue
-	CmtNewTSQ(DATA_QUEUE_SIZE, sizeof(double), 0, &dataQueue);
+	CmtNewTSQ(128, sizeof(struct event), 0, &dataQueue);
 
 	// register callback to execute when data will be in the data queue
-	CmtInstallTSQCallback(dataQueue, EVENT_TSQ_ITEMS_IN_QUEUE, DATA_EVENT_SIZE,
+	CmtInstallTSQCallback(dataQueue, EVENT_TSQ_ITEMS_IN_QUEUE, 1,
 		DataAvailableCB, NULL, CmtGetCurrentThreadID(), NULL);
 	
 	DisplayPanel(MainPanel);
@@ -548,28 +554,28 @@
 }
 
 
-void CVICALLBACK DataAvailableCB (CmtTSQHandle queueHandle, unsigned int event,
+void CVICALLBACK DataAvailableCB (CmtTSQHandle queueHandle, unsigned int ev,
 		int value, void *callbackData)
 {
-	double data[DATA_EVENT_SIZE];
-	int BoxChecked = FALSE;
+	struct event event;
 	int read;
+	int BoxChecked = FALSE;
 	
-	switch (event) {
+	switch (ev) {
 		case EVENT_TSQ_ITEMS_IN_QUEUE:
 			/* read data from the data queue */
-			while (value >= DATA_EVENT_SIZE) {
+			while (value > 0) {
 				
-				read = CmtReadTSQData(queueHandle, data, DATA_EVENT_SIZE, TSQ_INFINITE_TIMEOUT, 0);
-				if (read != DATA_EVENT_SIZE)
+				read = CmtReadTSQData(queueHandle, &event, 1, TSQ_INFINITE_TIMEOUT, 0);
+				if (read != 1)
 					logmsg("Error!");
 				value = value - read;
 				
-				utc = data[0];
-				Ch1 = data[1];
-				Ch2 = data[2];
-				Ch3 = data[3];
-				Ch4 = data[4];
+				utc = event.time.tv_sec + event.time.tv_usec * 1e-6;
+				Ch1 = event.data[0];
+				Ch2 = event.data[1];
+				Ch3 = event.data[2];
+				Ch4 = event.data[3];
 				
 				SetCtrlVal(MainPanel, PANEL_UTC, utc);
 				SetCtrlVal(MainPanel, PANEL_FREQ1, Ch1);
@@ -1282,29 +1288,39 @@
 					}
 				}
 				
-				int save;
+				// local time
+				struct tm *ltime = localtime(&event.time.tv_sec);
+				// round to milliseconds
+				int msec = round(event.time.tv_usec / 1000.0);
+				while (msec >= 1000) {
+					ltime->tm_sec += 1;
+					msec -= 1000;
+				}
+				// format local time
+				char timestr[24];
+				int len = strftime(timestr, sizeof(timestr), "%d/%m/%Y %H:%M:%S", ltime);
+				snprintf(timestr + len, sizeof(timestr) - len, ".%03d", msec);
+				// display local time
+				SetCtrlVal(MainPanel, PANEL_TIME, timestr);
 				
-				// run id derived from current date in the form YYMMDD
+				// run id derived from current local date in the form YYMMDD
 				char id[7];
-				FormatDateTimeString(utc, "%y%m%d", id, sizeof(id));
+				strftime(id, sizeof(id), "%y%m%d", ltime);
 				
-				// time
-				char timestr[24];
-				FormatDateTimeString(utc, "%d/%m/%Y %H:%M:%S.%3f", timestr, sizeof(timestr));
-				SetCtrlVal(MainPanel, PANEL_TIME, timestr);
+				int save;
 				
 				// write LO frequency (Math2) to disk
 				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2SAVE, &save);
 				if (save) {
 					writeData(DATAFOLDER, "Lo", id, timestr, utc, Math2);
-					writeData("C:\\Femto\\Results", "OptCavity", id, timestr, utc, Math2);
+					writeData("C:\\Femto\\Results", "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", "HgCavity", id, timestr, utc, Math3);
+					writeData("C:\\Femto\\Results", "Hg", id, timestr, utc, Math3);
 				}
 				
 				// write ExtraMath (Math5) to disk
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/data-provider.h	Fri Mar 22 16:32:15 2013 +0100
@@ -0,0 +1,31 @@
+#ifndef __DATA_PROVIDER_H__
+#define __DATA_PROVIDER_H__
+
+#include <future.h>
+
+/* data acquisition flag */
+extern int acquiring;
+
+/* data queue */
+extern CmtTSQHandle dataQueue;
+
+/* callback receiving messages in the main thread */
+void CVICALLBACK MessageCB (void *callbackData);
+
+/* data acquisition event */
+struct event {
+	struct timeval time;
+	double data[4];
+};
+
+/* message */
+static char message[1024]; 
+
+#define SendMessage(threadId, ...)                                \
+	do { 												          \
+		snprintf(message, sizeof(message) - 1, ##__VA_ARGS__);    \
+		PostDeferredCallToThread(MessageCB, message, threadId);   \
+	} while (0)
+
+
+#endif
--- a/fake-data-provider.c	Fri Mar 22 18:49:58 2013 +0100
+++ b/fake-data-provider.c	Fri Mar 22 16:32:15 2013 +0100
@@ -2,17 +2,18 @@
 
 #include <userint.h>
 #include <utility.h>
+#include <future.h>
 
-/* data acquisition flag */
-extern int acquiring;
-/* data queue */
-extern CmtTSQHandle dataQueue;
+#include "data-provider.h"
 
 int CVICALLBACK FakeDataProvider (void *functionData)
 {
 	int mainThreadId;
 	double mark;
-	double data[5] = {0.0, 10000.0, 2.0, 3.0, 4.0};
+	struct event event = {
+		.time = { 0, }, 
+		.data = { 1000.0, 2.0, 3.0, 4.0 }
+	};
 	
 	/* get main thread id to post messages to it */
 	mainThreadId = CmtGetMainThreadID();
@@ -21,11 +22,12 @@
    		mark = Timer();
 		
 		/* update data */
-		GetCurrentDateTime(&data[0]);
-		data[1] = data[1] + 0.1;
+		gettimeofday(&event.time, NULL);
+		//data[0] = time.tv_sec + time.tv_usec * 1e-6;
+		event.data[0] = event.data[0] + 0.1;
 		
 		/* push data into the data queue */
-		CmtWriteTSQData(dataQueue, data, 5, TSQ_INFINITE_TIMEOUT, 0);
+		CmtWriteTSQData(dataQueue, &event, 1, TSQ_INFINITE_TIMEOUT, 0);
 		
 		/* wait till next second */
    		SyncWait(mark, 1.00);
--- a/file-data-provider.c	Fri Mar 22 18:49:58 2013 +0100
+++ b/file-data-provider.c	Fri Mar 22 16:32:15 2013 +0100
@@ -5,24 +5,11 @@
 #include <utility.h>
 #include <formatio.h>
 
+#include "data-provider.h"
+
 //#define LOGFILEPATH "C:\\Femto\\Software\\FXQE80"
 #define LOGFILEPATH "C:\\temp"
 
-/* data acquisition flag */
-extern int acquiring;
-/* data queue */
-extern CmtTSQHandle dataQueue;
-/* callback receiving messages in the main thread */
-void CVICALLBACK MessageCB (void *callbackData);
-/* message */
-static char message[1024];
-
-#define SendMessage(threadId, ...)                                \
-	do { 												          \
-		snprintf(message, sizeof(message) - 1, ##__VA_ARGS__);    \
-		PostDeferredCallToThread(MessageCB, message, threadId);   \
-	} while (0)
-
 
 int CVICALLBACK FileDataProvider (void *functionData)
 {
@@ -33,7 +20,7 @@
 	int year, month, day, hour, min, sec, msec;
 	struct tm t;
 	char line[1024];
-	double data[5];
+	struct event event;
 	double now;
 	
 	/* get main thread id to post messages to it */
@@ -66,7 +53,8 @@
 		
 		/* parse data */
 		read = Scan(line, "%d[w2]%d[w2]%d[w2] %d[w2]%d[w2]%d[w2].%d %f %f %f %f", 
-			&year, &month, &day, &hour, &min, &sec, &msec, &data[1], &data[2], &data[3], &data[4]);
+			&year, &month, &day, &hour, &min, &sec, &msec,
+			&event.data[0], &event.data[1], &event.data[2], &event.data[3]);
 		if (read != 11) {
 			/* notify error to the main thread */
 			SendMessage(mainThreadId, "error parsing data log line");
@@ -82,20 +70,21 @@
 		t.tm_mon = month - 1;
 		/* years since 1900 */
 		t.tm_year = year + 2000 - 1900;
-		/* daylight saving must be set to -1 */
+		/* deduce if daylight saving is in place from the date and time itself */
 		t.tm_isdst = -1;
-			
+		
 		/* convert into seconds since 1 January 1900 00:00:00 and add milliseconds */
-		data[0] = mktime(&t) + 1e-3 * msec;
-				
+		event.time.tv_sec = mktime(&t);
+		event.time.tv_usec = msec * 1000;
+		
 		/* convert from kHz to Hz */
-		data[1] = data[1] * 1000.0;
-		data[2] = data[2] * 1000.0;
-		data[3] = data[3] * 1000.0;
-		data[4] = data[4] * 1000.0;
-			
+		event.data[0] *= 1000.0;
+		event.data[1] *= 1000.0;
+		event.data[2] *= 1000.0;
+		event.data[3] *= 1000.0;
+		
 		/* push data into the data queue */
-		CmtWriteTSQData(dataQueue, data, 5, TSQ_INFINITE_TIMEOUT, 0);
+		CmtWriteTSQData(dataQueue, &event, 1, TSQ_INFINITE_TIMEOUT, 0);
 			
 		/* handle switch to next data file */
 		if ((hour == 23) && (min == 59) && (sec == 59)) {
@@ -108,7 +97,7 @@
 			CloseFile(fd);
 				
 			/* wait for new data file to appear */
-			int retry = 0;
+			int retry = 20;
 			while (retry--) {
 				fd = OpenFile(dataFileName, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
 				if (fd != -1)
--- a/future.c	Fri Mar 22 18:49:58 2013 +0100
+++ b/future.c	Fri Mar 22 16:32:15 2013 +0100
@@ -54,18 +54,3 @@
 	return 0;
 }
 
-
-static struct tm * copy_tm_result(struct tm *dest, struct tm const *src)
-{
-	if (! src)
-		return NULL;
-	*dest = *src;
-	return dest;
-}
-
-
-struct tm * gmtime_r(time_t const *t, struct tm *tp)
-{
-	return copy_tm_result(tp, gmtime(t));
-}
-
--- a/future.h	Fri Mar 22 18:49:58 2013 +0100
+++ b/future.h	Fri Mar 22 16:32:15 2013 +0100
@@ -1,3 +1,8 @@
+#ifndef __FUTURE_H__
+#define __FUTURE_H__
+
+#include <ansi_c.h>
+
 double round(double x);
 
 typedef long int suseconds_t;
@@ -14,4 +19,4 @@
 
 int gettimeofday(struct timeval *tp, struct timezone *tzp);
 
-struct tm * gmtime_r(time_t const *t, struct tm *tp);
+#endif
--- a/kk-data-provider.c	Fri Mar 22 18:49:58 2013 +0100
+++ b/kk-data-provider.c	Fri Mar 22 16:32:15 2013 +0100
@@ -4,24 +4,17 @@
 #include <userint.h>
 #include <formatio.h>
 #include <utility.h>
+
+#include "data-provider.h"
 #include "KKFX80E.h"
 
 #define SERIAL "COM1:115200"
 
-/* data acquisition flag */
-extern int acquiring;
-/* data queue */
-extern CmtTSQHandle dataQueue;
-/* callback receiving messages in the main thread */
-void CVICALLBACK MessageCB (void *callbackData);
-/* message */
-char message[1024];
-
 int CVICALLBACK KKDataProvider (void *functionData)
 {
 	int mainThreadId;
 	char *resp;
-	double data[5];
+	struct event event;
 	
 	/* get main thread id to post messages to it */
 	mainThreadId = CmtGetMainThreadID();
@@ -54,26 +47,26 @@
 		if (strncmp(resp, "2900;", 5) == 0) {
 			
 			/* timestamp */
-			GetCurrentDateTime(&data[0]);
+			gettimeofday(&event.time, NULL);
 			
 			/* parse received data */
-			Scan(resp, "2900; %f; %f; %f; %f", &data[1], &data[2], &data[3], &data[4]);
+			Scan(resp, "2900; %f; %f; %f; %f",
+				&event.data[0], &event.data[1], &event.data[2], &event.data[3]);
 			
 			/* convert from kHz to Hz */
-			data[1] = data[1] * 1000.0;
-			data[2] = data[2] * 1000.0;
-			data[3] = data[3] * 1000.0;
-			data[4] = data[4] * 1000.0;
+			event.data[0] *= 1000.0;
+			event.data[1] *= 1000.0;
+			event.data[2] *= 1000.0;
+			event.data[3] *= 1000.0;
 			
 			/* push data into the data queue */
-			CmtWriteTSQData(dataQueue, data, 5, TSQ_INFINITE_TIMEOUT, 0);
+			CmtWriteTSQData(dataQueue, &event, 1, TSQ_INFINITE_TIMEOUT, 0);
 			
 		} else if (strncmp(resp, "7000;", 5) == 0) {
 			/* ignore heart beat response */
 		} else {
 			/* send message to the main thread */
-			strncpy(message, resp, sizeof(message));
-			PostDeferredCallToThread(MessageCB, message, mainThreadId);
+			SendMessage(mainThreadId, resp);
 		}
 	}