Template
fixed loop data persistance
This commit is contained in:
@@ -51,6 +51,7 @@ bool LED_ON = false;
|
|||||||
|
|
||||||
|
|
||||||
// https://www.pjrc.com/teensy/td_uart.html
|
// https://www.pjrc.com/teensy/td_uart.html
|
||||||
|
// these buffers are used by the UART interrupt
|
||||||
#define Serial1RxBufferSIZE 1024 // 10 seconds
|
#define Serial1RxBufferSIZE 1024 // 10 seconds
|
||||||
char Serial1RxBuffer[Serial1RxBufferSIZE]; // global scope or static char. power of 2.
|
char Serial1RxBuffer[Serial1RxBufferSIZE]; // global scope or static char. power of 2.
|
||||||
|
|
||||||
@@ -93,12 +94,15 @@ void setup()
|
|||||||
|
|
||||||
if (DEBUG) RTCDisplay();
|
if (DEBUG) RTCDisplay();
|
||||||
|
|
||||||
|
// Serial1 Rx @ Tx (0), Tx @ Tx (1)
|
||||||
Serial1.begin(9600); // MUST match Garmin
|
Serial1.begin(9600); // MUST match Garmin
|
||||||
Serial1.addMemoryForRead(Serial1RxBuffer, Serial1RxBufferSIZE);
|
Serial1.addMemoryForRead(Serial1RxBuffer, Serial1RxBufferSIZE);
|
||||||
|
|
||||||
|
// Serial2 Rx @ Rx1 (16), Tx @ Tx1 (17)
|
||||||
Serial2.begin(115200); // MUST match Dynon EFIS
|
Serial2.begin(115200); // MUST match Dynon EFIS
|
||||||
Serial2.addMemoryForRead(Serial2RxBuffer, Serial2RxBufferSIZE);
|
Serial2.addMemoryForRead(Serial2RxBuffer, Serial2RxBufferSIZE);
|
||||||
|
|
||||||
|
// Serial3 Rx @ A1 (15), Tx @ A0 (14)
|
||||||
Serial3.begin(115200); // MUST match Dynon EMS
|
Serial3.begin(115200); // MUST match Dynon EMS
|
||||||
Serial3.addMemoryForRead(Serial3RxBuffer, Serial3RxBufferSIZE);
|
Serial3.addMemoryForRead(Serial3RxBuffer, Serial3RxBufferSIZE);
|
||||||
|
|
||||||
@@ -120,7 +124,7 @@ void setup()
|
|||||||
if (DEBUG) Serial.printf(F(" fs started: %d\n"), myDrive1.filesystemsStarted());
|
if (DEBUG) Serial.printf(F(" fs started: %d\n"), myDrive1.filesystemsStarted());
|
||||||
|
|
||||||
if (DEBUG) Serial.printf("Find next file number...\n");
|
if (DEBUG) Serial.printf("Find next file number...\n");
|
||||||
i=next_file_number();
|
i=next_file_number(); /* global access to myFiles1 */
|
||||||
sprintf(fname,"Log%04d.txt",i);
|
sprintf(fname,"Log%04d.txt",i);
|
||||||
if (DEBUG) Serial.printf("Testing for file %s\n",fname);
|
if (DEBUG) Serial.printf("Testing for file %s\n",fname);
|
||||||
|
|
||||||
@@ -144,7 +148,7 @@ void setup()
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
int next_file_number() {
|
int next_file_number() { /* global access to myFiles1 */
|
||||||
// returns the first unused file number
|
// returns the first unused file number
|
||||||
char fname[16];
|
char fname[16];
|
||||||
bool fileexists = true; // assume true
|
bool fileexists = true; // assume true
|
||||||
@@ -251,55 +255,60 @@ time_t getdatetime(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// loop is called repeatedly
|
||||||
|
// static vars to persist between calls
|
||||||
void loop(void) {
|
void loop(void) {
|
||||||
int i;
|
int i;
|
||||||
|
static int t=0;
|
||||||
char fname[16];
|
char fname[16];
|
||||||
int msg1 = 0;
|
|
||||||
int msg2 = 0;
|
// these buffers are used for message processing
|
||||||
int msg3 = 0;
|
static char GPSRx[Serial1RxBufferSIZE];
|
||||||
int i1 = 0;
|
static char EFISRx[Serial2RxBufferSIZE];
|
||||||
int i2 = 0;
|
static char EMSRx[Serial3RxBufferSIZE];
|
||||||
int i3 = 0;
|
static int msg1 = 0;
|
||||||
char GPSRx[Serial1RxBufferSIZE];
|
static int msg2 = 0;
|
||||||
char EFISRx[Serial2RxBufferSIZE];
|
static int msg3 = 0;
|
||||||
char EMSRx[Serial3RxBufferSIZE];
|
static int i1 = 0;
|
||||||
bool MSG_OVFL = false;
|
static int i2 = 0;
|
||||||
|
static int i3 = 0;
|
||||||
|
static bool MSG_OVFL = false;
|
||||||
|
|
||||||
|
|
||||||
// process until end of message
|
// process until end of message or overflow
|
||||||
if (Serial1.available()) {
|
if (Serial1.available()) {
|
||||||
GPSRx[i1]=Serial1.read();
|
GPSRx[i1]=Serial1.read();
|
||||||
|
//Serial.print(GPSRx[i1]);
|
||||||
if (GPSRx[i1] == GPS_EOM) msg1 = i1;
|
if (GPSRx[i1] == GPS_EOM) msg1 = i1;
|
||||||
i1++;
|
i1++;
|
||||||
GPSRx[i1]=NULL;
|
|
||||||
if (i1 == Serial1RxBufferSIZE - 1) {
|
if (i1 == Serial1RxBufferSIZE - 1) {
|
||||||
MSG_OVFL = true;
|
MSG_OVFL = true;
|
||||||
msg1 = i1;
|
msg1 = i1;
|
||||||
}
|
} else GPSRx[i1]=NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// process until end of message
|
// process until end of message or overflow
|
||||||
if (Serial2.available()) {
|
if (Serial2.available()) {
|
||||||
EFISRx[i2]=Serial2.read();
|
EFISRx[i2]=Serial2.read();
|
||||||
|
//Serial.print(EFISRx[i2]);
|
||||||
if (EFISRx[i2] == EFIS_EOM) msg2 = i2;
|
if (EFISRx[i2] == EFIS_EOM) msg2 = i2;
|
||||||
i2++;
|
i2++;
|
||||||
EFISRx[i2]=NULL;
|
|
||||||
if (i2 == Serial2RxBufferSIZE - 1) {
|
if (i2 == Serial2RxBufferSIZE - 1) {
|
||||||
MSG_OVFL = true;
|
MSG_OVFL = true;
|
||||||
msg2 = i2;
|
msg2 = i2;
|
||||||
}
|
} else EFISRx[i2]=NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// process until end of message
|
// process until end of message or overflow
|
||||||
if (Serial3.available()) {
|
if (Serial3.available()) {
|
||||||
EMSRx[i3]=Serial3.read();
|
EMSRx[i3]=Serial3.read();
|
||||||
|
//Serial.print(EMSRx[i3]);
|
||||||
if (EMSRx[i3] == EMS_EOM) msg3 = i3;
|
if (EMSRx[i3] == EMS_EOM) msg3 = i3;
|
||||||
i3++;
|
i3++;
|
||||||
EMSRx[i3]=NULL;
|
|
||||||
if (i3 == Serial3RxBufferSIZE - 1) {
|
if (i3 == Serial3RxBufferSIZE - 1) {
|
||||||
MSG_OVFL = true;
|
MSG_OVFL = true;
|
||||||
msg3 = i3;
|
msg3 = i3;
|
||||||
}
|
} else EMSRx[i3]=NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -341,6 +350,14 @@ void loop(void) {
|
|||||||
tick = false;
|
tick = false;
|
||||||
toggleLED();
|
toggleLED();
|
||||||
|
|
||||||
|
if (myFile) {
|
||||||
|
if (t>4) {
|
||||||
|
myFile.flush(); // flush every 5 seconds
|
||||||
|
t=0;
|
||||||
|
}
|
||||||
|
t=t+1;
|
||||||
|
}
|
||||||
|
|
||||||
if (!myFile) { /* open a new file */
|
if (!myFile) { /* open a new file */
|
||||||
myDrive1.begin();
|
myDrive1.begin();
|
||||||
myDrive1.startFilesystems();
|
myDrive1.startFilesystems();
|
||||||
@@ -352,11 +369,12 @@ void loop(void) {
|
|||||||
if (!myFiles1.begin(&myDrive1)) {
|
if (!myFiles1.begin(&myDrive1)) {
|
||||||
if (DEBUG) Serial.print("initialization failed with code: ");
|
if (DEBUG) Serial.print("initialization failed with code: ");
|
||||||
if (DEBUG) myFiles1.printError(Serial);
|
if (DEBUG) myFiles1.printError(Serial);
|
||||||
|
} else {
|
||||||
|
i=next_file_number(); /* global access to myFiles1 */
|
||||||
|
sprintf(fname,"Log%04d.txt",i);
|
||||||
|
myFile = myFiles1.open(fname, FILE_WRITE);
|
||||||
|
myFile.println("Log Started "); // first line
|
||||||
}
|
}
|
||||||
i=next_file_number();
|
|
||||||
sprintf(fname,"Log%04d.txt",i);
|
|
||||||
myFile = myFiles1.open(fname, FILE_WRITE);
|
|
||||||
myFile.println("Log Started "); // first line
|
|
||||||
} /* open new file */
|
} /* open new file */
|
||||||
} /* tick */
|
} /* tick */
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user