Initial commit
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
/*******************************************************************************
|
||||
* JPEG Image Viewer
|
||||
* This is a simple JPEG image viewer example
|
||||
* Image Source: https://github.blog/2014-11-24-from-sticker-to-sculpture-the-making-of-the-octocat-figurine/
|
||||
*
|
||||
* Dependent libraries:
|
||||
* JPEGDEC: https://github.com/bitbank2/JPEGDEC.git
|
||||
*
|
||||
* Setup steps:
|
||||
* 1. Change your LCD parameters in Arduino_GFX setting
|
||||
* 2. Upload JPEG file
|
||||
* FFat (ESP32):
|
||||
* upload FFat (FatFS) data with ESP32 Sketch Data Upload:
|
||||
* ESP32: https://github.com/lorol/arduino-esp32fs-plugin
|
||||
* LittleFS (ESP32 / ESP8266 / Pico):
|
||||
* upload LittleFS data with ESP8266 LittleFS Data Upload:
|
||||
* ESP32: https://github.com/lorol/arduino-esp32fs-plugin
|
||||
* ESP8266: https://github.com/earlephilhower/arduino-esp8266littlefs-plugin
|
||||
* Pico: https://github.com/earlephilhower/arduino-pico-littlefs-plugin.git
|
||||
* SPIFFS (ESP32):
|
||||
* upload SPIFFS data with ESP32 Sketch Data Upload:
|
||||
* ESP32: https://github.com/lorol/arduino-esp32fs-plugin
|
||||
* SD:
|
||||
* Most Arduino system built-in support SD file system.
|
||||
* Wio Terminal require extra dependant Libraries:
|
||||
* - Seeed_Arduino_FS: https://github.com/Seeed-Studio/Seeed_Arduino_FS.git
|
||||
* - Seeed_Arduino_SFUD: https://github.com/Seeed-Studio/Seeed_Arduino_SFUD.git
|
||||
******************************************************************************/
|
||||
#define JPEG_FILENAME "/octocat.jpg"
|
||||
|
||||
/*******************************************************************************
|
||||
* Start of Arduino_GFX setting
|
||||
*
|
||||
* Arduino_GFX try to find the settings depends on selected board in Arduino IDE
|
||||
* Or you can define the display dev kit not in the board list
|
||||
* Defalult pin list for non display dev kit:
|
||||
* Arduino Nano, Micro and more: CS: 9, DC: 8, RST: 7, BL: 6, SCK: 13, MOSI: 11, MISO: 12
|
||||
* ESP32 various dev board : CS: 5, DC: 27, RST: 33, BL: 22, SCK: 18, MOSI: 23, MISO: nil
|
||||
* ESP32-C3 various dev board : CS: 7, DC: 2, RST: 1, BL: 3, SCK: 4, MOSI: 6, MISO: nil
|
||||
* ESP32-S2 various dev board : CS: 34, DC: 35, RST: 33, BL: 21, SCK: 36, MOSI: 35, MISO: nil
|
||||
* ESP32-S3 various dev board : CS: 40, DC: 41, RST: 42, BL: 48, SCK: 36, MOSI: 35, MISO: nil
|
||||
* ESP8266 various dev board : CS: 15, DC: 4, RST: 2, BL: 5, SCK: 14, MOSI: 13, MISO: 12
|
||||
* Raspberry Pi Pico dev board : CS: 17, DC: 27, RST: 26, BL: 28, SCK: 18, MOSI: 19, MISO: 16
|
||||
* RTL8720 BW16 old patch core : CS: 18, DC: 17, RST: 2, BL: 23, SCK: 19, MOSI: 21, MISO: 20
|
||||
* RTL8720_BW16 Official core : CS: 9, DC: 8, RST: 6, BL: 3, SCK: 10, MOSI: 12, MISO: 11
|
||||
* RTL8722 dev board : CS: 18, DC: 17, RST: 22, BL: 23, SCK: 13, MOSI: 11, MISO: 12
|
||||
* RTL8722_mini dev board : CS: 12, DC: 14, RST: 15, BL: 13, SCK: 11, MOSI: 9, MISO: 10
|
||||
* Seeeduino XIAO dev board : CS: 3, DC: 2, RST: 1, BL: 0, SCK: 8, MOSI: 10, MISO: 9
|
||||
* Teensy 4.1 dev board : CS: 39, DC: 41, RST: 40, BL: 22, SCK: 13, MOSI: 11, MISO: 12
|
||||
******************************************************************************/
|
||||
#include <Arduino_GFX_Library.h>
|
||||
|
||||
#define GFX_BL DF_GFX_BL // default backlight pin, you may replace DF_GFX_BL to actual backlight pin
|
||||
|
||||
/* More dev device declaration: https://github.com/moononournation/Arduino_GFX/wiki/Dev-Device-Declaration */
|
||||
#if defined(DISPLAY_DEV_KIT)
|
||||
Arduino_GFX *gfx = create_default_Arduino_GFX();
|
||||
#else /* !defined(DISPLAY_DEV_KIT) */
|
||||
|
||||
/* More data bus class: https://github.com/moononournation/Arduino_GFX/wiki/Data-Bus-Class */
|
||||
Arduino_DataBus *bus = create_default_Arduino_DataBus();
|
||||
|
||||
/* More display class: https://github.com/moononournation/Arduino_GFX/wiki/Display-Class */
|
||||
Arduino_GFX *gfx = new Arduino_ILI9341(bus, DF_GFX_RST, 3 /* rotation */, false /* IPS */);
|
||||
|
||||
#endif /* !defined(DISPLAY_DEV_KIT) */
|
||||
/*******************************************************************************
|
||||
* End of Arduino_GFX setting
|
||||
******************************************************************************/
|
||||
|
||||
/* Wio Terminal */
|
||||
#if defined(ARDUINO_ARCH_SAMD) && defined(SEEED_GROVE_UI_WIRELESS)
|
||||
#include <Seeed_FS.h>
|
||||
#include <SD/Seeed_SD.h>
|
||||
#elif defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_RASPBERRY_PI_PICO_W)
|
||||
#include <LittleFS.h>
|
||||
#include <SD.h>
|
||||
#elif defined(ESP32)
|
||||
#include <FFat.h>
|
||||
#include <LittleFS.h>
|
||||
#include <SPIFFS.h>
|
||||
#include <SD.h>
|
||||
#elif defined(ESP8266)
|
||||
#include <LittleFS.h>
|
||||
#include <SD.h>
|
||||
#else
|
||||
#include <SD.h>
|
||||
#endif
|
||||
|
||||
#include "JpegFunc.h"
|
||||
|
||||
// pixel drawing callback
|
||||
static int jpegDrawCallback(JPEGDRAW *pDraw)
|
||||
{
|
||||
// Serial.printf("Draw pos = %d,%d. size = %d x %d\n", pDraw->x, pDraw->y, pDraw->iWidth, pDraw->iHeight);
|
||||
gfx->draw16bitBeRGBBitmap(pDraw->x, pDraw->y, pDraw->pPixels, pDraw->iWidth, pDraw->iHeight);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
// while (!Serial);
|
||||
Serial.println("JPEG Image Viewer");
|
||||
|
||||
// Init Display
|
||||
gfx->begin();
|
||||
gfx->fillScreen(BLACK);
|
||||
|
||||
#ifdef GFX_BL
|
||||
pinMode(GFX_BL, OUTPUT);
|
||||
digitalWrite(GFX_BL, HIGH);
|
||||
#endif
|
||||
|
||||
/* Wio Terminal */
|
||||
#if defined(ARDUINO_ARCH_SAMD) && defined(SEEED_GROVE_UI_WIRELESS)
|
||||
if (!SD.begin(SDCARD_SS_PIN, SDCARD_SPI, 4000000UL))
|
||||
#elif defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_RASPBERRY_PI_PICO_W)
|
||||
if (!LittleFS.begin())
|
||||
// if (!SD.begin(SS))
|
||||
#elif defined(ESP32)
|
||||
// if (!FFat.begin())
|
||||
if (!LittleFS.begin())
|
||||
// if (!SPIFFS.begin())
|
||||
// if (!SD.begin(SS))
|
||||
#elif defined(ESP8266)
|
||||
if (!LittleFS.begin())
|
||||
// if (!SD.begin(SS))
|
||||
#else
|
||||
if (!SD.begin())
|
||||
#endif
|
||||
{
|
||||
Serial.println(F("ERROR: File System Mount Failed!"));
|
||||
gfx->println(F("ERROR: File System Mount Failed!"));
|
||||
}
|
||||
else
|
||||
{
|
||||
unsigned long start = millis();
|
||||
jpegDraw(JPEG_FILENAME, jpegDrawCallback, true /* useBigEndian */,
|
||||
0 /* x */, 0 /* y */, gfx->width() /* widthLimit */, gfx->height() /* heightLimit */);
|
||||
Serial.printf("Time used: %lu\n", millis() - start);
|
||||
}
|
||||
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
int w = gfx->width();
|
||||
int h = gfx->height();
|
||||
|
||||
unsigned long start = millis();
|
||||
jpegDraw(JPEG_FILENAME, jpegDrawCallback, true /* useBigEndian */,
|
||||
random(w * 2) - w /* x */,
|
||||
random(h * 2) - h /* y */,
|
||||
w /* widthLimit */, h /* heightLimit */);
|
||||
Serial.printf("Time used: %lu\n", millis() - start);
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/*******************************************************************************
|
||||
* JPEGDEC related function
|
||||
*
|
||||
* Dependent libraries:
|
||||
* JPEGDEC: https://github.com/bitbank2/JPEGDEC.git
|
||||
******************************************************************************/
|
||||
#ifndef _JPEGFUNC_H_
|
||||
#define _JPEGFUNC_H_
|
||||
|
||||
#include <JPEGDEC.h>
|
||||
|
||||
static JPEGDEC _jpeg;
|
||||
static File _f;
|
||||
static int _x, _y, _x_bound, _y_bound;
|
||||
|
||||
static void *jpegOpenFile(const char *szFilename, int32_t *pFileSize)
|
||||
{
|
||||
// Serial.println("jpegOpenFile");
|
||||
#if defined(ARDUINO_ARCH_SAMD) && defined(SEEED_GROVE_UI_WIRELESS)
|
||||
_f = SD.open(szFilename, "r");
|
||||
#elif defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_RASPBERRY_PI_PICO_W)
|
||||
_f = LittleFS.open(szFilename, "r");
|
||||
// _f = SDFS.open(szFilename, "r");
|
||||
#elif defined(ESP32)
|
||||
// _f = FFat.open(szFilename, "r");
|
||||
_f = LittleFS.open(szFilename, "r");
|
||||
// _f = SPIFFS.open(szFilename, "r");
|
||||
// _f = SD.open(szFilename, "r");
|
||||
#elif defined(ESP8266)
|
||||
_f = LittleFS.open(szFilename, "r");
|
||||
// _f = SD.open(szFilename, "r");
|
||||
#else
|
||||
_f = SD.open(szFilename, FILE_READ);
|
||||
#endif
|
||||
*pFileSize = _f.size();
|
||||
return &_f;
|
||||
}
|
||||
|
||||
static void jpegCloseFile(void *pHandle)
|
||||
{
|
||||
// Serial.println("jpegCloseFile");
|
||||
File *f = static_cast<File *>(pHandle);
|
||||
f->close();
|
||||
}
|
||||
|
||||
static int32_t jpegReadFile(JPEGFILE *pFile, uint8_t *pBuf, int32_t iLen)
|
||||
{
|
||||
// Serial.printf("jpegReadFile, iLen: %d\n", iLen);
|
||||
File *f = static_cast<File *>(pFile->fHandle);
|
||||
size_t r = f->read(pBuf, iLen);
|
||||
return r;
|
||||
}
|
||||
|
||||
static int32_t jpegSeekFile(JPEGFILE *pFile, int32_t iPosition)
|
||||
{
|
||||
// Serial.printf("jpegSeekFile, pFile->iPos: %d, iPosition: %d\n", pFile->iPos, iPosition);
|
||||
File *f = static_cast<File *>(pFile->fHandle);
|
||||
f->seek(iPosition);
|
||||
return iPosition;
|
||||
}
|
||||
|
||||
static void jpegDraw(
|
||||
const char *filename, JPEG_DRAW_CALLBACK *jpegDrawCallback, bool useBigEndian,
|
||||
int x, int y, int widthLimit, int heightLimit)
|
||||
{
|
||||
_x = x;
|
||||
_y = y;
|
||||
_x_bound = _x + widthLimit - 1;
|
||||
_y_bound = _y + heightLimit - 1;
|
||||
|
||||
_jpeg.open(filename, jpegOpenFile, jpegCloseFile, jpegReadFile, jpegSeekFile, 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();
|
||||
}
|
||||
|
||||
#endif // _JPEGFUNC_H_
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 101 KiB |
Reference in New Issue
Block a user