From 49f81af5275f36f209f2299447e3a167bca11cc9 Mon Sep 17 00:00:00 2001 From: Bart Slinger Date: Fri, 12 Jul 2024 21:35:45 +0800 Subject: [PATCH] Add blackbox device FILE for SITL build --- src/main/blackbox/blackbox.c | 3 ++ src/main/blackbox/blackbox_io.c | 70 +++++++++++++++++++++++++++++++++++++++++ src/main/blackbox/blackbox_io.h | 3 ++ src/main/fc/settings.yaml | 2 +- 4 files changed, 77 insertions(+), 1 deletion(-) diff --git a/src/main/blackbox/blackbox.c b/src/main/blackbox/blackbox.c index 419b7b4ad..324b41a10 100644 --- a/src/main/blackbox/blackbox.c +++ b/src/main/blackbox/blackbox.c @@ -1443,6 +1443,9 @@ static void blackboxValidateConfig(void) #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 break; diff --git a/src/main/blackbox/blackbox_io.c b/src/main/blackbox/blackbox_io.c index 3e6adb719..dfa79b16e 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) { @@ -104,6 +115,11 @@ void blackboxWrite(uint8_t value) 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: serialWrite(blackboxPort, value); @@ -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; } @@ -303,6 +352,11 @@ void blackboxDeviceClose(void) flashfsClose(); break; #endif +#if defined(SITL_BUILD) + case BLACKBOX_DEVICE_FILE: + fclose(blackboxFile.file_handler); + break; +#endif default: ; } @@ -506,6 +560,11 @@ bool isBlackboxDeviceFull(void) return afatfs_isFull(); #endif +#if defined (SITL_BUILD) + case BLACKBOX_DEVICE_FILE: + return false; +#endif + default: return false; } @@ -563,6 +622,11 @@ void blackboxReplenishHeaderBudget(void) 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 +695,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 d143b7209..76b03b5cd 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 52d6d63f6..341fe95bd 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 -- 2.11.4.GIT