| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- uint8_t output_row_native4c_buffer[max_row_width / 4]; // buffer for at least one row of native color bits
- uint8_t color4(uint16_t red, uint16_t green, uint16_t blue)
- {
- uint8_t cv4 = 0x00;
- if ((red < 0x80) && (green < 0x80) && (blue < 0x80)) cv4 = 0x00; // black
- else if ((red >= 0x80) && (green >= 0x80) && (blue >= 0x80)) cv4 = 0x01; // white
- else if ((red >= 0x80) && (blue >= 0x80)) cv4 = 0x03; // red, blue > red
- else if ((green >= 0x80) && (blue >= 0x80)) cv4 = 0x01; // green, blue > white
- else if ((red >= 0x80) && (green >= 0xC0)) cv4 = 0x02; // yellow
- else if ((red >= 0x80) && (green >= 0x40)) cv4 = 0x03; // orange > red
- else if (red >= 0x80) cv4 = 0x03; // red
- else if (green >= 0x80) cv4 = 0x00; // green > black
- else cv4 = 0x03; // blue
- return cv4;
- }
- void showNative4cFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y)
- {
- WiFiClient client;
- bool connection_ok = false;
- bool valid = false; // valid format to be handled
- bool flip = true; // bitmap is stored bottom-to-top
- uint32_t startTime = millis();
- if ((x >= display.epd2.WIDTH) || (y >= display.epd2.HEIGHT)) return;
- Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
- 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.epd2.WIDTH) w = display.epd2.WIDTH - x;
- if ((y + h - 1) >= display.epd2.HEIGHT) h = display.epd2.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;
- uint8_t cv4 = 0x00;
- 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;
- rgb_palette_buffer[pn] = color4(red, green, blue);
- }
- }
- display.clearScreen();
- 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
- uint8_t out_cv4_byte = 0x00;
- uint32_t out_idx = 0;
- 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
- cv4 = color4(red, green, blue);
- break;
- case 24:
- blue = input_buffer[in_idx++];
- green = input_buffer[in_idx++];
- red = input_buffer[in_idx++];
- cv4 = color4(red, green, blue);
- 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;
- }
- else // 565
- {
- blue = (lsb & 0x1F) << 3;
- green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
- red = (msb & 0xF8);
- }
- }
- cv4 = color4(red, green, blue);
- 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;
- cv4 = rgb_palette_buffer[pn];
- in_byte <<= depth;
- in_bits -= depth;
- }
- break;
- }
- out_cv4_byte |= cv4 << (2 * (3 - col % 4));
- if ((3 == col % 4) || (col == w - 1)) // write that last byte! (for w%4!=0 border)
- {
- output_row_native4c_buffer[out_idx++] = out_cv4_byte;
- out_cv4_byte = 0x00;
- }
- } // end pixel
- int16_t yrow = y + (flip ? h - row - 1 : row);
- display.writeNative(output_row_native4c_buffer, 0, x, yrow, w, 1, false, false, false);
- } // end line
- Serial.print("downloaded in ");
- Serial.print(millis() - startTime);
- Serial.println(" ms");
- display.refresh();
- }
- Serial.print("bytes read "); Serial.println(bytes_read);
- }
- }
- client.stop();
- if (!valid)
- {
- Serial.println("bitmap format not handled.");
- }
- }
- void showNative4cFrom_HTTPS(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) || defined(ARDUINO_RASPBERRY_PI_PICO_W)
- 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
- uint32_t startTime = millis();
- if ((x >= display.epd2.WIDTH) || (y >= display.epd2.HEIGHT)) return;
- Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
- Serial.print("connecting to "); Serial.println(host);
- #if defined (ESP8266) || defined(ARDUINO_RASPBERRY_PI_PICO_W)
- 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.epd2.WIDTH) w = display.epd2.WIDTH - x;
- if ((y + h - 1) >= display.epd2.HEIGHT) h = display.epd2.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;
- uint8_t cv4 = 0x00;
- 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;
- rgb_palette_buffer[pn] = color4(red, green, blue);
- }
- }
- display.clearScreen();
- 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
- uint8_t out_cv4_byte = 0x00;
- uint32_t out_idx = 0;
- 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
- cv4 = color4(red, green, blue);
- break;
- case 24:
- blue = input_buffer[in_idx++];
- green = input_buffer[in_idx++];
- red = input_buffer[in_idx++];
- cv4 = color4(red, green, blue);
- 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;
- }
- else // 565
- {
- blue = (lsb & 0x1F) << 3;
- green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
- red = (msb & 0xF8);
- }
- }
- cv4 = color4(red, green, blue);
- 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;
- cv4 = rgb_palette_buffer[pn];
- in_byte <<= depth;
- in_bits -= depth;
- }
- break;
- }
- out_cv4_byte |= cv4 << (2 * (3 - col % 4));
- if ((3 == col % 4) || (col == w - 1)) // write that last byte! (for w%4!=0 border)
- {
- output_row_native4c_buffer[out_idx++] = out_cv4_byte;
- out_cv4_byte = 0x00;
- }
- } // end pixel
- int16_t yrow = y + (flip ? h - row - 1 : row);
- // void writeNative(const uint8_t* data1, const uint8_t* data2, int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
- display.writeNative(output_row_native4c_buffer, 0, x, yrow, w, 1, false, false, false);
- } // end line
- Serial.print("downloaded in ");
- Serial.print(millis() - startTime);
- Serial.println(" ms");
- display.refresh();
- }
- Serial.print("bytes read "); Serial.println(bytes_read);
- }
- }
- client.stop();
- if (!valid)
- {
- Serial.println("bitmap format not handled.");
- }
- }
|