Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0bca71a

Browse filesBrowse files
committed
fix: 默认使用单精度浮点,节约flash空间,抄自 stm32duino/Arduino_Core_STM32#2036
1 parent dc39c7e commit 0bca71a
Copy full SHA for 0bca71a

File tree

Expand file treeCollapse file tree

3 files changed

+22
-6
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+22
-6
lines changed

‎cores/AirMCU/Print.cpp

Copy file name to clipboardExpand all lines: cores/AirMCU/Print.cpp
+16-3Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ size_t Print::print(unsigned long long n, int base)
129129
}
130130
}
131131

132+
size_t Print::print(float n, int digits)
133+
{
134+
return printFloat(n, digits);
135+
}
136+
132137
size_t Print::print(double n, int digits)
133138
{
134139
return printFloat(n, digits);
@@ -221,6 +226,13 @@ size_t Print::println(unsigned long long num, int base)
221226
return n;
222227
}
223228

229+
size_t Print::println(float num, int digits)
230+
{
231+
size_t n = print(num, digits);
232+
n += println();
233+
return n;
234+
}
235+
224236
size_t Print::println(double num, int digits)
225237
{
226238
size_t n = print(num, digits);
@@ -406,7 +418,8 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
406418
return bytes;
407419
}
408420

409-
size_t Print::printFloat(double number, uint8_t digits)
421+
template <class T>
422+
size_t Print::printFloat(T number, uint8_t digits)
410423
{
411424
size_t n = 0;
412425

@@ -430,7 +443,7 @@ size_t Print::printFloat(double number, uint8_t digits)
430443
}
431444

432445
// Round correctly so that print(1.999, 2) prints as "2.00"
433-
double rounding = 0.5;
446+
T rounding = 0.5;
434447
for (uint8_t i = 0; i < digits; ++i) {
435448
rounding /= 10.0;
436449
}
@@ -439,7 +452,7 @@ size_t Print::printFloat(double number, uint8_t digits)
439452

440453
// Extract the integer part of the number and print it
441454
unsigned long int_part = (unsigned long)number;
442-
double remainder = number - (double)int_part;
455+
T remainder = number - (T)int_part;
443456
n += print(int_part);
444457

445458
// Print the decimal point, but only if there are digits beyond

‎cores/AirMCU/Print.h

Copy file name to clipboardExpand all lines: cores/AirMCU/Print.h
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class Print {
3636
int write_error;
3737
size_t printNumber(unsigned long, uint8_t);
3838
size_t printULLNumber(unsigned long long, uint8_t);
39-
size_t printFloat(double, uint8_t);
39+
template <class T>
40+
size_t printFloat(T, uint8_t);
4041
protected:
4142
void setWriteError(int err = 1)
4243
{
@@ -86,6 +87,7 @@ class Print {
8687
size_t print(unsigned long, int = DEC);
8788
size_t print(long long, int = DEC);
8889
size_t print(unsigned long long, int = DEC);
90+
size_t print(float, int = 2);
8991
size_t print(double, int = 2);
9092
size_t print(const Printable &);
9193

@@ -100,6 +102,7 @@ class Print {
100102
size_t println(unsigned long, int = DEC);
101103
size_t println(long long, int = DEC);
102104
size_t println(unsigned long long, int = DEC);
105+
size_t println(float, int = 2);
103106
size_t println(double, int = 2);
104107
size_t println(const Printable &);
105108
size_t println(void);

‎platform.txt

Copy file name to clipboardExpand all lines: platform.txt
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ compiler.extra_flags=-mcpu={build.mcu} {build.fpu} {build.float-abi} -DUSE_FULL_
3131
# 汇编编译器参数
3232
compiler.S.flags={compiler.extra_flags} -c -x assembler-with-cpp {compiler.air.extra_include}
3333

34-
compiler.c.flags={compiler.extra_flags} -c {build.flags.optimize} {build.flags.debug} {compiler.warning_flags} -std={compiler.c.std} -ffunction-sections -fdata-sections --param max-inline-insns-single=500 -MMD {compiler.air.extra_include}
34+
compiler.c.flags={compiler.extra_flags} -c {build.flags.optimize} {build.flags.debug} {compiler.warning_flags} -std={compiler.c.std} -fsingle-precision-constant -ffunction-sections -fdata-sections --param max-inline-insns-single=500 -MMD {compiler.air.extra_include}
3535

36-
compiler.cpp.flags={compiler.extra_flags} -c {build.flags.optimize} {build.flags.debug} {compiler.warning_flags} -std={compiler.cpp.std} -ffunction-sections -fdata-sections -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -fno-use-cxa-atexit -MMD {compiler.air.extra_include}
36+
compiler.cpp.flags={compiler.extra_flags} -c {build.flags.optimize} {build.flags.debug} {compiler.warning_flags} -std={compiler.cpp.std} -fsingle-precision-constant -ffunction-sections -fdata-sections -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -fno-use-cxa-atexit -MMD {compiler.air.extra_include}
3737

3838
compiler.ar.flags=rcs
3939

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.