2011-06-27 15:00:05 +00:00
|
|
|
/*
|
2012-01-21 14:29:42 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
|
2013-02-02 10:58:09 +00:00
|
|
|
2011,2012,2013 Giovanni Di Sirio.
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2012-01-21 14:29:42 +00:00
|
|
|
This file is part of ChibiOS/RT.
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2012-01-21 14:29:42 +00:00
|
|
|
ChibiOS/RT is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2012-01-21 14:29:42 +00:00
|
|
|
ChibiOS/RT is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2012-01-21 14:29:42 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2012-03-11 17:02:06 +00:00
|
|
|
/*
|
|
|
|
Concepts and parts of this file have been contributed by Fabio Utzig.
|
|
|
|
*/
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2012-05-18 15:37:58 +00:00
|
|
|
/**
|
|
|
|
* @file chprintf.c
|
|
|
|
* @brief Mini printf-like functionality.
|
|
|
|
*
|
|
|
|
* @addtogroup chprintf
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2011-06-27 15:00:05 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "ch.h"
|
2012-03-11 17:02:06 +00:00
|
|
|
#include "chprintf.h"
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2011-06-27 19:57:11 +00:00
|
|
|
#define MAX_FILLER 11
|
2012-03-11 17:02:06 +00:00
|
|
|
#define FLOAT_PRECISION 100000
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2012-03-11 17:02:06 +00:00
|
|
|
static char *long_to_string_with_divisor(char *p,
|
|
|
|
long num,
|
|
|
|
unsigned radix,
|
|
|
|
long divisor) {
|
2011-06-27 15:00:05 +00:00
|
|
|
int i;
|
|
|
|
char *q;
|
2012-03-11 17:02:06 +00:00
|
|
|
long l, ll;
|
|
|
|
|
|
|
|
l = num;
|
|
|
|
if (divisor == 0) {
|
|
|
|
ll = num;
|
|
|
|
} else {
|
|
|
|
ll = divisor;
|
|
|
|
}
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2011-06-27 19:57:11 +00:00
|
|
|
q = p + MAX_FILLER;
|
2011-06-27 15:00:05 +00:00
|
|
|
do {
|
2012-03-11 17:02:06 +00:00
|
|
|
i = (int)(l % radix);
|
2011-06-27 15:00:05 +00:00
|
|
|
i += '0';
|
|
|
|
if (i > '9')
|
2011-06-27 19:57:11 +00:00
|
|
|
i += 'A' - '0' - 10;
|
2011-06-27 15:00:05 +00:00
|
|
|
*--q = i;
|
2012-03-11 17:02:06 +00:00
|
|
|
l /= radix;
|
|
|
|
} while ((ll /= radix) != 0);
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2011-06-27 19:57:11 +00:00
|
|
|
i = (int)(p + MAX_FILLER - q);
|
2011-06-27 15:00:05 +00:00
|
|
|
do
|
|
|
|
*p++ = *q++;
|
|
|
|
while (--i);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2012-03-11 17:02:06 +00:00
|
|
|
static char *ltoa(char *p, long num, unsigned radix) {
|
|
|
|
|
|
|
|
return long_to_string_with_divisor(p, num, radix, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CHPRINTF_USE_FLOAT
|
|
|
|
static char *ftoa(char *p, double num) {
|
|
|
|
long l;
|
|
|
|
unsigned long precision = FLOAT_PRECISION;
|
|
|
|
|
|
|
|
l = num;
|
|
|
|
p = long_to_string_with_divisor(p, l, 10, 0);
|
|
|
|
*p++ = '.';
|
|
|
|
l = (num - l) * precision;
|
|
|
|
return long_to_string_with_divisor(p, l, 10, precision / 10);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-06-28 06:29:04 +00:00
|
|
|
/**
|
|
|
|
* @brief System formatted output function.
|
|
|
|
* @details This function implements a minimal @p printf() like functionality
|
2012-05-08 17:09:20 +00:00
|
|
|
* with output on a @p BaseSequentialStream.
|
2012-01-21 19:43:40 +00:00
|
|
|
* The general parameters format is: %[-][width|*][.precision|*][l|L]p.
|
2011-06-28 06:29:04 +00:00
|
|
|
* The following parameter types (p) are supported:
|
|
|
|
* - <b>x</b> hexadecimal integer.
|
|
|
|
* - <b>X</b> hexadecimal long.
|
|
|
|
* - <b>o</b> octal integer.
|
|
|
|
* - <b>O</b> octal long.
|
|
|
|
* - <b>d</b> decimal signed integer.
|
|
|
|
* - <b>D</b> decimal signed long.
|
|
|
|
* - <b>u</b> decimal unsigned integer.
|
|
|
|
* - <b>U</b> decimal unsigned long.
|
|
|
|
* - <b>c</b> character.
|
|
|
|
* - <b>s</b> string.
|
|
|
|
* .
|
|
|
|
*
|
2012-05-08 17:09:20 +00:00
|
|
|
* @param[in] chp pointer to a @p BaseSequentialStream implementing object
|
2011-06-28 06:29:04 +00:00
|
|
|
* @param[in] fmt formatting string
|
|
|
|
*/
|
2012-05-08 17:09:20 +00:00
|
|
|
void chprintf(BaseSequentialStream *chp, const char *fmt, ...) {
|
2011-06-27 15:00:05 +00:00
|
|
|
va_list ap;
|
2011-06-27 19:57:11 +00:00
|
|
|
char *p, *s, c, filler;
|
2012-01-21 19:43:40 +00:00
|
|
|
int i, precision, width;
|
|
|
|
bool_t is_long, left_align;
|
2011-06-27 15:00:05 +00:00
|
|
|
long l;
|
2012-03-11 17:02:06 +00:00
|
|
|
#if CHPRINTF_USE_FLOAT
|
|
|
|
float f;
|
2012-07-22 08:45:48 +00:00
|
|
|
char tmpbuf[2*MAX_FILLER + 1];
|
|
|
|
#else
|
|
|
|
char tmpbuf[MAX_FILLER + 1];
|
2012-03-11 17:02:06 +00:00
|
|
|
#endif
|
2011-06-27 15:00:05 +00:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
while (TRUE) {
|
|
|
|
c = *fmt++;
|
|
|
|
if (c == 0) {
|
|
|
|
va_end(ap);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (c != '%') {
|
2012-06-18 16:22:34 +00:00
|
|
|
chSequentialStreamPut(chp, (uint8_t)c);
|
2011-06-27 15:00:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-01-16 11:41:04 +00:00
|
|
|
p = tmpbuf;
|
|
|
|
s = tmpbuf;
|
2012-01-21 19:43:40 +00:00
|
|
|
left_align = FALSE;
|
2011-06-27 15:00:05 +00:00
|
|
|
if (*fmt == '-') {
|
|
|
|
fmt++;
|
2012-01-21 19:43:40 +00:00
|
|
|
left_align = TRUE;
|
2011-06-27 15:00:05 +00:00
|
|
|
}
|
2011-06-27 19:57:11 +00:00
|
|
|
filler = ' ';
|
|
|
|
if (*fmt == '.') {
|
2011-06-27 15:00:05 +00:00
|
|
|
fmt++;
|
2011-06-27 19:57:11 +00:00
|
|
|
filler = '0';
|
2011-06-27 15:00:05 +00:00
|
|
|
}
|
2012-01-16 11:41:04 +00:00
|
|
|
width = 0;
|
|
|
|
while (TRUE) {
|
2011-06-27 15:00:05 +00:00
|
|
|
c = *fmt++;
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
c -= '0';
|
|
|
|
else if (c == '*')
|
|
|
|
c = va_arg(ap, int);
|
|
|
|
else
|
|
|
|
break;
|
2011-06-28 06:29:04 +00:00
|
|
|
width = width * 10 + c;
|
2011-06-27 15:00:05 +00:00
|
|
|
}
|
2012-01-21 19:43:40 +00:00
|
|
|
precision = 0;
|
2011-06-27 15:00:05 +00:00
|
|
|
if (c == '.') {
|
2012-01-16 11:41:04 +00:00
|
|
|
while (TRUE) {
|
2011-06-27 15:00:05 +00:00
|
|
|
c = *fmt++;
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
c -= '0';
|
|
|
|
else if (c == '*')
|
|
|
|
c = va_arg(ap, int);
|
|
|
|
else
|
|
|
|
break;
|
2012-01-21 19:43:40 +00:00
|
|
|
precision *= 10;
|
|
|
|
precision += c;
|
2011-06-27 15:00:05 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-21 19:43:40 +00:00
|
|
|
/* Long modifier.*/
|
2011-06-27 15:00:05 +00:00
|
|
|
if (c == 'l' || c == 'L') {
|
2012-01-21 19:43:40 +00:00
|
|
|
is_long = TRUE;
|
2011-06-27 15:00:05 +00:00
|
|
|
if (*fmt)
|
|
|
|
c = *fmt++;
|
|
|
|
}
|
2012-01-21 19:43:40 +00:00
|
|
|
else
|
|
|
|
is_long = (c >= 'A') && (c <= 'Z');
|
|
|
|
|
|
|
|
/* Command decoding.*/
|
2011-06-27 15:00:05 +00:00
|
|
|
switch (c) {
|
2012-01-21 19:43:40 +00:00
|
|
|
case 'c':
|
|
|
|
filler = ' ';
|
|
|
|
*p++ = va_arg(ap, int);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
filler = ' ';
|
|
|
|
if ((s = va_arg(ap, char *)) == 0)
|
|
|
|
s = "(null)";
|
|
|
|
if (precision == 0)
|
|
|
|
precision = 32767;
|
|
|
|
for (p = s; *p && (--precision >= 0); p++)
|
|
|
|
;
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
case 'd':
|
|
|
|
if (is_long)
|
|
|
|
l = va_arg(ap, long);
|
|
|
|
else
|
|
|
|
l = va_arg(ap, int);
|
|
|
|
if (l < 0) {
|
|
|
|
*p++ = '-';
|
|
|
|
l = -l;
|
|
|
|
}
|
|
|
|
p = ltoa(p, l, 10);
|
|
|
|
break;
|
2012-03-11 17:02:06 +00:00
|
|
|
#if CHPRINTF_USE_FLOAT
|
|
|
|
case 'f':
|
|
|
|
f = (float) va_arg(ap, double);
|
|
|
|
if (f < 0) {
|
|
|
|
*p++ = '-';
|
|
|
|
f = -f;
|
|
|
|
}
|
|
|
|
p = ftoa(p, f);
|
|
|
|
break;
|
|
|
|
#endif
|
2011-06-27 15:00:05 +00:00
|
|
|
case 'X':
|
|
|
|
case 'x':
|
|
|
|
c = 16;
|
2012-01-21 19:43:40 +00:00
|
|
|
goto unsigned_common;
|
2011-06-27 15:00:05 +00:00
|
|
|
case 'U':
|
|
|
|
case 'u':
|
|
|
|
c = 10;
|
2012-01-21 19:43:40 +00:00
|
|
|
goto unsigned_common;
|
2011-06-27 15:00:05 +00:00
|
|
|
case 'O':
|
|
|
|
case 'o':
|
|
|
|
c = 8;
|
2012-01-21 19:43:40 +00:00
|
|
|
unsigned_common:
|
2012-01-16 11:41:04 +00:00
|
|
|
if (is_long)
|
2013-04-07 08:02:48 +00:00
|
|
|
l = va_arg(ap, unsigned long);
|
2011-06-27 19:57:11 +00:00
|
|
|
else
|
2013-04-07 08:02:48 +00:00
|
|
|
l = va_arg(ap, unsigned int);
|
2011-06-27 19:57:11 +00:00
|
|
|
p = ltoa(p, l, c);
|
2011-06-27 15:00:05 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*p++ = c;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i = (int)(p - s);
|
|
|
|
if ((width -= i) < 0)
|
|
|
|
width = 0;
|
2012-01-21 19:43:40 +00:00
|
|
|
if (left_align == FALSE)
|
2011-06-27 15:00:05 +00:00
|
|
|
width = -width;
|
|
|
|
if (width < 0) {
|
2011-06-27 19:57:11 +00:00
|
|
|
if (*s == '-' && filler == '0') {
|
2012-06-18 16:22:34 +00:00
|
|
|
chSequentialStreamPut(chp, (uint8_t)*s++);
|
2011-06-27 15:00:05 +00:00
|
|
|
i--;
|
|
|
|
}
|
|
|
|
do
|
2012-06-18 16:22:34 +00:00
|
|
|
chSequentialStreamPut(chp, (uint8_t)filler);
|
2011-06-27 15:00:05 +00:00
|
|
|
while (++width != 0);
|
|
|
|
}
|
|
|
|
while (--i >= 0)
|
2012-06-18 16:22:34 +00:00
|
|
|
chSequentialStreamPut(chp, (uint8_t)*s++);
|
2011-06-27 15:00:05 +00:00
|
|
|
|
|
|
|
while (width) {
|
2012-06-18 16:22:34 +00:00
|
|
|
chSequentialStreamPut(chp, (uint8_t)filler);
|
2011-06-27 15:00:05 +00:00
|
|
|
width--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-14 16:45:57 +00:00
|
|
|
|
|
|
|
/** @} */
|