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 "rprintf.h"
00027
00028 #ifndef TRUE
00029 #define TRUE -1
00030 #define FALSE 0
00031 #endif
00032
00033 #define INF 32766 // maximum field size to print
00034 #define READMEMBYTE(a,char_ptr) ((a)?(PRG_RDB(char_ptr)):(*char_ptr))
00035
00036 #ifdef RPRINTF_COMPLEX
00037 static unsigned char buf[128];
00038 #endif
00039
00040
00041
00042
00043
00044 static char __attribute__ ((progmem)) HexChars[] = "0123456789ABCDEF";
00045
00046
00047 static void (*rputchar)(unsigned char c);
00048
00049
00050
00051
00052 void rprintfInit(void (*putchar_func)(unsigned char c))
00053 {
00054 rputchar = putchar_func;
00055 }
00056
00057
00058
00059 inline void rprintfChar(unsigned char c)
00060 {
00061
00062 rputchar(c);
00063 }
00064
00065
00066
00067 void rprintfStr(char str[])
00068 {
00069
00070
00071 if (!str) return;
00072
00073
00074 while (*str)
00075 rprintfChar(*str++);
00076 }
00077
00078
00079
00080
00081
00082 void rprintfStrLen(char str[], unsigned char start, unsigned char len)
00083 {
00084 register char i;
00085
00086
00087 if (!str) return;
00088
00089 for(i=0; i<start; i++)
00090 {
00091
00092 if(*str) str++;
00093 }
00094
00095
00096 for(i=0; i<len; i++)
00097 {
00098
00099
00100 if(*str)
00101 rprintfChar(*str++);
00102 else
00103 rprintfChar(' ');
00104 }
00105
00106 }
00107
00108
00109
00110 void rprintfProgStr(char str[])
00111 {
00112
00113 register char c;
00114
00115
00116 if (!str) return;
00117
00118
00119 while((c = PRG_RDB(str++)))
00120 rprintfChar(c);
00121 }
00122
00123
00124
00125 void rprintfCRLF(void)
00126 {
00127
00128 rprintfChar('\r');
00129 rprintfChar('\n');
00130 }
00131
00132
00133
00134 void rprintfu04(unsigned char data)
00135 {
00136
00137
00138
00139
00140
00141
00142 rprintfChar(PRG_RDB( HexChars+(data&0x0f) ));
00143 }
00144
00145
00146
00147 void rprintfu08(unsigned char data)
00148 {
00149
00150 rprintfu04(data>>4);
00151 rprintfu04(data);
00152 }
00153
00154
00155
00156 void rprintfu16(unsigned short data)
00157 {
00158
00159 rprintfu08(data>>8);
00160 rprintfu08(data);
00161 }
00162
00163
00164
00165 void rprintfu32(unsigned long data)
00166 {
00167
00168 rprintfu16(data>>16);
00169 rprintfu16(data);
00170 }
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 void rprintfNum(char base, char numDigits, char isSigned, char padchar, long n)
00185 {
00186
00187
00188 char *p, buf[32];
00189 unsigned long x;
00190 unsigned char count;
00191
00192
00193 if( isSigned && (n < 0) )
00194 {
00195 x = -n;
00196 }
00197 else
00198 {
00199 x = n;
00200 }
00201
00202
00203 count = (numDigits-1)-(isSigned?1:0);
00204 p = buf + sizeof (buf);
00205 *--p = '\0';
00206
00207
00208
00209 *--p = PRG_RDB(HexChars + (x%base)); x /= base;
00210
00211 while(count--)
00212 {
00213 if(x != 0)
00214 {
00215
00216 *--p = PRG_RDB(HexChars + (x%base)); x /= base;
00217 }
00218 else
00219 {
00220
00221 *--p = padchar;
00222 }
00223 }
00224
00225
00226 if( isSigned )
00227 {
00228 if(n < 0)
00229 {
00230 *--p = '-';
00231 }
00232 else if(n > 0)
00233 {
00234 *--p = '+';
00235 }
00236 else
00237 {
00238 *--p = ' ';
00239 }
00240 }
00241
00242
00243 count = numDigits;
00244 while(count--)
00245 {
00246 rprintfChar(*p++);
00247 }
00248 }
00249
00250
00251
00252 void rprintfFloat(char numDigits, double x)
00253 {
00254 unsigned char firstplace = FALSE;
00255 unsigned char negative;
00256 unsigned char i, digit;
00257 double place = 1.0;
00258
00259
00260 negative = (x<0);
00261
00262 x = (x>0)?(x):(-x);
00263
00264
00265 for(i=0; i<15; i++)
00266 {
00267 if((x/place) < 10.0)
00268 break;
00269 else
00270 place *= 10.0;
00271 }
00272
00273 if(negative)
00274 rprintfChar('-');
00275 else
00276 rprintfChar('+');
00277
00278
00279 for(i=0; i<numDigits; i++)
00280 {
00281 digit = (x/place);
00282
00283 if(digit | firstplace | (place == 1.0))
00284 {
00285 firstplace = TRUE;
00286 rprintfChar(digit+0x30);
00287 }
00288 else
00289 rprintfChar(' ');
00290
00291 if(place == 1.0)
00292 {
00293 rprintfChar('.');
00294 }
00295
00296 x -= (digit*place);
00297 place /= 10.0;
00298 }
00299 }
00300
00301 #ifdef RPRINTF_SIMPLE
00302
00303
00304 // Supports:
00305
00306
00307
00308 int rprintf1RamRom(unsigned char stringInRom, const char *format, ...)
00309 {
00310
00311
00312
00313 char format_flag;
00314 unsigned int u_val, div_val, base;
00315 va_list ap;
00316
00317 va_start(ap, format);
00318 for (;;)
00319 {
00320 while ((format_flag = READMEMBYTE(stringInRom,format++) ) != '%')
00321 {
00322 if (!format_flag)
00323 {
00324 va_end(ap);
00325 return(0);
00326 }
00327 rprintfChar(format_flag);
00328 }
00329
00330 switch (format_flag = READMEMBYTE(stringInRom,format++) )
00331 {
00332 case 'c': format_flag = va_arg(ap,int);
00333 default: rprintfChar(format_flag); continue;
00334 case 'd': base = 10; div_val = 10000; goto CONVERSION_LOOP;
00335 case 'x': base = 16; div_val = 0x10;
00336
00337 CONVERSION_LOOP:
00338 u_val = va_arg(ap,int);
00339 if (format_flag == 'd')
00340 {
00341 if (((int)u_val) < 0)
00342 {
00343 u_val = - u_val;
00344 rprintfChar('-');
00345 }
00346 while (div_val > 1 && div_val > u_val) div_val /= 10;
00347 }
00348 do
00349 {
00350 rprintfChar(PRG_RDB(HexChars+(u_val/div_val)));
00351 u_val %= div_val;
00352 div_val /= base;
00353 } while (div_val);
00354 }
00355 }
00356 va_end(ap);
00357 }
00358 #endif
00359
00360
00361 #ifdef RPRINTF_COMPLEX
00362
00363
00364 // Supports:
00365
00366
00367
00368
00369
00370
00371
00372
00373 int rprintf2RamRom(unsigned char stringInRom, const char *sfmt, ...)
00374 {
00375 register unsigned char *f, *bp;
00376 register long l;
00377 register unsigned long u;
00378 register int i;
00379 register int fmt;
00380 register unsigned char pad = ' ';
00381 int flush_left = 0, f_width = 0, prec = INF, hash = 0, do_long = 0;
00382 int sign = 0;
00383
00384 va_list ap;
00385 va_start(ap, sfmt);
00386
00387 f = (unsigned char *) sfmt;
00388
00389 for (; READMEMBYTE(stringInRom,f); f++)
00390 {
00391 if (READMEMBYTE(stringInRom,f) != '%')
00392 {
00393
00394 rprintfChar(READMEMBYTE(stringInRom,f));
00395 }
00396 else
00397 {
00398 f++;
00399 if (READMEMBYTE(stringInRom,f) == '-')
00400 {
00401 flush_left = 1;
00402 f++;
00403 }
00404 if (READMEMBYTE(stringInRom,f) == '0'
00405 || READMEMBYTE(stringInRom,f) == '.')
00406 {
00407
00408 pad = '0';
00409 f++;
00410 }
00411 if (READMEMBYTE(stringInRom,f) == '*')
00412 {
00413 f_width = va_arg(ap, int);
00414 f++;
00415 }
00416 else if (Isdigit(READMEMBYTE(stringInRom,f)))
00417 {
00418 f_width = atoiRamRom(stringInRom, (char *) f);
00419 while (Isdigit(READMEMBYTE(stringInRom,f)))
00420 f++;
00421 }
00422 if (READMEMBYTE(stringInRom,f) == '.')
00423 {
00424 f++;
00425 if (READMEMBYTE(stringInRom,f) == '*')
00426 {
00427 prec = va_arg(ap, int);
00428 f++;
00429 }
00430 else if (Isdigit(READMEMBYTE(stringInRom,f)))
00431 {
00432 prec = atoiRamRom(stringInRom, (char *) f);
00433 while (Isdigit(READMEMBYTE(stringInRom,f)))
00434 f++;
00435 }
00436 }
00437 if (READMEMBYTE(stringInRom,f) == '#')
00438 {
00439 hash = 1;
00440 f++;
00441 }
00442 if (READMEMBYTE(stringInRom,f) == 'l')
00443 {
00444 do_long = 1;
00445 f++;
00446 }
00447
00448 fmt = READMEMBYTE(stringInRom,f);
00449 bp = buf;
00450 switch (fmt) {
00451 case 'd':
00452 if (do_long)
00453 l = va_arg(ap, long);
00454 else
00455 l = (long) (va_arg(ap, int));
00456 if (l < 0)
00457 {
00458 sign = 1;
00459 l = -l;
00460 }
00461 do {
00462 *bp++ = l % 10 + '0';
00463 } while ((l /= 10) > 0);
00464 if (sign)
00465 *bp++ = '-';
00466 f_width = f_width - (bp - buf);
00467 if (!flush_left)
00468 while (f_width-- > 0)
00469 rprintfChar(pad);
00470 for (bp--; bp >= buf; bp--)
00471 rprintfChar(*bp);
00472 if (flush_left)
00473 while (f_width-- > 0)
00474 rprintfChar(' ');
00475 break;
00476 case 'o':
00477 case 'x':
00478 case 'u':
00479 if (do_long)
00480 u = va_arg(ap, unsigned long);
00481 else
00482 u = (unsigned long) (va_arg(ap, unsigned));
00483 if (fmt == 'u')
00484 {
00485 do {
00486 *bp++ = u % 10 + '0';
00487 } while ((u /= 10) > 0);
00488 }
00489 else if (fmt == 'o')
00490 {
00491 do {
00492 *bp++ = u % 8 + '0';
00493 } while ((u /= 8) > 0);
00494 if (hash)
00495 *bp++ = '0';
00496 }
00497 else if (fmt == 'x')
00498 {
00499 do {
00500 i = u % 16;
00501 if (i < 10)
00502 *bp++ = i + '0';
00503 else
00504 *bp++ = i - 10 + 'a';
00505 } while ((u /= 16) > 0);
00506 if (hash)
00507 {
00508 *bp++ = 'x';
00509 *bp++ = '0';
00510 }
00511 }
00512 i = f_width - (bp - buf);
00513 if (!flush_left)
00514 while (i-- > 0)
00515 rprintfChar(pad);
00516 for (bp--; bp >= buf; bp--)
00517 rprintfChar((int) (*bp));
00518 if (flush_left)
00519 while (i-- > 0)
00520 rprintfChar(' ');
00521 break;
00522 case 'c':
00523 i = va_arg(ap, int);
00524 rprintfChar((int) (i));
00525 break;
00526 case 's':
00527 bp = va_arg(ap, unsigned char *);
00528 if (!bp)
00529 bp = (unsigned char *) "(nil)";
00530 f_width = f_width - strlen((char *) bp);
00531 if (!flush_left)
00532 while (f_width-- > 0)
00533 rprintfChar(pad);
00534 for (i = 0; *bp && i < prec; i++)
00535 {
00536 rprintfChar(*bp);
00537 bp++;
00538 }
00539 if (flush_left)
00540 while (f_width-- > 0)
00541 rprintfChar(' ');
00542 break;
00543 case '%':
00544 rprintfChar('%');
00545 break;
00546 }
00547 flush_left = 0, f_width = 0, prec = INF, hash = 0, do_long = 0;
00548 sign = 0;
00549 pad = ' ';
00550 }
00551 }
00552
00553 va_end(ap);
00554 return 0;
00555 }
00556
00557 unsigned char Isdigit(char c)
00558 {
00559 if((c >= 0x30) && (c <= 0x39))
00560 return TRUE;
00561 else
00562 return FALSE;
00563 }
00564
00565 int atoiRamRom(unsigned char stringInRom, char *str)
00566 {
00567 int num = 0;;
00568
00569 while(Isdigit(READMEMBYTE(stringInRom,str)))
00570 {
00571 num *= 10;
00572 num += ((READMEMBYTE(stringInRom,str++)) - 0x30);
00573 }
00574 return num;
00575 }
00576
00577 #endif
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
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