자동로깅 추가, usb port추가
This commit is contained in:
@@ -4,11 +4,15 @@
|
||||
|
||||
static SPIClass vspi(VSPI);
|
||||
volatile bool sdLoggingActive = false;
|
||||
volatile bool sdAutoStart = false;
|
||||
char currentLogFileName[64] = "";
|
||||
static File logFile;
|
||||
static bool sdReady = false;
|
||||
static SemaphoreHandle_t sdMutex = NULL;
|
||||
|
||||
// --- Persistent autostart flag ---
|
||||
#define AUTOSTART_FLAG "/logs/.autostart"
|
||||
|
||||
// --- File rotation tracking ---
|
||||
static int logStartDay = -1; // Day-of-year when current log started
|
||||
static size_t logFileSize = 0; // Running byte count of current file
|
||||
@@ -70,8 +74,19 @@ void sdTaskInit() {
|
||||
SD.mkdir(LOG_DIR);
|
||||
Serial.println("[SD] Created /logs directory");
|
||||
}
|
||||
sdLoggingActive = false;
|
||||
Serial.println("[SD] Ready (logging OFF - start via web UI)");
|
||||
|
||||
// Check persistent autostart flag
|
||||
sdAutoStart = SD.exists(AUTOSTART_FLAG);
|
||||
|
||||
if (sdAutoStart) {
|
||||
// Auto-start logging immediately (field deployment mode)
|
||||
Serial.println("[SD] *** AUTOSTART MODE - starting logging immediately ***");
|
||||
// sdCreateNewLogFile takes mutex, so don't hold it here
|
||||
sdLoggingActive = false; // Will be set true after file creation
|
||||
} else {
|
||||
sdLoggingActive = false;
|
||||
Serial.println("[SD] Ready (logging OFF - start via web UI)");
|
||||
}
|
||||
Serial.printf("[SD] Auto-rotate: midnight=%s, maxSize=%luMB\n",
|
||||
LOG_ROTATE_MIDNIGHT ? "ON" : "OFF",
|
||||
(unsigned long)(LOG_MAX_FILE_SIZE / (1024 * 1024)));
|
||||
@@ -80,6 +95,13 @@ void sdTaskInit() {
|
||||
|
||||
xTaskCreatePinnedToCore(sdLoggingTask, "SDLog", TASK_STACK_SD_LOG,
|
||||
NULL, TASK_PRIORITY_SD_LOG, NULL, 0);
|
||||
|
||||
// Deferred autostart (after task is running)
|
||||
if (sdReady && sdAutoStart) {
|
||||
sdCreateNewLogFile();
|
||||
sdLoggingActive = true;
|
||||
Serial.printf("[SD] Autostart logging: %s\n", currentLogFileName);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
@@ -403,3 +425,36 @@ bool sdDeleteFile(const char *filename) {
|
||||
}
|
||||
|
||||
bool sdCardPresent() { return sdReady; }
|
||||
|
||||
// ============================================================
|
||||
// Autostart - persistent flag on SD card
|
||||
// ============================================================
|
||||
bool sdGetAutoStart() {
|
||||
if (!sdReady) return false;
|
||||
return SD.exists(AUTOSTART_FLAG);
|
||||
}
|
||||
|
||||
void sdSetAutoStart(bool enable) {
|
||||
if (!sdReady) return;
|
||||
xSemaphoreTake(sdMutex, portMAX_DELAY);
|
||||
|
||||
if (enable) {
|
||||
// Create flag file
|
||||
File f = SD.open(AUTOSTART_FLAG, FILE_WRITE);
|
||||
if (f) {
|
||||
f.println("autostart=1");
|
||||
f.close();
|
||||
}
|
||||
sdAutoStart = true;
|
||||
Serial.println("[SD] Autostart ENABLED (will log on next boot)");
|
||||
} else {
|
||||
// Remove flag file
|
||||
if (SD.exists(AUTOSTART_FLAG)) {
|
||||
SD.remove(AUTOSTART_FLAG);
|
||||
}
|
||||
sdAutoStart = false;
|
||||
Serial.println("[SD] Autostart DISABLED");
|
||||
}
|
||||
|
||||
xSemaphoreGive(sdMutex);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user