新闻  |   论坛  |   博客  |   在线研讨会
哈哈,发现altera官方资料的一个错误。。。
502593045 | 2011-04-16 11:50:30    阅读:1758   发布文章

最近在做TFT-LCD显示的实验,参考altera嵌入式系统开发套件中关于屏幕显示字符的驱动,发现了一个驱动程序的错误。
字符串显示程序如下:
  while (string[i]) {
    //Handle newline char here.
    if (string[i] == '\n') {
      horiz_offset = original_horiz_offset;
      vert_offset += font['|' - 33].bounds_height;  // we'll use "|" to add the line to line spacing
      i++;
      continue;
    }
    // Lay down that character and increment our offsets.
    if(string[i] == 32)  // this is a space in ASCII
    {
      if(background_color != CLEAR_BACKGROUND)  // need to fill in this spot (setting foreground to background color so that it looks like a drawn in box)
      {
        vid_print_char_alpha (horiz_offset, vert_offset, background_color, '-', background_color, font, display);
      }
      horiz_offset += font[45 - 33].bounds_width;  // using the width of the '-' character for the space width since it's not present in the font
    }

    else if( string[i] < 33 || string[i] > 126 ) // Print '.' if it's a character we dont support
    {
      vid_print_char_alpha (horiz_offset, vert_offset, color, '.', background_color, font, display);
      horiz_offset += font['.' - 33].bounds_width;
    }  
  
    else if(string[i] == '\t')  // this is a tab  '\t'
    {
      for( tab = 0; tab < TAB_SPACING; tab++ )
      {
        if(background_color != CLEAR_BACKGROUND)  // need to fill in this spot (setting foreground to background color so that it looks like a drawn in box)
        {
          vid_print_char_alpha (horiz_offset, vert_offset, background_color, '-', background_color, font, display);
        }
        horiz_offset += font[45 - 33].bounds_width;  // using the width of the '-' character for the space width since it's not present in the font
      }
    }


    else
    {
      vid_print_char_alpha (horiz_offset, vert_offset, color, string[i], background_color, font, display);
      horiz_offset += font[string[i] - 33].bounds_width;
    }
    i++;
  }
功能是将预先定义的字符串显示在彩屏上。可以显示的字符为ASCII码值为33和126之间的字符,如果有'\n'则换行,有空格符(ASCII码为32)则空一格,有'\t'(ASCII码为9)则空出tab(可以自己定义tab的长度);如果字符串中有其他ASCII码值的字符,则显示"."(一个小数点)。
实验发现,定义字符串如"abcd\tefg",显示的却是abcd.efg。即'\t'只显示了一个小数点。经检查,原来是因为'\t'程序段放在了判别ASCII码值大小之后,而'\t'的ASCII码值为9,所以,字符串中的'\t'被首先认为是33~126之外的字符而输出"."。如果将'\t'判别程序段放在判别ASCII码值大小程序段之前,问题得到了解决。更正后的程序如下:
 while (string[i]) {
    //Handle newline char here.
    if (string[i] == '\n') {
      horiz_offset = original_horiz_offset;
      vert_offset += font['|' - 33].bounds_height;  // we'll use "|" to add the line to line spacing
      i++;
      continue;
    }
    // Lay down that character and increment our offsets.
    if(string[i] == 32)  // this is a space in ASCII
    {
      if(background_color != CLEAR_BACKGROUND)  // need to fill in this spot (setting foreground to background color so that it looks like a drawn in box)
      {
        vid_print_char_alpha (horiz_offset, vert_offset, background_color, '-', background_color, font, display);
      }
      horiz_offset += font[45 - 33].bounds_width;  // using the width of the '-' character for the space width since it's not present in the font
    }

    else if(string[i] == '\t')  // this is a tab  '\t'
    {
      for( tab = 0; tab < TAB_SPACING; tab++ )
      {
        if(background_color != CLEAR_BACKGROUND)  // need to fill in this spot (setting foreground to background color so that it looks like a drawn in box)
        {
          vid_print_char_alpha (horiz_offset, vert_offset, background_color, '-', background_color, font, display);
        }
        horiz_offset += font[45 - 33].bounds_width;  // using the width of the '-' character for the space width since it's not present in the font
      }
    }

    else if( string[i] < 33 || string[i] > 126 ) // Print '.' if it's a character we dont support
    {
      vid_print_char_alpha (horiz_offset, vert_offset, color, '.', background_color, font, display);
      horiz_offset += font['.' - 33].bounds_width;
    }  
  

    else
    {
      vid_print_char_alpha (horiz_offset, vert_offset, color, string[i], background_color, font, display);
      horiz_offset += font[string[i] - 33].bounds_width;
    }
    i++;
  }

参与讨论
登录后参与讨论
jackwang  2011-04-20 11:02:20 

厉害的说~~

dreamjsc  2011-04-16 13:13:23 

很细心,我们也不能完全相信官方资料,官方资料也是人写的!

虾虽在江湖,江湖却没有关于虾的传说!
推荐文章
最近访客