Merge pull request #15314 from aabadie/pr/sys/arduino_print_float

sys/arduino: fix print float
This commit is contained in:
Marian Buschsieweke 2020-11-17 16:15:39 +01:00 committed by GitHub
commit 45b8902659
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 1 deletions

View File

@ -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

View File

@ -22,6 +22,7 @@ extern "C" {
#include <string.h>
#include <stdio.h>
#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;
}

View File

@ -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)

View File

@ -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__":