Skip to content

Commit

Permalink
Merge pull request #10228 from bartslinger/blackbox_device_file
Browse files Browse the repository at this point in the history
Add blackbox device FILE for SITL build
  • Loading branch information
mmosca authored Jul 15, 2024
2 parents e9b5bd0 + 934fb4c commit 13bdac2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/blackbox/blackbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
75 changes: 75 additions & 0 deletions src/main/blackbox/blackbox_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
#include <stdarg.h>
#include <string.h>

#if defined(SITL_BUILD)
#include <stdio.h>
#include <time.h>
#endif

#include "platform.h"

#ifdef USE_BLACKBOX
Expand Down Expand Up @@ -81,6 +86,12 @@ static struct {

#endif

#if defined(SITL_BUILD)
static struct {
FILE *file_handler;
} blackboxFile;
#endif

#ifndef UNIT_TEST
void blackboxOpen(void)
{
Expand All @@ -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:
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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:
;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/blackbox/blackbox_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/fc/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 13bdac2

Please sign in to comment.