📄 printf.c
字号:
/* ***********************************************************************
**
** Copyright (C) 2002 Jesper Hansen <jesperh@telia.com>
**
**
** Yampp-3/USB - output formating routines
**
** File printf.c
**
*************************************************************************
**
** This file is part of the yampp system.
**
** This program 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 2
** of the License, or (at your option) any later version.
**
** This program 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.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation,
** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**
*************************************************************************
**
** Revision History
**
** when what who why
**
** 2002-09-01 1.0 MIS initial public release
**
*********************************************************************** */
#include <stdarg.h>
#include <ctype.h>
#include <string.h>
#include <progmem.h>
#include "types.h"
#include "yampp3lib.h"
extern void putchar(u08);
void _p_puts(char const *p)
{
u08 b;
while (p && (b = PRG_RDB(p++)))
{
if(b == '\n')
putchar('\r');
putchar(b);
}
}
void _p_fputs(PCFUNC func, char const *p)
{
u08 b;
while (p && (b = PRG_RDB(p++)))
{
if(b == '\n')
func('\r');
func(b);
}
}
void both_progputs(char const *p)
{
lcd_progputs((u08*)p);
_p_puts((u08*)p);
}
void both_putchar(u08 c)
{
putchar(c);
lcd_putchar(c);
}
void both_puts(char *p)
{
while(*p)
both_putchar(*p++);
}
void uart_EOL(void)
{
_p_puts(PSTR("\n"));
}
int _p_vprintf(char const *format,va_list ap)
{
u08 scratch[16];
u08 format_flag;
u32 u_val=0;
u08 base;
u08 *ptr;
u08 width;
u08 fill;
u08 long_modifier;
u08 usigned = 0;
while(1)
{
long_modifier = 0;
width = 0;
fill = ' ';
while ((format_flag = PRG_RDB(format++)) != '%')
{
if (!format_flag)
{
va_end (ap);
return (0);
}
//*buf = format_flag; buf++; *buf=0;
if(format_flag == '\n')
putchar('\r');
putchar(format_flag);
}
// check for zero pad
format_flag = PRG_RDB(format) - '0';
if (format_flag == 0) // zero pad
{
fill = '0';
format++;
}
// check for width spec
format_flag = PRG_RDB(format) - '0';
if (format_flag > 0 && format_flag <= 9) // width set
{
width = format_flag;
format++;
}
// check modifier
format_flag = PRG_RDB(format);
if (format_flag == 'l') // long modifier
{
long_modifier = 1;
format++;
}
switch (format_flag = PRG_RDB(format++))
{
case 'c':
format_flag = va_arg(ap,int);
//fall through
default:
putchar(format_flag);
//*buf = format_flag;
//buf++;
//*buf=0;
continue;
case 'S':
case 's':
ptr = va_arg(ap,char *);
while (*ptr)
putchar(/**buf++ = */ *ptr++);
continue;
case 'p':
ptr = va_arg(ap,char *);
while (PRG_RDB(ptr))
putchar(/**buf++ = */ PRG_RDB(ptr++));
continue;
case 'o':
base = 8;
putchar('0');
//*buf = '0';
//buf++;
//*buf=0;
goto CONVERSION_LOOP;
/*
case 'i':
if (((int)u_val) < 0)
{
u_val = - u_val;
*buf = '-';
buf++;
*buf=0;
}
*/
case 'u':
usigned = 1;
case 'd':
base = 10;
goto CONVERSION_LOOP;
case 'x':
usigned = 1;
base = 16;
CONVERSION_LOOP:
if (long_modifier)
if (usigned)
u_val = va_arg(ap,unsigned long);
else
u_val = va_arg(ap,long);
else
if (usigned)
u_val = va_arg(ap,unsigned int);
else
u_val = va_arg(ap,int);
ptr = scratch + 16;
*--ptr = 0;
do
{
char ch = u_val % base + '0';
if (ch > '9')
ch += 'a' - '9' - 1;
*--ptr = ch;
u_val /= base;
if (width)
if(--width == 0) break;
} while (u_val);
while (width--)
*--ptr = fill;
while (*ptr)
putchar( /**buf++ = */ *ptr++);
}
}
}
int _p_fprintf(PCFUNC func , char const *format, ...)
{
int r;
va_list ap;
PCFUNC oldfunc = setputchar(func);
va_start (ap, format);
r = _p_vprintf(format,ap);
setputchar(oldfunc);
return r;
}
int _p_printf(char const *format, ...)
{
va_list ap;
va_start (ap, format);
return _p_vprintf(format,ap);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -