#ifndef __PRINTF_UART_H__
#define __PRINTF_UART_H__
#define PSTR(x) x
typedef void* va_list;
#define _INTSIZEOF(n) ((sizeof(n)+sizeof(int)-1)&~(sizeof(int) - 1))
#define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) )
#define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)))
#define va_end(ap) ( ap = (va_list)0 )
#endif
#include "printf_uart.h"
unsigned long ltostr(unsigned long Val,char *pString,unsigned char Base,unsigned long Length);
char ss[10]={0};
int tm_printf (const char *format, ...)
{
unsigned int d;
int count = 0;
char c;
char* s;
va_list ap;
va_start(ap, format);
while(0 != *format){
if('%' != *format){
tm_putchar(*format);
}
else{
switch(*++format){
case 'd':
d = va_arg(ap, unsigned int);
ltostr((unsigned long)d,ss,16,10);
tm_putstring((unsigned char *)ss);
break;
case 'c':
c = va_arg(ap, char);
tm_putchar((int)c);
break;
case 's':
s = va_arg(ap, char*);
tm_putstring((unsigned char *)s);
break;
default:
break;
}
}
count++;
format++;
}
va_end(ap);
return count;
}
unsigned long ltostr(unsigned long Val,char *pString,unsigned char Base,unsigned long Length)
{
char c, *pInit;
unsigned char i,j;
unsigned long Len,SwapLen;
*pString='\0';
pInit=pString;
if(Val==0) {
*pString++='0';
*pString='\0';
}
switch(Base) {
case 10:
for(;Val>0;) {
i=Val�se;
Val=Val/Base;
*pString++=(char)((unsigned char)'0'+i);
}
*pString='\0';
break;
case 16:
for(;Val>0;) {
i=Val�se;
Val=Val/Base;
if(i<=9) {
*pString++=(char)((unsigned char)'0'+i);
} else {
*pString++=(char)((unsigned char)'A'+i-10);
}
}
*pString='\0';
break;
default:
break;
}
Len=strlen(pInit);
SwapLen=Len;
if(Length>Len) {
for(i=Len;i
*(pInit+i)='\0';
}
*(pInit+Length)='\0';
}
if(SwapLen>0) {
for(i=0,j=SwapLen - 1;i
c=*(pInit+i);
*(pInit+i)=*(pInit+j);
*(pInit+j)=c;
}
}
return SwapLen;
}
一个尚不完善的printf
最新推荐文章于 2023-01-16 22:43:59 发布