c++ - 没有 PROGMEM 的 Adafruit gfx 库 drawBitmap

标签 c++ arrays arduino gfx progmem

所以我试图将图像的字节数组放入外部 eeprom (c24LC16B) 并使用 Adafruit gfx 库中的 drawBitmap() 函数在 Nokia 3310 LCD(带有 Adafruit PCD8544 库)上绘制它。但问题是,drawBitmap() 只能使用静态字节 PROGMEM 数组。使用不好,我需要从 eeprom 读取 img 数组到缓冲区(字节 buf[504]{}; ),然后在显示器上绘制它。

我尝试了一些我在网上找到的修改,例如将其添加到 Adafruit_GFX.ccp:

void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
                  uint8_t *bitmap, int16_t w, int16_t h,
                  uint16_t color) {

  int16_t i, j, byteWidth = (w + 7) / 8;

  for(j=0; j<h; j++) {
    for(i=0; i<w; i++ ) {
      if(bitRead(bitmap[j], 7-i))  
        drawPixel(x+i, y+j, color);
    }
  }
}

但它仍然只显示垃圾

那么为什么 PROGMEM 和普通数组有这么大的区别呢?来自 PROGMEM 和来自 SRAM 的字节不一样吗? 也为我的语法感到抱歉。

最佳答案

我做到了! 我所要做的就是编写自己的函数! =D

只需将其添加到 Adafruit_GFX.ccp

void Adafruit_GFX::drawRamBitmap(int pozXi, int pozYi, int h, int w, byte color, byte bg, byte bitmap[], int mapSize) {
  int pozX = pozXi;
  int pozY = pozYi;

  for (int x = 0; x < mapSize; x++) {
    for (byte y = 0; y < 8; y++) {
      byte dummy = bitmap[x] << y;
      if (dummy >= 128) {
        drawPixel(pozX, pozY, color);
      }
      else {
        drawPixel(pozX, pozY, bg);
      }
      pozX++;
      if (pozX == w + pozXi) {
        pozX = pozXi;
        pozY++;
      }
    }
  }
}

void Adafruit_GFX::drawRamBitmap(int pozXi, int pozYi, int h, int w, byte color, byte bitmap[], int mapSize) {
  int pozX = pozXi;
  int pozY = pozYi;

  for (int x = 0; x < mapSize; x++) {
    for (byte y = 0; y < 8; y++) {
      byte dummy = bitmap[x] << y;
      if (dummy >= 128) {
        drawPixel(pozX, pozY, color);
      }
      pozX++;
      if (pozX == w + pozXi) {
        pozX = pozXi;
        pozY++;
      }
    }
  }
}

这是 Adafruit_GFX.h

drawRamBitmap(int pozXi, int pozYi, int h, int w, byte color, byte bg, byte bitmap[], int mapSize),
drawRamBitmap(int pozXi, int pozYi, int h, int w, byte color, byte bitmap[], int mapSize),

用法:

drawRambitmap(x,y,h,w,color,byte_array_of_img, size_of_array);

drawRambitmap(x,y,h,w,color,background_color,byte_array_of_img, size_of_array);

关于c++ - 没有 PROGMEM 的 Adafruit gfx 库 drawBitmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32417848/

相关文章:

c++ - 函数模板作为成员 - GCC 与 CLANG

c++ - 无法将合并排序设为 O(n log n)

c - 将数组传递给c中的函数

Python 3 - 解码包含十六进制和 unicode 混合的字节

audio - Wav 文件内容。这些字节值是什么意思

c# - Visual Studio C# 项目中未显示 C++ 错误

c++ - 在 boost 中禁用中断会禁用上下文切换吗?

javascript - 如何在对象中使用 .includes() 或类似方法?

php - 将数组插入数据库表

c++ - 阿杜诺 | RGB LED 灯条 Controller