主角登场 串口printf打印函数void uart_printf( const charptr ctrl1, ...)
{
int long_flag;
int dot_flag;
params_t par;
char ch;
va_list argp;
charptr ctrl = ctrl1;
va_start( argp, ctrl1);
for ( ; *ctrl; ctrl++)
{
/* move format string chars to buffer until a */
/* format control is found. */
if (*ctrl != '%')
{
outbyte(*ctrl);
continue;
}
/* initialize all the flags for this format. */
dot_flag = long_flag = par.left_flag = par.do_padding = 0;
par.pad_character = ' ';
par.num2 = 32767;
try_next:
ch = *(++ctrl);
if (isdigit(ch))
{
if (dot_flag)
par.num2 = getnum(&ctrl);
else
{
if (ch == '0')
par.pad_character = '0';
par.num1 = getnum(&ctrl);
par.do_padding = 1;
}
ctrl--;
goto try_next;
}
switch (tolower(ch))
{
case '%':
outbyte( '%');
continue;
case '-':
par.left_flag = 1;
break;
case '.':
dot_flag = 1;
break;
case 'l':
long_flag = 1;
break;
case 'd':
if (long_flag || ch == 'D')
{
outnum( va_arg(argp, long), 10L, &par);
continue;
}
else
{
outnum( va_arg(argp, int), 10L, &par);
continue;
}
case 'x':
outnum((long)va_arg(argp, int), 16L, &par);
continue;
case 's':
outs( va_arg( argp, charptr), &par);
continue;
case 'c':
outbyte( va_arg( argp, int));
continue;
case '\\':
switch (*ctrl)
{
case 'a':
outbyte( 0x07);
break;
case 'h':
outbyte( 0x08);
break;
case 'r':
outbyte( 0x0D);
break;
case 'n':
outbyte( 0x0D);
outbyte( 0x0A);
break;
default:
outbyte( *ctrl);
break;
}
ctrl++;
break;
default:
continue;
}
goto try_next;
}
va_end( argp);
}
|