float timeStamp;
char filenameBuffer[23];
String filename;
bool logStarted = 0;
uint32_t lastSyncTime = 0, logStartTime = 0;
File file;
void initSD()
{
if (SD.begin(5, SPI, 80000000))
{
Serial.println("Card Mount OK");
updateFromFS(SD);
generateFileName();
sdInitialised = true;
}
else
{
Serial.println("Card Mount Failed");
return;
}
}
void logToSD()
{
if (!sdInitialised) return;
if (!EMUfresh) return;
if (!logStarted)
{
file = SD.open(filenameBuffer, FILE_APPEND);
if (file)
{
file.print("Time,RPM,MAP,TPS,IAT,CLT,Lambda,PulseWidth,EGT1,EGT2,Vbatt,Baro,IgnAngle,LambdaCorr,Gear,Pwm1,Vss,EmuTemp,CEL,FLAGS1,LambdaTarget,OilTemp,OilPress,CanState\r\n");
logStartTime = millis();
logStarted = true;
}
}
uint32_t duration = millis() - logStartTime;
uint32_t seconds = duration / 1000;
uint32_t milliseconds = duration % 1000;
String dataString = "";
dataString += seconds;
dataString += ".";
if (milliseconds < 100) { dataString += "0"; }
if (milliseconds < 10) { dataString += "0"; }
dataString += milliseconds;
dataString += ",";
dataString += String(emu_data.RPM);
dataString += ",";
dataString += String(emu_data.MAP);
dataString += ",";
dataString += String(emu_data.TPS);
dataString += ",";
dataString += String(emu_data.IAT);
dataString += ",";
dataString += String(emu_data.CLT);
dataString += ",";
dataString += String(emu_data.lambda);
dataString += ",";
dataString += String(emu_data.pulseWidth);
dataString += ",";
dataString += String(emu_data.EGT1);
dataString += ",";
dataString += String(emu_data.EGT2);
dataString += ",";
dataString += String(emu_data.vBat);
dataString += ",";
dataString += String(emu_data.baro);
dataString += ",";
dataString += String(emu_data.ignAngle, 0);
dataString += ",";
dataString += String(emu_data.lambdaCorr, 0);
dataString += ",";
dataString += String(emu_data.gear);
dataString += ",";
dataString += String(emu_data.pwm1);
dataString += ",";
dataString += String(emu_data.vssSpeed);
dataString += ",";
dataString += String(emu_data.emuTemp);
dataString += ",";
dataString += String(emu_data.cel);
dataString += ",";
dataString += String(emu_data.flags1);
dataString += ",";
dataString += String(emu_data.lambdaTarget);
dataString += ",";
dataString += String(emu_data.oilTemperature);
dataString += ",";
dataString += String(emu_data.oilPressure);
//dataString += ",";
//dataString += String(can_error_data.state[1]); //Only first character
dataString += "\r\n";
file.print(dataString.c_str());
if (millis() - lastSyncTime > 1000)
{
lastSyncTime = millis();
file.flush();
}
}
void generateFileName()
{
char intBuffer[5];
strcpy(filenameBuffer, "/");
itoa(random(1000, 9999), intBuffer, 10);
strcat(filenameBuffer, intBuffer);
strcat(filenameBuffer, "-");
itoa(random(10), intBuffer, 10);
strcat(filenameBuffer, intBuffer);
strcat(filenameBuffer, "-");
itoa(random(10), intBuffer, 10);
strcat(filenameBuffer, intBuffer);
strcat(filenameBuffer, "_");
itoa(random(10), intBuffer, 10);
strcat(filenameBuffer, intBuffer);
strcat(filenameBuffer, ".");
itoa(random(10), intBuffer, 10);
strcat(filenameBuffer, intBuffer);
strcat(filenameBuffer, ".");
itoa(random(10), intBuffer, 10);
strcat(filenameBuffer, intBuffer);
strcat(filenameBuffer, ".csv");
Serial.println(filenameBuffer);
}