Skip to content

Commit

Permalink
Merge pull request #4 from Bodmer/master
Browse files Browse the repository at this point in the history
Make library Arduino IDE compatible
  • Loading branch information
kikuchan authored Mar 25, 2022
2 parents a17def5 + 86f28ae commit 73631e9
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 1 deletion.
141 changes: 141 additions & 0 deletions examples/TFT_eSPI_pngle_test/TFT_eSPI_pngle_test.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Sketch to fetch images from web and display on TFT

#include <TFT_eSPI.h> // Hardware-specific library
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library

#include <HTTPClient.h>

#include "pngle.h"

#define WIFI_SSID "XXXXXXXX"
#define WIFI_PASS "XXXXXXXX"

// Clear screen and reset text cursor etc
void cls()
{
tft.fillScreen(TFT_WHITE);

tft.setCursor(0, 0);
tft.setTextColor(TFT_BLACK);
tft.setTextFont(4);
}

// ===================================================
// pngle example for TFT_eSPI
// ===================================================

double g_scale = 1.0;
// pngle callback, called during decode process for each pixel
void pngle_on_draw(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint8_t rgba[4])
{
// Convert to RGB 565 format
uint16_t color = tft.color565(rgba[0], rgba[1], rgba[2]);

// If alpha > 0 then draw
if (rgba[3]) {
if (g_scale <= 1.0) tft.drawPixel(x, y, color);
else {
x = ceil(x * g_scale);
y = ceil(y * g_scale);
w = ceil(w * g_scale);
h = ceil(h * g_scale);
tft.fillRect(x, y, w, h, color);
}
}

}

void load_png(const char *url, double scale = 1.0)
{
HTTPClient http;

http.begin(url);

int httpCode = http.GET();
if (httpCode != HTTP_CODE_OK) {
tft.printf("HTTP ERROR: %u\n", httpCode);
http.end();
return ;
}

int total = http.getSize();

WiFiClient *stream = http.getStreamPtr();

pngle_t *pngle = pngle_new();
pngle_set_draw_callback(pngle, pngle_on_draw);
g_scale = scale;

uint8_t buf[2048];
int remain = 0;
uint32_t timeout = millis();
while (http.connected() && (total > 0 || remain > 0)) {

// Break out of loop after 10s
if ((millis() - timeout) > 10000UL)
{
tft.printf("HTTP request timeout\n");
break;
}

size_t size = stream->available();

if (!size) { delay(1); continue; }

if (size > sizeof(buf) - remain) {
size = sizeof(buf) - remain;
}

int len = stream->readBytes(buf + remain, size);
if (len > 0) {
int fed = pngle_feed(pngle, buf, remain + len);
if (fed < 0) {
cls();
tft.printf("ERROR: %s\n", pngle_error(pngle));
break;
}
total -= len;
remain = remain + len - fed;
if (remain > 0) memmove(buf, buf + fed, remain);
} else {
delay(1);
}
}

pngle_destroy(pngle);

http.end();
}
// ===================================================

void setup()
{
Serial.begin(115200);

tft.begin();
cls();

tft.printf("Welcome.\n");

WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
}

tft.printf("WiFi connected.\n");
}

void loop()
{
cls();
load_png("https://raw.githubusercontent.com/kikuchan/pngle/master/tests/pngsuite/PngSuite.png");
delay(5000);

cls();
load_png("https://raw.githubusercontent.com/kikuchan/pngle/master/tests/pngsuite/tbrn2c08.png", 7);
delay(5000);

cls();
load_png("https://avatars3.githubusercontent.com/u/17420673?s=240");
delay(5000);
}
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pngle",
"version": "1.0.0",
"version": "1.0.1",
"description": "A stream based portable PNG Loader for Embedding",
"keywords": "png, png-loader, png-decoder, c, arduino, mbed, esp32, esp8266, stm32, m5stack",
"repository": {
Expand Down
10 changes: 10 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=pngle
version=1.0.1
author=kikuchan
maintainer=kikuchan
sentence=A stream based portable PNG Loader for Embedding
paragraph=A memory frugal stream based portable PNG image for decoder embedded systems. All standard types of PNG files are supported (as tested with PngSuite), including interlaced images.
category=Display
url=https://github.com/kikuchan/pngle
architectures=*
includes=pngle.h
File renamed without changes.
File renamed without changes.

0 comments on commit 73631e9

Please sign in to comment.