Add web JPEG support

This commit is contained in:
Thorinair 2020-08-31 13:35:30 +02:00
parent e904398dca
commit cef06b82d6
5 changed files with 142 additions and 14 deletions

View File

@ -538,7 +538,7 @@ int Inkplate::drawBitmapFromWeb(char *url, int x, int y, bool dither, bool inver
int Inkplate::drawJpegFromSD(Inkplate *display, SdFile *p, int x, int y, bool dither, bool invert) int Inkplate::drawJpegFromSD(Inkplate *display, SdFile *p, int x, int y, bool dither, bool invert)
{ {
uint16_t w = 0, h = 0; uint8_t ret = 0;
TJpgDec.setJpgScale(1); TJpgDec.setJpgScale(1);
TJpgDec.setCallback(jpegCallback); TJpgDec.setCallback(jpegCallback);
@ -559,13 +559,14 @@ int Inkplate::drawJpegFromSD(Inkplate *display, SdFile *p, int x, int y, bool di
} }
p->close(); p->close();
//TJpgDec.getJpgSize(&w, &h, buf, total);
//Serial.print("Width = "); Serial.print(w); Serial.print(", height = "); Serial.println(h);
selectDisplayMode(INKPLATE_3BIT); selectDisplayMode(INKPLATE_3BIT);
TJpgDec.drawJpg(x, y, buf, total, display); if(TJpgDec.drawJpg(x, y, buf, total, display) == 0)
ret = 1;
free(buf); free(buf);
return ret;
} }
int Inkplate::drawJpegFromSD(Inkplate *display, char *fileName, int x, int y, bool dither, bool invert) int Inkplate::drawJpegFromSD(Inkplate *display, char *fileName, int x, int y, bool dither, bool invert)
@ -583,6 +584,67 @@ int Inkplate::drawJpegFromSD(Inkplate *display, char *fileName, int x, int y, bo
} }
} }
int Inkplate::drawJpegFromWeb(Inkplate *display, WiFiClient *s, int x, int y, int len, bool dither, bool invert)
{
uint8_t ret = 0;
TJpgDec.setJpgScale(1);
TJpgDec.setCallback(jpegCallback);
uint32_t pnt = 0;
uint8_t *buf = (uint8_t *)ps_malloc(len);
if (buf == NULL)
return 0;
while (pnt < len) {
uint32_t toread = s->available();
if (toread > 0) {
int read = s->read(buf + pnt, toread);
if (read > 0)
pnt += read;
}
}
selectDisplayMode(INKPLATE_3BIT);
if(TJpgDec.drawJpg(x, y, buf, len, display) == 0)
ret = 1;
free(buf);
return ret;
}
int Inkplate::drawJpegFromWeb(Inkplate *display, char *url, int x, int y, bool dither, bool invert)
{
if (WiFi.status() != WL_CONNECTED)
return 0;
int ret = 0;
bool sleep = WiFi.getSleep();
WiFi.setSleep(false);
HTTPClient http;
http.getStream().setNoDelay(true);
http.getStream().setTimeout(1);
http.begin(url);
int httpCode = http.GET();
if (httpCode == 200)
{
int32_t len = http.getSize();
if (len > 0)
{
WiFiClient *dat = http.getStreamPtr();
ret = drawJpegFromWeb(display, dat, x, y, len, dither, invert);
}
}
http.end();
WiFi.setSleep(sleep);
return ret;
}
void Inkplate::drawElipse(int rx, int ry, void Inkplate::drawElipse(int rx, int ry,
int xc, int yc, int xc, int yc,
int c) int c)

View File

@ -233,12 +233,14 @@ public:
void einkOn(void); void einkOn(void);
void selectDisplayMode(uint8_t _mode); void selectDisplayMode(uint8_t _mode);
uint8_t getDisplayMode(); uint8_t getDisplayMode();
int drawBitmapFromSD(SdFile *p, int x, int y, bool dither = false, bool invert = false); int drawBitmapFromSD(SdFile *p, int x, int y, bool invert = false, bool dither = false);
int drawBitmapFromSD(char *fileName, int x, int y, bool dither = false, bool invert = false); int drawBitmapFromSD(char *fileName, int x, int y, bool invert = false, bool dither = false);
int drawBitmapFromWeb(WiFiClient *s, int x, int y, int len, bool dither = false, bool invert = false); int drawBitmapFromWeb(WiFiClient *s, int x, int y, int len, bool invert = false, bool dither = false);
int drawBitmapFromWeb(char *url, int x, int y, bool dither = false, bool invert = false); int drawBitmapFromWeb(char *url, int x, int y, bool invert = false, bool dither = false);
int drawJpegFromSD(Inkplate *display, SdFile *p, int x, int y, bool dither = false, bool invert = false); int drawJpegFromSD(Inkplate *display, SdFile *p, int x, int y, bool invert = false, bool dither = false);
int drawJpegFromSD(Inkplate *display, char *fileName, int x, int y, bool dither = false, bool invert = false); int drawJpegFromSD(Inkplate *display, char *fileName, int x, int y, bool invert = false, bool dither = false);
int drawJpegFromWeb(Inkplate *display, WiFiClient *s, int x, int y, int len, bool invert = false, bool dither = false);
int drawJpegFromWeb(Inkplate *display, char *url, int x, int y, bool invert = false, bool dither = false);
void drawElipse(int rx, int ry, int xc, int yc, int c); void drawElipse(int rx, int ry, int xc, int yc, int c);
void fillElipse(int rx, int ry, int xc, int yc, int c); void fillElipse(int rx, int ry, int xc, int yc, int c);
void drawPolygon(int *x, int *y, int n, int color); void drawPolygon(int *x, int *y, int n, int color);

