I’m trying to read a JPG file from SD card and display it on ST7789. But it can read the SD card file, but it can’t display it.
Also, once I use the relevant code shown, the screen doesn’t fill completely, it just shows a yellow line in the upper left corner.
The following code fills the screen completely:
#include <Arduino_GFX_Library.h>
#define MISO 2
#define SCLK 14 //SCL
#define MOSI 15 //SDA
#define SD_CS 13
#define TFT_CS 18
#define TFT_BLK -1
#define TFT_DC 19
#define TFT_RST 5Arduino_DataBus *bus = new Arduino_ESP32SPI(TFT_DC, TFT_CS, 22, 21, -1, VSPI);
Arduino_GFX *gfx = new Arduino_ST7789(bus, TFT_RST, 3, true, 240, 240, 0, 80, 0, 80);void screenTest(){
gfx->fillScreen(BLACK);
gfx->fillScreen(RED);
delay(1000);
gfx->fillScreen(BLACK);
gfx->fillScreen(GREEN);
delay(1000);
gfx->fillScreen(BLACK);
gfx->fillScreen(BLUE);
delay(1000);
gfx->fillScreen(BLACK);
}void setup(){
gfx->begin();
gfx->fillScreen(BLACK);
}void loop(){
screenTest();
delay(1000);
}
The following code fails to display the image and completely fill the screen:
main.cpp
#include <Arduino.h>
#include <Arduino_GFX_Library.h>
#include “JpegClass.h”
#include <FS.h>
#include <SD.h>
#include <SPI.h>#define MISO 2
#define SCLK 14 //SCL
#define MOSI 15 //SDA
#define SD_CS 13
#define TFT_RST 5Arduino_DataBus *bus = new Arduino_ESP32SPI(19, 18, 22, 21, -1);
Arduino_GFX *gfx = new Arduino_ST7789(bus, TFT_RST, 3, true, 240, 240, 0, 80, 0, 80);static JpegClass jpegClass;
static int jpegDrawCallback(JPEGDRAW *pDraw){
gfx->draw16bitBeRGBBitmap(pDraw->x, pDraw->y, pDraw->pPixels, pDraw->iWidth, pDraw->iHeight);
return 1;
}void printDirectory(File dir, int numTabs){
while( true ){
File entry = dir.openNextFile();
if( !entry ){
// No more files. Return to the first file in the directory
dir.rewindDirectory();
break;
}for( uint8_t i=0 ; i<numTabs ; i++ ){ Serial.print('\t'); } Serial.print(entry.name()); if( entry.isDirectory() ){ Serial.println("/"); printDirectory(entry, numTabs+1); } else{ //Files have sizes, directories do not Serial.print("\t\t"); Serial.println(entry.size(), DEC); jpegClass.draw(entry, jpegDrawCallback, true, 0, 0, gfx->width(), gfx->height()); delay(5000); //图片显示时长,单位ms } }
}
void setup(){
Serial.begin(115200);//Init Display gfx->begin(); gfx->fillScreen(BLACK); //Init MicroSD SPI.begin(SCLK, MISO, MOSI, SD_CS); if( !SD.begin(SD_CS) ){ Serial.println("Card Mount Failed"); gfx->fillScreen(RED); return; } uint8_t cardType = SD.cardType(); if( cardType == CARD_NONE ){ Serial.println("No SD card attached"); gfx->fillScreen(RED); return; } Serial.print("SD Card Type: "); switch( cardType ){ case CARD_MMC: Serial.println("MMC"); break; case CARD_SD: Serial.println("SDSC"); break; case CARD_SDHC: Serial.println("SDHC"); break; default: Serial.println("UNKNOWN"); break; }
}
void loop(){
unsigned long start = millis();
File root = SD.open(“/img”);
printDirectory(root, 0);
Serial.printf(“Time used: %lu\n”, millis() - start);gfx->fillScreen(YELLOW); delay(2000);
}
JpegClass.h
/*******************************************************************************
- JPEGDEC Wrapper Class
- Dependent libraries:
- JPEGDEC: GitHub - bitbank2/JPEGDEC: An optimized JPEG decoder for Arduino
******************************************************************************/
#ifndef JPEGCLASS_H
#define JPEGCLASS_H#include <JPEGDEC.h>
class JpegClass
{
public:
void draw( File f, JPEG_DRAW_CALLBACK *jpegDrawCallback, bool useBigEndian, int x, int y, int widthLimit, int heightLimit )
{
_jpeg.open(f, jpegDrawCallback);// scale to fit height int _scale; int iMaxMCUs; float ratio = (float)_jpeg.getHeight() / heightLimit; if (ratio <= 1){ _scale = 0; iMaxMCUs = widthLimit / 16; } else if (ratio <= 2){ _scale = JPEG_SCALE_HALF; iMaxMCUs = widthLimit / 8; } else if (ratio <= 4){ _scale = JPEG_SCALE_QUARTER; iMaxMCUs = widthLimit / 4; } else{ _scale = JPEG_SCALE_EIGHTH; iMaxMCUs = widthLimit / 2; } _jpeg.setMaxOutputSize(iMaxMCUs); if (useBigEndian){ _jpeg.setPixelType(RGB565_BIG_ENDIAN); } _jpeg.decode(x, y, _scale); _jpeg.close(); }
private:
JPEGDEC _jpeg;
};#endif // JPEGCLASS_H