Skip to content

Instantly share code, notes, and snippets.

@charlie-x
Last active February 25, 2024 20:47
Show Gist options
  • Save charlie-x/bc32ee0ed6d71ee097b0ecf59839d3bc to your computer and use it in GitHub Desktop.
Save charlie-x/bc32ee0ed6d71ee097b0ecf59839d3bc to your computer and use it in GitHub Desktop.
segger RTT logger function for meshtastic that works with %i and floating point, it does use more stack etc
// i added in DebugConfiguration.cpp
#ifdef USE_SEGGER
// largest string it can process
const int BUFFER_SIZE = 512;
/*********************************************************************
*
* cx_SEGGER_RTT_vprintf
*
* Function description
* Stores a formatted string in SEGGER RTT control block.
* This data is read by the host. Instead of using SEGGER_vprintf it uses the system one
* so that %i and floating point works
*
* Parameters
* BufferIndex Index of "Up"-buffer to be used. (e.g. 0 for "Terminal")
* sFormat Pointer to format string
* pParamList Pointer to the list of arguments for the format string
*
* Return values
* >= 0: Number of bytes which have been stored in the "Up"-buffer.
* < 0: Error
*/
int cx_SEGGER_RTT_printf(unsigned bufferIndex, const char *sFormat, ...)
{
char buffer[BUFFER_SIZE]; // Allocate a buffer for the formatted string
int r;
va_list paramList;
va_start(paramList, sFormat);
// Format the string and store it in 'buffer'
r = vsnprintf(buffer, BUFFER_SIZE, sFormat, paramList);
va_end(paramList);
// Check if the formatting was successful
if (r > 0)
{
// If successful, send the formatted string to SEGGER RTT
SEGGER_RTT_WriteString(bufferIndex, buffer);
}
return r; // Return the number of characters formatted
}
// and changed DebugConfiguration.h
#ifdef USE_SEGGER
// #undef DEBUG_PORT
int cx_SEGGER_RTT_printf(unsigned bufferIndex, const char *sFormat, ...);
#define LOG_DEBUG(...) cx_SEGGER_RTT_printf(0, __VA_ARGS__)
#define LOG_INFO(...) cx_SEGGER_RTT_printf(0, __VA_ARGS__)
#define LOG_WARN(...) cx_SEGGER_RTT_printf(0, __VA_ARGS__)
#define LOG_ERROR(...) cx_SEGGER_RTT_printf(0, __VA_ARGS__)
#define LOG_CRIT(...) cx_SEGGER_RTT_printf(0, __VA_ARGS__)
#define LOG_TRACE(...) cx_SEGGER_RTT_printf(0, __VA_ARGS__)
#else
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment