From 9cb48c97fdfa226784bd38a3040dd1d0ecfb7bea Mon Sep 17 00:00:00 2001 From: Bruce Luckcuck Date: Tue, 17 Sep 2019 19:14:27 -0400 Subject: [PATCH] Add cliDebugPrint functions to facilitate easy debug printing to CLI To use, include `cli/cli_debug_print.h` in your code and be sure `USE_CLI_DEBUG_PRINT` is defined. 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. 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. All of the debugging code must be removed from any completed code before submission. --- src/main/build/build_config.c | 12 +++++++++ src/main/build/version.c | 8 ------ src/main/cli/cli.c | 22 +++++++++++---- src/main/cli/cli.h | 8 ++++++ src/main/cli/cli_debug_print.h | 49 ++++++++++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 13 deletions(-) create mode 100644 src/main/cli/cli_debug_print.h diff --git a/src/main/build/build_config.c b/src/main/build/build_config.c index 3bc3aee584..f840e9f6a4 100644 --- a/src/main/build/build_config.c +++ b/src/main/build/build_config.c @@ -24,3 +24,15 @@ #include "platform.h" #include "build_config.h" + +#ifdef STM32F1 +#warning STM32F1 based targets are unsupported as of Betaflight 3.3. +#endif + +#ifdef STM32F3 +#warning STM32F3 based targets are unsupported as of Betaflight 4.1. +#endif + +#ifdef USE_CLI_DEBUG_PRINT +#warning Do not use USE_CLI_DEBUG_PRINT for production builds. +#endif diff --git a/src/main/build/version.c b/src/main/build/version.c index 75c7fbf4b9..99d5c45d8f 100644 --- a/src/main/build/version.c +++ b/src/main/build/version.c @@ -26,11 +26,3 @@ const char * const targetName = __TARGET__; const char * const shortGitRevision = __REVISION__; const char * const buildDate = __DATE__; const char * const buildTime = __TIME__; - -#ifdef STM32F1 -#warning STM32F1 based targets are unsupported as of Betaflight 3.3. -#endif - -#ifdef STM32F3 -#warning STM32F3 based targets are unsupported as of Betaflight 4.1. -#endif diff --git a/src/main/cli/cli.c b/src/main/cli/cli.c index 27bbdba288..15bea26318 100644 --- a/src/main/cli/cli.c +++ b/src/main/cli/cli.c @@ -310,8 +310,12 @@ static void cliWriterFlush() } -static void cliPrint(const char *str) +void cliPrint(const char *str) { + if (!cliMode) { + return; + } + if (cliWriter) { while (*str) { bufWriterAppend(cliWriter, *str++); @@ -320,12 +324,12 @@ static void cliPrint(const char *str) } } -static void cliPrintLinefeed(void) +void cliPrintLinefeed(void) { cliPrint("\r\n"); } -static void cliPrintLine(const char *str) +void cliPrintLine(const char *str) { cliPrint(str); cliPrintLinefeed(); @@ -391,8 +395,12 @@ static bool cliDefaultPrintLinef(dumpFlags_t dumpMask, bool equalsDefault, const } } -static void cliPrintf(const char *format, ...) +void cliPrintf(const char *format, ...) { + if (!cliMode) { + return; + } + va_list va; va_start(va, format); cliPrintfva(format, va); @@ -400,8 +408,12 @@ static void cliPrintf(const char *format, ...) } -static void cliPrintLinef(const char *format, ...) +void cliPrintLinef(const char *format, ...) { + if (!cliMode) { + return; + } + va_list va; va_start(va, format); cliPrintfva(format, va); diff --git a/src/main/cli/cli.h b/src/main/cli/cli.h index e0281bbafd..2692bbd3c2 100644 --- a/src/main/cli/cli.h +++ b/src/main/cli/cli.h @@ -29,3 +29,11 @@ bool hasCustomDefaults(void); struct serialPort_s; void cliEnter(struct serialPort_s *serialPort); bool resetConfigToCustomDefaults(void); + +#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 diff --git a/src/main/cli/cli_debug_print.h b/src/main/cli/cli_debug_print.h new file mode 100644 index 0000000000..b167d62b19 --- /dev/null +++ b/src/main/cli/cli_debug_print.h @@ -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 . + */ + +// 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 "cli/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