加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

string – 在FLASH中保留数据数组(Font) – 在AVR GCC中的PROGME

发布时间:2020-12-15 07:22:53 所属栏目:百科 来源:网络整理
导读:啊,PROGMEM,指针,指针指针,指针地址……我的头颅. 我有一个相关字体的数据数组 const uint8_t dejaVuSans9ptBitmaps[] = { /* @0 ' ' (5 pixels wide) */ 0x00,/* */ 0x00,/* */... 我添加了PROGMEM const uint8_t dejaVuSans9ptBitmaps[] PROGMEM = 这是在
啊,PROGMEM,指针,指针指针,指针地址……我的头颅.

我有一个相关字体的数据数组

const uint8_t dejaVuSans9ptBitmaps[] = 
{
    /* @0 ' ' (5 pixels wide) */
    0x00,/*          */
    0x00,/*          */
...

我添加了PROGMEM

const uint8_t dejaVuSans9ptBitmaps[] PROGMEM =

这是在另一个结构中引用的;

const FONT_INFO dejaVuSans9ptFontInfo = {
   13,' ','~',dejaVuSans9ptDescriptors,dejaVuSans9ptBitmaps,};

结构定义为;

typedef struct {
   const uint8_t           height;       
   const uint8_t           startChar;    
   const uint8_t           endChar;      
   const FONT_CHAR_INFO*   charInfo;    
   const uint8_t*          data;         
} FONT_INFO;

假设需要改变,我是否正确;

typedef struct {
   const uint8_t           height;       
   const uint8_t           startChar;    
   const uint8_t           endChar;      
   const FONT_CHAR_INFO*   charInfo;    
   const PGM_P             data;         
} FONT_INFO;

当我这样做时,它会抱怨

warning: pointer targets in initialization differ in signedness

对于FONT_INFO变量中的这一特定行;

const FONT_INFO dejaVuSans9ptFontInfo = {
    13,--> dejaVuSans9ptBitmaps,<--
};

然后使用该函数绘制;

void drawString(uint16_t x,uint16_t y,uint16_t color,const FONT_INFO *fontInfo,char *str) {
    ...
    drawCharBitmap(currentX,y,color,&fontInfo->data[charOffset],charWidth,fontInfo->height);
    ...

最终绘制了字形;

void drawCharBitmap(const uint16_t xPixel,const uint16_t yPixel,const uint8_t *glyph,uint8_t cols,uint8_t rows) {
   ...
      if (glyph[indexIntoGlyph] & (0X80)) drawPixel(currentX,currentY,color);
   ...

我在我的头上:/任何人都可以给我一些方向吗?我花了好几个小时尝试使用PGM_P,而pgm_read_byte等无济于事 – 我总是在屏幕上看到垃圾.

救救我!

解决方法

好的,我想我明白这里发生了什么.

首先,const uint8_t * data是指向存储在PROGMEM中的数据的指针.

在函数void drawString(uint16_t x,const FONT_INFO * fontInfo,char * str)中,我们传递一个指向fontInfo的指针.

要继续,以下是很重要的理解;

fontInfo.data
(*ptr_to_fontInfo).data
ptr_to_fontInfo->data

都是一样的.所以ptr_to_fontInfo->数据返回数据(不是地址)

然后使用&运算符,我们将此数据的’地址’传递给下一个函数

drawCharBitmap(currentX,fontInfo->height)

这个地址存储在声明的指针变量unint8_t * glyph here中;

void drawCharBitmap(const uint16_t xPixel,uint8_t rows)

牢记这一点;

int *ptr;
int a;

ptr = &a;

然后,字形现在指向与fontInfo-> data [charOffset]相同的地址.

接下来要知道的是;

a[b] in C is just a fancy way for writing *(a + b)

所以字形是一个指针,当像这个字形[indexIntoGlyph]一样使用时,它与*(字形indexIntoGlyph)相同,解除引用运算符*表示我们获取该地址的数据.

从那里,我们可以像wex描述的那样使用pgm规则;

If the variable is in PROGMEM,you use pgm_read_byte() as a
replacement for the dereference operator *. For “normal” variables in
RAM you can always write *(&a) instead of just a to return the value
of the variable a; so to return a 8-bit wide variable from progmem you
write pgm_read_byte(&x).

希望这个解释是正确的,并帮助人们(像我这样的新手!)更好地理解它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读