esp12f + tft 显示图片问题
问题描述
#include <TFT_eSPI.h> #include <TJpg_Decoder.h>
在使用这两库时候,发现图片虽然显示后,但会强制esp8266复位,一直没找到原因,经过一次一次测试,发现TJpg_Decoder仅仅是一个tjpg图片解码的库,并不能之间显示图片,而且库中提供了一个回调函数,来调用屏幕显示位图的函数。
解决
注意这个函数
bool tft_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t *bitmap) { if (y >= tft.height()) return 0; tft.pushImage(x, y, w, h, bitmap); // Return 1 to decode next block return 1; }
在setup中添加这个回调函数即可 下面就是全部代码: 只需要将图片换成自己的就可以完整的显示一个图片。
#include <Arduino.h> #include <TFT_eSPI.h> // Include the graphics library (this includes the sprite functions) #include <TJpg_Decoder.h> #include<EEPROM.h> TFT_eSPI tft = TFT_eSPI(); // Create object "tft" TFT_eSprite img = TFT_eSprite(&tft); // Create Sprite object "img" with pointer to "tft" object #include "../include/jpeg/jpeg1.h" #define LCD_BC 5 // 这个是我的硬件调节背光的IO,大家可以自己忽略 // TFT屏幕输出函数 bool tft_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t *bitmap) { if (y >= tft.height()) return 0; tft.pushImage(x, y, w, h, bitmap); // Return 1 to decode next block return 1; } void setup() { pinMode(LCD_BC,OUTPUT);// 这个是我的硬件调节背光的IO,大家可以自己忽略 analogWrite(LCD_BC,0);// 这个是我的硬件调节背光的IO,大家可以自己忽略 Serial.begin(115200); tft.begin(); tft.fillScreen(TFT_BLUE); // The jpeg image can be scaled by a factor of 1, 2, 4, or 8 TJpgDec.setJpgScale(1); TJpgDec.setSwapBytes(true); TJpgDec.setCallback(tft_output); } void loop() { // Time recorded for test purposes uint32_t t = millis(); // Get the width and height in pixels of the jpeg if you wish uint16_t w = 0, h = 0; TJpgDec.getJpgSize(&w, &h, panda, sizeof(panda)); Serial.print("Width = "); Serial.print(w); Serial.print(", height = "); Serial.println(h); // Draw the image, top left at 0,0 TJpgDec.drawJpg(0, 0, panda, sizeof(panda)); // How much time did rendering take (ESP8266 80MHz 473ms, 160MHz 266ms, ESP32 SPI 116ms) t = millis() - t; Serial.print(t); Serial.println(" ms"); // Wait before drawing again delay(2000); }
下一篇:
MFC编程 -- 列表删除单行及多行操作