summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSolomon Peachy <pizza@shaftnet.org>2019-12-31 00:08:58 -0500
committerSolomon Peachy <pizza@shaftnet.org>2019-12-31 00:08:58 -0500
commitbc06547e059f7d8be2913fedf26b07c71236c700 (patch)
treea624b24390962cd3def0adcd211505a797297fa8
parentd2ca4b9ae0b2294ae73582f6a1ba7112b963eb7c (diff)
downloadmcu_base-bc06547e059f7d8be2913fedf26b07c71236c700.tar.gz
mcu_base-bc06547e059f7d8be2913fedf26b07c71236c700.tar.bz2
mcu_base-bc06547e059f7d8be2913fedf26b07c71236c700.zip
printf: Partially implement floating point support.
-rw-r--r--src/printf.c42
1 files changed, 28 insertions, 14 deletions
diff --git a/src/printf.c b/src/printf.c
index 451928f..2aeb4e3 100644
--- a/src/printf.c
+++ b/src/printf.c
@@ -1,5 +1,7 @@
#include "global.h"
+//#define WITH_FLOAT // this is busted still
+
#include <string.h>
uint32_t parse_int(const char *src, int b, int width)
@@ -25,7 +27,7 @@ uint32_t parse_int(const char *src, int b, int width)
val += *src - 'A' + 10;
else if (*src >= '0' && *src <= '9')
val += *src - '0';
- else
+ else
break;
skip:
src++;
@@ -39,11 +41,12 @@ uint32_t parse_int(const char *src, int b, int width)
}
#ifdef WITH_FLOAT
-static int __print_float(char *buf, int buf_len, float i, int width, int width2)
+static int __print_float(char *buf, int buf_len, double i, int width, int width2)
{
char *s, *p = buf;
uint8_t use_width = width;
- uint8_t use_width2 = width2;
+ double t;
+ double u = i;
if (i == 0.0f) {
s = "0.0\0";
@@ -60,13 +63,13 @@ static int __print_float(char *buf, int buf_len, float i, int width, int width2)
*s = '\0';
while (i != 0.0f && (!use_width || (width > 0))) {
- t = (unsigned int) u % 10.0f;
+ t = u % 10.0f;
*--s = (char)(t + '0');
u /= 10.0f;
width--;
}
-
+
almost:
while (width > 0) {
*buf++ = '0';
@@ -74,7 +77,7 @@ almost:
}
strcpy(buf, s);
- return strlen(p);
+ return strlen(p);
}
#endif
@@ -83,7 +86,7 @@ static int __print_int(char *buf, int buf_len, int i, int base, int sign, int wi
char *s, *p = buf;
unsigned int u = (unsigned int) i;
uint8_t use_width;
- int t;
+ int t;
use_width = width;
@@ -149,7 +152,7 @@ int mcu_vsnprintf(char *str, size_t size, const char *format, va_list ap)
if (*format == 'l') {
format++;
// XXX handle LONG?
- }
+ }
switch(*format) {
case 'c': { /* Character */
char tmp = va_arg(ap, int);
@@ -158,7 +161,7 @@ int mcu_vsnprintf(char *str, size_t size, const char *format, va_list ap)
break;
}
case 'u': { /* Unsigned integer */
- uint8_t tmp = __print_int(str, size-len,
+ uint8_t tmp = __print_int(str, size-len,
va_arg(ap, unsigned int),
10, 0, width);
str += tmp;
@@ -166,16 +169,16 @@ int mcu_vsnprintf(char *str, size_t size, const char *format, va_list ap)
break;
}
case 'd': { /* Signed integer */
- uint8_t tmp = __print_int(str, size-len,
+ uint8_t tmp = __print_int(str, size-len,
va_arg(ap, int),
10, 1, width);
str += tmp;
len += tmp;
break;
- }
+ }
case 'p': { /* Pointer */
width = 8;
- uint8_t tmp = __print_int(str, size-len,
+ uint8_t tmp = __print_int(str, size-len,
va_arg(ap, unsigned long),
16, 1, width);
str += tmp;
@@ -201,7 +204,7 @@ int mcu_vsnprintf(char *str, size_t size, const char *format, va_list ap)
16, 0, width);
str += tmp;
len += tmp;
-
+
if (i+1 < MAC_ADDR_LEN) {
*(str++) = ':';
len++;
@@ -234,7 +237,18 @@ int mcu_vsnprintf(char *str, size_t size, const char *format, va_list ap)
len++;
}
break;
- }
+ }
+#ifdef WITH_FLOAT
+ case 'f': { /* Floating point */
+ uint8_t tmp = __print_float(str, size-len,
+ va_arg(ap, double),
+ width, width);
+ str += tmp;
+ len += tmp;
+ break;
+
+ }
+#endif
default: {
// char *ptr = va_arg(ap, char *);
// XXX insert error into output?