Update for ESP8266 and latest Arduino board support package

ESP8266 board support package 2.3.0 or later required
This commit is contained in:
Bodmer
2023-09-24 21:09:12 +01:00
parent c0fad79035
commit bba81c8976
8 changed files with 65 additions and 51 deletions

View File

@ -3,7 +3,7 @@ Arduino JPEGDecoder library
News: October 2019 - *I have created a new Arduino compatible [Jpeg decoder library here.](https://github.com/Bodmer/TJpg_Decoder) This new library is based on TinyJpegDec, this is 60% faster on 32bit processors, it also has a much simpler interface.*
This Arduino library supports the rendering of Jpeg files stored both on SD card and in arrays within program memory (FLASH) onto a TFT display. In addition images stored in the SPIFFS (or LittleFS) Flash filing system or "PROGMEM" arrays can be used with the ESP8266 and ESP32 processors. For the ESP8266 use board Core 2.3.0 (or later) in the Arduino IDE to avoid File definition conflicts if the SPIFFS and SD libraries are used together.
This Arduino library supports the rendering of Jpeg files stored both on SD card and in arrays within program memory (FLASH) onto a TFT display. In addition images stored in the LittleFS Flash filing system or "PROGMEM" arrays can be used with the RP2040, ESP8266 and ESP32 processors. For the ESP8266 use board Core 2.3.0 (or later) in the Arduino IDE to avoid File definition conflicts if the SPIFFS and SD libraries are used together.
The library works on the Arduino Due, ESP32 and ESP8266 (e.g. NodeMCU 1.0). Users have also reported success with the STM32 based processor boards.

View File

@ -8,7 +8,7 @@
//====================================================================================
// Print a SPIFFS directory list (root directory)
//====================================================================================
#ifdef ESP8266
#ifdef ARDUINO_ARCH_ESP8266
void listFiles(void) {
Serial.println();
Serial.println("SPIFFS files found:");

View File

@ -1,6 +1,6 @@
{
"name": "JPEGDecoder",
"version": "1.8.1",
"version": "2.0.0",
"keywords": "jpeg, jpg, decoder, TFT",
"description": "A JPEG decoder library, tested on Mega, Due and ESP8266",
"repository":

View File

@ -1,5 +1,5 @@
name=JPEGDecoder
version=1.8.1
version=2.0.0
author=Bodmer <bodmer@anola.net>, Makoto Kurauchi, Rich Geldreich
maintainer=Bodmer
sentence= Jpeg decoder tested with Arduino Mega, Arduino Due and ESP8266 based NodeMCU 1.0

View File

@ -34,6 +34,8 @@ Bodmer (20/1/17): Prevent deleting the pImage pointer twice (causes an exception
tidy up code.
Bodmer (24/1/17): Correct greyscale images, update examples
Bodmer (26/2/22): Removed deprecated SPIFFS
*/
#include "JPEGDecoder.h"
@ -65,18 +67,18 @@ uint8_t JPEGDecoder::pjpeg_callback(uint8_t* pBuf, uint8_t buf_size, uint8_t *pB
uint8_t JPEGDecoder::pjpeg_need_bytes_callback(uint8_t* pBuf, uint8_t buf_size, uint8_t *pBytes_actually_read, void *pCallback_data) {
uint n;
//pCallback_data;
pCallback_data = pCallback_data; // Supress warning
n = jpg_min(g_nInFileSize - g_nInFileOfs, buf_size);
if (jpg_source == JPEG_ARRAY) { // We are handling an array
for (int i = 0; i < n; i++) {
for (uint i = 0; i < n; i++) {
pBuf[i] = pgm_read_byte(jpg_data++);
//Serial.println(pBuf[i],HEX);
}
}
#ifdef LOAD_SPIFFS
#ifdef LOAD_FLASH_FS
if (jpg_source == JPEG_FS_FILE) g_pInFileFs.read(pBuf,n); // else we are handling a file
#endif
@ -270,10 +272,10 @@ int JPEGDecoder::readSwappedBytes(void) {
}
// Generic file call for SD or SPIFFS, uses leading / to distinguish SPIFFS files
// Generic file call for SD or Little_FS, uses leading / to distinguish Little_FS files
int JPEGDecoder::decodeFile(const char *pFilename){
#if defined (ESP8266) || defined (ESP32)
#if defined (ARDUINO_ARCH_ESP8266) || defined (ESP32)
#if defined (LOAD_SD_LIBRARY) || defined (LOAD_SDFAT_LIBRARY)
if (*pFilename == '/')
#endif
@ -289,7 +291,7 @@ int JPEGDecoder::decodeFile(const char *pFilename){
int JPEGDecoder::decodeFile(const String& pFilename){
#if defined (ESP8266) || defined (ESP32)
#if defined (ARDUINO_ARCH_ESP8266) || defined (ESP32)
#if defined (LOAD_SD_LIBRARY) || defined (LOAD_SDFAT_LIBRARY)
if (pFilename.charAt(0) == '/')
#endif
@ -304,32 +306,32 @@ int JPEGDecoder::decodeFile(const String& pFilename){
}
#ifdef LOAD_SPIFFS
#ifdef LOAD_FLASH_FS
// Call specific to SPIFFS
// Call specific to Little_FS
int JPEGDecoder::decodeFsFile(const char *pFilename) {
fs::File pInFile = SPIFFS.open( pFilename, "r");
fs::File pInFile = LittleFS.open( pFilename, "r");
return decodeFsFile(pInFile);
}
int JPEGDecoder::decodeFsFile(const String& pFilename) {
fs::File pInFile = SPIFFS.open( pFilename, "r");
fs::File pInFile = LittleFS.open( pFilename, "r");
return decodeFsFile(pInFile);
}
int JPEGDecoder::decodeFsFile(fs::File jpgFile) { // This is for the SPIFFS library
int JPEGDecoder::decodeFsFile(fs::File jpgFile) { // This is for the Little_FS library
g_pInFileFs = jpgFile;
jpg_source = JPEG_FS_FILE; // Flag to indicate a SPIFFS file
jpg_source = JPEG_FS_FILE; // Flag to indicate a Little_FS file
if (!g_pInFileFs) {
#ifdef DEBUG
Serial.println("ERROR: SPIFFS file not found!");
Serial.println("ERROR: Little_FS file not found!");
#endif
return -1;
@ -465,7 +467,7 @@ void JPEGDecoder::abort(void) {
if(pImage) delete[] pImage;
pImage = NULL;
#ifdef LOAD_SPIFFS
#ifdef LOAD_FLASH_FS
if (jpg_source == JPEG_FS_FILE) if (g_pInFileFs) g_pInFileFs.close();
#endif

View File

@ -27,32 +27,31 @@ https://github.com/Bodmer/JPEGDecoder
#define PROGMEM __attribute__((section(".fini2")))
#endif
#if defined (ESP8266) || defined (ESP32)
#ifdef ESP32 // SDFAT library not compatible with ESP32
//#undef LOAD_SD_LIBRARY
#undef LOAD_SDFAT_LIBRARY
#endif
//#include "arduino.h"
// New ESP8266 board package uses ARDUINO_ARCH_ESP8266
// old package defined ESP8266
#if defined (ESP8266)
#ifndef ARDUINO_ARCH_ESP8266
#define ARDUINO_ARCH_ESP8266
#endif
#endif
#if defined (ARDUINO_ARCH_ESP8266) || defined (ESP32)
#define LOAD_FLASH_FS
#include <pgmspace.h>
// If the sketch has included FS.h without setting FS_NO_GLOBALS first then it is likely
// there will be a redefinition of 'class fs::File' error due to conflict with the
// SD library, so we can't load the SD library. (At 12/1/18 this appears to be fixed)
//#if !defined (FS_NO_GLOBALS) && defined (FS_H)
//#undef LOAD_SD_LIBRARY
//#undef LOAD_SDFAT_LIBRARY
//#endif
#ifdef ESP32 // SDFAT library not compatible with ESP32
//#undef LOAD_SD_LIBRARY
#undef LOAD_SDFAT_LIBRARY
#endif
#define LOAD_SPIFFS
#define FS_NO_GLOBALS
#include <FS.h>
#ifdef ESP32
#include "SPIFFS.h" // ESP32 only
#endif
#include <LittleFS.h>
#define SPIFFS LittleFS
#elif defined (ARDUINO_ARCH_RP2040)
#define LOAD_FLASH_FS
#include <FS.h>
#include <LittleFS.h>
#define SPIFFS LittleFS
#define TJPGD_LOAD_FFS
#endif
#if defined (LOAD_SD_LIBRARY) || defined (LOAD_SDFAT_LIBRARY)
@ -90,7 +89,7 @@ private:
#if defined (LOAD_SD_LIBRARY) || defined (LOAD_SDFAT_LIBRARY)
File g_pInFileSd;
#endif
#ifdef LOAD_SPIFFS
#ifdef LOAD_FLASH_FS
fs::File g_pInFileFs;
#endif
pjpeg_scan_type_t scan_type;
@ -144,7 +143,7 @@ public:
int decodeSdFile (File g_pInFile);
#endif
#ifdef LOAD_SPIFFS
#ifdef LOAD_FLASH_FS
int decodeFsFile (const char *pFilename);
int decodeFsFile (const String& pFilename);
int decodeFsFile (fs::File g_pInFile);

View File

@ -29,7 +29,7 @@ File jpegFile = SD.open( filename, FILE_READ);
// duplicate definitions in the SD library and the SPIFFS library.
#ifdef ESP8266
#ifdef ARDUINO_ARCH_ESP8266
// Uncomment out the next #define if you want the bytes swapped in the image blocks
// returned by read().
@ -38,5 +38,5 @@ File jpegFile = SD.open( filename, FILE_READ);
// the sketch. Images will look psychedelic with wrong colours if the SPI transmit byte
// order is not right for your sketch!
// #define SWAP_BYTES // Deprecated, only included for backwards compatibility
#define SWAP_BYTES // Deprecated, only included for backwards compatibility
#endif

View File

@ -211,7 +211,9 @@ static uint8 gMaxMCUXSize;
static uint8 gMaxMCUYSize;
static uint16 gMaxMCUSPerRow;
static uint16 gMaxMCUSPerCol;
static uint16 gNumMCUSRemaining;
static uint16 gNumMCUSRemainingX, gNumMCUSRemainingY;
static uint8 gMCUOrg[6];
static pjpeg_need_bytes_callback_t g_pNeedBytesCallback;
@ -1025,6 +1027,7 @@ static uint8 processRestart(void)
//------------------------------------------------------------------------------
// FIXME: findEOI() is not actually called at the end of the image
// (it's optional, and probably not needed on embedded devices)
/*
static uint8 findEOI(void)
{
uint8 c;
@ -1048,6 +1051,7 @@ static uint8 findEOI(void)
return 0;
}
*/
//------------------------------------------------------------------------------
static uint8 checkHuffTables(void)
{
@ -1196,7 +1200,10 @@ static uint8 initFrame(void)
gMaxMCUSPerRow = (gImageXSize + (gMaxMCUXSize - 1)) >> ((gMaxMCUXSize == 8) ? 3 : 4);
gMaxMCUSPerCol = (gImageYSize + (gMaxMCUYSize - 1)) >> ((gMaxMCUYSize == 8) ? 3 : 4);
gNumMCUSRemaining = gMaxMCUSPerRow * gMaxMCUSPerCol;
// This can overflow on large JPEG's.
//gNumMCUSRemaining = gMaxMCUSPerRow * gMaxMCUSPerCol;
gNumMCUSRemainingX = gMaxMCUSPerRow;
gNumMCUSRemainingY = gMaxMCUSPerCol;
return 0;
}
@ -1207,7 +1214,7 @@ static uint8 initFrame(void)
#define PJPG_DCT_SCALE (1U << PJPG_DCT_SCALE_BITS)
#define PJPG_DESCALE(x) PJPG_ARITH_SHIFT_RIGHT_N_16(((x) + (1U << (PJPG_DCT_SCALE_BITS - 1))), PJPG_DCT_SCALE_BITS)
#define PJPG_DESCALE(x) PJPG_ARITH_SHIFT_RIGHT_N_16(((x) + (1 << (PJPG_DCT_SCALE_BITS - 1))), PJPG_DCT_SCALE_BITS)
#define PJPG_WFIX(x) ((x) * PJPG_DCT_SCALE + 0.5f)
@ -2267,14 +2274,20 @@ unsigned char pjpeg_decode_mcu(void)
if (gCallbackStatus)
return gCallbackStatus;
if (!gNumMCUSRemaining)
if ((!gNumMCUSRemainingX) && (!gNumMCUSRemainingY))
return PJPG_NO_MORE_BLOCKS;
status = decodeNextMCU();
if ((status) || (gCallbackStatus))
return gCallbackStatus ? gCallbackStatus : status;
gNumMCUSRemaining--;
gNumMCUSRemainingX--;
if (!gNumMCUSRemainingX)
{
gNumMCUSRemainingY--;
if (gNumMCUSRemainingY > 0)
gNumMCUSRemainingX = gMaxMCUSPerRow;
}
return 0;
}