Skip to content

Commit

Permalink
Add cliDebugPrint functions to facilitate easy debug printing to CLI (#…
Browse files Browse the repository at this point in the history
…448)

port of betaflight/betaflight#8905

thanks to @etracer65 for original commit in betalfight

To use, include cli/cli_debug_print.h in your code and be sure USE_CLI_DEBUG_PRINT is defined. Or alternately, build using make TARGET=<target_name> OPTIONS="USE_CLI_DEBUG_PRINT". Then you'll have access to the following functions to print debugging messages in the CLI:

cliDebugPrintLineFeed
cliDebugPrint
cliDebugPrintLine
cliDebugPrintf
cliDebugPrintLinef

Output is suppressed when the CLI is not open. The functions are equivalent to their cliPrint... counterparts.

May interfere with the autocomplete initialization when first entering the CLI if your code is outputting data when the CLI first opens. But as this is only meant for debugging it shouldn't be much of a concern.

You may also need to rate limit your messages if printing data in a loop.

Using these calls in other modules should only used for debugging and not left in any finished code.

Co-authored-by: Igor Shpakov <[email protected]>
Co-authored-by: etracer65 <[email protected]>
  • Loading branch information
3 people authored May 4, 2023
1 parent 11e4402 commit c6022f8
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/main/interface/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,10 @@ static void backupAndResetConfigs(void) {
resetConfigs();
}

static void cliPrint(const char *str) {
void cliPrint(const char *str) {
if (!cliMode) {
return;
}
while (*str) {
if(cliSmartMode) {
//no carriage returns. Those are dumb.
Expand All @@ -299,14 +302,14 @@ static void cliPrint(const char *str) {
bufWriterFlush(cliWriter);
}

static void cliPrintLinefeed(void) {
void cliPrintLinefeed(void) {
cliPrint("\r\n");
if(cliSmartMode) {
bufWriterFlush(cliWriter);
}
}

static void cliPrintLine(const char *str) {
void cliPrintLine(const char *str) {
cliPrint(str);
cliPrintLinefeed();
}
Expand Down Expand Up @@ -370,15 +373,22 @@ static bool cliDefaultPrintLinef(uint8_t dumpMask, bool equalsDefault, const cha
}
}

static void cliPrintf(const char *format, ...) {
void cliPrintf(const char *format, ...) {
if (!cliMode) {
return;
}
va_list va;
va_start(va, format);
cliPrintfva(format, va);
va_end(va);
}


static void cliPrintLinef(const char *format, ...) {
void cliPrintLinef(const char *format, ...) {
if (!cliMode) {
return;
}

va_list va;
va_start(va, format);
cliPrintfva(format, va);
Expand Down Expand Up @@ -2395,7 +2405,7 @@ void cliRxBind(char *cmdline) {
case RX_SPI_REDPINE:
case RX_SPI_SFHSS:
cc2500SpiBind();
cliPrint("Binding...");
cliPrintf("Binding...");
break;
#endif
default:
Expand Down
8 changes: 8 additions & 0 deletions src/main/interface/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ void cliInit(const struct serialConfig_s *serialConfig);
void cliProcess(void);
struct serialPort_s;
void cliEnter(struct serialPort_s *serialPort);

#ifdef USE_CLI_DEBUG_PRINT
void cliPrint(const char *str);
void cliPrintLinefeed(void);
void cliPrintLine(const char *str);
void cliPrintf(const char *format, ...);
void cliPrintLinef(const char *format, ...);
#endif
49 changes: 49 additions & 0 deletions src/main/interface/cli_debug_print.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* This file is part of Cleanflight and Betaflight.
*
* Cleanflight and Betaflight are free software. You can redistribute
* this software and/or modify this software under the terms of the
* GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Cleanflight and Betaflight are distributed in the hope that they
* will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software.
*
* If not, see <http://www.gnu.org/licenses/>.
*/

// Provides: cliPrintDebug... functions for displaying debugging information in the CLI
//
// Usage: Make sure USE_CLI_DEBUG_PRINT is defined
// Include this header in your code
// Add cliDebugPrint... statements as needed in your code
// Use the CLI to see the output of the debugging statements
//
// Cautions: Be sure to include rate limiting logic to your debug printing
// if needed otherwise you can flood the output.
//
// Be sure to reverse the Usage steps above to remove the debugging
// elements before submitting final code.

#include "platform.h"

#ifdef USE_CLI_DEBUG_PRINT

#include "interface/cli.h"

// Commands to print debugging information to the CLI
#define cliDebugPrintLinefeed cliPrintLinefeed
#define cliDebugPrintLinef cliPrintLinef
#define cliDebugPrintLine cliPrintLine
#define cliDebugPrintf cliPrintf
#define cliDebugPrint cliPrint

#else
#error "Do not #include cli_debug_print.h unless you intend to do debugging and also define USE_CLI_DEBUG_PRINT"
#endif

0 comments on commit c6022f8

Please sign in to comment.