mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-25 17:25:20 +03:00
Modify prinf-like functions to return number of characters written
This commit is contained in:
parent
4cf6fe6571
commit
ae67870db1
2 changed files with 34 additions and 24 deletions
|
@ -56,29 +56,36 @@ typedef void (*putcf) (void *, char);
|
||||||
static putcf stdout_putf;
|
static putcf stdout_putf;
|
||||||
static void *stdout_putp;
|
static void *stdout_putp;
|
||||||
|
|
||||||
static void putchw(void *putp, putcf putf, int n, char z, char *bf)
|
// print bf, padded from left to at least n characters.
|
||||||
|
// padding is zero ('0') if z!=0, space (' ') otherwise
|
||||||
|
static int putchw(void *putp, putcf putf, int n, char z, char *bf)
|
||||||
{
|
{
|
||||||
|
int written = 0;
|
||||||
char fc = z ? '0' : ' ';
|
char fc = z ? '0' : ' ';
|
||||||
char ch;
|
char ch;
|
||||||
char *p = bf;
|
char *p = bf;
|
||||||
while (*p++ && n > 0)
|
while (*p++ && n > 0)
|
||||||
n--;
|
n--;
|
||||||
while (n-- > 0)
|
while (n-- > 0) {
|
||||||
putf(putp, fc);
|
putf(putp, fc); written++;
|
||||||
while ((ch = *bf++))
|
}
|
||||||
putf(putp, ch);
|
while ((ch = *bf++)) {
|
||||||
|
putf(putp, ch); written++;
|
||||||
|
}
|
||||||
|
return written;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tfp_format(void *putp, putcf putf, const char *fmt, va_list va)
|
// retrun number of bytes written
|
||||||
|
int tfp_format(void *putp, putcf putf, const char *fmt, va_list va)
|
||||||
{
|
{
|
||||||
char bf[12];
|
char bf[12];
|
||||||
|
int written = 0;
|
||||||
char ch;
|
char ch;
|
||||||
|
|
||||||
while ((ch = *(fmt++))) {
|
while ((ch = *(fmt++))) {
|
||||||
if (ch != '%')
|
if (ch != '%') {
|
||||||
putf(putp, ch);
|
putf(putp, ch); written++;
|
||||||
else {
|
} else {
|
||||||
char lz = 0;
|
char lz = 0;
|
||||||
#ifdef REQUIRE_PRINTF_LONG_SUPPORT
|
#ifdef REQUIRE_PRINTF_LONG_SUPPORT
|
||||||
char lng = 0;
|
char lng = 0;
|
||||||
|
@ -108,7 +115,7 @@ void tfp_format(void *putp, putcf putf, const char *fmt, va_list va)
|
||||||
else
|
else
|
||||||
#endif
|
#endif
|
||||||
ui2a(va_arg(va, unsigned int), 10, 0, bf);
|
ui2a(va_arg(va, unsigned int), 10, 0, bf);
|
||||||
putchw(putp, putf, w, lz, bf);
|
written += putchw(putp, putf, w, lz, bf);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'd':{
|
case 'd':{
|
||||||
|
@ -118,7 +125,7 @@ void tfp_format(void *putp, putcf putf, const char *fmt, va_list va)
|
||||||
else
|
else
|
||||||
#endif
|
#endif
|
||||||
i2a(va_arg(va, int), bf);
|
i2a(va_arg(va, int), bf);
|
||||||
putchw(putp, putf, w, lz, bf);
|
written += putchw(putp, putf, w, lz, bf);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'x':
|
case 'x':
|
||||||
|
@ -129,16 +136,16 @@ void tfp_format(void *putp, putcf putf, const char *fmt, va_list va)
|
||||||
else
|
else
|
||||||
#endif
|
#endif
|
||||||
ui2a(va_arg(va, unsigned int), 16, (ch == 'X'), bf);
|
ui2a(va_arg(va, unsigned int), 16, (ch == 'X'), bf);
|
||||||
putchw(putp, putf, w, lz, bf);
|
written += putchw(putp, putf, w, lz, bf);
|
||||||
break;
|
break;
|
||||||
case 'c':
|
case 'c':
|
||||||
putf(putp, (char) (va_arg(va, int)));
|
putf(putp, (char) (va_arg(va, int))); written++;
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
putchw(putp, putf, w, 0, va_arg(va, char *));
|
written += putchw(putp, putf, w, 0, va_arg(va, char *));
|
||||||
break;
|
break;
|
||||||
case '%':
|
case '%':
|
||||||
putf(putp, ch);
|
putf(putp, ch); written++;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
@ -146,6 +153,7 @@ void tfp_format(void *putp, putcf putf, const char *fmt, va_list va)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
abort:;
|
abort:;
|
||||||
|
return written;
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_printf(void *putp, void (*putf) (void *, char))
|
void init_printf(void *putp, void (*putf) (void *, char))
|
||||||
|
@ -154,13 +162,14 @@ void init_printf(void *putp, void (*putf) (void *, char))
|
||||||
stdout_putp = putp;
|
stdout_putp = putp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tfp_printf(const char *fmt, ...)
|
int tfp_printf(const char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list va;
|
va_list va;
|
||||||
va_start(va, fmt);
|
va_start(va, fmt);
|
||||||
tfp_format(stdout_putp, stdout_putf, fmt, va);
|
int written = tfp_format(stdout_putp, stdout_putf, fmt, va);
|
||||||
va_end(va);
|
va_end(va);
|
||||||
while (!isSerialTransmitBufferEmpty(printfSerialPort));
|
while (!isSerialTransmitBufferEmpty(printfSerialPort));
|
||||||
|
return written;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void putcp(void *p, char c)
|
static void putcp(void *p, char c)
|
||||||
|
@ -168,14 +177,15 @@ static void putcp(void *p, char c)
|
||||||
*(*((char **) p))++ = c;
|
*(*((char **) p))++ = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tfp_sprintf(char *s, const char *fmt, ...)
|
int tfp_sprintf(char *s, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list va;
|
va_list va;
|
||||||
|
|
||||||
va_start(va, fmt);
|
va_start(va, fmt);
|
||||||
tfp_format(&s, putcp, fmt, va);
|
int written = tfp_format(&s, putcp, fmt, va);
|
||||||
putcp(&s, 0);
|
putcp(&s, 0);
|
||||||
va_end(va);
|
va_end(va);
|
||||||
|
return written;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -196,7 +206,7 @@ void printfSupportInit(void)
|
||||||
int fputc(int c, FILE *f)
|
int fputc(int c, FILE *f)
|
||||||
{
|
{
|
||||||
// let DMA catch up a bit when using set or dump, we're too fast.
|
// let DMA catch up a bit when using set or dump, we're too fast.
|
||||||
while (!isSerialTransmitBufferEmpty(serialPorts.mainport));
|
while (!isSerialTransmitBufferEmpty(printfSerialPort));
|
||||||
serialWrite(printfSerialPort, c);
|
serialWrite(printfSerialPort, c);
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
|
@ -107,10 +107,10 @@ regs Kusti, 23.10.2004
|
||||||
|
|
||||||
void init_printf(void *putp, void (*putf) (void *, char));
|
void init_printf(void *putp, void (*putf) (void *, char));
|
||||||
|
|
||||||
void tfp_printf(const char *fmt, ...);
|
int tfp_printf(const char *fmt, ...);
|
||||||
void tfp_sprintf(char *s, const char *fmt, ...);
|
int tfp_sprintf(char *s, const char *fmt, ...);
|
||||||
|
|
||||||
void tfp_format(void *putp, void (*putf) (void *, char), const char *fmt, va_list va);
|
int tfp_format(void *putp, void (*putf) (void *, char), const char *fmt, va_list va);
|
||||||
|
|
||||||
#define printf tfp_printf
|
#define printf tfp_printf
|
||||||
#define sprintf tfp_sprintf
|
#define sprintf tfp_sprintf
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue