From cef06b82d6accd7a14b245229424fcf4703650bd Mon Sep 17 00:00:00 2001 From: Thorinair Date: Mon, 31 Aug 2020 13:35:30 +0200 Subject: [PATCH] Add web JPEG support --- Inkplate.cpp | 70 +++++++++++++++++-- Inkplate.h | 14 ++-- .../10-Inkplate_Web_BMP_pictures.ino | 2 +- .../11-Inkplate_SD_JPEG_pictures.ino | 8 ++- .../12-Inkplate_Web_JPEG_pictures.ino | 62 ++++++++++++++++ 5 files changed, 142 insertions(+), 14 deletions(-) create mode 100644 examples/2. Advanced Inkplate Features/12-Inkplate_Web_JPEG_pictures/12-Inkplate_Web_JPEG_pictures.ino diff --git a/Inkplate.cpp b/Inkplate.cpp index 392b5c0..8b32300 100644 --- a/Inkplate.cpp +++ b/Inkplate.cpp @@ -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) { - uint16_t w = 0, h = 0; + uint8_t ret = 0; TJpgDec.setJpgScale(1); TJpgDec.setCallback(jpegCallback); @@ -559,13 +559,14 @@ int Inkplate::drawJpegFromSD(Inkplate *display, SdFile *p, int x, int y, bool di } p->close(); - //TJpgDec.getJpgSize(&w, &h, buf, total); - //Serial.print("Width = "); Serial.print(w); Serial.print(", height = "); Serial.println(h); selectDisplayMode(INKPLATE_3BIT); - TJpgDec.drawJpg(x, y, buf, total, display); + if(TJpgDec.drawJpg(x, y, buf, total, display) == 0) + ret = 1; free(buf); + + return ret; } 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, int xc, int yc, int c) diff --git a/Inkplate.h b/Inkplate.h index 9d0186c..cd88bbd 100644 --- a/Inkplate.h +++ b/Inkplate.h @@ -233,12 +233,14 @@ public: void einkOn(void); void selectDisplayMode(uint8_t _mode); uint8_t getDisplayMode(); - int drawBitmapFromSD(SdFile *p, int x, int y, bool dither = false, bool invert = false); - int drawBitmapFromSD(char *fileName, int x, int y, bool dither = false, bool invert = false); - int drawBitmapFromWeb(WiFiClient *s, int x, int y, int len, bool dither = false, bool invert = false); - int drawBitmapFromWeb(char *url, int x, int y, bool dither = false, bool invert = false); - int drawJpegFromSD(Inkplate *display, SdFile *p, int x, int y, bool dither = false, bool invert = false); - int drawJpegFromSD(Inkplate *display, char *fileName, 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 invert = false, bool dither = 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 invert = false, bool dither = 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 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 fillElipse(int rx, int ry, int xc, int yc, int c); void drawPolygon(int *x, int *y, int n, int color); diff --git a/examples/2. Advanced Inkplate Features/10-Inkplate_Web_BMP_pictures/10-Inkplate_Web_BMP_pictures.ino b/examples/2. Advanced Inkplate Features/10-Inkplate_Web_BMP_pictures/10-Inkplate_Web_BMP_pictures.ino index 431c311..43c8054 100644 --- a/examples/2. Advanced Inkplate Features/10-Inkplate_Web_BMP_pictures/10-Inkplate_Web_BMP_pictures.ino +++ b/examples/2. Advanced Inkplate Features/10-Inkplate_Web_BMP_pictures/10-Inkplate_Web_BMP_pictures.ino @@ -22,7 +22,7 @@ Inkplate display(INKPLATE_1BIT); //Create an object on Inkplate library and also set library into 1 Bit mode (Monochrome) const char* ssid = ""; //Your WiFi SSID -const char* password = ""; //Your WiFi password +const char* password = ""; //Your WiFi password void setup() { display.begin(); //Init Inkplate library (you should call this function ONLY ONCE) diff --git a/examples/2. Advanced Inkplate Features/11-Inkplate_SD_JPEG_pictures/11-Inkplate_SD_JPEG_pictures.ino b/examples/2. Advanced Inkplate Features/11-Inkplate_SD_JPEG_pictures/11-Inkplate_SD_JPEG_pictures.ino index 95741ad..1e6cc32 100644 --- a/examples/2. Advanced Inkplate Features/11-Inkplate_SD_JPEG_pictures/11-Inkplate_SD_JPEG_pictures.ino +++ b/examples/2. Advanced Inkplate Features/11-Inkplate_SD_JPEG_pictures/11-Inkplate_SD_JPEG_pictures.ino @@ -38,11 +38,13 @@ void setup() { display.partialUpdate(); //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. - 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. - //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.display(); } diff --git a/examples/2. Advanced Inkplate Features/12-Inkplate_Web_JPEG_pictures/12-Inkplate_Web_JPEG_pictures.ino b/examples/2. Advanced Inkplate Features/12-Inkplate_Web_JPEG_pictures/12-Inkplate_Web_JPEG_pictures.ino new file mode 100644 index 0000000..9ed62ca --- /dev/null +++ b/examples/2. Advanced Inkplate Features/12-Inkplate_Web_JPEG_pictures/12-Inkplate_Web_JPEG_pictures.ino @@ -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... +}