| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- void drawBitmapFrom_HTTP_ToBuffer(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
- {
- WiFiClient client;
- bool connection_ok = false;
- bool valid = false; // valid format to be handled
- bool flip = true; // bitmap is stored bottom-to-top
- bool has_multicolors = (display.epd2.panel == GxEPD2::ACeP565) || (display.epd2.panel == GxEPD2::GDEY073D46)|| (display.epd2.panel == GxEPD2::GDEP073E01);
- uint32_t startTime = millis();
- if ((x >= display.width()) || (y >= display.height())) return;
- display.fillScreen(GxEPD_WHITE);
- Serial.print("connecting to "); Serial.println(host);
- if (!client.connect(host, httpPort))
- {
- Serial.println("connection failed");
- return;
- }
- Serial.print("requesting URL: ");
- Serial.println(String("http://") + host + path + filename);
- client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
- "Host: " + host + "\r\n" +
- "User-Agent: GxEPD2_WiFi_Example\r\n" +
- "Connection: close\r\n\r\n");
- Serial.println("request sent");
- while (client.connected())
- {
- String line = client.readStringUntil('\n');
- if (!connection_ok)
- {
- connection_ok = line.startsWith("HTTP/1.1 200 OK");
- if (connection_ok) Serial.println(line);
- }
- if (!connection_ok) Serial.println(line);
- if (line == "\r")
- {
- Serial.println("headers received");
- break;
- }
- }
- if (!connection_ok) return;
- // Parse BMP header
- if (read16(client) == 0x4D42) // BMP signature
- {
- uint32_t fileSize = read32(client);
- uint32_t creatorBytes = read32(client); (void)creatorBytes; //unused
- uint32_t imageOffset = read32(client); // Start of image data
- uint32_t headerSize = read32(client);
- uint32_t width = read32(client);
- int32_t height = (int32_t) read32(client);
- uint16_t planes = read16(client);
- uint16_t depth = read16(client); // bits per pixel
- uint32_t format = read32(client);
- uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
- if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
- {
- Serial.print("File size: "); Serial.println(fileSize);
- Serial.print("Image Offset: "); Serial.println(imageOffset);
- Serial.print("Header size: "); Serial.println(headerSize);
- Serial.print("Bit Depth: "); Serial.println(depth);
- Serial.print("Image size: ");
- Serial.print(width);
- Serial.print('x');
- Serial.println(abs(height));
- // BMP rows are padded (if needed) to 4-byte boundary
- uint32_t rowSize = (width * depth / 8 + 3) & ~3;
- if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
- if (height < 0)
- {
- height = -height;
- flip = false;
- }
- uint16_t w = width;
- uint16_t h = height;
- if ((x + w - 1) >= display.width()) w = display.width() - x;
- if ((y + h - 1) >= display.height()) h = display.height() - y;
- //if (w <= max_row_width) // handle with direct drawing
- {
- valid = true;
- uint8_t bitmask = 0xFF;
- uint8_t bitshift = 8 - depth;
- uint16_t red, green, blue;
- bool whitish = false;
- bool colored = false;
- if (depth == 1) with_color = false;
- if (depth <= 8)
- {
- if (depth < 8) bitmask >>= depth;
- bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
- for (uint16_t pn = 0; pn < (1 << depth); pn++)
- {
- blue = client.read();
- green = client.read();
- red = client.read();
- client.read();
- bytes_read += 4;
- whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
- colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
- if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
- mono_palette_buffer[pn / 8] |= whitish << pn % 8;
- if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
- color_palette_buffer[pn / 8] |= colored << pn % 8;
- //Serial.print("0x00"); Serial.print(red, HEX); Serial.print(green, HEX); Serial.print(blue, HEX);
- //Serial.print(" : "); Serial.print(whitish); Serial.print(", "); Serial.println(colored);
- rgb_palette_buffer[pn] = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
- }
- }
- uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
- bytes_read += skip(client, rowPosition - bytes_read);
- for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
- {
- if (!connection_ok || !(client.connected() || client.available())) break;
- delay(1); // yield() to avoid WDT
- uint32_t in_remain = rowSize;
- uint32_t in_idx = 0;
- uint32_t in_bytes = 0;
- uint8_t in_byte = 0; // for depth <= 8
- uint8_t in_bits = 0; // for depth <= 8
- uint16_t color = GxEPD_WHITE;
- for (uint16_t col = 0; col < w; col++) // for each pixel
- {
- yield();
- if (!connection_ok || !(client.connected() || client.available())) break;
- // Time to read more pixel data?
- if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
- {
- uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
- uint32_t got = read8n(client, input_buffer, get);
- while ((got < get) && connection_ok)
- {
- //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
- uint32_t gotmore = read8n(client, input_buffer + got, get - got);
- got += gotmore;
- connection_ok = gotmore > 0;
- }
- in_bytes = got;
- in_remain -= got;
- bytes_read += got;
- in_idx = 0;
- }
- if (!connection_ok)
- {
- Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
- break;
- }
- switch (depth)
- {
- case 32:
- blue = input_buffer[in_idx++];
- green = input_buffer[in_idx++];
- red = input_buffer[in_idx++];
- in_idx++; // skip alpha
- whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
- colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
- color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
- break;
- case 24:
- blue = input_buffer[in_idx++];
- green = input_buffer[in_idx++];
- red = input_buffer[in_idx++];
- whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
- colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
- color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
- break;
- case 16:
- {
- uint8_t lsb = input_buffer[in_idx++];
- uint8_t msb = input_buffer[in_idx++];
- if (format == 0) // 555
- {
- blue = (lsb & 0x1F) << 3;
- green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
- red = (msb & 0x7C) << 1;
- color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
- }
- else // 565
- {
- blue = (lsb & 0x1F) << 3;
- green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
- red = (msb & 0xF8);
- color = (msb << 8) | lsb;
- }
- whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
- colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
- }
- break;
- case 1:
- case 2:
- case 4:
- case 8:
- {
- if (0 == in_bits)
- {
- in_byte = input_buffer[in_idx++];
- in_bits = 8;
- }
- uint16_t pn = (in_byte >> bitshift) & bitmask;
- whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
- colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
- in_byte <<= depth;
- in_bits -= depth;
- color = rgb_palette_buffer[pn];
- }
- break;
- }
- if (with_color && has_multicolors)
- {
- // keep color
- }
- else if (whitish)
- {
- color = GxEPD_WHITE;
- }
- else if (colored && with_color)
- {
- color = GxEPD_COLORED;
- }
- else
- {
- color = GxEPD_BLACK;
- }
- uint16_t yrow = y + (flip ? h - row - 1 : row);
- display.drawPixel(x + col, yrow, color);
- } // end pixel
- } // end line
- }
- Serial.print("bytes read "); Serial.println(bytes_read);
- }
- }
- Serial.print("loaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
- client.stop();
- if (!valid)
- {
- Serial.println("bitmap format not handled.");
- }
- }
- void showBitmapFrom_HTTP_Buffered(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
- {
- Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
- display.setFullWindow();
- display.firstPage();
- do
- {
- drawBitmapFrom_HTTP_ToBuffer(host, path, filename, x, y, with_color);
- }
- while (display.nextPage());
- }
- void drawBitmapFrom_HTTPS_ToBuffer(const char* host, const char* path, const char* filename, const char* fingerprint, int16_t x, int16_t y, bool with_color, const char* certificate)
- {
- // Use WiFiClientSecure class to create TLS connection
- #if defined (ESP8266)
- BearSSL::WiFiClientSecure client;
- BearSSL::X509List cert(certificate ? certificate : certificate_rawcontent);
- #else
- WiFiClientSecure client;
- #endif
- bool connection_ok = false;
- bool valid = false; // valid format to be handled
- bool flip = true; // bitmap is stored bottom-to-top
- bool has_multicolors = (display.epd2.panel == GxEPD2::ACeP565) || (display.epd2.panel == GxEPD2::GDEY073D46)|| (display.epd2.panel == GxEPD2::GDEP073E01);
- uint32_t startTime = millis();
- if ((x >= display.width()) || (y >= display.height())) return;
- display.fillScreen(GxEPD_WHITE);
- Serial.print("connecting to "); Serial.println(host);
- #if defined (ESP8266)
- if (certificate) client.setTrustAnchors(&cert);
- else if (fingerprint) client.setFingerprint(fingerprint);
- else client.setInsecure();
- #elif defined (ESP32)
- if (certificate) client.setCACert(certificate);
- #endif
- if (!client.connect(host, httpsPort))
- {
- Serial.println("connection failed");
- return;
- }
- Serial.print("requesting URL: ");
- Serial.println(String("https://") + host + path + filename);
- client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
- "Host: " + host + "\r\n" +
- "User-Agent: GxEPD2_WiFi_Example\r\n" +
- "Connection: close\r\n\r\n");
- Serial.println("request sent");
- while (client.connected())
- {
- String line = client.readStringUntil('\n');
- if (!connection_ok)
- {
- connection_ok = line.startsWith("HTTP/1.1 200 OK");
- if (connection_ok) Serial.println(line);
- }
- if (!connection_ok) Serial.println(line);
- if (line == "\r")
- {
- Serial.println("headers received");
- break;
- }
- }
- if (!connection_ok) return;
- // Parse BMP header
- uint16_t signature = 0;
- for (int16_t i = 0; i < 50; i++)
- {
- if (!client.available()) delay(100);
- else signature = read16(client);
- if (signature == 0x4D42) break;
- }
- if (signature == 0x4D42) // BMP signature
- {
- uint32_t fileSize = read32(client);
- uint32_t creatorBytes = read32(client); (void)creatorBytes; //unused
- uint32_t imageOffset = read32(client); // Start of image data
- uint32_t headerSize = read32(client);
- uint32_t width = read32(client);
- int32_t height = (int32_t) read32(client);
- uint16_t planes = read16(client);
- uint16_t depth = read16(client); // bits per pixel
- uint32_t format = read32(client);
- uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
- if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
- {
- Serial.print("File size: "); Serial.println(fileSize);
- Serial.print("Image Offset: "); Serial.println(imageOffset);
- Serial.print("Header size: "); Serial.println(headerSize);
- Serial.print("Bit Depth: "); Serial.println(depth);
- Serial.print("Image size: ");
- Serial.print(width);
- Serial.print('x');
- Serial.println(abs(height));
- // BMP rows are padded (if needed) to 4-byte boundary
- uint32_t rowSize = (width * depth / 8 + 3) & ~3;
- if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
- if (height < 0)
- {
- height = -height;
- flip = false;
- }
- uint16_t w = width;
- uint16_t h = height;
- if ((x + w - 1) >= display.width()) w = display.width() - x;
- if ((y + h - 1) >= display.height()) h = display.height() - y;
- //if (w <= max_row_width) // handle with direct drawing
- {
- valid = true;
- uint8_t bitmask = 0xFF;
- uint8_t bitshift = 8 - depth;
- uint16_t red, green, blue;
- bool whitish = false;
- bool colored = false;
- if (depth == 1) with_color = false;
- if (depth <= 8)
- {
- if (depth < 8) bitmask >>= depth;
- //bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
- bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
- for (uint16_t pn = 0; pn < (1 << depth); pn++)
- {
- blue = client.read();
- green = client.read();
- red = client.read();
- client.read();
- bytes_read += 4;
- whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
- colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
- if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
- mono_palette_buffer[pn / 8] |= whitish << pn % 8;
- if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
- color_palette_buffer[pn / 8] |= colored << pn % 8;
- //Serial.print("0x00"); Serial.print(red, HEX); Serial.print(green, HEX); Serial.print(blue, HEX);
- //Serial.print(" : "); Serial.print(whitish); Serial.print(", "); Serial.println(colored);
- rgb_palette_buffer[pn] = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
- }
- }
- uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
- //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
- bytes_read += skip(client, rowPosition - bytes_read);
- for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
- {
- if (!connection_ok || !(client.connected() || client.available())) break;
- delay(1); // yield() to avoid WDT
- uint32_t in_remain = rowSize;
- uint32_t in_idx = 0;
- uint32_t in_bytes = 0;
- uint8_t in_byte = 0; // for depth <= 8
- uint8_t in_bits = 0; // for depth <= 8
- uint16_t color = GxEPD_WHITE;
- for (uint16_t col = 0; col < w; col++) // for each pixel
- {
- yield();
- if (!connection_ok || !(client.connected() || client.available())) break;
- // Time to read more pixel data?
- if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
- {
- uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
- uint32_t got = read8n(client, input_buffer, get);
- while ((got < get) && connection_ok)
- {
- //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
- uint32_t gotmore = read8n(client, input_buffer + got, get - got);
- got += gotmore;
- connection_ok = gotmore > 0;
- }
- in_bytes = got;
- in_remain -= got;
- bytes_read += got;
- in_idx = 0;
- }
- if (!connection_ok)
- {
- Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
- break;
- }
- switch (depth)
- {
- case 32:
- blue = input_buffer[in_idx++];
- green = input_buffer[in_idx++];
- red = input_buffer[in_idx++];
- in_idx++; // skip alpha
- whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
- colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
- color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
- break;
- case 24:
- blue = input_buffer[in_idx++];
- green = input_buffer[in_idx++];
- red = input_buffer[in_idx++];
- whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
- colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
- color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
- break;
- case 16:
- {
- uint8_t lsb = input_buffer[in_idx++];
- uint8_t msb = input_buffer[in_idx++];
- if (format == 0) // 555
- {
- blue = (lsb & 0x1F) << 3;
- green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
- red = (msb & 0x7C) << 1;
- color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
- }
- else // 565
- {
- blue = (lsb & 0x1F) << 3;
- green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
- red = (msb & 0xF8);
- color = (msb << 8) | lsb;
- }
- whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
- colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
- }
- break;
- case 1:
- case 2:
- case 4:
- case 8:
- {
- if (0 == in_bits)
- {
- in_byte = input_buffer[in_idx++];
- in_bits = 8;
- }
- uint16_t pn = (in_byte >> bitshift) & bitmask;
- whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
- colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
- in_byte <<= depth;
- in_bits -= depth;
- color = rgb_palette_buffer[pn];
- }
- break;
- }
- if (with_color && has_multicolors)
- {
- // keep color
- }
- else if (whitish)
- {
- color = GxEPD_WHITE;
- }
- else if (colored && with_color)
- {
- color = GxEPD_COLORED;
- }
- else
- {
- color = GxEPD_BLACK;
- }
- uint16_t yrow = y + (flip ? h - row - 1 : row);
- display.drawPixel(x + col, yrow, color);
- } // end pixel
- } // end line
- }
- Serial.print("bytes read "); Serial.println(bytes_read);
- }
- }
- Serial.print("loaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
- client.stop();
- if (!valid)
- {
- Serial.println("bitmap format not handled.");
- }
- }
- void showBitmapFrom_HTTPS_Buffered(const char* host, const char* path, const char* filename, const char* fingerprint, int16_t x, int16_t y, bool with_color, const char* certificate)
- {
- Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
- display.setFullWindow();
- display.firstPage();
- do
- {
- drawBitmapFrom_HTTPS_ToBuffer(host, path, filename, fingerprint, x, y, with_color, certificate);
- }
- while (display.nextPage());
- }
|