diff --git a/sys/Makefile.dep b/sys/Makefile.dep index a4551af11c..e8c33fffdf 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -13,6 +13,7 @@ ifneq (,$(filter arduino,$(USEMODULE))) FEATURES_OPTIONAL += arduino_pwm FEATURES_OPTIONAL += periph_adc FEATURES_REQUIRED += periph_gpio + USEMODULE += fmt USEMODULE += xtimer endif diff --git a/sys/arduino/serialport.cpp b/sys/arduino/serialport.cpp index e7c4f5e0c4..9b527b3efb 100644 --- a/sys/arduino/serialport.cpp +++ b/sys/arduino/serialport.cpp @@ -22,6 +22,7 @@ extern "C" { #include #include +#include "fmt.h" #include "irq.h" } @@ -148,7 +149,7 @@ size_t SerialPort::print(float val) size_t SerialPort::print(float val, int format) { char buf[64]; - size_t len = sprintf(buf, "%.*f", format, (double)val); + size_t len = fmt_float(buf, val, format); write(buf, len); return len; } diff --git a/tests/sys_arduino/arduino-test.sketch b/tests/sys_arduino/arduino-test.sketch index 1aa30a9cb4..2c42189c68 100644 --- a/tests/sys_arduino/arduino-test.sketch +++ b/tests/sys_arduino/arduino-test.sketch @@ -105,6 +105,15 @@ static void print_test(void) Serial.print("): "); Serial.println(ul, f); } + + Serial.print("print(float): "); + Serial.print((float)3.1415); + Serial.println(); + for (int i = 0; i < 4; i++) { + Serial.print("print(float): "); + Serial.print((float)3.1415, i); + Serial.println(); + } } void loop(void) diff --git a/tests/sys_arduino/tests/01-run.py b/tests/sys_arduino/tests/01-run.py index abc320348b..3e2a6260f2 100755 --- a/tests/sys_arduino/tests/01-run.py +++ b/tests/sys_arduino/tests/01-run.py @@ -64,6 +64,11 @@ def testfunc(child): child.expect_exact("println(unsigned long, DEC): 1234567890") child.expect_exact("print(unsigned long, HEX): 499602d2") child.expect_exact("println(unsigned long, HEX): 499602d2") + child.expect_exact("print(float): 3.14") + child.expect_exact("print(float): 3") + child.expect_exact("print(float): 3.1") + child.expect_exact("print(float): 3.14") + child.expect_exact("print(float): 3.141") if __name__ == "__main__":