NXC: ANSI printf like function printfEx also for \n, %s
Posted: 24 Jul 2011, 15:36
just to share:
these new macros
printfEx()
together with scrclr()
provide a printf() more like ANSI C with support of "\n"
(but only for 1 variable each):
these new macros
printfEx()
together with scrclr()
provide a printf() more like ANSI C with support of "\n"
(but only for 1 variable each):
Code: Select all
int __CURSOR_X__=0, __CURSOR_Y__=56;
// scrclr() in conio.h
#define scrclr() { \
ClearScreen(); \
__CURSOR_X__=0; __CURSOR_Y__=56; \
}
#define printf1( _x, _y, _format1, _value1) { \
__CURSOR_Y__=_y; \
string sval1 = FormatNum(_format1, _value1); \
TextOut(_x, _y, sval1); \
__CURSOR_X__=_x+6*(strlen(sval1)); \
}
// printf() in stdio.h
#define printfEx(_format1, _value1) { \
int _x=__CURSOR_X__; int _y=__CURSOR_Y__; \
int posn, posf, slen, rlen; \
string rstr, lstr; \
slen=strlen(_format1); \
posn=Pos("\n", _format1); \
posf=Pos("%", _format1); \
if (posn>=0) { \
rlen=slen-posn-1; \
lstr= LeftStr(_format1,posn); \
rstr= RightStr(_format1,rlen); \
strcat(lstr,rstr); \
if(posn<posf) { \
if(_y>=8)_y-=8; _x=0;} \
printf1(_x, _y, lstr, _value1); \
if(posf<posn) { \
if(__CURSOR_Y__>=8) __CURSOR_Y__-=8; \
__CURSOR_X__=0;} \
} \
else printf1(_x, _y, _format1, _value1); \
}
//********************************************************************
task main()
{
printfEx("%s", "text 1");
printfEx("\n%s", "LF => text 2");
printfEx("\n%d ", 1);
printfEx("%d (LF 1 2 LF)\n", 2);
printfEx("%d ", 3);
printfEx("%d ", 4);
printfEx("\n","");
printfEx("\n","");
printfEx("\n","");
printfEx("\n","");
printfEx("\n","");
printfEx("\n","");
printfEx("\n",""); // only valid if Cursor pos > last line!
printfEx("\n%s","press BtnCenter");
while(!ButtonPressed(BTNCENTER,0));
scrclr();
printfEx("\n%d ", 1);
printfEx("%d (LF 1 2 LF)\n", 2);
printfEx("%d ", 3);
printfEx("%d ", 4);
while (1);
}