3232#define BUFFERLENGTH 512 // The length of string buffers
3333#define DELIMITER ' \n '
3434#define SEPARATOR " ,"
35+ #define LEAVE_AS_IS " ="
3536#define ADC_NAME " ASGARD" // The name of the ADC device
3637#define PROTOCOL_VERSION " 0.4" // The version of the communication protocol
3738#define DEFAULT_LOGFILE " DATALOG.CSV" // The default log file name
3839#define DEFAULT_CONFIGFILE " CONFIG.CFG" // The configuration file name
3940
4041const int chipSelect = BUILTIN_SDCARD; // HW pin for micro SD adaptor CS
4142File dataFile; // The file on SD
43+ File configFile; // The setup configuration file on SD
4244bool isSDCardPresent; // The presence of a (formatted) card into the slot of SD
45+ bool loadConfiguration; // The flag is true if the config file is present into SD (and must be loaded on start)
4346Sd2Card card; // The SD Card
4447SdVolume volume; // The volume (partition) on SD
4548double iTAS, ip1TAS, res, iof; // Accessory variables for Air Data calculations
@@ -51,6 +54,7 @@ SSC absp(0x28, 1); // Create an SSC sensor with I2C address 0x28 on I
5154
5255String message_BT; // string that stores the incoming BT message
5356String message_COM; // string that stores the incoming COM message
57+ String message_CFG; // string that stores the configuration messages
5458const int ledPin = 13 ; // The led is ON when the application is logging on SD
5559
5660float acquisition_freq = 1 ; // The minimum frequency of acquisition = 1 Hz (1 s)
@@ -123,6 +127,11 @@ void setup() {
123127 strcpy (Data[0 ]," \0 " ); // Initialize the Data
124128 strcpy (Data[1 ]," \0 " ); // Initialize the Data
125129 p_Data = &Data[1 ][0 ]; // The current valid status index is 1
130+
131+ if (isSDCardPresent) { // Searches the configuration file
132+ configFile = SD.open (DEFAULT_CONFIGFILE);
133+ if (configFile) loadConfiguration = true ;
134+ }
126135}
127136
128137
@@ -150,7 +159,7 @@ void acquisition()
150159 }
151160
152161 // Differential Pressure sensor
153- if (AirDataComputer._status [AIRDC_STATUS_TDELTAP ] == ' 1' ) { // Differential pressure sensor present
162+ if (AirDataComputer._status [AIRDC_STATUS_DELTAP ] == ' 1' ) { // Differential pressure sensor present
154163 diffp.update ();
155164 AirDataComputer._qc = diffp.pressure ();
156165 AirDataComputer._qcRaw = diffp.pressure_Raw ();
@@ -325,6 +334,7 @@ void sendtosd(void)
325334void loop () {
326335 bool endmsg_BT = false ; // it becomes true when a message is received from BLUETOOTH
327336 bool endmsg_COM = false ; // it becomes true when a message is received from SERIAL
337+ bool endmsg_CFG = false ; // it becomes true when a message is received from CONFIGURATION FILE
328338 char workbuff[BUFFERLENGTH] = " " ; // General purpose buffer, used for itoa conversion
329339 char Message[BUFFERLENGTH] = " " ; // it contains the message to be processed
330340 char Answer[BUFFERLENGTH] = " " ;
@@ -359,37 +369,51 @@ void loop() {
359369 }
360370
361371
362- // 1) Read input from Serial and Bluetooth:
363-
364- while (Serial.available ()) { // while there is SERIAL data available
365- char ch = Serial.read ();
366- if (ch != DELIMITER) message_COM += char (ch); // Store string from serial command
367- else endmsg_COM = true ;
368- }
369- if (endmsg_COM) { // This comes from the Serial port (USB)
370- if (message_COM != " " ) { // if data is available
371- message_COM.toCharArray (Message, BUFFERLENGTH-1 );
372- message_COM = " " ;
372+ // 1) Read input from Configuration file, Serial, and Bluetooth:
373+
374+ if (loadConfiguration) {
375+ while (configFile.available () && !endmsg_CFG) {
376+ char ch = configFile.read ();
377+ if ((ch != DELIMITER) && (ch != ' \r ' )) message_CFG += char (ch); // Store string from CFG file
378+ else endmsg_CFG = true ;
373379 }
374- }
375- if (!endmsg_COM) {
376- while (Serial1.available ()) { // while there is BLUETOOTH data available
377- char ch = Serial1.read ();
378- if (ch != DELIMITER) message_BT += char (ch); // Store string from BT command
379- else endmsg_BT = true ;
380+ if (!configFile.available ()) {
381+ loadConfiguration = false ;
382+ configFile.close ();
383+ endmsg_CFG = true ;
384+ }
385+ message_CFG.toCharArray (Message, BUFFERLENGTH-1 );
386+ message_CFG = " " ;
387+ } else {
388+ while (Serial.available ()) { // while there is SERIAL data available
389+ char ch = Serial.read ();
390+ if (ch != DELIMITER) message_COM += char (ch); // Store string from serial command
391+ else endmsg_COM = true ;
392+ }
393+ if (endmsg_COM) { // This comes from the Serial port (USB)
394+ if (message_COM != " " ) { // if data is available
395+ message_COM.toCharArray (Message, BUFFERLENGTH-1 );
396+ message_COM = " " ;
397+ }
380398 }
381- if (endmsg_BT) { // This comes from the bluetooth device (such as phone)
382- if (message_BT != " " ) { // if data is available
383- message_BT.toCharArray (Message, BUFFERLENGTH-1 );
384- message_BT = " " ;
399+ if (!endmsg_COM) {
400+ while (Serial1.available ()) { // while there is BLUETOOTH data available
401+ char ch = Serial1.read ();
402+ if (ch != DELIMITER) message_BT += char (ch); // Store string from BT command
403+ else endmsg_BT = true ;
404+ }
405+ if (endmsg_BT) { // This comes from the bluetooth device (such as phone)
406+ if (message_BT != " " ) { // if data is available
407+ message_BT.toCharArray (Message, BUFFERLENGTH-1 );
408+ message_BT = " " ;
409+ }
385410 }
386411 }
387412 }
388413
389-
390414 // 2) Process messages
391415
392- if (endmsg_COM || endmsg_BT) {
416+ if (endmsg_COM || endmsg_BT || endmsg_CFG ) {
393417 char *command = strtok (Message, SEPARATOR);
394418
395419 // --------------------------------------------------
@@ -463,6 +487,7 @@ void loop() {
463487 // #5 – STS – STATUS_SET --> Reply STA - STATUS_ASSERT
464488 // --------------------------------------------------
465489 // $STS,0,1,1,1,1,1,0,0,0
490+ // $STS,=,=,=,0,=,=,=,=,= Deactivate the external temperature sensor. Don't touch other fields
466491
467492 if (!strcmp (command, " $STS" )) {
468493 int i;
@@ -522,26 +547,50 @@ void loop() {
522547 // --------------------------------------------------
523548 // #8 – DTS – DATA_SET
524549 // --------------------------------------------------
525- // $DTS,0,0,0,0,0,0,0,0 ,300,0 Sets the external temperature to 300 [K] (if the sensor is not present)
550+ // $DTS,=,=,=,=,=,=,=,= ,300 Sets the external temperature to 300 [K] (if the sensor is not present)
526551
527552 if (!strcmp (command, " $DTS" )) {
528553 int i;
529554 i=0 ;
530555 for (i=0 ; i<AIRDC_DATA_VECTOR_SIZE; i++) { // Check the fields
531556 command = strtok (NULL , SEPARATOR);
532557 if (strlen (command)<1 ) goto endeval;
533-
534- switch (i) {
535- case AIRDC_DATA_TAT:
536- if (AirDataComputer._status [AIRDC_STATUS_TAT] == ' 0' ) {
537- AirDataComputer._TAT = atol (command);
538- AirDataComputer._TRaw = 0l ;
539- }
540- break ;
558+ if (strcmp (command, LEAVE_AS_IS)) {
559+ switch (i) {
560+ case AIRDC_DATA_TAT: // External Temperature
561+ if (AirDataComputer._status [AIRDC_STATUS_TAT] == ' 0' ) {
562+ AirDataComputer._TAT = atol (command);
563+ AirDataComputer._TRaw = 0l ;
564+ }
565+ break ;
566+ case AIRDC_DATA_QC: // Differential pressure
567+ if (AirDataComputer._status [AIRDC_STATUS_DELTAP] == ' 0' ) {
568+ AirDataComputer._qc = atol (command);
569+ AirDataComputer._qcRaw = 0l ;
570+ }
571+ break ;
572+ case AIRDC_DATA_TDELTAP: // Temperature differential pressure sensor
573+ if (AirDataComputer._status [AIRDC_STATUS_TDELTAP] == ' 0' ) {
574+ AirDataComputer._Tdeltap = atol (command);
575+ AirDataComputer._TdeltapRaw = 0l ;
576+ }
577+ break ;
578+ case AIRDC_DATA_P: // Absolute pressure
579+ if (AirDataComputer._status [AIRDC_STATUS_P] == ' 0' ) {
580+ AirDataComputer._p = atol (command);
581+ AirDataComputer._pRaw = 0l ;
582+ }
583+ break ;
584+ case AIRDC_DATA_TABSP: // Temperature absolute pressure sensor
585+ if (AirDataComputer._status [AIRDC_STATUS_TABSP] == ' 0' ) {
586+ AirDataComputer._Tabsp = atol (command);
587+ AirDataComputer._TabspRaw = 0l ;
588+ }
589+ break ;
590+ default : // IT DOES NOTHING
591+ break ;
541592
542- default :
543- break ;
544- // IT DOES NOTHING
593+ }
545594 }
546595 }
547596 goto endeval;
@@ -556,38 +605,40 @@ void loop() {
556605 // $DTQ,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
557606 // Request time, deltap, p, ext temperature
558607 // $DTQ,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
608+ // Toggle ON the ext temperature field
609+ // $DTQ,=,=,=,=,=,=,=,=,1
559610 // Request a $DTA string
560611 // $DTQ
561612
562613 if (!strcmp (command, " $DTQ" )) {
563- strcpy (Answer, p_Data);
614+ if (!strcmp (command, " $DTQ," )) {
615+ strcpy (Answer, p_Data); // Simply return the data
616+ goto endeval;
617+ }
618+ strcpy (Answer, " " );
564619 int i;
565620 i=0 ;
566621 for (i=0 ; i<AIRDC_DATA_VECTOR_SIZE; i++) {
567622 command = strtok (NULL , SEPARATOR);
568623 if (strlen (command)<1 ) goto endeval;
569- AirDataComputer._datasel [i]=command[0 ];
570- workbuff[2 *i]=command[0 ];
571- workbuff[2 *i+1 ]=' ,' ;
624+ if (strcmp (command, LEAVE_AS_IS)) AirDataComputer._datasel [i]=command[0 ];
572625 }
573- workbuff[2 *i-1 ]=' \0 ' ;
574- strcpy (Answer, " " );
575626
576627 // Save to the SD the configuration data
577- SDCardCheck ();
578- if (isSDCardPresent) {
579- SD.remove (DEFAULT_CONFIGFILE);
580- File dataFile = SD.open (DEFAULT_CONFIGFILE, FILE_WRITE);
581- if (dataFile) { // if the file is available, write to it:
582- AirDataComputer._status [AIRDC_STATUS_SD] = ' 1' ; // SD Card present
583- dataFile.print (" $DTQ," );
584- dataFile.println (workbuff);
585- dataFile.close ();
586- } else { // if the file isn't open, pop up an error:
587- AirDataComputer._status [AIRDC_STATUS_SD] = ' 0' ; // SD Card not present
588- goto endeval;
589- }
590- }
628+ // SDCardCheck();
629+ // if (isSDCardPresent) {
630+ // SD.remove(DEFAULT_CONFIGFILE);
631+ // File dataFile = SD.open(DEFAULT_CONFIGFILE, FILE_WRITE);
632+ // if (dataFile) { // if the file is available, write to it:
633+ // AirDataComputer._status[AIRDC_STATUS_SD] = '1'; // SD Card present
634+ // dataFile.print("$DTQ,");
635+ // dataFile.println(workbuff);
636+ // dataFile.close();
637+ // } else { // if the file isn't open, pop up an error:
638+ // AirDataComputer._status[AIRDC_STATUS_SD] = '0'; // SD Card not present
639+ // goto endeval;
640+ // }
641+ // }
591642 // Modified #10 reply message. No data will be transmitted. It is a plain acknowledge.
592643 // strcpy (outstr,"$DTA,");
593644 // strcat (Answer, workbuff);
@@ -663,18 +714,18 @@ void loop() {
663714 // --------------------------------------------------
664715 // $DFS,Serial_freq,Bluetooth_freq,SD_freq <-- SPECS CHANGED
665716 // $DFS,5,0.5,0 Serial = 1 Hz Bluetooth = 0.5 Hz No SD logging
666- // $DFS,-1 ,0.5,5 Serial = leave as is Bluetooth = 0.5 Hz SD = 5 Hz
717+ // $DFS,= ,0.5,5 Serial = leave as is Bluetooth = 0.5 Hz SD = 5 Hz
667718
668719 if (!strcmp (command, " $DFS" )) { // $DFS,Serial,Bluetooth,SD
669720 command = strtok (NULL , SEPARATOR);
670721 if (command != NULL ) {
671- float DataFrequency_COM = atof ( command); // Read the value after the comma
722+ float DataFrequency_COM = ( strcmp ( command, LEAVE_AS_IS) ? atof (command) : - 1 ); // Read the freq value
672723 command = strtok (NULL , SEPARATOR);
673724 if (command != NULL ) {
674- float DataFrequency_BT = atof ( command); // Read the value after the comma
725+ float DataFrequency_BT = ( strcmp ( command, LEAVE_AS_IS) ? atof (command) : - 1 ); // Read the freq value
675726 command = strtok (NULL , SEPARATOR);
676727 if (command != NULL ) {
677- float DataFrequency_SD = atof ( command); // Read the value after the comma
728+ float DataFrequency_SD = ( strcmp ( command, LEAVE_AS_IS) ? atof (command) : - 1 ); // Read the freq value
678729 // Change frequencies
679730 if ((DataFrequency_COM >= 0 ) && (DataFrequency_COM != sendtoserial_freq)) {
680731 sendtoserial_freq = DataFrequency_COM;
0 commit comments