diff --git a/src/main/blackbox/blackbox.c b/src/main/blackbox/blackbox.c index 419b7b4add4..324b41a1095 100644 --- a/src/main/blackbox/blackbox.c +++ b/src/main/blackbox/blackbox.c @@ -1442,6 +1442,9 @@ static void blackboxValidateConfig(void) #endif #ifdef USE_SDCARD case BLACKBOX_DEVICE_SDCARD: +#endif +#if defined(SITL_BUILD) + case BLACKBOX_DEVICE_FILE: #endif case BLACKBOX_DEVICE_SERIAL: // Device supported, leave the setting alone diff --git a/src/main/blackbox/blackbox_io.c b/src/main/blackbox/blackbox_io.c index 3e6adb719b7..b344b0dfff6 100644 --- a/src/main/blackbox/blackbox_io.c +++ b/src/main/blackbox/blackbox_io.c @@ -19,6 +19,11 @@ #include #include +#if defined(SITL_BUILD) +#include +#include +#endif + #include "platform.h" #ifdef USE_BLACKBOX @@ -81,6 +86,12 @@ static struct { #endif +#if defined(SITL_BUILD) +static struct { + FILE *file_handler; +} blackboxFile; +#endif + #ifndef UNIT_TEST void blackboxOpen(void) { @@ -103,6 +114,11 @@ void blackboxWrite(uint8_t value) case BLACKBOX_DEVICE_SDCARD: afatfs_fputc(blackboxSDCard.logFile, value); break; +#endif +#if defined(SITL_BUILD) + case BLACKBOX_DEVICE_FILE: + fputc(value, blackboxFile.file_handler); + break; #endif case BLACKBOX_DEVICE_SERIAL: default: @@ -133,6 +149,13 @@ int blackboxPrint(const char *s) break; #endif +#if defined(SITL_BUILD) + case BLACKBOX_DEVICE_FILE: + length = strlen(s); + fputs(s, blackboxFile.file_handler); + break; +#endif + case BLACKBOX_DEVICE_SERIAL: default: pos = (uint8_t*) s; @@ -196,6 +219,12 @@ bool blackboxDeviceFlushForce(void) return afatfs_flush(); #endif +#if defined(SITL_BUILD) + case BLACKBOX_DEVICE_FILE: + fflush(blackboxFile.file_handler); + return true; +#endif + default: return false; } @@ -271,6 +300,26 @@ bool blackboxDeviceOpen(void) return true; break; #endif +#if defined(SITL_BUILD) + case BLACKBOX_DEVICE_FILE: + { + const time_t now = time(NULL); + const struct tm *t = localtime(&now); + char filename[32]; + strftime(filename, sizeof(filename), "%Y_%m_%d_%H%M%S.TXT", t); + + blackboxFile.file_handler = fopen(filename, "wb"); + if (blackboxFile.file_handler == NULL) { + fprintf(stderr, "[BlackBox] Failed to create log file\n"); + return false; + } + fprintf(stderr, "[BlackBox] Created %s\n", filename); + } + + blackboxMaxHeaderBytesPerIteration = BLACKBOX_TARGET_HEADER_BUDGET_PER_ITERATION; + return true; + break; +#endif default: return false; } @@ -302,6 +351,12 @@ void blackboxDeviceClose(void) // Some flash device, e.g., NAND devices, require explicit close to flush internally buffered data. flashfsClose(); break; +#endif +#if defined(SITL_BUILD) + case BLACKBOX_DEVICE_FILE: + fclose(blackboxFile.file_handler); + blackboxFile.file_handler = NULL; + break; #endif default: ; @@ -506,6 +561,11 @@ bool isBlackboxDeviceFull(void) return afatfs_isFull(); #endif +#if defined (SITL_BUILD) + case BLACKBOX_DEVICE_FILE: + return false; +#endif + default: return false; } @@ -523,6 +583,10 @@ bool isBlackboxDeviceWorking(void) #ifdef USE_FLASHFS case BLACKBOX_DEVICE_FLASH: return flashfsIsReady(); +#endif +#if defined(SITL_BUILD) + case BLACKBOX_DEVICE_FILE: + return blackboxFile.file_handler != NULL; #endif default: return false; @@ -562,6 +626,11 @@ void blackboxReplenishHeaderBudget(void) case BLACKBOX_DEVICE_SDCARD: freeSpace = afatfs_getFreeBufferSpace(); break; +#endif +#if defined(SITL_BUILD) + case BLACKBOX_DEVICE_FILE: + freeSpace = BLACKBOX_MAX_ACCUMULATED_HEADER_BUDGET; + break; #endif default: freeSpace = 0; @@ -631,6 +700,12 @@ blackboxBufferReserveStatus_e blackboxDeviceReserveBufferSpace(int32_t bytes) return BLACKBOX_RESERVE_TEMPORARY_FAILURE; #endif +#if defined(SITL_BUILD) + case BLACKBOX_DEVICE_FILE: + // Assume that all writes will fit in the file's buffers + return BLACKBOX_RESERVE_TEMPORARY_FAILURE; +#endif + default: return BLACKBOX_RESERVE_PERMANENT_FAILURE; } diff --git a/src/main/blackbox/blackbox_io.h b/src/main/blackbox/blackbox_io.h index d143b72098d..76b03b5cd6a 100644 --- a/src/main/blackbox/blackbox_io.h +++ b/src/main/blackbox/blackbox_io.h @@ -31,6 +31,9 @@ typedef enum BlackboxDevice { #ifdef USE_SDCARD BLACKBOX_DEVICE_SDCARD = 2, #endif +#if defined(SITL_BUILD) + BLACKBOX_DEVICE_FILE = 3, +#endif BLACKBOX_DEVICE_END } BlackboxDevice; diff --git a/src/main/fc/settings.yaml b/src/main/fc/settings.yaml index 3f2c738d1ab..00b08ca698c 100644 --- a/src/main/fc/settings.yaml +++ b/src/main/fc/settings.yaml @@ -25,7 +25,7 @@ tables: - name: serial_rx values: ["SPEK1024", "SPEK2048", "SBUS", "SUMD", "IBUS", "JETIEXBUS", "CRSF", "FPORT", "SBUS_FAST", "FPORT2", "SRXL2", "GHST", "MAVLINK", "FBUS"] - name: blackbox_device - values: ["SERIAL", "SPIFLASH", "SDCARD"] + values: ["SERIAL", "SPIFLASH", "SDCARD", "FILE"] - name: motor_pwm_protocol values: ["STANDARD", "ONESHOT125", "MULTISHOT", "BRUSHED", "DSHOT150", "DSHOT300", "DSHOT600"] - name: servo_protocol