From ff02b8ea83daea0601d4a584c6bbe5f3a882a90a Mon Sep 17 00:00:00 2001 From: Bigbits Date: Sat, 4 May 2019 17:43:19 +0800 Subject: [PATCH] add serial.printf() function. --- cores/arduino/Print.cpp | 24 ++++++++++++++++++++++++ cores/arduino/Print.h | 1 + platform.txt | 3 +-- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/cores/arduino/Print.cpp b/cores/arduino/Print.cpp index 156dc26..4cf86ca 100644 --- a/cores/arduino/Print.cpp +++ b/cores/arduino/Print.cpp @@ -28,6 +28,30 @@ #include "Print.h" // Public Methods ////////////////////////////////////////////////////////////// +size_t Print::printf(const char *format, ...) +{ + char loc_buf[64]; + char * temp = loc_buf; + va_list arg; + va_list copy; + va_start(arg, format); + va_copy(copy, arg); + size_t len = vsnprintf(NULL, 0, format, arg); + va_end(copy); + if(len >= sizeof(loc_buf)){ + temp = new char[len+1]; + if(temp == NULL) { + return 0; + } + } + len = vsnprintf(temp, len+1, format, arg); + write((uint8_t*)temp, len); + va_end(arg); + if(len >= sizeof(loc_buf)){ + delete[] temp; + } + return len; +} /* default implementation: may be overridden */ size_t Print::write(const uint8_t *buffer, size_t size) diff --git a/cores/arduino/Print.h b/cores/arduino/Print.h index a50bcf5..f9a1f8f 100644 --- a/cores/arduino/Print.h +++ b/cores/arduino/Print.h @@ -61,6 +61,7 @@ class Print return write((const uint8_t *)buffer, size); } + size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3))); size_t print(const __FlashStringHelper *); size_t print(const String &); size_t print(const char[]); diff --git a/platform.txt b/platform.txt index 4970f35..54b1316 100644 --- a/platform.txt +++ b/platform.txt @@ -15,11 +15,10 @@ compiler.objcopy.cmd=riscv64-unknown-elf-objcopy compiler.elf2hex.cmd=riscv64-unknown-elf-objcopy compiler.size.cmd=riscv64-unknown-elf-size -compiler.clib.path={runtime.tools.riscv64-unknown-elf-gcc.path}/include compiler.sdk.path={runtime.platform.path}/cores/arduino/kendryte-standalone-sdk/lib compiler.lib_hal_inc.path={runtime.platform.path}/cores/arduino/hal/include compiler.cores.path={runtime.platform.path}/cores/arduino/ -compiler.preproc.flags=-I{build.system.path}/include -I{compiler.cores.path} -I{compiler.lib_hal_inc.path} -I{compiler.sdk.path}/bsp/include -I{compiler.sdk.path}/drivers/include -I{compiler.sdk.path}/utils/include -I{compiler.sdk.path}/freertos/conf -I{compiler.sdk.path}/freertos/include -I{compiler.sdk.path}/freertos/portable -I{compiler.clib.path} +compiler.preproc.flags=-I{build.system.path}/include -I{compiler.cores.path} -I{compiler.cores.path}/avr -I{compiler.lib_hal_inc.path} -I{compiler.sdk.path}/bsp/include -I{compiler.sdk.path}/drivers/include -I{compiler.sdk.path}/utils/include -I{compiler.sdk.path}/freertos/conf -I{compiler.sdk.path}/freertos/include -I{compiler.sdk.path}/freertos/portable compiler.both.flags=-mcmodel=medany -mabi=lp64f -march=rv64imafc -fno-common -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -fno-zero-initialized-in-bss -Os -ggdb -Wall -Werror=all -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wextra -Werror=frame-larger-than=65536 -Wno-unused-parameter -Wno-sign-compare -Wno-error=missing-braces -Wno-error=return-type -Wno-error=pointer-sign -Wno-missing-braces -Wno-strict-aliasing -Wno-implicit-fallthrough -Wno-missing-field-initializers -Wno-int-to-pointer-cast -Wno-error=comment -Wno-error=logical-not-parentheses -Wno-error=duplicate-decl-specifier -Wno-error=parentheses -Wno-error=narrowing -Wno-error=unused-value