comparison FXAnalyse.c @ 169:97112b45b838

Get Sr data logger parameters from configuration file. Code reorganization.
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Fri, 14 Feb 2014 16:58:19 +0100
parents 4de7f2c9f328
children a4fcebb5941b
comparison
equal deleted inserted replaced
168:4de7f2c9f328 169:97112b45b838
15 #include "muParserDLL.h" 15 #include "muParserDLL.h"
16 #include "utils.h" 16 #include "utils.h"
17 #include "stat.h" 17 #include "stat.h"
18 #include "future.h" 18 #include "future.h"
19 #include "data-provider.h" 19 #include "data-provider.h"
20 20 #include "sr-data-logger.h"
21 21 #include "config.h"
22 #define CONFIGFILE "FXAnalyse.ini" 22 #include "logging.h"
23 #define SR_LOGGER_IP "145.238.204.91" 23
24
24 #define DATAFOLDER "Z:\\Measures-2014" 25 #define DATAFOLDER "Z:\\Measures-2014"
25 26
26 #define FREP_STEP_SIZE 50000.0 27 #define FREP_STEP_SIZE 50000.0
27 28
28 #define SPEED_OF_LIGHT 299792458.0 // m/s 29 #define SPEED_OF_LIGHT 299792458.0 // m/s
49 double Ch1, Ch2, Ch3, Ch4; 50 double Ch1, Ch2, Ch3, Ch4;
50 double Math1, Math2, Math3, Math4, Math5; 51 double Math1, Math2, Math3, Math4, Math5;
51 double N1, N2, N3; 52 double N1, N2, N3;
52 double Ndiv = 8.0; 53 double Ndiv = 8.0;
53 double Sign1 = 1.0, Sign2 = 1.0, Sign3 = 0.0; 54 double Sign1 = 1.0, Sign2 = 1.0, Sign3 = 0.0;
54
55
56 void *MathParser1, *MathParser2, *MathParser3, *MathParser4, *MathParser5; 55 void *MathParser1, *MathParser2, *MathParser3, *MathParser4, *MathParser5;
57 56
58 57
59 // panels 58 // panels
60 static int MainPanel; 59 static int MainPanel;
61 static int LoggingPanel;
62 static int CalcNPanel; 60 static int CalcNPanel;
63 static int EstimateNPanel; 61 static int EstimateNPanel;
64 62
65 63
66 struct adev { 64 struct adev {
332 if (d->write) 330 if (d->write)
333 write_data(DATAFOLDER, d->name, id, timestr, utc, d->data, d->nchan); 331 write_data(DATAFOLDER, d->name, id, timestr, utc, d->data, d->nchan);
334 } 332 }
335 333
336 334
337 void logmsg(const char *frmt, ...) 335 static struct datalogger datalogger;
338 {
339 char msg[1024];
340 int len = 0;
341
342 // timestamp
343 len += sprintf(msg, "%014.3f ", utc);
344 time_t now = time(NULL);
345 struct tm *t = localtime(&now);
346 len += strftime(msg + len, sizeof(msg) - len, "%d-%m-%Y %H:%M:%S ", t);
347
348 // message
349 va_list args;
350 va_start(args, frmt);
351 len += vsnprintf(msg + len, sizeof(msg) - len, frmt, args);
352 va_end(args);
353
354 // add newline
355 len = MIN(len, sizeof(msg) - 2);
356 msg[len] = '\n';
357 msg[len + 1] = '\0';
358
359 // display message
360 SetCtrlVal(LoggingPanel, LOGGING_LOGGING, msg);
361 }
362
363
364 int Sr_datalogger_enabled = FALSE;
365
366
367 // Sr data logger
368 int Sr_datalogger_send(const char* id, double utc, double data)
369 {
370 static unsigned int handle = 0;
371 char buffer[1024];
372
373 // try to connect and quit if unsuccessfull
374 if (handle == 0) {
375 if (ConnectToTCPServer(&handle, 3491, SR_LOGGER_IP, NULL, NULL, 1) < 0) {
376 logmsg("Sr data logger connection error");
377 return -1;
378 }
379 logmsg("connected to Sr data logger");
380 }
381
382 snprintf(buffer, sizeof(buffer), "%s %.7f %.8f", id, utc2mjd(utc), data);
383 if (ClientTCPWrite(handle, buffer, strlen(buffer) + 1, 0) < 0) {
384 // try to reconnect and resend
385 handle = 0;
386 Sr_datalogger_send(id, utc, data);
387 }
388
389 return 0;
390 }
391 336
392 337
393 void * muParserNew() 338 void * muParserNew()
394 { 339 {
395 void *parser = mupCreate(); 340 void *parser = mupCreate();
439 double frequency; 384 double frequency;
440 char expr[1024]; 385 char expr[1024];
441 386
442 if ((MainPanel = LoadPanel (0, "FXAnalyse.uir", PANEL)) < 0) 387 if ((MainPanel = LoadPanel (0, "FXAnalyse.uir", PANEL)) < 0)
443 return -1; 388 return -1;
444 if ((LoggingPanel = LoadPanel (0, "FXAnalyse.uir", LOGGING)) < 0)
445 return -1;
446 if ((CalcNPanel = LoadPanel (MainPanel, "FXAnalyse.uir", CALCN)) < 0) 389 if ((CalcNPanel = LoadPanel (MainPanel, "FXAnalyse.uir", CALCN)) < 0)
447 return -1; 390 return -1;
448 if ((EstimateNPanel = LoadPanel (MainPanel, "FXAnalyse.uir", ESTIMATEN)) < 0) 391 if ((EstimateNPanel = LoadPanel (MainPanel, "FXAnalyse.uir", ESTIMATEN)) < 0)
449 return -1; 392 return -1;
450 393
451 // construct configuration file path 394 // logging
452 char pathname[MAX_PATHNAME_LEN]; 395 logger_init();
453 char project[MAX_PATHNAME_LEN]; 396
454 GetProjectDir(project);
455 MakePathname(project, CONFIGFILE, pathname);
456
457 // load configuration file 397 // load configuration file
398 char path[MAX_PATHNAME_LEN];
399 GetIniFilePath(path);
458 IniText configuration = Ini_New(TRUE); 400 IniText configuration = Ini_New(TRUE);
459 Ini_ReadFromFile(configuration, pathname); 401 Ini_ReadFromFile(configuration, path);
460 402
461 // get AD9956 configuration parameters 403 // get AD9956 configuration parameters
462 char host[256]; 404 char host[256];
463 int port; 405 int port;
464 double clock; 406 double clock;
473 return -1; 415 return -1;
474 416
475 // dispose configuration 417 // dispose configuration
476 Ini_Dispose(configuration); 418 Ini_Dispose(configuration);
477 419
420 // Sr data logger
421 sr_datalogger_init(&datalogger);
422
478 // dedrift DDS 423 // dedrift DDS
479 DDSFox_Initialize(&DDS1xAD9956, host, port, dedrift.freq0); 424 DDSFox_Initialize(&DDS1xAD9956, host, port, dedrift.freq0);
480 425
481 // initialize 4x AD9959 DDS box 426 // initialize 4x AD9959 DDS box
482 DDS4xAD9912_Reset(&DDS4xAD9912); 427 DDS4xAD9912_Reset(&DDS4xAD9912);
617 switch (event) 562 switch (event)
618 { 563 {
619 case EVENT_COMMIT: 564 case EVENT_COMMIT:
620 if (acquiring) 565 if (acquiring)
621 break; 566 break;
622 567
623 logmsg("start"); 568 logmsg("start");
624 SetCtrlAttribute(panel, PANEL_STARTBUTTON, ATTR_DIMMED, TRUE); 569 SetCtrlAttribute(panel, PANEL_STARTBUTTON, ATTR_DIMMED, TRUE);
625 acquiring = 1; 570 acquiring = 1;
626 571
627 // start data provider thread 572 // start data provider thread
1322 // write datafiles 1267 // write datafiles
1323 for (struct datafile *d = datafiles; d->data; d++) 1268 for (struct datafile *d = datafiles; d->data; d++)
1324 datafile_append(d, id, timestr); 1269 datafile_append(d, id, timestr);
1325 1270
1326 // send Sr frequency (Math4) to Sr data logger 1271 // send Sr frequency (Math4) to Sr data logger
1327 if (Sr_datalogger_enabled) 1272 sr_datalogger_send(&datalogger, utc, Math4);
1328 Sr_datalogger_send("FEMTO2", utc, Math4);
1329 } 1273 }
1330 break; 1274 break;
1331 } 1275 }
1332 } 1276 }
1333 1277
2053 int visible; 1997 int visible;
2054 switch (event) 1998 switch (event)
2055 { 1999 {
2056 case EVENT_COMMIT: 2000 case EVENT_COMMIT:
2057 GetCtrlVal(panel, PANEL_SHOWLOG, &visible); 2001 GetCtrlVal(panel, PANEL_SHOWLOG, &visible);
2058 SetPanelAttribute(LoggingPanel, ATTR_VISIBLE, visible); 2002 logger_panel_visible(visible);
2059 break; 2003 break;
2060 } 2004 }
2061 return 0; 2005 return 0;
2062 } 2006 }
2063 2007
2065 void *callbackData, int eventData1, int eventData2) 2009 void *callbackData, int eventData1, int eventData2)
2066 { 2010 {
2067 switch (event) 2011 switch (event)
2068 { 2012 {
2069 case EVENT_CLOSE: 2013 case EVENT_CLOSE:
2070 SetPanelAttribute(LoggingPanel, ATTR_VISIBLE, 0); 2014 logger_panel_visible(0);
2071 SetCtrlVal(MainPanel, PANEL_SHOWLOG, 0); 2015 SetCtrlVal(MainPanel, PANEL_SHOWLOG, 0);
2072 break; 2016 break;
2073 } 2017 }
2074 return 0; 2018 return 0;
2075 } 2019 }
2153 void *callbackData, int eventData1, int eventData2) 2097 void *callbackData, int eventData1, int eventData2)
2154 { 2098 {
2155 switch (event) 2099 switch (event)
2156 { 2100 {
2157 case EVENT_COMMIT: 2101 case EVENT_COMMIT:
2158 GetCtrlVal(panel, control, &Sr_datalogger_enabled); 2102 GetCtrlVal(panel, control, &datalogger.enabled);
2159 break; 2103 break;
2160 } 2104 }
2161 return 0; 2105 return 0;
2162 } 2106 }
2163 int CVICALLBACK CB_DedriftDDSFreq (int panel, int control, int event, 2107 int CVICALLBACK CB_DedriftDDSFreq (int panel, int control, int event,