From 71e9fd7d4f09d1319238fda3f45b828b1d902328 Mon Sep 17 00:00:00 2001 From: Fisch Date: Sat, 13 Jul 2024 18:18:58 +0200 Subject: [PATCH] add logging disabled when connected via usb --- controller_teensy/include/display.h | 6 ++- controller_teensy/include/logging.h | 72 +++++++++++++++++------------ controller_teensy/src/main.cpp | 11 +++-- 3 files changed, 54 insertions(+), 35 deletions(-) diff --git a/controller_teensy/include/display.h b/controller_teensy/include/display.h index 65a0246..ea34fb8 100644 --- a/controller_teensy/include/display.h +++ b/controller_teensy/include/display.h @@ -318,7 +318,11 @@ void display_standingDisarmedDisplay(ESCSerialComm& escFront, ESCSerialComm& esc display.setFont(); display.setCursor(0,0); - display.print(getLogFilename()); + if (datalogging) { + display.print(getLogFilename()); + }else{ + display.print("LOG DISABLED"); + } display.print(F(" ")); display.print(loopmillis/1000); display.print(F("s")); diff --git a/controller_teensy/include/logging.h b/controller_teensy/include/logging.h index 93c37db..6170732 100644 --- a/controller_teensy/include/logging.h +++ b/controller_teensy/include/logging.h @@ -7,6 +7,7 @@ #include //Format sd cart with FAT or FAT16. FAT32 for >1GB Cards on Teensy4.1 //#define SDCHIPSELECT 14 +boolean sdcard_available=false; boolean datalogging=true; String datalogging_filename="UNKNOWN.txt"; @@ -23,34 +24,46 @@ void serialCommandLoop(); bool initLogging() { - Serial.print("Initializing SD card..."); - // see if the card is present and can be initialized: - if (!SD.begin(BUILTIN_SDCARD)) { - Serial.println("Card failed, or not present"); - display.println(F("SD Init Fail!")); display.display(); - datalogging=false; //disable logging - delay(1000); - return false; - }else{ - Serial.println("Card initialized."); - display.print(F("LOG=")); display.display(); - if (datalogging){ - int filenumber=0; - char buffer[6]; + Serial.print("Datalogging is "); + if (datalogging) { + Serial.println("enabled"); + }else{ + Serial.println("disabled"); + } + Serial.print("Initializing SD card..."); + // see if the card is present and can be initialized: + if (!SD.begin(BUILTIN_SDCARD)) { + Serial.println("Card failed, or not present"); + display.println(F("SD Init Fail!")); display.display(); + sdcard_available=false; //disable logging + datalogging=false; + delay(1000); + return false; + }else{ + sdcard_available=true; + Serial.println("Card initialized."); + display.print(F("LOG=")); display.display(); + + int filenumber=0; + char buffer[6]; + sprintf(buffer, "%04d", filenumber); + datalogging_filename="LOG_"+String(buffer)+".TXT"; + while(SD.exists(datalogging_filename.c_str()) && filenumber<10000) { + //Serial.print(datalogging_filename); Serial.println(" exists"); + filenumber++; sprintf(buffer, "%04d", filenumber); datalogging_filename="LOG_"+String(buffer)+".TXT"; - while(SD.exists(datalogging_filename.c_str()) && filenumber<10000) { - //Serial.print(datalogging_filename); Serial.println(" exists"); - filenumber++; - sprintf(buffer, "%04d", filenumber); - datalogging_filename="LOG_"+String(buffer)+".TXT"; - } - Serial.print(datalogging_filename); Serial.println(" is free"); - display.print(datalogging_filename); display.println(); display.display(); - } - } - return true; + } + Serial.print(datalogging_filename); Serial.println(" is free"); + display.print(datalogging_filename); + if (!datalogging) { //datalogging is disabled during boot + display.print( "NO LOG"); + } + display.println(); display.display(); + + } + return true; } @@ -58,7 +71,7 @@ void loggingLoop(unsigned long loopmillis,ESCSerialComm& escFront, ESCSerialComm static unsigned long last_datalogging_write=0; static boolean logging_headerWritten=false; - if (datalogging) { + if (datalogging && sdcard_available) { #define LOGGINGINTERVAL 100 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) { //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); if (dataFile) { // if the file is available, write to it @@ -156,6 +169,7 @@ bool loadTripSD() myFile.close(); Serial.println("TripSD: time written"); }else{ + Serial.println("TripSD: could not open stats.txt"); return false; } @@ -198,7 +212,7 @@ bool loadTripSD() } void writeTrip(unsigned long loopmillis,ESCSerialComm& escFront, ESCSerialComm& escRear) { - if (datalogging) { + if (datalogging && sdcard_available) { File myFile = SD.open("stats.txt", FILE_WRITE); if (myFile) { myFile.print("trp="); @@ -216,7 +230,7 @@ void writeTrip(unsigned long loopmillis,ESCSerialComm& escFront, ESCSerialComm& } void resetTrip() { - if (datalogging) { + if (datalogging && sdcard_available) { File myFile = SD.open("stats.txt", FILE_WRITE); if (myFile) { myFile.print("trp="); diff --git a/controller_teensy/src/main.cpp b/controller_teensy/src/main.cpp index c2825a3..678a005 100644 --- a/controller_teensy/src/main.cpp +++ b/controller_teensy/src/main.cpp @@ -69,11 +69,12 @@ void setup() 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 + 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 @@ -102,7 +103,7 @@ void setup() 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(); }else{ initResult=false;