What a strange implementation (v1.7): static void DRAW_DisplayStringWithMode( u8 x, u8 y, const u8* ptr, u8 len, int mode ) { u8 ref_x = x, i = 0;
/* Send the string character by character on LCD */ while ((*ptr!=0)&&(i<18)) { /* Display one character on LCD */ LCD_DisplayChar( ref_x, y, *ptr, mode ? BGndColor : TextColor, mode ? TextColor : BGndColor, CharMagniCoeff );
/* Increment the column position by 7 */ ref_x+= (7*CharMagniCoeff);
/* Point on the next character */ ptr++;
/* Increment the character counter */ i++; /* If we reach the maximum Line character */ }
while ( i < len ) { /* Display one character on LCD */ LCD_DisplayChar( ref_x, y, ' ', mode ? BGndColor : TextColor, mode ? TextColor : BGndColor, CharMagniCoeff );
/* Increment the column position by 7 */ ref_x += ( 7 * CharMagniCoeff );
/* Increment the character counter */ i++; } }
Why two while loops ?
IMHO should be: Take a look at *ptr!='\0' and then len parameter variable can omited.
1. static void DRAW_DisplayStringWithMode( u8 x, u8 y, const u8* ptr, int mode ) { u8 ref_x = x, i = 0;
/* Send the string character by character on LCD */ while ((*ptr!='\0')&&(i<18)) { /* Display one character on LCD */ LCD_DisplayChar( ref_x, y, *ptr, mode ? BGndColor : TextColor, mode ? TextColor : BGndColor, CharMagniCoeff );
/* Increment the column position by 7 */ ref_x+= (7*CharMagniCoeff);
/* Point on the next character */ ptr++;
/* Increment the character counter */ i++; /* If we reach the maximum Line character */ } } 2. or (not safe due to possibility of string length exceeding):
static void DRAW_DisplayStringWithMode( u8 x, u8 y, const u8* ptr, u8 len, int mode ) { u8 ref_x = x, i = 0;
while ( i < len ) { /* Display one character on LCD */ LCD_DisplayChar( ref_x, y, *ptr + i , mode ? BGndColor : TextColor, mode ? TextColor : BGndColor, CharMagniCoeff );
/* Increment the column position by 7 */ ref_x += ( 7 * CharMagniCoeff );
/* Increment the character counter */ i++; } }
Am I right ?
Last edited by gregor (2008-04-02 22:51:58)
_if technology is the answer what was the question ?
To display the exact number of characters, and to fill with blank characters when the length string is smaller than len, I propose the following (it fixes the issue reported by Alexholy and, as suggested by Gregor, it reduces the overall code size): //------------------------------------------------------------------------------------------------------- static void DRAW_DisplayStringWithMode( u8 x, u8 y, const u8* ptr, u8 len, int mode ) { u8 ref_x = x, i = 0, c;
//up to 18 characters if ( len >= 18 ) { len = 18; }
/* Display each character on LCD */ for ( i = 0 ; i < len ; i++ ) { c = *ptr; if ( c ) { ptr++; /* Point to the next character */ } else { c = ' '; /* fill with space when len exceeds strlen(ptr) */ } /* Display one character on LCD */ LCD_DisplayChar( ref_x, y, c, mode ? BGndColor : TextColor, mode ? TextColor : BGndColor, CharMagniCoeff );
/* Increment the column position by 7 */ ref_x+= (7*CharMagniCoeff); } }
Here is version without unneeded braces and equal sign in the first condition:
static void DRAW_DisplayStringWithMode( u8 x, u8 y, const u8* ptr, u8 len, int mode ) { u8 ref_x = x, i = 0, c;
//up to 18 characters if ( len > 18 ) len = 18;
/* Display each character on LCD */ for ( i = 0 ; i < len ; i++ ) { c = *ptr; if ( c ) ptr++; /* Point to the next character */ else c = ' '; /* fill with space when len exceeds strlen(ptr) */
/* Display one character on LCD */ LCD_DisplayChar( ref_x, y, c, mode ? BGndColor : TextColor, mode ? TextColor : BGndColor, CharMagniCoeff );
/* Increment the column position by 7 */ ref_x+= (7*CharMagniCoeff); } }
_if technology is the answer what was the question ?