/var/www/restricted/ssh/stm32/www/stm32circle/ STM CircleOS forum / DRAW_DisplayString - faulty implementation (1.6, 1.7)

Username:     
Password:     
             

Forum
  • Index
  •  » circleOS
  •  » DRAW_DisplayString - faulty implementation (1.6, 1.7)

# 1   2008-04-02 17:40:00 DRAW_DisplayString - faulty implementation (1.6, 1.7)

alexholy
New member
Registered: 2008-03-27
Posts: 3

DRAW_DisplayString - faulty implementation (1.6, 1.7)

There is a fundamental flaw in the DRAW_DisplayStringWithMode base function.

In this function, the string is scanned for \0 terminator, regardless of the length supplied as parameter.
This example shows the bug:

      char *str = "01234567";
      DRAW_DisplayString(0, 24, str, 4);

prints "01234567" instead of printing "0123".

This is clearly a bug and should be fixed by correcting the first loop in DRAW_DisplayStringWithMode .

Offline

 

# 2   2008-04-02 20:57:51 DRAW_DisplayString - faulty implementation (1.6, 1.7)

gregor
Member
From: Bydgoszcz/POLAND
Registered: 2008-03-12
Posts: 16
Website

Re: DRAW_DisplayString - faulty implementation (1.6, 1.7)

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 ?

Offline

 

# 3   2008-04-03 06:50:33 DRAW_DisplayString - faulty implementation (1.6, 1.7)

Francis
Administrator
From: France-Grenoble
Registered: 2007-07-09
Posts: 890

Re: DRAW_DisplayString - faulty implementation (1.6, 1.7)

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);
      }
   }


Thanks.

Offline

 

# 4   2008-04-03 16:09:56 DRAW_DisplayString - faulty implementation (1.6, 1.7)

gregor
Member
From: Bydgoszcz/POLAND
Registered: 2008-03-12
Posts: 16
Website

Re: DRAW_DisplayString - faulty implementation (1.6, 1.7)

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 ?

Offline

 

  • Index
  •  » circleOS
  •  » DRAW_DisplayString - faulty implementation (1.6, 1.7)

Board footer