TFT_espi库显示图片问题

《Arduino》开发 之 TFT_eSPI 库 显示一张彩色图片

https://blog.csdn.net/qq_41868901/article/details/105050371?spm=1001.2101.3001.6650.7&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-7-105050371-blog-125065026.235%5Ev27%5Epc_relevant_3mothn_strategy_recovery&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-7-105050371-blog-125065026.235%5Ev27%5Epc_relevant_3mothn_strategy_recovery&utm_relevant_index=14

中出现了报错,哪里有问题?求纠错

Arduino:1.8.19 (Windows 7), 开发板:"NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Disabled (new aborts on oom), Disabled, Basic SSL ciphers (lower ROM use), 32KB cache + 32KB IRAM (balanced), Use pgm_read macros for IRAM/PROGMEM, 4MB (FS:2MB OTA:~1019KB), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"


In file included from C:\Users\Administrator\Documents\Arduino\libraries\TFT_eSPI/TFT_eSPI.h:102,

                 from C:\Users\Administrator\Desktop\tft\tft.ino:5:

C:\Users\Administrator\Desktop\tft\tft.ino: In function 'void showImage(int32_t, int32_t, int32_t, int32_t, const uint16_t*)':

C:\Users\Administrator\Documents\Arduino\libraries\TFT_eSPI/Processors/TFT_eSPI_ESP8266.h:74:23: error: 'cspinmask' was not declared in this scope

   74 |     #define CS_L GPOC=cspinmask

      |                       ^~~~~~~~~

C:\Users\Administrator\Desktop\tft\tft.ino:32:3: note: in expansion of macro 'CS_L'

   32 |   CS_L;

      |   ^~~~

exit status 1

为开发板 NodeMCU 1.0 (ESP-12E Module) 编译时出错。



在文件 -> 首选项开启
“编译过程中显示详细输出”选项
这份报告会包含更多信息。




环境
```bash
arduino IDE 1.18.9
TFT_eSPI   2.5

报错的代码如下



#include 

#include 
#include "img.h"

TFT_eSPI tft = TFT_eSPI();

void showImage(int32_t x, int32_t y, int32_t w, int32_t h, const uint16_t *data);//函数声明


void setup() {
  tft.init();

  tft.fillScreen(TFT_BLACK);
  tft.setSwapBytes(true);
  //tft.pushImage(0, 0, 128, 72, w);
  //tft.drawCircle(100,100,50,TFT_RED);
  // tft.pushImage(0, 0, 128, 72, w);
  showImage(0, 0, 128, 72, w);
}

void loop() {
 

}
#define PI_BUF_SIZE 128
void showImage(int32_t x, int32_t y, int32_t w, int32_t h, const uint16_t *data) {
  int32_t dx = 0;
  int32_t dy = 0;
  int32_t dw = w;
  int32_t dh = h * 2;

  if (x < 0) {
    dw += x;
    dx = -x;
    x = 0;
  }
  if (y < 0) {
    dh += y;
    dy = -y;
    y = 0;
  }

  if (dw < 1 || dh < 1) return;

  CS_L;

  data += dx + dy * w;

  uint16_t  buffer[PI_BUF_SIZE];
  uint16_t* pix_buffer = buffer;
  uint16_t  high, low;

  tft.setWindow(x, y, x + dw - 1, y + dh - 1);

  // Work out the number whole buffers to send
  uint16_t nb = (dw * dh) / (2 * PI_BUF_SIZE);

  // Fill and send "nb" buffers to TFT
  for (int32_t i = 0; i < nb; i++) {
    for (int32_t j = 0; j < PI_BUF_SIZE; j++) {
      high = pgm_read_word(&data[(i * 2 * PI_BUF_SIZE) + 2 * j + 1]);
      low = pgm_read_word(&data[(i * 2 * PI_BUF_SIZE) + 2 * j ]);
      pix_buffer[j] = (high << 8) + low;
    }
    tft.pushPixels(pix_buffer, PI_BUF_SIZE);
  }

  // Work out number of pixels not yet sent
  uint16_t np = (dw * dh) % (2 * PI_BUF_SIZE);

  // Send any partial buffer left over
  if (np) {
    for (int32_t i = 0; i < np; i++)
    {
      high = pgm_read_word(&data[(nb * 2 * PI_BUF_SIZE) + 2 * i + 1]);
      low = pgm_read_word(&data[(nb * 2 * PI_BUF_SIZE) + 2 * i ]);
      pix_buffer[i] = (high << 8) + low;
    }
    tft.pushPixels(pix_buffer, np);
  }

  CS_H;
}




图片


#include 

const unsigned short w[18440] PROGMEM = { 0X10,0X10,0X00,0X80,0X00,0X48,0X01,0X1B,
0X21,0XE4,0X21,0XE4,0X21,0XE4,0X29,0XC4,0X39,0XA4,0X41,0X64,0X39,0X44,0X39,0X44,

......
};

参考GPT和自己的思路:根据报错信息,可以看到在文件 "TFT_eSPI_ESP8266.h" 中的第74行,在宏定义 "CS_L" 中使用了未定义的变量 "cspinmask",导致编译出错。具体来说,这个变量应该在同样的文件中的第72行进行定义,但是可能在你的环境中没有定义成功。

解决这个问题的方法是,在你的程序中手动定义这个变量。可以在全局范围内添加一个类似下面这样的定义:

const int cspinmask = digitalPinToBitMask(TFT_CS);

这里的 "TFT_CS" 是指 TFT_eSPI 库中定义的片选引脚号,在这个示例中默认为 15。如果你使用的是不同的引脚号,需要对应修改这个值。

把这个定义添加到你的程序中后重新编译即可,应该不会再出现这个报错了。

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/270354
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:关于Ubuntu 安装tftp服务器的问题解决
  • 除此之外, 这篇博客: Arduino开发-TFT_eSPI库学习中的 图片 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    图片取模的画,好多博客,按照bmp格式取RGB565的数组即可
    如果要显示jpg图片的话需要用到图片解码库

    /*  */
    void TFT_eSPI::setSwapBytes(bool swap)
    // 开启显示,一般需要图片显示的时候都加上这个函数即可
    tft.setSwapBytes(true);
    
    void TFT_eSPI::drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color)
    void TFT_eSPI::drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t fgcolor, uint16_t bgcolor)
    

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^