Adding feature to log to SD Card (#708)

* Adding feature to log to SD Card

Co-authored-by: mvgalen <marijnvangalen@gmail.com>
This commit is contained in:
Matt Holmes 2025-01-02 19:46:10 +00:00 committed by GitHub
parent 3d1f535b09
commit f138f97905
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 234 additions and 68 deletions

View file

@ -5,11 +5,18 @@
defined(SD_MISO_PIN) // ensure code is only compiled if all SD card pins are defined
File can_log_file;
File log_file;
RingbufHandle_t can_bufferHandle;
RingbufHandle_t log_bufferHandle;
bool can_logging_paused = false;
bool can_file_open = false;
bool delete_can_file = false;
bool logging_paused = false;
bool log_file_open = false;
bool delete_log_file = false;
bool sd_card_active = false;
void delete_can_log() {
@ -27,6 +34,26 @@ void pause_can_writing() {
can_logging_paused = true;
}
void delete_log() {
logging_paused = true;
if (log_file_open) {
log_file.close();
log_file_open = false;
}
SD.remove(LOG_FILE);
logging_paused = false;
}
void resume_log_writing() {
logging_paused = false;
log_file = SD.open(LOG_FILE, FILE_APPEND);
log_file_open = true;
}
void pause_log_writing() {
logging_paused = true;
}
void add_can_frame_to_buffer(CAN_frame frame, frameDirection msgDir) {
if (!sd_card_active)
@ -84,17 +111,65 @@ void write_can_frame_to_sdcard() {
can_log_file.print(" ");
}
can_log_file.println("");
can_log_file.flush();
vRingbufferReturnItem(can_bufferHandle, (void*)log_frame);
}
}
void init_logging_buffer() {
can_bufferHandle = xRingbufferCreate(64 * 1024, RINGBUF_TYPE_BYTEBUF);
void add_log_to_buffer(const uint8_t* buffer, size_t size) {
if (!sd_card_active)
return;
if (xRingbufferSend(log_bufferHandle, buffer, size, 0) != pdTRUE) {
Serial.println("Failed to send log to ring buffer!");
return;
}
}
void write_log_to_sdcard() {
if (!sd_card_active)
return;
size_t receivedMessageSize;
uint8_t* buffer = (uint8_t*)xRingbufferReceive(log_bufferHandle, &receivedMessageSize, pdMS_TO_TICKS(10));
if (buffer != NULL) {
if (logging_paused) {
vRingbufferReturnItem(log_bufferHandle, (void*)buffer);
return;
}
if (log_file_open == false) {
log_file = SD.open(LOG_FILE, FILE_APPEND);
log_file_open = true;
}
log_file.write(buffer, receivedMessageSize);
log_file.flush();
vRingbufferReturnItem(log_bufferHandle, (void*)buffer);
}
}
void init_logging_buffers() {
#if defined(LOG_CAN_TO_SD)
can_bufferHandle = xRingbufferCreate(32 * 1024, RINGBUF_TYPE_BYTEBUF);
if (can_bufferHandle == NULL) {
Serial.println("Failed to create CAN ring buffer!");
return;
}
#endif // defined(LOG_CAN_TO_SD)
#if defined(LOG_TO_SD)
log_bufferHandle = xRingbufferCreate(1024, RINGBUF_TYPE_BYTEBUF);
if (log_bufferHandle == NULL) {
Serial.println("Failed to create log ring buffer!");
return;
}
#endif // defined(LOG_TO_SD)
}
void init_sdcard() {