1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| #include "my_printf.h" #include "uart.h"
typedef char * 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=ap + _INTSIZEOF(t), ap- _INTSIZEOF(t)) ) #define va_end(ap) ( ap = (va_list)0 )
unsigned char hex_tab[]={'0','1','2','3','4','5','6','7',\ '8','9','a','b','c','d','e','f'};
static int outc(int c) { PutChar(c); return 0; }
static int outs (const char *s) { while (*s != '\0') PutChar(*s++); return 0; }
static int out_num(long n, int base,char lead,int maxwidth) { unsigned long m=0; char buf[MAX_NUMBER_BYTES], *s = buf + sizeof(buf); int count=0,i=0;
*--s = '\0'; if (n < 0){ m = -n; } else{ m = n; } do{ *--s = hex_tab[m%base]; count++; }while ((m /= base) != 0); if( maxwidth && count < maxwidth){ for (i=maxwidth - count; i; i--) *--s = lead; }
if (n < 0) *--s = '-'; return outs(s); }
static int my_vprintf(const char *fmt, va_list ap) { char lead=' '; int maxwidth=0; for(; *fmt != '\0'; fmt++) { if (*fmt != '%') { outc(*fmt); continue; }
lead=' '; maxwidth=0; fmt++; if(*fmt == '0'){ lead = '0'; fmt++; }
while(*fmt >= '0' && *fmt <= '9'){ maxwidth *=10; maxwidth += (*fmt - '0'); fmt++; } switch (*fmt) { case 'd': out_num(va_arg(ap, int), 10,lead,maxwidth); break; case 'o': out_num(va_arg(ap, unsigned int), 8,lead,maxwidth); break; case 'u': out_num(va_arg(ap, unsigned int), 10,lead,maxwidth); break; case 'x': out_num(va_arg(ap, unsigned int), 16,lead,maxwidth); break; case 'c': outc(va_arg(ap, int )); break; case 's': outs(va_arg(ap, char *)); break; default: outc(*fmt); break; } } return 0; }
int printf(const char *fmt, ...) { va_list ap;
va_start(ap, fmt); my_vprintf(fmt, ap); va_end(ap); return 0; }
int my_printf_test(void) { printf("This is www.100ask.org my_printf test\n\r") ; printf("test char =%c,%c\n\r", 'A','a') ; printf("test decimal number =%d\n\r", 123456) ; printf("test decimal number =%d\n\r", -123456) ; printf("test hex number =0x%x\n\r", 0x55aa55aa) ; printf("test string =%s\n\r", "www.100ask.org") ; printf("num=%08d\n\r", 12345); printf("num=%8d\n\r", 12345); printf("num=0x%08x\n\r", 0x12345); printf("num=0x%8x\n\r", 0x12345); printf("num=0x%02x\n\r", 0x1); printf("num=0x%2x\n\r", 0x1);
printf("num=%05d\n\r", 0x1); printf("num=%5d\n\r", 0x1);
return 0; }
|