00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <progmem.h>
00023 #include <string-avr.h>
00024
00025 #include <stdarg.h>
00026
00027
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)?(PRG_RDB(char_ptr)):(*char_ptr))
00036
00037 static unsigned char buf[128];
00038
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 void rprintfNum(char base, char numDigits, char isSigned, char padchar, long n)
00176 {
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 char *p, buf[32];
00190 unsigned long x;
00191 unsigned char count;
00192
00193
00194 if( isSigned && (n < 0) )
00195 {
00196 x = -n;
00197 }
00198 else
00199 {
00200 x = n;
00201 }
00202
00203
00204 count = (numDigits-1)-(isSigned?1:0);
00205 p = buf + sizeof (buf);
00206 *--p = '\0';
00207
00208
00209
00210 *--p = PRG_RDB(HexChars + (x%base)); x /= base;
00211
00212 while(count--)
00213 {
00214 if(x != 0)
00215 {
00216
00217 *--p = PRG_RDB(HexChars + (x%base)); x /= base;
00218 }
00219 else
00220 {
00221
00222 *--p = padchar;
00223 }
00224 }
00225
00226
00227 if( isSigned )
00228 {
00229 if(n < 0)
00230 {
00231 *--p = '-';
00232 }
00233 else if(n > 0)
00234 {
00235 *--p = '+';
00236 }
00237 else
00238 {
00239 *--p = ' ';
00240 }
00241 }
00242
00243
00244 count = numDigits;
00245 while(count--)
00246 {
00247 rprintfChar(*p++);
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
00302
00303 int rprintf1RamRom(unsigned char stringInRom, const char *format, ...)
00304 {
00305
00306
00307
00308 char format_flag;
00309 unsigned int u_val, div_val, base;
00310 va_list ap;
00311
00312 va_start(ap, format);
00313 for (;;)
00314 {
00315 while ((format_flag = READMEMBYTE(stringInRom,format++) ) != '%')
00316 {
00317 if (!format_flag)
00318 {
00319 va_end(ap);
00320 return(0);
00321 }
00322 rprintfChar(format_flag);
00323 }
00324
00325 switch (format_flag = READMEMBYTE(stringInRom,format++) )
00326 {
00327 case 'c': format_flag = va_arg(ap,int);
00328 default: rprintfChar(format_flag); continue;
00329 case 'd': base = 10; div_val = 10000; goto CONVERSION_LOOP;
00330 case 'x': base = 16; div_val = 0x10;
00331
00332 CONVERSION_LOOP:
00333 u_val = va_arg(ap,int);
00334 if (format_flag == 'd')
00335 {
00336 if (((int)u_val) < 0)
00337 {
00338 u_val = - u_val;
00339 rprintfChar('-');
00340 }
00341 while (div_val > 1 && div_val > u_val) div_val /= 10;
00342 }
00343 do
00344 {
00345 rprintfChar(PRG_RDB(HexChars+(u_val/div_val)));
00346 u_val %= div_val;
00347 div_val /= base;
00348 } while (div_val);
00349 }
00350 }
00351 va_end(ap);
00352 }
00353
00354 unsigned char Isdigit(char c)
00355 {
00356 if((c >= 0x30) && (c <= 0x39))
00357 return TRUE;
00358 else
00359 return FALSE;
00360 }
00361
00362 int atoiRamRom(unsigned char stringInRom, char *str)
00363 {
00364 int num = 0;;
00365
00366 while(Isdigit(READMEMBYTE(stringInRom,str)))
00367 {
00368 num *= 10;
00369 num += ((READMEMBYTE(stringInRom,str++)) - 0x30);
00370 }
00371 return num;
00372 }
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384 int rprintf2RamRom(unsigned char stringInRom, const char *sfmt, ...)
00385 {
00386 register unsigned char *f, *bp;
00387 register long l;
00388 register unsigned long u;
00389 register int i;
00390 register int fmt;
00391 register unsigned char pad = ' ';
00392 int flush_left = 0, f_width = 0, prec = INF, hash = 0, do_long = 0;
00393 int sign = 0;
00394
00395 va_list ap;
00396 va_start(ap, sfmt);
00397
00398 f = (unsigned char *) sfmt;
00399
00400 for (; READMEMBYTE(stringInRom,f); f++)
00401 {
00402 if (READMEMBYTE(stringInRom,f) != '%')
00403 {
00404
00405 rprintfChar(READMEMBYTE(stringInRom,f));
00406 }
00407 else
00408 {
00409 f++;
00410 if (READMEMBYTE(stringInRom,f) == '-')
00411 {
00412 flush_left = 1;
00413 f++;
00414 }
00415 if (READMEMBYTE(stringInRom,f) == '0'
00416 || READMEMBYTE(stringInRom,f) == '.')
00417 {
00418
00419 pad = '0';
00420 f++;
00421 }
00422 if (READMEMBYTE(stringInRom,f) == '*')
00423 {
00424 f_width = va_arg(ap, int);
00425 f++;
00426 }
00427 else if (Isdigit(READMEMBYTE(stringInRom,f)))
00428 {
00429 f_width = atoiRamRom(stringInRom, (char *) f);
00430 while (Isdigit(READMEMBYTE(stringInRom,f)))
00431 f++;
00432 }
00433 if (READMEMBYTE(stringInRom,f) == '.')
00434 {
00435 f++;
00436 if (READMEMBYTE(stringInRom,f) == '*')
00437 {
00438 prec = va_arg(ap, int);
00439 f++;
00440 }
00441 else if (Isdigit(READMEMBYTE(stringInRom,f)))
00442 {
00443 prec = atoiRamRom(stringInRom, (char *) f);
00444 while (Isdigit(READMEMBYTE(stringInRom,f)))
00445 f++;
00446 }
00447 }
00448 if (READMEMBYTE(stringInRom,f) == '#')
00449 {
00450 hash = 1;
00451 f++;
00452 }
00453 if (READMEMBYTE(stringInRom,f) == 'l')
00454 {
00455 do_long = 1;
00456 f++;
00457 }
00458
00459 fmt = READMEMBYTE(stringInRom,f);
00460 bp = buf;
00461 switch (fmt) {
00462 case 'd':
00463 if (do_long)
00464 l = va_arg(ap, long);
00465 else
00466 l = (long) (va_arg(ap, int));
00467 if (l < 0)
00468 {
00469 sign = 1;
00470 l = -l;
00471 }
00472 do {
00473 *bp++ = l % 10 + '0';
00474 } while ((l /= 10) > 0);
00475 if (sign)
00476 *bp++ = '-';
00477 f_width = f_width - (bp - buf);
00478 if (!flush_left)
00479 while (f_width-- > 0)
00480 rprintfChar(pad);
00481 for (bp--; bp >= buf; bp--)
00482 rprintfChar(*bp);
00483 if (flush_left)
00484 while (f_width-- > 0)
00485 rprintfChar(' ');
00486 break;
00487 case 'o':
00488 case 'x':
00489 case 'u':
00490 if (do_long)
00491 u = va_arg(ap, unsigned long);
00492 else
00493 u = (unsigned long) (va_arg(ap, unsigned));
00494 if (fmt == 'u')
00495 {
00496 do {
00497 *bp++ = u % 10 + '0';
00498 } while ((u /= 10) > 0);
00499 }
00500 else if (fmt == 'o')
00501 {
00502 do {
00503 *bp++ = u % 8 + '0';
00504 } while ((u /= 8) > 0);
00505 if (hash)
00506 *bp++ = '0';
00507 }
00508 else if (fmt == 'x')
00509 {
00510 do {
00511 i = u % 16;
00512 if (i < 10)
00513 *bp++ = i + '0';
00514 else
00515 *bp++ = i - 10 + 'a';
00516 } while ((u /= 16) > 0);
00517 if (hash)
00518 {
00519 *bp++ = 'x';
00520 *bp++ = '0';
00521 }
00522 }
00523 i = f_width - (bp - buf);
00524 if (!flush_left)
00525 while (i-- > 0)
00526 rprintfChar(pad);
00527 for (bp--; bp >= buf; bp--)
00528 rprintfChar((int) (*bp));
00529 if (flush_left)
00530 while (i-- > 0)
00531 rprintfChar(' ');
00532 break;
00533 case 'c':
00534 i = va_arg(ap, int);
00535 rprintfChar((int) (i));
00536 break;
00537 case 's':
00538 bp = va_arg(ap, unsigned char *);
00539 if (!bp)
00540 bp = (unsigned char *) "(nil)";
00541 f_width = f_width - strlen((char *) bp);
00542 if (!flush_left)
00543 while (f_width-- > 0)
00544 rprintfChar(pad);
00545 for (i = 0; *bp && i < prec; i++)
00546 {
00547 rprintfChar(*bp);
00548 bp++;
00549 }
00550 if (flush_left)
00551 while (f_width-- > 0)
00552 rprintfChar(' ');
00553 break;
00554 case '%':
00555 rprintfChar('%');
00556 break;
00557 }
00558 flush_left = 0, f_width = 0, prec = INF, hash = 0, do_long = 0;
00559 sign = 0;
00560 pad = ' ';
00561 }
00562 }
00563
00564 va_end(ap);
00565 return 0;
00566 }
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
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
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950