changeset 250:9b35a2b2c759

Implement selection of gate type (PI or LAMBDA) in configuration file
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 04 May 2015 17:48:07 +0200
parents b3581a75a0b7
children 307fd62f376d
files kk-data-provider.c
diffstat 1 files changed, 46 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/kk-data-provider.c	Mon May 04 17:47:18 2015 +0200
+++ b/kk-data-provider.c	Mon May 04 17:48:07 2015 +0200
@@ -10,14 +10,30 @@
 #include "data-provider.h"
 #include "KKFX80E.h"
 
+
+#define FX_MODE_PI 0x02
+#define FX_MODE_LAMBDA 0x03
+
 #define DEFAULT_PORT "COM4:115200"
 #define DEFAULT_NCHAN 4
+#define DEFAULT_MODE FX_MODE_PI
 #define TIMEOUT 1 /* seconds */
 #define MAXRETRY 100
 
 #define strneq(a, b, len) (strncmp((a), (b), (len)) == 0)
 
 
+const char * FX_ModeString(int mode)
+{
+	const static char *modes[] = {
+		[FX_MODE_PI] = "PI",
+		[FX_MODE_LAMBDA] = "LAMBDA",
+	};
+
+	return modes[mode];
+}
+
+
 static char * FX_Command(unsigned char cmd, unsigned mask, unsigned value)
 {
 	char *resp;
@@ -49,9 +65,12 @@
 	int main_thread_id;
 	int rv;
 	char *resp;
+	int nchan = DEFAULT_NCHAN;
+	int mode = DEFAULT_MODE;
+	char port[256];
+	char buffer[16];
+	char header[16];
 	struct event event;
-	int nchan;
-	char port[256];
 	
 	/* get main thread id to post messages to it */
 	main_thread_id = CmtGetMainThreadID();
@@ -64,10 +83,21 @@
 	IniText configuration = Ini_New(TRUE);
 	Ini_ReadFromFile(configuration, path);
 
-	/* get serial port name configuration */
+	/* serial port name */
 	rv = Ini_GetStringIntoBuffer(configuration, "KK", "port", port, sizeof(port));
 	if (rv < 1)
 		strncpy(port, DEFAULT_PORT, sizeof(port));
+
+	/* gate type */
+	rv = Ini_GetStringIntoBuffer(configuration, "KK", "mode", buffer, sizeof(buffer));
+	if (rv < 1)
+		mode = DEFAULT_MODE;
+	else if (stricmp(buffer, "PI") == 0)
+		mode = FX_MODE_PI;
+	else if (stricmp(buffer, "LAMBDA") == 0)
+		mode = FX_MODE_LAMBDA;
+	else
+		send_message(main_thread_id, "KK Counter: unrecognised mode parameter '%s'", buffer);
 	
 	/* channel number */
 	rv = Ini_GetInt(configuration, "KK", "nchan", &nchan);
@@ -93,7 +123,7 @@
 		send_message(main_thread_id, FX_Error());
 		goto error;
 	}
-	send_message(main_thread_id, "KK Counter version: %s", resp + 4);
+	send_message(main_thread_id, "KK Counter: version %s", resp + 4);
 	
 	/* set report interval 1sec */
 	resp = FX_Command(0x29, 0x0F00, 0x0900);
@@ -102,22 +132,23 @@
 		goto error;
 	}
 	
-	/* set mode to instantaneous frequency measurement */
-	resp = FX_Command(0x42, 0x6F00, 0x2900);
+	/* set mode */
+	resp = FX_Command(0x40 + mode, 0x7000, (mode << 12));
 	if (! resp) {
 		send_message(main_thread_id, FX_Error());
 		goto error;
 	}
-
+	send_message(main_thread_id, "KK Counter: set mode %s", FX_ModeString(mode));
+	
 	/* disable scrambler */
-	resp = FX_Command(0x50, 0x6FFF, 0x2900);
+	resp = FX_Command(0x50, 0xFFFF, (mode << 12) + 0x0900);
 	if (! resp) {
  		send_message(main_thread_id, FX_Error());
  		goto error;
  	}
 	
 	/* read nchan channels */
-	resp = FX_Command(0x30 + nchan, 0x6FFF, 0x2900);
+	resp = FX_Command(0x30 + nchan, 0xFFFF, (mode << 12) + 0x0900);
 	if (! resp) {
 		send_message(main_thread_id, FX_Error());
 		goto error;
@@ -130,6 +161,9 @@
 		goto error;
 	}
 	
+	/* expected packet header */
+	snprintf(header, sizeof(header), "%X", (mode << 12) + 0x0900);
+	
 	while (acquiring) {
 		/* receive data from counter */
 		FX_Recv(&resp, TIMEOUT);
@@ -139,7 +173,7 @@
 		}
 		
 		/* data packets */
-		if (strneq(resp, "2900", 2)) {
+		if (strneq(resp, header, 4)) {
 			
 			/* timestamp */
 			gettimeofday(&event.time, NULL);
@@ -169,10 +203,10 @@
 		} else if (strneq(resp, "7020", 4)) {
 			/* undocumented packet. it probably reports the header for
 			   subsequent data packets */
-			send_message(main_thread_id, "KK Counter packet header: %s", resp + 7);
+			send_message(main_thread_id, "KK Counter: packet header %s", resp + 7);
 		} else if (strneq(resp, "7F51", 4)) {
 			/* measurement interval synchronized */
-			send_message(main_thread_id, "KK Counter measurement interval synchronized");
+			send_message(main_thread_id, "KK Counter: measurement interval synchronized");
 		} else {
 			/* send message to the main thread */
 			send_message(main_thread_id, "KK Counter: %s", resp);