add logging disabled when connected via usb

This commit is contained in:
interfisch 2024-07-13 18:18:58 +02:00
parent 7b757c3c0b
commit 71e9fd7d4f
3 changed files with 54 additions and 35 deletions

View File

@ -318,7 +318,11 @@ void display_standingDisarmedDisplay(ESCSerialComm& escFront, ESCSerialComm& esc
display.setFont(); display.setFont();
display.setCursor(0,0); display.setCursor(0,0);
if (datalogging) {
display.print(getLogFilename()); display.print(getLogFilename());
}else{
display.print("LOG DISABLED");
}
display.print(F(" ")); display.print(loopmillis/1000); display.print(F(" ")); display.print(loopmillis/1000);
display.print(F("s")); display.print(F("s"));

View File

@ -7,6 +7,7 @@
#include <SD.h> //Format sd cart with FAT or FAT16. FAT32 for >1GB Cards on Teensy4.1 #include <SD.h> //Format sd cart with FAT or FAT16. FAT32 for >1GB Cards on Teensy4.1
//#define SDCHIPSELECT 14 //#define SDCHIPSELECT 14
boolean sdcard_available=false;
boolean datalogging=true; boolean datalogging=true;
String datalogging_filename="UNKNOWN.txt"; String datalogging_filename="UNKNOWN.txt";
@ -23,18 +24,26 @@ void serialCommandLoop();
bool initLogging() { bool initLogging() {
Serial.print("Datalogging is ");
if (datalogging) {
Serial.println("enabled");
}else{
Serial.println("disabled");
}
Serial.print("Initializing SD card..."); Serial.print("Initializing SD card...");
// see if the card is present and can be initialized: // see if the card is present and can be initialized:
if (!SD.begin(BUILTIN_SDCARD)) { if (!SD.begin(BUILTIN_SDCARD)) {
Serial.println("Card failed, or not present"); Serial.println("Card failed, or not present");
display.println(F("SD Init Fail!")); display.display(); display.println(F("SD Init Fail!")); display.display();
datalogging=false; //disable logging sdcard_available=false; //disable logging
datalogging=false;
delay(1000); delay(1000);
return false; return false;
}else{ }else{
sdcard_available=true;
Serial.println("Card initialized."); Serial.println("Card initialized.");
display.print(F("LOG=")); display.display(); display.print(F("LOG=")); display.display();
if (datalogging){
int filenumber=0; int filenumber=0;
char buffer[6]; char buffer[6];
sprintf(buffer, "%04d", filenumber); sprintf(buffer, "%04d", filenumber);
@ -47,8 +56,12 @@ bool initLogging() {
} }
Serial.print(datalogging_filename); Serial.println(" is free"); Serial.print(datalogging_filename); Serial.println(" is free");
display.print(datalogging_filename); display.println(); display.display(); display.print(datalogging_filename);
if (!datalogging) { //datalogging is disabled during boot
display.print( "NO LOG");
} }
display.println(); display.display();
} }
return true; return true;
} }
@ -58,7 +71,7 @@ void loggingLoop(unsigned long loopmillis,ESCSerialComm& escFront, ESCSerialComm
static unsigned long last_datalogging_write=0; static unsigned long last_datalogging_write=0;
static boolean logging_headerWritten=false; static boolean logging_headerWritten=false;
if (datalogging) { if (datalogging && sdcard_available) {
#define LOGGINGINTERVAL 100 #define LOGGINGINTERVAL 100
if (loopmillis-last_datalogging_write>LOGGINGINTERVAL) if (loopmillis-last_datalogging_write>LOGGINGINTERVAL)
{ {
@ -123,7 +136,7 @@ void loggingLoop(unsigned long loopmillis,ESCSerialComm& escFront, ESCSerialComm
void writeLogComment(unsigned long time, String msg) { void writeLogComment(unsigned long time, String msg) {
//SerialRef.print("#"); SerialRef.print(time/1000.0,3); SerialRef.print(","); SerialRef.print(msg); SerialRef.println(); //SerialRef.print("#"); SerialRef.print(time/1000.0,3); SerialRef.print(","); SerialRef.print(msg); SerialRef.println();
if (datalogging) { if (datalogging && sdcard_available) {
File dataFile = SD.open(datalogging_filename.c_str(), FILE_WRITE); File dataFile = SD.open(datalogging_filename.c_str(), FILE_WRITE);
if (dataFile) { // if the file is available, write to it if (dataFile) { // if the file is available, write to it
@ -156,6 +169,7 @@ bool loadTripSD()
myFile.close(); myFile.close();
Serial.println("TripSD: time written"); Serial.println("TripSD: time written");
}else{ }else{
Serial.println("TripSD: could not open stats.txt");
return false; return false;
} }
@ -198,7 +212,7 @@ bool loadTripSD()
} }
void writeTrip(unsigned long loopmillis,ESCSerialComm& escFront, ESCSerialComm& escRear) { void writeTrip(unsigned long loopmillis,ESCSerialComm& escFront, ESCSerialComm& escRear) {
if (datalogging) { if (datalogging && sdcard_available) {
File myFile = SD.open("stats.txt", FILE_WRITE); File myFile = SD.open("stats.txt", FILE_WRITE);
if (myFile) { if (myFile) {
myFile.print("trp="); myFile.print("trp=");
@ -216,7 +230,7 @@ void writeTrip(unsigned long loopmillis,ESCSerialComm& escFront, ESCSerialComm&
} }
void resetTrip() { void resetTrip() {
if (datalogging) { if (datalogging && sdcard_available) {
File myFile = SD.open("stats.txt", FILE_WRITE); File myFile = SD.open("stats.txt", FILE_WRITE);
if (myFile) { if (myFile) {
myFile.print("trp="); myFile.print("trp=");

View File

@ -69,11 +69,12 @@ void setup()
Serial1.begin(SERIAL_LOG_BAUD); //TX1=1, RX1=0 Serial1.begin(SERIAL_LOG_BAUD); //TX1=1, RX1=0
//Serial2.begin(SERIAL_CONTROL_BAUD); //control, TX2=10, RX2=9
//Serial3.begin(SERIAL_CONTROL_BAUD); //control, TX3=8, RX3=7
pinMode(PIN_PWRBUTTON, INPUT_PULLUP); //Pressed=High pinMode(PIN_PWRBUTTON, INPUT_PULLUP); //Pressed=High
if (!digitalRead(PIN_PWRBUTTON)) { //button is not pressed during startup means teensy is powered externally (usb)
datalogging=false; //disable logging when connected via usb to not clutter up sd card
Serial.println("PWRBUTTON not pressed. Logging disabled!");
}
pinMode(PIN_LED_START, OUTPUT); //Active High pinMode(PIN_LED_START, OUTPUT); //Active High
@ -102,7 +103,7 @@ void setup()
initResult=initLogging(); initResult=initLogging();
led_simpeProgress(2,initResult); led_simpeProgress(2,(initResult ? (datalogging ? 1:2):0)); //0=sd card fail, 1=sd ok and logging, 2(warn)=sd ok and logging off
@ -146,7 +147,7 @@ void setup()
} }
if (datalogging) { //sd init was successful if (sdcard_available) { //sd init was successful
initResult=loadTripSD(); initResult=loadTripSD();
}else{ }else{
initResult=false; initResult=false;