printf: Partially implement floating point support.
This commit is contained in:
parent
47fc7071bf
commit
dfd5c4ed80
42
src/printf.c
42
src/printf.c
|
@ -1,5 +1,7 @@
|
||||||
#include "global.h"
|
#include "global.h"
|
||||||
|
|
||||||
|
//#define WITH_FLOAT // this is busted still
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
uint32_t parse_int(const char *src, int b, int width)
|
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;
|
val += *src - 'A' + 10;
|
||||||
else if (*src >= '0' && *src <= '9')
|
else if (*src >= '0' && *src <= '9')
|
||||||
val += *src - '0';
|
val += *src - '0';
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
skip:
|
skip:
|
||||||
src++;
|
src++;
|
||||||
|
@ -39,11 +41,12 @@ uint32_t parse_int(const char *src, int b, int width)
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef WITH_FLOAT
|
#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;
|
char *s, *p = buf;
|
||||||
uint8_t use_width = width;
|
uint8_t use_width = width;
|
||||||
uint8_t use_width2 = width2;
|
double t;
|
||||||
|
double u = i;
|
||||||
|
|
||||||
if (i == 0.0f) {
|
if (i == 0.0f) {
|
||||||
s = "0.0\0";
|
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';
|
*s = '\0';
|
||||||
|
|
||||||
while (i != 0.0f && (!use_width || (width > 0))) {
|
while (i != 0.0f && (!use_width || (width > 0))) {
|
||||||
t = (unsigned int) u % 10.0f;
|
t = u % 10.0f;
|
||||||
*--s = (char)(t + '0');
|
*--s = (char)(t + '0');
|
||||||
u /= 10.0f;
|
u /= 10.0f;
|
||||||
width--;
|
width--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
almost:
|
almost:
|
||||||
while (width > 0) {
|
while (width > 0) {
|
||||||
*buf++ = '0';
|
*buf++ = '0';
|
||||||
|
@ -74,7 +77,7 @@ almost:
|
||||||
}
|
}
|
||||||
strcpy(buf, s);
|
strcpy(buf, s);
|
||||||
|
|
||||||
return strlen(p);
|
return strlen(p);
|
||||||
}
|
}
|
||||||
#endif
|
#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;
|
char *s, *p = buf;
|
||||||
unsigned int u = (unsigned int) i;
|
unsigned int u = (unsigned int) i;
|
||||||
uint8_t use_width;
|
uint8_t use_width;
|
||||||
int t;
|
int t;
|
||||||
|
|
||||||
use_width = width;
|
use_width = width;
|
||||||
|
|
||||||
|
@ -149,7 +152,7 @@ int mcu_vsnprintf(char *str, size_t size, const char *format, va_list ap)
|
||||||
if (*format == 'l') {
|
if (*format == 'l') {
|
||||||
format++;
|
format++;
|
||||||
// XXX handle LONG?
|
// XXX handle LONG?
|
||||||
}
|
}
|
||||||
switch(*format) {
|
switch(*format) {
|
||||||
case 'c': { /* Character */
|
case 'c': { /* Character */
|
||||||
char tmp = va_arg(ap, int);
|
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;
|
break;
|
||||||
}
|
}
|
||||||
case 'u': { /* Unsigned integer */
|
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),
|
va_arg(ap, unsigned int),
|
||||||
10, 0, width);
|
10, 0, width);
|
||||||
str += tmp;
|
str += tmp;
|
||||||
|
@ -166,16 +169,16 @@ int mcu_vsnprintf(char *str, size_t size, const char *format, va_list ap)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'd': { /* Signed integer */
|
case 'd': { /* Signed integer */
|
||||||
uint8_t tmp = __print_int(str, size-len,
|
uint8_t tmp = __print_int(str, size-len,
|
||||||
va_arg(ap, int),
|
va_arg(ap, int),
|
||||||
10, 1, width);
|
10, 1, width);
|
||||||
str += tmp;
|
str += tmp;
|
||||||
len += tmp;
|
len += tmp;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'p': { /* Pointer */
|
case 'p': { /* Pointer */
|
||||||
width = 8;
|
width = 8;
|
||||||
uint8_t tmp = __print_int(str, size-len,
|
uint8_t tmp = __print_int(str, size-len,
|
||||||
va_arg(ap, unsigned long),
|
va_arg(ap, unsigned long),
|
||||||
16, 1, width);
|
16, 1, width);
|
||||||
str += tmp;
|
str += tmp;
|
||||||
|
@ -201,7 +204,7 @@ int mcu_vsnprintf(char *str, size_t size, const char *format, va_list ap)
|
||||||
16, 0, width);
|
16, 0, width);
|
||||||
str += tmp;
|
str += tmp;
|
||||||
len += tmp;
|
len += tmp;
|
||||||
|
|
||||||
if (i+1 < MAC_ADDR_LEN) {
|
if (i+1 < MAC_ADDR_LEN) {
|
||||||
*(str++) = ':';
|
*(str++) = ':';
|
||||||
len++;
|
len++;
|
||||||
|
@ -234,7 +237,18 @@ int mcu_vsnprintf(char *str, size_t size, const char *format, va_list ap)
|
||||||
len++;
|
len++;
|
||||||
}
|
}
|
||||||
break;
|
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: {
|
default: {
|
||||||
// char *ptr = va_arg(ap, char *);
|
// char *ptr = va_arg(ap, char *);
|
||||||
// XXX insert error into output?
|
// XXX insert error into output?
|
||||||
|
|
Loading…
Reference in New Issue