Finished example.

This commit is contained in:
Thorinair 2020-07-24 18:49:07 +02:00
parent 3eb98ff9f4
commit c69240ff1a
4 changed files with 74 additions and 160 deletions

View File

@ -381,19 +381,28 @@ int Inkplate::drawBitmapFromWeb(WiFiClient* s, int x, int y, int len) {
int Inkplate::drawBitmapFromWeb(char* url, int x, int y) { int Inkplate::drawBitmapFromWeb(char* url, int x, int y) {
if (WiFi.status() != WL_CONNECTED) return 0; if (WiFi.status() != WL_CONNECTED) return 0;
int ret = 0;
bool sleep = WiFi.getSleep();
WiFi.setSleep(false);
HTTPClient http; HTTPClient http;
http.getStream().setNoDelay(true); http.getStream().setNoDelay(true);
http.getStream().setTimeout(10); http.getStream().setTimeout(1);
http.begin(url); http.begin(url);
int httpCode = http.GET(); int httpCode = http.GET();
if (httpCode != 200) return 0; if (httpCode == 200) {
int32_t len = http.getSize();
if (len > 0) {
WiFiClient * dat = http.getStreamPtr();
ret = drawBitmapFromWeb(dat, x, y, len);
}
}
int32_t len = http.getSize(); http.end();
if (len <= 0) return 0; WiFi.setSleep(sleep);
return ret;
WiFiClient * dat = http.getStreamPtr();
return drawBitmapFromWeb(dat, x, y, len);
} }
int Inkplate::sdCardInit() { int Inkplate::sdCardInit() {
@ -894,25 +903,51 @@ int Inkplate::drawMonochromeBitmapWeb(WiFiClient *s, struct bitmapHeader bmpHead
int w = bmpHeader.width; int w = bmpHeader.width;
int h = bmpHeader.height; int h = bmpHeader.height;
uint8_t paddingBits = w % 32; uint8_t paddingBits = w % 32;
int total = len - 34;
w /= 32; w /= 32;
uint8_t buffer[100]; uint8_t* buf = (uint8_t*) ps_malloc(total);
s->readBytes(buffer, bmpHeader.startRAW); if (buf == NULL)
int i, j; return 0;
int pnt = 0;
while (pnt < total) {
int toread = s->available();
if (toread > 0) {
int read = s->read(buf+pnt, toread);
if (read > 0)
pnt += read;
}
}
int i, j, k = bmpHeader.startRAW - 34;
for (j = 0; j < h; j++) { for (j = 0; j < h; j++) {
for (i = 0; i < w; i++) { for (i = 0; i < w; i++) {
uint32_t pixelRow = s->read() << 24 | s->read() << 16 | s->read() << 8 | s->read(); uint8_t a = buf[k++];
uint8_t b = buf[k++];
uint8_t c = buf[k++];
uint8_t d = buf[k++];
uint32_t pixelRow = a << 24 | b << 16 | c << 8 | d;
pixelRow = ~pixelRow;
for (int n = 0; n < 32; n++) { for (int n = 0; n < 32; n++) {
drawPixel((i * 32) + n + x, h - j + y, !(pixelRow & (1ULL << (31 - n)))); drawPixel((i * 32) + n + x, h - j + y, !(pixelRow & (1ULL << (31 - n))));
} }
} }
if (paddingBits) { if (paddingBits) {
uint32_t pixelRow = s->read() << 24 | s->read() << 16 | s->read() << 8 | s->read(); uint8_t a = buf[k++];
uint8_t b = buf[k++];
uint8_t c = buf[k++];
uint8_t d = buf[k++];
uint32_t pixelRow = a << 24 | b << 16 | c << 8 | d;
pixelRow = ~pixelRow;
for (int n = 0; n < paddingBits; n++) { for (int n = 0; n < paddingBits; n++) {
drawPixel((i * 32) + n + x, h - j + y, !(pixelRow & (1ULL << (31 - n)))); drawPixel((i * 32) + n + x, h - j + y, !(pixelRow & (1ULL << (31 - n))));
} }
} }
} }
free(buf);
return 1; return 1;
} }
@ -920,64 +955,22 @@ int Inkplate::drawGrayscaleBitmap24Web(WiFiClient *s, struct bitmapHeader bmpHea
int w = bmpHeader.width; int w = bmpHeader.width;
int h = bmpHeader.height; int h = bmpHeader.height;
char padding = w % 4; char padding = w % 4;
//Serial.println(len);
//Serial.println(bmpHeader.startRAW);
int total = len - 34; int total = len - 34;
uint8_t* buf = (uint8_t*) ps_malloc(total); uint8_t* buf = (uint8_t*) ps_malloc(total);
if (buf == NULL) if (buf == NULL)
return 0; return 0;
Serial.println("Starting data read!");
long t_start = millis();
//int i, j;
//size_t read = s->read(buf, len - 34);
//for (i = 0; i < len - 34; i++)
// s->read();
//int read;
int pnt = 0; int pnt = 0;
//int cnk = 512;
//while (pnt < total) {
// if (total - pnt < cnk)
// cnk = total - pnt;
// read = s->read(buf+pnt, cnk);
// if (read > 0) {
// pnt += read;
// Serial.println(" Read: " + String(read));
// }
// //delay(10);
//}
while (pnt < total) { while (pnt < total) {
int toread = s->available(); int toread = s->available();
if (toread > 0) { if (toread > 0) {
int read = s->read(buf+pnt, toread); int read = s->read(buf+pnt, toread);
if (read > 0) { if (read > 0)
pnt += read; pnt += read;
Serial.println(" Read: " + String(read) + " Total: " + String(pnt) + "/" + String(total));
}
} }
} }
//while (pnt < total) {
// if (total - pnt < cnk)
// cnk = total - pnt;
// s->read(buf+pnt, cnk);
// pnt += cnk;
// Serial.println(" Read: " + String(cnk));
//}
//for (j = 0; j < h; j++) {
// size_t read = s->read(buf, 64);
// Serial.println(" Read: " + String(read));
// delay(1);
//}
long t_stop = millis();
Serial.println("Time: " + String((float)(t_stop - t_start)/1000) + "s");
int i, j, k = bmpHeader.startRAW - 34; int i, j, k = bmpHeader.startRAW - 34;
for (j = 0; j < h; j++) { for (j = 0; j < h; j++) {
for (i = 0; i < w; i++) { for (i = 0; i < w; i++) {

View File

@ -21,40 +21,36 @@
*/ */
#include "Inkplate.h" //Include Inkplate library to the sketch #include "Inkplate.h" //Include Inkplate library to the sketch
#include "SdFat.h" //Include library for SD card
#include "WiFi.h" //Include library for WiFi #include "WiFi.h" //Include library for WiFi
#include "HTTPClient.h" //Include library for HTTP downloading
Inkplate display(INKPLATE_1BIT); //Create an object on Inkplate library and also set library into 1 Bit mode (Monochrome) Inkplate display(INKPLATE_1BIT); //Create an object on Inkplate library and also set library into 1 Bit mode (Monochrome)
SdFile file; //Create SdFile object used for accessing files on SD card SdFile file; //Create SdFile object used for accessing files on SD card
const char* ssid = "Twilight Sparkle"; //Your WiFi SSID const char* ssid = "ssid"; //Your WiFi SSID
const char* password = "thori4twily"; //Your WiFi password const char* password = "password"; //Your WiFi password
//Photo taken by: Paulian Prajitura
char* url = "https://dl.thorinair.net/neowise2.bmp"; //URL of image to download
void setup() { void setup() {
Serial.begin(115200);
display.begin(); //Init Inkplate library (you should call this function ONLY ONCE) display.begin(); //Init Inkplate library (you should call this function ONLY ONCE)
display.clearDisplay(); //Clear frame buffer of display display.clearDisplay(); //Clear frame buffer of display
display.display(); //Put clear image on display display.display(); //Put clear image on display
display.println("Connecting to WiFi..."); display.print("Connecting to WiFi...");
display.partialUpdate(); display.partialUpdate();
//Connect to the WiFi network. //Connect to the WiFi network.
WiFi.mode(WIFI_MODE_STA); WiFi.mode(WIFI_MODE_STA);
WiFi.setSleep(false);
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
delay(500); delay(500);
Serial.print("."); display.print(".");
display.partialUpdate();
} }
display.println("WiFi OK! Downloading..."); display.println("\nWiFi OK! Downloading...");
display.partialUpdate(); display.partialUpdate();
if(!display.drawBitmapFromWeb(url, 0, 0)) { //Draw the first image from web.
//24 bit example, will take around 20 secs to load.
//Photo taken by: Roberto Fernandez
if(!display.drawBitmapFromWeb("https://varipass.org/neowise.bmp", 0, 0)) {
//If is something failed (wrong filename or wrong bitmap format), write error message on the screen. //If is something failed (wrong filename or wrong bitmap format), write error message on the screen.
//REMEMBER! You can only use Windows Bitmap file with color depth of 1 or 24 bits with no compression! //REMEMBER! You can only use Windows Bitmap file with color depth of 1 or 24 bits with no compression!
display.println("Image open error"); display.println("Image open error");
@ -62,95 +58,20 @@ void setup() {
} }
display.display(); display.display();
//HTTPClient http; delay(5000);
//
//http.begin(url); //Draw the second image from web.
// //1 bit example, will load very quickly.
////Get the image, check if connection was successful. //Photo taken by: Roberto Fernandez
//uint32_t httpCode = http.GET(); if(!display.drawBitmapFromWeb("https://varipass.org/neowise_mono.bmp", 0, 0)) {
//if (httpCode == 200) { //If is something failed (wrong filename or wrong bitmap format), write error message on the screen.
// //REMEMBER! You can only use Windows Bitmap file with color depth of 1 or 24 bits with no compression!
// display.println("HTTP opened! Size: " + String(len) + " - Saving to SD..."); display.println("Image open error");
// display.partialUpdate(); display.display();
// }
// display.display();
//
// //String str = http.getString(); WiFi.mode(WIFI_OFF);
// //int str_len = http.getSize();
// //char char_array[str_len];
// //str.toCharArray(char_array, str_len);
// //http.end();
//
// SdFat sd = display.getSdFat();
// sd.remove("image.bmp");
// File image = sd.open("image.bmp", O_CREAT | O_WRITE);
// if (image) {
//
//
//
// http.writeToStream(&stream);
////
// //String body = http.getString();
////
// //for (int i = 0; i < http.getSize(); i++) {
// // //Serial.print(body[i]);
// // image.write(body[i]);
// //}
//
// //WiFiClient* stream = http.getStreamPtr();
// //while (stream->available()) {
// // uint8_t c = stream->read();
// // //Serial.print(c);
// // image.write(c);
// //}
////
// ////image.write(char_array, str_len); //Write HTTP content to file
//
// //WiFiClient * stream = http.getStreamPtr();
// //byte buffer[len];
// //stream->readBytes(buffer, len);
// //for (int i = 0; i < len; i++)
// // image.write(buffer[i]);
//
// //uint32_t pos = 0;
// //char buff[2048];
// //while (pos < len && http.available()) {
// // ESP.wdtFeed();
// // yield();
// // http.seek(pos, fs::SeekMode::SeekSet);
// // const auto read = http.readBytes(buff, sizeof(buff));
// // pos += read;
// // image.write(buff, read);
// //}
//
// image.close(); //Close the file
// display.println("Image saved!");
// display.partialUpdate();
//
// delay(1000);
//
// //Load and display the image on e-paper at position X=0, Y=0
// if(!display.drawBitmapFromSD("image.bmp", 0, 0)) {
// //If is something failed (wrong filename or wrong bitmap format), write error message on the screen.
// //REMEMBER! You can only use Windows Bitmap file with color depth of 1 or 24 bits with no compression!
// display.println("Image open error");
// display.display();
// }
// display.display();
// }
// else {
// //If image file was not open successfully, display error on screen
// display.println("File error!");
// display.partialUpdate();
// return;
// }
//}
//else {
// //If HTTP connection was not successful, display error on screen
// display.println("HTTP connection error!");
// display.partialUpdate();
// return;
//}
} }
void loop() { void loop() {

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB