4040
4141const int chipSelect = BUILTIN_SDCARD; // HW pin for micro SD adaptor CS
4242File dataFile; // The file on SD
43+ File configFile; // The setup configuration file on SD
4344bool 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)
4446Sd2Card card; // The SD Card
4547SdVolume volume; // The volume (partition) on SD
4648double iTAS, ip1TAS, res, iof; // Accessory variables for Air Data calculations
@@ -52,6 +54,7 @@ SSC absp(0x28, 1); // Create an SSC sensor with I2C address 0x28 on I
5254
5355String message_BT; // string that stores the incoming BT message
5456String message_COM; // string that stores the incoming COM message
57+ String message_CFG; // string that stores the configuration messages
5558const int ledPin = 13 ; // The led is ON when the application is logging on SD
5659
5760float acquisition_freq = 1 ; // The minimum frequency of acquisition = 1 Hz (1 s)
@@ -124,6 +127,11 @@ void setup() {
124127 strcpy (Data[0 ]," \0 " ); // Initialize the Data
125128 strcpy (Data[1 ]," \0 " ); // Initialize the Data
126129 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+ }
127135}
128136
129137
@@ -326,6 +334,7 @@ void sendtosd(void)
326334void loop () {
327335 bool endmsg_BT = false ; // it becomes true when a message is received from BLUETOOTH
328336 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
329338 char workbuff[BUFFERLENGTH] = " " ; // General purpose buffer, used for itoa conversion
330339 char Message[BUFFERLENGTH] = " " ; // it contains the message to be processed
331340 char Answer[BUFFERLENGTH] = " " ;
@@ -360,37 +369,51 @@ void loop() {
360369 }
361370
362371
363- // 1) Read input from Serial and Bluetooth:
364-
365- while (Serial.available ()) { // while there is SERIAL data available
366- char ch = Serial.read ();
367- if (ch != DELIMITER) message_COM += char (ch); // Store string from serial command
368- else endmsg_COM = true ;
369- }
370- if (endmsg_COM) { // This comes from the Serial port (USB)
371- if (message_COM != " " ) { // if data is available
372- message_COM.toCharArray (Message, BUFFERLENGTH-1 );
373- 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 ;
374379 }
375- }
376- if (!endmsg_COM) {
377- while (Serial1.available ()) { // while there is BLUETOOTH data available
378- char ch = Serial1.read ();
379- if (ch != DELIMITER) message_BT += char (ch); // Store string from BT command
380- else endmsg_BT = true ;
380+ if (!configFile.available ()) {
381+ loadConfiguration = false ;
382+ configFile.close ();
383+ endmsg_CFG = true ;
381384 }
382- if (endmsg_BT) { // This comes from the bluetooth device (such as phone)
383- if (message_BT != " " ) { // if data is available
384- message_BT.toCharArray (Message, BUFFERLENGTH-1 );
385- message_BT = " " ;
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+ }
398+ }
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+ }
386410 }
387411 }
388412 }
389413
390-
391414 // 2) Process messages
392415
393- if (endmsg_COM || endmsg_BT) {
416+ if (endmsg_COM || endmsg_BT || endmsg_CFG ) {
394417 char *command = strtok (Message, SEPARATOR);
395418
396419 // --------------------------------------------------
0 commit comments