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,
|
|
|
|
2011,2012 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/>.
|
|
|
|
*/
|
2011-06-27 15:00:05 +00:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
|
2011-06-27 19:57:11 +00:00
|
|
|
#define MAX_FILLER 11
|
2011-06-27 15:00:05 +00:00
|
|
|
|
|
|
|
static char *ltoa(char *p, long num, unsigned radix) {
|
|
|
|
int i;
|
|
|
|
char *q;
|
|
|
|
|
2011-06-27 19:57:11 +00:00
|
|
|
q = p + MAX_FILLER;
|
2011-06-27 15:00:05 +00:00
|
|
|
do {
|
|
|
|
i = (int)(num % radix);
|
|
|
|
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;
|
|
|
|
} while ((num /= radix) != 0);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-06-28 06:29:04 +00:00
|
|
|
/**
|
|
|
|
* @brief System formatted output function.
|
|
|
|
* @details This function implements a minimal @p printf() like functionality
|
|
|
|
* with output on a @p BaseChannel.
|
|
|
|
* The general parameters format is: %[.][width|*][l|L]p.
|
|
|
|
* 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.
|
|
|
|
* .
|
|
|
|
* @note Floating point types are not implemented, this function is meant
|
|
|
|
* as a system utility and not a full implementation.
|
|
|
|
*
|
|
|
|
* @param[in] chp pointer to a @p BaseChannel implementing object
|
|
|
|
* @param[in] fmt formatting string
|
|
|
|
*/
|
2011-06-27 15:00:05 +00:00
|
|
|
void chprintf(BaseChannel *chp, const char *fmt, ...) {
|
|
|
|
va_list ap;
|
2012-01-16 11:41:04 +00:00
|
|
|
char tmpbuf[MAX_FILLER + 1];
|
2011-06-27 19:57:11 +00:00
|
|
|
char *p, *s, c, filler;
|
|
|
|
int i, n, width;
|
2012-01-16 11:41:04 +00:00
|
|
|
bool_t is_long, left_just;
|
2011-06-27 15:00:05 +00:00
|
|
|
long l;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
while (TRUE) {
|
|
|
|
c = *fmt++;
|
|
|
|
if (c == 0) {
|
|
|
|
va_end(ap);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (c != '%') {
|
|
|
|
chIOPut(chp, (uint8_t)c);
|
|
|
|
continue;
|
|
|
|
}
|
2012-01-16 11:41:04 +00:00
|
|
|
p = tmpbuf;
|
|
|
|
s = tmpbuf;
|
|
|
|
left_just = FALSE;
|
2011-06-27 15:00:05 +00:00
|
|
|
if (*fmt == '-') {
|
|
|
|
fmt++;
|
2012-01-16 11:41:04 +00:00
|
|
|
left_just = 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
|
|
|
}
|
2011-06-27 19:57:11 +00:00
|
|
|
n = 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;
|
2011-06-27 19:57:11 +00:00
|
|
|
n *= 10;
|
|
|
|
n += c;
|
2011-06-27 15:00:05 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-16 11:41:04 +00:00
|
|
|
is_long = FALSE;
|
2011-06-27 15:00:05 +00:00
|
|
|
if (c == 'l' || c == 'L') {
|
2012-01-16 11:41:04 +00:00
|
|
|
is_long++;
|
2011-06-27 15:00:05 +00:00
|
|
|
if (*fmt)
|
|
|
|
c = *fmt++;
|
|
|
|
}
|
|
|
|
switch (c) {
|
|
|
|
case 'X':
|
2012-01-16 11:41:04 +00:00
|
|
|
is_long = TRUE;
|
2011-06-27 15:00:05 +00:00
|
|
|
case 'x':
|
|
|
|
c = 16;
|
2012-01-16 11:41:04 +00:00
|
|
|
goto skip;
|
2011-06-27 15:00:05 +00:00
|
|
|
case 'U':
|
2012-01-16 11:41:04 +00:00
|
|
|
is_long = TRUE;
|
2011-06-27 15:00:05 +00:00
|
|
|
case 'u':
|
|
|
|
c = 10;
|
2012-01-16 11:41:04 +00:00
|
|
|
goto skip;
|
2011-06-27 15:00:05 +00:00
|
|
|
case 'O':
|
2012-01-16 11:41:04 +00:00
|
|
|
is_long = TRUE;
|
2011-06-27 15:00:05 +00:00
|
|
|
case 'o':
|
|
|
|
c = 8;
|
2012-01-16 11:41:04 +00:00
|
|
|
skip:
|
|
|
|
if (is_long)
|
2011-06-27 19:57:11 +00:00
|
|
|
l = va_arg(ap, long);
|
|
|
|
else
|
|
|
|
l = va_arg(ap, int);
|
|
|
|
p = ltoa(p, l, c);
|
2011-06-27 15:00:05 +00:00
|
|
|
break;
|
|
|
|
case 'D':
|
2012-01-16 11:41:04 +00:00
|
|
|
is_long = TRUE;
|
2011-06-27 15:00:05 +00:00
|
|
|
case 'd':
|
2012-01-16 11:41:04 +00:00
|
|
|
if (is_long)
|
2011-06-27 19:57:11 +00:00
|
|
|
l = va_arg(ap, long);
|
|
|
|
else
|
|
|
|
l = va_arg(ap, int);
|
|
|
|
if (l < 0) {
|
2011-06-27 15:00:05 +00:00
|
|
|
*p++ = '-';
|
2011-06-27 19:57:11 +00:00
|
|
|
l = -l;
|
2011-06-27 15:00:05 +00:00
|
|
|
}
|
2011-06-27 19:57:11 +00:00
|
|
|
p = ltoa(p, l, 10);
|
2011-06-27 15:00:05 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
2011-06-27 19:57:11 +00:00
|
|
|
filler = ' ';
|
2011-06-27 15:00:05 +00:00
|
|
|
*p++ = va_arg(ap, int);
|
|
|
|
break;
|
|
|
|
case 's':
|
2011-06-27 19:57:11 +00:00
|
|
|
filler = ' ';
|
2011-06-27 15:00:05 +00:00
|
|
|
if ((s = va_arg(ap, char *)) == 0)
|
|
|
|
s = "(null)";
|
2011-06-27 19:57:11 +00:00
|
|
|
if (n == 0)
|
|
|
|
n = 32767;
|
|
|
|
for (p = s; *p && --n >= 0; p++)
|
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-16 11:41:04 +00:00
|
|
|
if (left_just == 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') {
|
2011-06-27 15:00:05 +00:00
|
|
|
chIOPut(chp, (uint8_t)*s++);
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
do
|
2011-06-27 19:57:11 +00:00
|
|
|
chIOPut(chp, (uint8_t)filler);
|
2011-06-27 15:00:05 +00:00
|
|
|
while (++width != 0);
|
|
|
|
}
|
|
|
|
while (--i >= 0)
|
|
|
|
chIOPut(chp, (uint8_t)*s++);
|
|
|
|
|
|
|
|
while (width) {
|
2011-06-27 19:57:11 +00:00
|
|
|
chIOPut(chp, (uint8_t)filler);
|
2011-06-27 15:00:05 +00:00
|
|
|
width--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|