00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <avr/pgmspace.h>
00023
00024
00025 #include <stdarg.h>
00026 #include "global.h"
00027 #include "rprintf.h"
00028
00029 #ifndef TRUE
00030 #define TRUE -1
00031 #define FALSE 0
00032 #endif
00033
00034 #define INF 32766 // maximum field size to print
00035 #define READMEMBYTE(a,char_ptr) ((a)?(pgm_read_byte(char_ptr)):(*char_ptr))
00036
00037 #ifdef RPRINTF_COMPLEX
00038 static unsigned char buf[128];
00039 #endif
00040
00041
00042
00043
00044
00045 static char __attribute__ ((progmem)) HexChars[] = "0123456789ABCDEF";
00046
00047 #define hexchar(x) pgm_read_byte( HexChars+((x)&0x0f) )
00048
00049
00050
00051 static void (*rputchar)(unsigned char c);
00052
00053
00054
00055
00056 void rprintfInit(void (*putchar_func)(unsigned char c))
00057 {
00058 rputchar = putchar_func;
00059 }
00060
00061
00062
00063 void rprintfChar(unsigned char c)
00064 {
00065
00066 if(c == '\n')
00067 rputchar('\r');
00068
00069 rputchar(c);
00070 }
00071
00072
00073
00074 void rprintfStr(char str[])
00075 {
00076
00077
00078 if (!str) return;
00079
00080
00081 while (*str)
00082 rprintfChar(*str++);
00083 }
00084
00085
00086
00087
00088
00089 void rprintfStrLen(char str[], unsigned int start, unsigned int len)
00090 {
00091 register int i=0;
00092
00093
00094 if (!str) return;
00095
00096
00097 while((i++<start) && (*str++));
00098
00099
00100
00101
00102
00103
00104
00105 for(i=0; i<len; i++)
00106 {
00107
00108
00109 if(*str)
00110 rprintfChar(*str++);
00111 else
00112 rprintfChar(' ');
00113 }
00114
00115 }
00116
00117
00118
00119 void rprintfProgStr(const prog_char str[])
00120 {
00121
00122 register char c;
00123
00124
00125 if (!str) return;
00126
00127
00128 while((c = pgm_read_byte(str++)))
00129 rprintfChar(c);
00130 }
00131
00132
00133
00134 void rprintfCRLF(void)
00135 {
00136
00137
00138
00139 rprintfChar('\n');
00140 }
00141
00142
00143
00144 void rprintfu04(unsigned char data)
00145 {
00146
00147
00148
00149
00150
00151
00152 rprintfChar(hexchar(data));
00153 }
00154
00155
00156
00157 void rprintfu08(unsigned char data)
00158 {
00159
00160 rprintfu04(data>>4);
00161 rprintfu04(data);
00162 }
00163
00164
00165
00166 void rprintfu16(unsigned short data)
00167 {
00168
00169 rprintfu08(data>>8);
00170 rprintfu08(data);
00171 }
00172
00173
00174
00175 void rprintfu32(unsigned long data)
00176 {
00177
00178 rprintfu16(data>>16);
00179 rprintfu16(data);
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 void rprintfNum(char base, char numDigits, char isSigned, char padchar, long n)
00195 {
00196
00197
00198 char *p, buf[32];
00199 unsigned long x;
00200 unsigned char count;
00201
00202
00203 if( isSigned && (n < 0) )
00204 {
00205 x = -n;
00206 }
00207 else
00208 {
00209 x = n;
00210 }
00211
00212
00213 count = (numDigits-1)-(isSigned?1:0);
00214 p = buf + sizeof (buf);
00215 *--p = '\0';
00216
00217
00218
00219 *--p = hexchar(x%base); x /= base;
00220
00221 while(count--)
00222 {
00223 if(x != 0)
00224 {
00225
00226 *--p = hexchar(x%base); x /= base;
00227 }
00228 else
00229 {
00230
00231 *--p = padchar;
00232 }
00233 }
00234
00235
00236 if( isSigned )
00237 {
00238 if(n < 0)
00239 {
00240 *--p = '-';
00241 }
00242 else if(n > 0)
00243 {
00244 *--p = '+';
00245 }
00246 else
00247 {
00248 *--p = ' ';
00249 }
00250 }
00251
00252
00253 count = numDigits;
00254 while(count--)
00255 {
00256 rprintfChar(*p++);
00257 }
00258 }
00259
00260 #ifdef RPRINTF_FLOAT
00261
00262
00263 void rprintfFloat(char numDigits, double x)
00264 {
00265 unsigned char firstplace = FALSE;
00266 unsigned char negative;
00267 unsigned char i, digit;
00268 double place = 1.0;
00269
00270
00271 negative = (x<0);
00272
00273 x = (x>0)?(x):(-x);
00274
00275
00276 for(i=0; i<15; i++)
00277 {
00278 if((x/place) < 10.0)
00279 break;
00280 else
00281 place *= 10.0;
00282 }
00283
00284 if(negative)
00285 rprintfChar('-');
00286 else
00287 rprintfChar('+');
00288
00289
00290 for(i=0; i<numDigits; i++)
00291 {
00292 digit = (x/place);
00293
00294 if(digit | firstplace | (place == 1.0))
00295 {
00296 firstplace = TRUE;
00297 rprintfChar(digit+0x30);
00298 }
00299 else
00300 rprintfChar(' ');
00301
00302 if(place == 1.0)
00303 {
00304 rprintfChar('.');
00305 }
00306
00307 x -= (digit*place);
00308 place /= 10.0;
00309 }
00310 }
00311 #endif
00312
00313 #ifdef RPRINTF_SIMPLE
00314
00315
00316
00317
00318
00319
00320 int rprintf1RamRom(unsigned char stringInRom, const char *format, ...)
00321 {
00322
00323
00324
00325 char format_flag;
00326 unsigned int u_val, div_val, base;
00327 va_list ap;
00328
00329 va_start(ap, format);
00330 for (;;)
00331 {
00332 while ((format_flag = READMEMBYTE(stringInRom,format++) ) != '%')
00333 {
00334 if (!format_flag)
00335 {
00336 va_end(ap);
00337 return(0);
00338 }
00339 rprintfChar(format_flag);
00340 }
00341
00342 switch (format_flag = READMEMBYTE(stringInRom,format++) )
00343 {
00344 case 'c': format_flag = va_arg(ap,int);
00345 default: rprintfChar(format_flag); continue;
00346 case 'd': base = 10; div_val = 10000; goto CONVERSION_LOOP;
00347
00348 case 'x': base = 16; div_val = 0x1000;
00349
00350 CONVERSION_LOOP:
00351 u_val = va_arg(ap,int);
00352 if (format_flag == 'd')
00353 {
00354 if (((int)u_val) < 0)
00355 {
00356 u_val = - u_val;
00357 rprintfChar('-');
00358 }
00359 while (div_val > 1 && div_val > u_val) div_val /= 10;
00360 }
00361 do
00362 {
00363
00364 rprintfu04(u_val/div_val);
00365 u_val %= div_val;
00366 div_val /= base;
00367 } while (div_val);
00368 }
00369 }
00370 va_end(ap);
00371 }
00372 #endif
00373
00374
00375 #ifdef RPRINTF_COMPLEX
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387 int rprintf2RamRom(unsigned char stringInRom, const char *sfmt, ...)
00388 {
00389 register unsigned char *f, *bp;
00390 register long l;
00391 register unsigned long u;
00392 register int i;
00393 register int fmt;
00394 register unsigned char pad = ' ';
00395 int flush_left = 0, f_width = 0, prec = INF, hash = 0, do_long = 0;
00396 int sign = 0;
00397
00398 va_list ap;
00399 va_start(ap, sfmt);
00400
00401 f = (unsigned char *) sfmt;
00402
00403 for (; READMEMBYTE(stringInRom,f); f++)
00404 {
00405 if (READMEMBYTE(stringInRom,f) != '%')
00406 {
00407
00408 rprintfChar(READMEMBYTE(stringInRom,f));
00409 }
00410 else
00411 {
00412 f++;
00413 if (READMEMBYTE(stringInRom,f) == '-')
00414 {
00415 flush_left = 1;
00416 f++;
00417 }
00418 if (READMEMBYTE(stringInRom,f) == '0'
00419 || READMEMBYTE(stringInRom,f) == '.')
00420 {
00421
00422 pad = '0';
00423 f++;
00424 }
00425 if (READMEMBYTE(stringInRom,f) == '*')
00426 {
00427 f_width = va_arg(ap, int);
00428 f++;
00429 }
00430 else if (Isdigit(READMEMBYTE(stringInRom,f)))
00431 {
00432 f_width = atoiRamRom(stringInRom, (char *) f);
00433 while (Isdigit(READMEMBYTE(stringInRom,f)))
00434 f++;
00435 }
00436 if (READMEMBYTE(stringInRom,f) == '.')
00437 {
00438 f++;
00439 if (READMEMBYTE(stringInRom,f) == '*')
00440 {
00441 prec = va_arg(ap, int);
00442 f++;
00443 }
00444 else if (Isdigit(READMEMBYTE(stringInRom,f)))
00445 {
00446 prec = atoiRamRom(stringInRom, (char *) f);
00447 while (Isdigit(READMEMBYTE(stringInRom,f)))
00448 f++;
00449 }
00450 }
00451 if (READMEMBYTE(stringInRom,f) == '#')
00452 {
00453 hash = 1;
00454 f++;
00455 }
00456 if (READMEMBYTE(stringInRom,f) == 'l')
00457 {
00458 do_long = 1;
00459 f++;
00460 }
00461
00462 fmt = READMEMBYTE(stringInRom,f);
00463 bp = buf;
00464 switch (fmt) {
00465 case 'd':
00466 if (do_long)
00467 l = va_arg(ap, long);
00468 else
00469 l = (long) (va_arg(ap, int));
00470 if (l < 0)
00471 {
00472 sign = 1;
00473 l = -l;
00474 }
00475 do {
00476 *bp++ = l % 10 + '0';
00477 } while ((l /= 10) > 0);
00478 if (sign)
00479 *bp++ = '-';
00480 f_width = f_width - (bp - buf);
00481 if (!flush_left)
00482 while (f_width-- > 0)
00483 rprintfChar(pad);
00484 for (bp--; bp >= buf; bp--)
00485 rprintfChar(*bp);
00486 if (flush_left)
00487 while (f_width-- > 0)
00488 rprintfChar(' ');
00489 break;
00490 case 'o':
00491 case 'x':
00492 case 'u':
00493 if (do_long)
00494 u = va_arg(ap, unsigned long);
00495 else
00496 u = (unsigned long) (va_arg(ap, unsigned));
00497 if (fmt == 'u')
00498 {
00499 do {
00500 *bp++ = u % 10 + '0';
00501 } while ((u /= 10) > 0);
00502 }
00503 else if (fmt == 'o')
00504 {
00505 do {
00506 *bp++ = u % 8 + '0';
00507 } while ((u /= 8) > 0);
00508 if (hash)
00509 *bp++ = '0';
00510 }
00511 else if (fmt == 'x')
00512 {
00513 do {
00514 i = u % 16;
00515 if (i < 10)
00516 *bp++ = i + '0';
00517 else
00518 *bp++ = i - 10 + 'a';
00519 } while ((u /= 16) > 0);
00520 if (hash)
00521 {
00522 *bp++ = 'x';
00523 *bp++ = '0';
00524 }
00525 }
00526 i = f_width - (bp - buf);
00527 if (!flush_left)
00528 while (i-- > 0)
00529 rprintfChar(pad);
00530 for (bp--; bp >= buf; bp--)
00531 rprintfChar((int) (*bp));
00532 if (flush_left)
00533 while (i-- > 0)
00534 rprintfChar(' ');
00535 break;
00536 case 'c':
00537 i = va_arg(ap, int);
00538 rprintfChar((int) (i));
00539 break;
00540 case 's':
00541 bp = va_arg(ap, unsigned char *);
00542 if (!bp)
00543 bp = (unsigned char *) "(nil)";
00544 f_width = f_width - strlen((char *) bp);
00545 if (!flush_left)
00546 while (f_width-- > 0)
00547 rprintfChar(pad);
00548 for (i = 0; *bp && i < prec; i++)
00549 {
00550 rprintfChar(*bp);
00551 bp++;
00552 }
00553 if (flush_left)
00554 while (f_width-- > 0)
00555 rprintfChar(' ');
00556 break;
00557 case '%':
00558 rprintfChar('%');
00559 break;
00560 }
00561 flush_left = 0, f_width = 0, prec = INF, hash = 0, do_long = 0;
00562 sign = 0;
00563 pad = ' ';
00564 }
00565 }
00566
00567 va_end(ap);
00568 return 0;
00569 }
00570
00571 unsigned char Isdigit(char c)
00572 {
00573 if((c >= 0x30) && (c <= 0x39))
00574 return TRUE;
00575 else
00576 return FALSE;
00577 }
00578
00579 int atoiRamRom(unsigned char stringInRom, char *str)
00580 {
00581 int num = 0;;
00582
00583 while(Isdigit(READMEMBYTE(stringInRom,str)))
00584 {
00585 num *= 10;
00586 num += ((READMEMBYTE(stringInRom,str++)) - 0x30);
00587 }
00588 return num;
00589 }
00590
00591 #endif
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782