View File

@ -38,11 +38,13 @@ void setup() {
display.partialUpdate(); display.partialUpdate();
//If card is properly init, try to load image and display it on e-paper at position X=100, Y=0 //If card is properly init, try to load image and display it on e-paper at position X=100, Y=0
//NOTE: Both drawJpegFromSD methods allow for an optional fourth "invert" parameter. Setting this parameter to true //NOTE: These methods require you to pass a reference to the display object as first parameter.
//NOTE: Both drawJpegFromSD methods allow for an optional fifth "invert" parameter. Setting this parameter to true
//will flip all colors on the image, making black white and white black. //will flip all colors on the image, making black white and white black.
if (!display.drawJpegFromSD(&display, "pyramid.jpg", 100, 0, 0, 1)) { //Sixth parameter will dither the image.
if (!display.drawJpegFromSD(&display, "pyramid.jpg", 100, 0, false, true)) {
//If is something failed (wrong filename or wrong format), write error message on the screen. //If is something failed (wrong filename or wrong format), write error message on the screen.
//You can turn off dithering for somewhat faster image load by changing the last 1 to 0, or removing the 1 argument completely //You can turn off dithering for somewhat faster image load by changing the last true to false, or removing the argument completely
display.println("Image open error"); display.println("Image open error");
display.display(); display.display();
} }

View File

@ -0,0 +1,62 @@
/*
12-Inkplate_Web_JPEG_pictures for e-radionica Inkplate6
For this example you will need a micro USB cable, Inkplate6, and an available WiFi connection.
Select "Inkplate 6(ESP32)" from Tools -> Board menu.
Don't have "Inkplate 6(ESP32)" option? Follow our tutorial and add it:
https://e-radionica.com/en/blog/add-inkplate-6-to-arduino-ide/
You can open .jpg files that have resoluton smaller than 800x600 or otherwise it won't fit on screen.
This example will show you how you can download a .jpg file (picture) from the web and
display that image on e-paper display.
Want to learn more about Inkplate? Visit www.inkplate.io
Looking to get support? Write on our forums: http://forum.e-radionica.com/en/
31 August 2020 by e-radionica.com
*/
#include "Inkplate.h" //Include Inkplate library to the sketch
#include "HTTPClient.h" //Include library for HTTPClient
#include "WiFi.h" //Include library for WiFi
Inkplate display(INKPLATE_1BIT); //Create an object on Inkplate library and also set library into 1 Bit mode (Monochrome)
const char* ssid = "Twilight Sparkle"; //Your WiFi SSID
const char* password = "thori4twily"; //Your WiFi password
void setup() {
display.begin(); //Init Inkplate library (you should call this function ONLY ONCE)
display.clearDisplay(); //Clear frame buffer of display
display.display(); //Put clear image on display
display.print("Connecting to WiFi...");
display.partialUpdate();
//Connect to the WiFi network.
WiFi.mode(WIFI_MODE_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
display.print(".");
display.partialUpdate();
}
display.println("\nWiFi OK! Downloading...");
display.partialUpdate();
//Try to load image and display it on e-paper at position X=0, Y=100
//NOTE: These methods require you to pass a reference to the display object as first parameter.
//NOTE: Both drawJpegFromWeb methods allow for an optional fifth "invert" parameter. Setting this parameter to true
//will flip all colors on the image, making black white and white black.
//Sixth parameter will dither the image.
if (!display.drawJpegFromWeb(&display, "https://varipass.org/destination.jpg", 0, 100, false, true)) {
//If is something failed (wrong filename or format), write error message on the screen.
display.println("Image open error");
display.display();
}
display.display();
WiFi.mode(WIFI_OFF);
}
void loop() {
//Nothing...
}