70 lines
1.9 KiB
C
70 lines
1.9 KiB
C
// Helper code for saving data to USB
|
|
|
|
// Global log buffer and usage:
|
|
static u8 log_buffer[4096];
|
|
static s32 m_log_usage = 0;
|
|
// Sending logs
|
|
static bool m_send_logs = 0;
|
|
static s32 m_log_buffer_send_index = 0;
|
|
static volatile bool tx_complete = true;
|
|
static bool full_tx_complete = false;
|
|
|
|
// Functions for adding to and resetting buffer:
|
|
#define LOG_MODE 0
|
|
// Mode 0: do not wrap around, do not append to buffer
|
|
// Mode 1: wrap around to beginning.
|
|
|
|
static void reset_log_buffer(void) {
|
|
// If debug, then memset buffer
|
|
memset(log_buffer, 0, sizeof(log_buffer));
|
|
m_log_usage = 0;
|
|
m_log_buffer_send_index = 0;
|
|
tx_complete = true;
|
|
full_tx_complete = false;
|
|
}
|
|
|
|
static void add_to_log_buffer(String str) {
|
|
// Bounds check the buffer:
|
|
if (m_log_usage + str.count >= sizeof(log_buffer)) {
|
|
// Failure!.. How to handle? Turn on LEDs?
|
|
return; // Do not add to buffer
|
|
}
|
|
memcpy(&log_buffer[m_log_usage], str.data, str.count);
|
|
m_log_usage += str.count;
|
|
}
|
|
|
|
// FOR STRING LITERALS ONLY
|
|
// #define JIIM_LOG_USB(strlit) add_to_log_buffer(Create_String(strlit))
|
|
|
|
// Dynamic strings
|
|
static u8 sprint_buffer[128];
|
|
static s32 sprint_length;
|
|
|
|
// #TODO: Inline
|
|
void add_format_string_to_log_buffer(void) {
|
|
// Replace null terminator with \n where required.
|
|
sprint_buffer[sprint_length] = '\n';
|
|
String s = {sprint_length+1, sprint_buffer};
|
|
add_to_log_buffer(s);
|
|
}
|
|
|
|
// #TODO: I need to append a \n here
|
|
#define JIIM_LOG(fmt, ...) \
|
|
sprint_length = sprintf(sprint_buffer, fmt, ##__VA_ARGS__); \
|
|
add_format_string_to_log_buffer()
|
|
|
|
/*
|
|
void send_usb_log(String str) {
|
|
// Zero buffer:
|
|
//memset(str.data, 0, NRF_DRV_USBD_EPSIZE);
|
|
// Copy string:
|
|
//memcpy(m_tx_buffer, str.data, str.count);
|
|
|
|
app_usbd_cdc_acm_write(&m_app_cdc_acm, str.data, str.count);
|
|
}
|
|
|
|
void log_usb(char* data) {
|
|
send_usb_log(create_string(data));
|
|
}
|
|
|
|
*/ |