37 #include <stdnoreturn.h>
41 #define FLOAT_PRECISION 100000
45 BaseSequentialStream *chp;
61 chRegSetThreadName(
"serialPrint");
64 thread_t *sender = chMsgWait();
72 chMsgRelease(sender, MSG_OK);
77 #if CHPRINTF_USE_FLOAT
78 static int intPow(
int a,
int b)
108 i = (int)(l % radix);
115 }
while ((ll /= radix) != 0);
125 static char *
ltoa(
char *
p,
long num,
unsigned radix)
131 #if CHPRINTF_USE_FLOAT
132 static char *ftoa(
char *
p,
double num,
uint32_t precision)
140 l = (num - l) * precision;
169 static void _chvsnprintf(
char *buffer, BaseSequentialStream *chp,
size_t size,
const char *fmt, va_list ap)
171 char *
p, *
s,
c, filler;
172 int i, precision, width;
173 bool is_long, left_align, plus_on_float;
175 #if CHPRINTF_USE_FLOAT
184 bool _putChar(
const char _c) {
185 if (buffer != NULL) {
189 return (--size == 0);
193 }
else if (chp != NULL) {
207 if (buffer != NULL) {
213 if (_putChar(
c)) {
return; }
218 left_align = plus_on_float =
false;
222 }
else if (*fmt ==
'+') {
224 plus_on_float =
true;
230 #if CHPRINTF_USE_FLOAT
231 fprec = intPow(10, (*fmt) -
'0');
237 if (
c >=
'0' &&
c <=
'9') {
239 }
else if (
c ==
'*') {
244 width = width * 10 +
c;
250 if (
c >=
'0' &&
c <=
'9') {
252 #if CHPRINTF_USE_FLOAT
253 fprec = intPow(10,
c);
255 }
else if (
c ==
'*') {
265 if (
c ==
'l' ||
c ==
'L') {
271 is_long = (
c >=
'A') && (
c <=
'Z');
278 *
p++ = va_arg(ap,
int);
282 if ((
s = va_arg(ap,
char *)) == 0) {
285 if (precision == 0) {
288 for (
p =
s; *
p && (--precision >= 0);
p++)
294 l = va_arg(ap,
long);
304 #if CHPRINTF_USE_FLOAT
306 d = (double) va_arg(ap,
double);
310 }
else if (plus_on_float) {
313 p = ftoa(
p, d, fprec);
319 goto unsigned_common;
323 goto unsigned_common;
329 l = va_arg(ap,
long);
340 if ((width -= i) < 0) {
343 if (left_align ==
false) {
347 if (*
s ==
'-' && filler ==
'0') {
348 if (_putChar((
uint8_t)*
s++)) {
return; }
352 if (_putChar((
uint8_t)filler)) {
return; }
353 while (++width != 0);
356 if (_putChar((
uint8_t)*
s++)) {
return; }
359 if (_putChar((
uint8_t)filler)) {
return; }
372 void chvsnprintf(
char *buffer,
size_t size,
const char *fmt, va_list ap)
377 void chsnprintf(
char *buffer,
size_t size,
const char *fmt, ...)
395 void chprintf(BaseSequentialStream *lchp,
const char *fmt, ...)
415 void chvprintf(BaseSequentialStream *lchp,
const char *fmt, va_list ap)