GxEPD2_WiFi_Example.ino 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. // GxEPD2_WiFi_Example : Display Library example for SPI e-paper panels from Dalian Good Display and boards from Waveshare.
  2. // Requires HW SPI and Adafruit_GFX. Caution: the e-paper panels require 3.3V supply AND data lines!
  3. //
  4. // Display Library based on Demo Example from Good Display: https://www.good-display.com/companyfile/32/
  5. //
  6. // BMP handling code extracts taken from: https://github.com/prenticedavid/MCUFRIEND_kbv/tree/master/examples/showBMP_kbv_Uno
  7. //
  8. // Author: Jean-Marc Zingg
  9. //
  10. // Version: see library.properties
  11. //
  12. // Library: https://github.com/ZinggJM/GxEPD2
  13. //
  14. // note that BMP bitmaps are drawn at physical position in physical orientation of the screen
  15. // Supporting Arduino Forum Topics (closed, read only):
  16. // Good Display ePaper for Arduino: https://forum.arduino.cc/t/good-display-epaper-for-arduino/419657
  17. // Waveshare e-paper displays with SPI: https://forum.arduino.cc/t/waveshare-e-paper-displays-with-spi/467865
  18. //
  19. // Add new topics in https://forum.arduino.cc/c/using-arduino/displays/23 for new questions and issues
  20. // see GxEPD2_wiring_examples.h for wiring suggestions and examples
  21. // base class GxEPD2_GFX can be used to pass references or pointers to the display instance as parameter, uses ~1.2k more code
  22. // enable or disable GxEPD2_GFX base class
  23. #define ENABLE_GxEPD2_GFX 0
  24. // uncomment next line to use class GFX of library GFX_Root instead of Adafruit_GFX
  25. //#include <GFX.h>
  26. // Note: if you use this with ENABLE_GxEPD2_GFX 1:
  27. // uncomment it in GxEPD2_GFX.h too, or add #include <GFX.h> before any #include <GxEPD2_GFX.h>
  28. #include <GxEPD2_BW.h>
  29. #include <GxEPD2_3C.h>
  30. #include <GxEPD2_4C.h>
  31. #include <GxEPD2_7C.h>
  32. #include "GxEPD2_WiFi_DisplaySelection.h"
  33. #if defined (ESP8266)
  34. #include <ESP8266WiFi.h>
  35. #else
  36. #include <WiFi.h>
  37. #endif
  38. #include <WiFiClient.h>
  39. #include <WiFiClientSecure.h>
  40. const char* ssid = "........";
  41. const char* password = "........";
  42. const int httpPort = 80;
  43. const int httpsPort = 443;
  44. // note: the certificates have been moved to a separate header file, as R"CERT( destroys IDE Auto Format capability
  45. #include "GxEPD2_github_raw_certs.h"
  46. const char* certificate_rawcontent = github_io_pem; // ok, should work until Fri, 14 Mar 2025 23:59:59 GMT
  47. const char* host_rawcontent = "raw.githubusercontent.com";
  48. const char* path_rawcontent = "/ZinggJM/GxEPD2/master/extras/bitmaps/";
  49. const char* path_workcontent = "/ZinggJM/GxEPD2/work_in_progress/extras/bitmaps/";
  50. const char* path_prenticedavid = "/prenticedavid/MCUFRIEND_kbv/master/extras/bitmaps/";
  51. const char* path_waveshare_c = "/waveshare/e-Paper/master/RaspberryPi_JetsonNano/c/pic/";
  52. const char* path_waveshare_py = "/waveshare/e-Paper/master/RaspberryPi_JetsonNano/python/pic/";
  53. const char* fp_rawcontent = "97:D8:C5:70:0F:12:24:6C:88:BC:FA:06:7E:8C:A7:4D:A8:62:67:28"; // SHA-1 as of 12.04.2024
  54. // note that BMP bitmaps are drawn at physical position in physical orientation of the screen
  55. void showBitmapFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color = true);
  56. void showBitmapFrom_HTTPS(const char* host, const char* path, const char* filename, const char* fingerprint, int16_t x, int16_t y, bool with_color = true,
  57. const char* certificate = certificate_rawcontent);
  58. // draws BMP bitmap according to set orientation
  59. void showBitmapFrom_HTTP_Buffered(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color = true);
  60. 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 = true,
  61. const char* certificate = certificate_rawcontent);
  62. void showNative4cFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color = true);
  63. void showNative4cFrom_HTTPS(const char* host, const char* path, const char* filename, const char* fingerprint, int16_t x, int16_t y, bool with_color = true,
  64. const char* certificate = certificate_rawcontent);
  65. void showNative7cFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color = true);
  66. void showNative7cFrom_HTTPS(const char* host, const char* path, const char* filename, const char* fingerprint, int16_t x, int16_t y, bool with_color = true,
  67. const char* certificate = certificate_rawcontent);
  68. #if defined(ESP32)
  69. // uncomment next line to use HSPI for EPD (and VSPI for SD), e.g. with Waveshare ESP32 Driver Board
  70. //#define USE_HSPI_FOR_EPD
  71. #endif
  72. #if defined(ARDUINO_RASPBERRY_PI_PICO_W)
  73. // SPIClassRP2040(spi_inst_t *spi, pin_size_t rx, pin_size_t cs, pin_size_t sck, pin_size_t tx);
  74. // uncomment next line for use with Waveshare Pico-ePaper-2.9.
  75. SPIClassRP2040 SPIn(spi1, 12, 13, 10, 11); // need be valid pins for same SPI channel, else fails blinking 4 long 4 short
  76. #endif
  77. #if defined(ESP32) && defined(USE_HSPI_FOR_EPD)
  78. SPIClass hspi(HSPI);
  79. #endif
  80. void setup()
  81. {
  82. #if defined(ESP32) && defined(USE_HSPI_FOR_EPD)
  83. hspi.begin(13, 12, 14, 15); // remap hspi for EPD (swap pins)
  84. display.epd2.selectSPI(hspi, SPISettings(4000000, MSBFIRST, SPI_MODE0));
  85. #endif
  86. #if defined(ARDUINO_RASPBERRY_PI_PICO_W)
  87. delay(2000); // USB serial available
  88. display.epd2.selectSPI(SPIn, SPISettings(4000000, MSBFIRST, SPI_MODE0));
  89. #endif
  90. Serial.begin(115200);
  91. Serial.println();
  92. Serial.println("GxEPD2_WiFi_Example");
  93. //display.init(115200); // default 10ms reset pulse, e.g. for bare panels with DESPI-C02
  94. display.init(115200, true, 2, false); // USE THIS for Waveshare boards with "clever" reset circuit, 2ms reset pulse
  95. if (display.pages() > 1)
  96. {
  97. delay(100);
  98. Serial.println();
  99. Serial.print("pages = "); Serial.print(display.pages()); Serial.print(" page height = "); Serial.println(display.pageHeight());
  100. delay(1000);
  101. }
  102. Serial.println();
  103. WiFi.mode(WIFI_STA); // switch off AP
  104. Serial.print("Connecting to ");
  105. Serial.println(ssid);
  106. WiFi.begin(ssid, password);
  107. int ConnectTimeout = 60; // 30 seconds
  108. while (WiFi.status() != WL_CONNECTED)
  109. {
  110. delay(500);
  111. Serial.print(".");
  112. Serial.print(WiFi.status());
  113. if (--ConnectTimeout <= 0)
  114. {
  115. Serial.println();
  116. Serial.println("WiFi connect timeout");
  117. return;
  118. }
  119. }
  120. Serial.println();
  121. Serial.println("WiFi connected");
  122. // Print the IP address
  123. Serial.println(WiFi.localIP());
  124. setClock();
  125. if ((display.epd2.panel == GxEPD2::GDEW0154Z04) || (display.epd2.panel == GxEPD2::ACeP565) || (display.epd2.panel == GxEPD2::GDEY073D46) || (display.epd2.panel == GxEPD2::GDEP073E01) || false)
  126. {
  127. //drawBitmapsBuffered_200x200();
  128. //drawBitmapsBuffered_other();
  129. //drawBitmapsBuffered_7C();
  130. drawBitmaps_7C();
  131. //drawBitmaps_7C_mixed();
  132. }
  133. else
  134. {
  135. drawBitmaps_200x200();
  136. drawBitmaps_other();
  137. }
  138. //drawBitmaps_test();
  139. //drawBitmapsBuffered_test();
  140. Serial.println("GxEPD2_WiFi_Example done");
  141. }
  142. void loop(void)
  143. {
  144. }
  145. void drawBitmaps_200x200()
  146. {
  147. int16_t x = (display.width() - 200) / 2;
  148. int16_t y = (display.height() - 200) / 2;
  149. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "logo200x200.bmp", fp_rawcontent, x, y);
  150. delay(2000);
  151. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "first200x200.bmp", fp_rawcontent, x, y);
  152. delay(2000);
  153. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "second200x200.bmp", fp_rawcontent, x, y);
  154. delay(2000);
  155. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "third200x200.bmp", fp_rawcontent, x, y);
  156. delay(2000);
  157. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "fourth200x200.bmp", fp_rawcontent, x, y);
  158. delay(2000);
  159. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "fifth200x200.bmp", fp_rawcontent, x, y);
  160. delay(2000);
  161. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "sixth200x200.bmp", fp_rawcontent, x, y);
  162. delay(2000);
  163. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "seventh200x200.bmp", fp_rawcontent, x, y);
  164. delay(2000);
  165. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "eighth200x200.bmp", fp_rawcontent, x, y);
  166. delay(2000);
  167. }
  168. void drawBitmaps_other()
  169. {
  170. int16_t w2 = display.width() / 2;
  171. int16_t h2 = display.height() / 2;
  172. //showBitmapFrom_HTTP("www.packescape.com", "/img/assets/", "IniciMenusTV2.bmp", w2 - 200, h2 - 150, false);
  173. //delay(2000);
  174. showBitmapFrom_HTTP("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", w2 - 50, h2 - 50, false);
  175. delay(2000);
  176. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_1.bmp", fp_rawcontent, w2 - 100, h2 - 160);
  177. delay(2000);
  178. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  179. delay(2000);
  180. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "marilyn_240x240x8.bmp", fp_rawcontent, w2 - 120, h2 - 120);
  181. delay(2000);
  182. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "miniwoof.bmp", fp_rawcontent, w2 - 60, h2 - 80);
  183. delay(2000);
  184. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "test.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  185. delay(2000);
  186. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger.bmp", fp_rawcontent, w2 - 160, h2 - 120);
  187. delay(2000);
  188. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_178x160x4.bmp", fp_rawcontent, w2 - 89, h2 - 80);
  189. delay(2000);
  190. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_240x317x4.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  191. delay(2000);
  192. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_320x200x24.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  193. delay(2000);
  194. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger16T.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  195. delay(2000);
  196. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "woof.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  197. delay(2000);
  198. }
  199. void drawBitmaps_test()
  200. {
  201. int16_t w2 = display.width() / 2;
  202. int16_t h2 = display.height() / 2;
  203. //showBitmapFrom_HTTPS(host_rawcontent, path_workcontent, "z0gs/screenshot.bmp", fp_rawcontent, 0, 0); delay(2000); return;
  204. //showNative4cFrom_HTTPS(host_rawcontent, path_workcontent, "z0gs/screenshot.bmp", fp_rawcontent, 0, 0); delay(2000); return;
  205. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  206. delay(2000);
  207. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "output5.bmp", fp_rawcontent, 0, 0);
  208. delay(2000);
  209. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "output6.bmp", fp_rawcontent, 0, 0);
  210. delay(2000);
  211. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_1.bmp", fp_rawcontent, 0, 0);
  212. delay(2000);
  213. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_4.bmp", fp_rawcontent, 0, 0);
  214. delay(2000);
  215. //showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_8.bmp", fp_rawcontent, 0, 0); // format 1: BI_RLE8 is not supported
  216. //delay(2000);
  217. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_11.bmp", fp_rawcontent, 0, 0);
  218. delay(2000);
  219. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_44.bmp", fp_rawcontent, 0, 0);
  220. delay(2000);
  221. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_88.bmp", fp_rawcontent, 0, 0);
  222. delay(2000);
  223. //showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "liberato640x384.bmp", fp_rawcontent, 0, 0);
  224. //delay(2000);
  225. }
  226. void drawBitmapsBuffered_200x200()
  227. {
  228. int16_t x = (display.width() - 200) / 2;
  229. int16_t y = (display.height() - 200) / 2;
  230. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "logo200x200.bmp", fp_rawcontent, x, y);
  231. delay(2000);
  232. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "first200x200.bmp", fp_rawcontent, x, y);
  233. delay(2000);
  234. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "second200x200.bmp", fp_rawcontent, x, y);
  235. delay(2000);
  236. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "third200x200.bmp", fp_rawcontent, x, y);
  237. delay(2000);
  238. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "fourth200x200.bmp", fp_rawcontent, x, y);
  239. delay(2000);
  240. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "fifth200x200.bmp", fp_rawcontent, x, y);
  241. delay(2000);
  242. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "sixth200x200.bmp", fp_rawcontent, x, y);
  243. delay(2000);
  244. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "seventh200x200.bmp", fp_rawcontent, x, y);
  245. delay(2000);
  246. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "eighth200x200.bmp", fp_rawcontent, x, y);
  247. delay(2000);
  248. }
  249. void drawBitmapsBuffered_other()
  250. {
  251. int16_t w2 = display.width() / 2;
  252. int16_t h2 = display.height() / 2;
  253. showBitmapFrom_HTTP_Buffered("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", w2 - 50, h2 - 50, false);
  254. delay(2000);
  255. //showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_1.bmp", fp_rawcontent, w2 - 100, h2 - 160);
  256. delay(2000);
  257. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  258. delay(2000);
  259. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "marilyn_240x240x8.bmp", fp_rawcontent, w2 - 120, h2 - 120);
  260. delay(2000);
  261. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "miniwoof.bmp", fp_rawcontent, w2 - 60, h2 - 80);
  262. delay(2000);
  263. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "test.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  264. delay(2000);
  265. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger.bmp", fp_rawcontent, w2 - 160, h2 - 120);
  266. delay(2000);
  267. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_178x160x4.bmp", fp_rawcontent, w2 - 89, h2 - 80);
  268. delay(2000);
  269. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_240x317x4.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  270. delay(2000);
  271. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_320x200x24.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  272. delay(2000);
  273. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger16T.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  274. delay(2000);
  275. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "woof.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  276. delay(2000);
  277. }
  278. void drawBitmapsBuffered_7C()
  279. {
  280. if ((display.epd2.panel == GxEPD2::ACeP565) || (display.epd2.panel == GxEPD2::GDEY073D46) || (display.epd2.panel == GxEPD2::GDEP073E01))
  281. {
  282. int16_t w2 = display.width() / 2;
  283. int16_t h2 = display.height() / 2;
  284. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  285. delay(2000);
  286. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f2.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  287. delay(2000);
  288. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f3.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  289. delay(2000);
  290. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f4.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  291. delay(2000);
  292. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_py, "5in65f4.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  293. delay(2000);
  294. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_py, "N-Color1.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  295. delay(2000);
  296. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "displayed_bmp_small_but_padded.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  297. delay(2000);
  298. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "displayed_bmp_large.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  299. delay(2000);
  300. }
  301. }
  302. void drawBitmaps_7C()
  303. {
  304. if ((display.epd2.panel == GxEPD2::ACeP565) || (display.epd2.panel == GxEPD2::GDEY073D46) || (display.epd2.panel == GxEPD2::GDEP073E01))
  305. {
  306. int16_t w2 = display.epd2.WIDTH / 2;
  307. int16_t h2 = display.epd2.HEIGHT / 2;
  308. showNative7cFrom_HTTPS(host_rawcontent, path_waveshare_c, "5in65f.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  309. delay(2000);
  310. showNative7cFrom_HTTPS(host_rawcontent, path_waveshare_c, "5in65f2.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  311. delay(2000);
  312. showNative7cFrom_HTTPS(host_rawcontent, path_waveshare_c, "5in65f3.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  313. delay(2000);
  314. showNative7cFrom_HTTPS(host_rawcontent, path_waveshare_c, "5in65f4.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  315. delay(2000);
  316. showNative7cFrom_HTTPS(host_rawcontent, path_waveshare_py, "5in65f4.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  317. delay(2000);
  318. showNative7cFrom_HTTPS(host_rawcontent, path_waveshare_py, "N-Color1.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  319. delay(2000);
  320. showNative7cFrom_HTTPS(host_rawcontent, path_rawcontent, "displayed_bmp_small_but_padded.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  321. delay(2000);
  322. showNative7cFrom_HTTPS(host_rawcontent, path_rawcontent, "displayed_bmp_large.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  323. delay(2000);
  324. }
  325. }
  326. void drawBitmaps_7C_mixed()
  327. {
  328. if ((display.epd2.panel == GxEPD2::ACeP565) || (display.epd2.panel == GxEPD2::GDEY073D46) || (display.epd2.panel == GxEPD2::GDEP073E01))
  329. {
  330. int16_t w2 = display.epd2.WIDTH / 2;
  331. int16_t h2 = display.epd2.HEIGHT / 2;
  332. display.setRotation(2);
  333. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  334. delay(2000);
  335. showNative7cFrom_HTTPS(host_rawcontent, path_waveshare_c, "5in65f.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  336. delay(2000);
  337. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f2.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  338. delay(2000);
  339. showNative7cFrom_HTTPS(host_rawcontent, path_waveshare_c, "5in65f2.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  340. delay(2000);
  341. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f3.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  342. delay(2000);
  343. showNative7cFrom_HTTPS(host_rawcontent, path_waveshare_c, "5in65f3.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  344. delay(2000);
  345. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f4.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  346. delay(2000);
  347. showNative7cFrom_HTTPS(host_rawcontent, path_waveshare_c, "5in65f4.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  348. delay(2000);
  349. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_py, "5in65f4.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  350. delay(2000);
  351. showNative7cFrom_HTTPS(host_rawcontent, path_waveshare_py, "5in65f4.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  352. delay(2000);
  353. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_py, "N-Color1.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  354. delay(2000);
  355. showNative7cFrom_HTTPS(host_rawcontent, path_waveshare_py, "N-Color1.bmp", fp_rawcontent, w2 - 300, h2 - 224);
  356. delay(2000);
  357. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "displayed_bmp_small_but_padded.bmp", fp_rawcontent, w2 - 400, h2 - 240);
  358. delay(2000);
  359. showNative7cFrom_HTTPS(host_rawcontent, path_rawcontent, "displayed_bmp_small_but_padded.bmp", fp_rawcontent, w2 - 400, h2 - 240);
  360. delay(2000);
  361. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "displayed_bmp_large.bmp", fp_rawcontent, w2 - 400, h2 - 240);
  362. delay(2000);
  363. showNative7cFrom_HTTPS(host_rawcontent, path_rawcontent, "displayed_bmp_large.bmp", fp_rawcontent, w2 - 400, h2 - 240);
  364. delay(2000);
  365. }
  366. }
  367. void drawBitmapsBuffered_test()
  368. {
  369. int16_t w2 = display.width() / 2;
  370. int16_t h2 = display.height() / 2;
  371. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  372. delay(2000);
  373. }
  374. static const uint16_t input_buffer_pixels = 800; // may affect performance
  375. //static const uint16_t input_buffer_pixels = 960; // may affect performance
  376. static const uint16_t max_row_width = 1872; // for up to 7.8" display 1872x1404
  377. static const uint16_t max_palette_pixels = 256; // for depth <= 8
  378. uint8_t input_buffer[3 * input_buffer_pixels]; // up to depth 24
  379. uint8_t output_row_mono_buffer[max_row_width / 8]; // buffer for at least one row of b/w bits
  380. uint8_t output_row_color_buffer[max_row_width / 8]; // buffer for at least one row of color bits
  381. uint8_t mono_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 b/w
  382. uint8_t color_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 c/w
  383. uint16_t rgb_palette_buffer[max_palette_pixels]; // palette buffer for depth <= 8 for buffered graphics, needed for 7-color display
  384. void showBitmapFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  385. {
  386. WiFiClient client;
  387. bool connection_ok = false;
  388. bool valid = false; // valid format to be handled
  389. bool flip = true; // bitmap is stored bottom-to-top
  390. uint32_t startTime = millis();
  391. if ((x >= display.epd2.WIDTH) || (y >= display.epd2.HEIGHT)) return;
  392. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  393. Serial.print("connecting to "); Serial.println(host);
  394. if (!client.connect(host, httpPort))
  395. {
  396. Serial.println("connection failed");
  397. return;
  398. }
  399. Serial.print("requesting URL: ");
  400. Serial.println(String("http://") + host + path + filename);
  401. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  402. "Host: " + host + "\r\n" +
  403. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  404. "Connection: close\r\n\r\n");
  405. Serial.println("request sent");
  406. while (client.connected())
  407. {
  408. String line = client.readStringUntil('\n');
  409. if (!connection_ok)
  410. {
  411. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  412. if (connection_ok) Serial.println(line);
  413. }
  414. if (!connection_ok) Serial.println(line);
  415. if (line == "\r")
  416. {
  417. Serial.println("headers received");
  418. break;
  419. }
  420. }
  421. if (!connection_ok) return;
  422. // Parse BMP header
  423. if (read16(client) == 0x4D42) // BMP signature
  424. {
  425. uint32_t fileSize = read32(client);
  426. uint32_t creatorBytes = read32(client); (void)creatorBytes; //unused
  427. uint32_t imageOffset = read32(client); // Start of image data
  428. uint32_t headerSize = read32(client);
  429. uint32_t width = read32(client);
  430. int32_t height = (int32_t) read32(client);
  431. uint16_t planes = read16(client);
  432. uint16_t depth = read16(client); // bits per pixel
  433. uint32_t format = read32(client);
  434. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  435. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  436. {
  437. Serial.print("File size: "); Serial.println(fileSize);
  438. Serial.print("Image Offset: "); Serial.println(imageOffset);
  439. Serial.print("Header size: "); Serial.println(headerSize);
  440. Serial.print("Bit Depth: "); Serial.println(depth);
  441. Serial.print("Image size: ");
  442. Serial.print(width);
  443. Serial.print('x');
  444. Serial.println(abs(height));
  445. // BMP rows are padded (if needed) to 4-byte boundary
  446. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  447. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  448. if (height < 0)
  449. {
  450. height = -height;
  451. flip = false;
  452. }
  453. uint16_t w = width;
  454. uint16_t h = height;
  455. if ((x + w - 1) >= display.epd2.WIDTH) w = display.epd2.WIDTH - x;
  456. if ((y + h - 1) >= display.epd2.HEIGHT) h = display.epd2.HEIGHT - y;
  457. if (w <= max_row_width) // handle with direct drawing
  458. {
  459. valid = true;
  460. uint8_t bitmask = 0xFF;
  461. uint8_t bitshift = 8 - depth;
  462. uint16_t red, green, blue;
  463. bool whitish = false;
  464. bool colored = false;
  465. if (depth == 1) with_color = false;
  466. if (depth <= 8)
  467. {
  468. if (depth < 8) bitmask >>= depth;
  469. bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
  470. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  471. {
  472. blue = client.read();
  473. green = client.read();
  474. red = client.read();
  475. client.read();
  476. bytes_read += 4;
  477. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  478. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  479. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  480. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  481. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  482. color_palette_buffer[pn / 8] |= colored << pn % 8;
  483. }
  484. }
  485. display.clearScreen();
  486. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  487. bytes_read += skip(client, rowPosition - bytes_read);
  488. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  489. {
  490. if (!connection_ok || !(client.connected() || client.available())) break;
  491. delay(1); // yield() to avoid WDT
  492. uint32_t in_remain = rowSize;
  493. uint32_t in_idx = 0;
  494. uint32_t in_bytes = 0;
  495. uint8_t in_byte = 0; // for depth <= 8
  496. uint8_t in_bits = 0; // for depth <= 8
  497. uint8_t out_byte = 0xFF; // white (for w%8!=0 border)
  498. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 border)
  499. uint32_t out_idx = 0;
  500. for (uint16_t col = 0; col < w; col++) // for each pixel
  501. {
  502. yield();
  503. if (!connection_ok || !(client.connected() || client.available())) break;
  504. // Time to read more pixel data?
  505. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  506. {
  507. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  508. uint32_t got = read8n(client, input_buffer, get);
  509. while ((got < get) && connection_ok)
  510. {
  511. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  512. uint32_t gotmore = read8n(client, input_buffer + got, get - got);
  513. got += gotmore;
  514. connection_ok = gotmore > 0;
  515. }
  516. in_bytes = got;
  517. in_remain -= got;
  518. bytes_read += got;
  519. in_idx = 0;
  520. }
  521. if (!connection_ok)
  522. {
  523. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  524. break;
  525. }
  526. switch (depth)
  527. {
  528. case 32:
  529. blue = input_buffer[in_idx++];
  530. green = input_buffer[in_idx++];
  531. red = input_buffer[in_idx++];
  532. in_idx++; // skip alpha
  533. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  534. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  535. break;
  536. case 24:
  537. blue = input_buffer[in_idx++];
  538. green = input_buffer[in_idx++];
  539. red = input_buffer[in_idx++];
  540. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  541. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  542. break;
  543. case 16:
  544. {
  545. uint8_t lsb = input_buffer[in_idx++];
  546. uint8_t msb = input_buffer[in_idx++];
  547. if (format == 0) // 555
  548. {
  549. blue = (lsb & 0x1F) << 3;
  550. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  551. red = (msb & 0x7C) << 1;
  552. }
  553. else // 565
  554. {
  555. blue = (lsb & 0x1F) << 3;
  556. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  557. red = (msb & 0xF8);
  558. }
  559. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  560. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  561. }
  562. break;
  563. case 1:
  564. case 2:
  565. case 4:
  566. case 8:
  567. {
  568. if (0 == in_bits)
  569. {
  570. in_byte = input_buffer[in_idx++];
  571. in_bits = 8;
  572. }
  573. uint16_t pn = (in_byte >> bitshift) & bitmask;
  574. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  575. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  576. in_byte <<= depth;
  577. in_bits -= depth;
  578. }
  579. break;
  580. }
  581. if (whitish)
  582. {
  583. // keep white
  584. }
  585. else if (colored && with_color)
  586. {
  587. out_color_byte &= ~(0x80 >> col % 8); // colored
  588. }
  589. else
  590. {
  591. out_byte &= ~(0x80 >> col % 8); // black
  592. }
  593. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 border)
  594. {
  595. output_row_color_buffer[out_idx] = out_color_byte;
  596. output_row_mono_buffer[out_idx++] = out_byte;
  597. out_byte = 0xFF; // white (for w%8!=0 border)
  598. out_color_byte = 0xFF; // white (for w%8!=0 border)
  599. }
  600. } // end pixel
  601. int16_t yrow = y + (flip ? h - row - 1 : row);
  602. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  603. } // end line
  604. Serial.print("downloaded in ");
  605. Serial.print(millis() - startTime);
  606. Serial.println(" ms");
  607. display.refresh();
  608. }
  609. Serial.print("bytes read "); Serial.println(bytes_read);
  610. }
  611. }
  612. client.stop();
  613. if (!valid)
  614. {
  615. Serial.println("bitmap format not handled.");
  616. }
  617. }
  618. void showBitmapFrom_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)
  619. {
  620. // Use WiFiClientSecure class to create TLS connection
  621. #if defined (ESP8266) || defined(ARDUINO_RASPBERRY_PI_PICO_W)
  622. BearSSL::WiFiClientSecure client;
  623. BearSSL::X509List cert(certificate ? certificate : certificate_rawcontent);
  624. #else
  625. WiFiClientSecure client;
  626. #endif
  627. bool connection_ok = false;
  628. bool valid = false; // valid format to be handled
  629. bool flip = true; // bitmap is stored bottom-to-top
  630. uint32_t startTime = millis();
  631. if ((x >= display.epd2.WIDTH) || (y >= display.epd2.HEIGHT)) return;
  632. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  633. Serial.print("connecting to "); Serial.println(host);
  634. #if defined (ESP8266) || defined(ARDUINO_RASPBERRY_PI_PICO_W)
  635. if (certificate) client.setTrustAnchors(&cert);
  636. else if (fingerprint) client.setFingerprint(fingerprint);
  637. else client.setInsecure();
  638. #elif defined (ESP32)
  639. if (certificate) client.setCACert(certificate);
  640. #endif
  641. if (!client.connect(host, httpsPort))
  642. {
  643. Serial.println("connection failed");
  644. return;
  645. }
  646. Serial.print("requesting URL: ");
  647. Serial.println(String("https://") + host + path + filename);
  648. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  649. "Host: " + host + "\r\n" +
  650. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  651. "Connection: close\r\n\r\n");
  652. Serial.println("request sent");
  653. while (client.connected())
  654. {
  655. String line = client.readStringUntil('\n');
  656. if (!connection_ok)
  657. {
  658. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  659. if (connection_ok) Serial.println(line);
  660. }
  661. if (!connection_ok) Serial.println(line);
  662. if (line == "\r")
  663. {
  664. Serial.println("headers received");
  665. break;
  666. }
  667. }
  668. if (!connection_ok) return;
  669. // Parse BMP header
  670. uint16_t signature = 0;
  671. for (int16_t i = 0; i < 50; i++)
  672. {
  673. if (!client.available()) delay(100);
  674. else signature = read16(client);
  675. if (signature == 0x4D42) break;
  676. }
  677. if (signature == 0x4D42) // BMP signature
  678. {
  679. uint32_t fileSize = read32(client);
  680. uint32_t creatorBytes = read32(client); (void)creatorBytes; //unused
  681. uint32_t imageOffset = read32(client); // Start of image data
  682. uint32_t headerSize = read32(client);
  683. uint32_t width = read32(client);
  684. int32_t height = (int32_t) read32(client);
  685. uint16_t planes = read16(client);
  686. uint16_t depth = read16(client); // bits per pixel
  687. uint32_t format = read32(client);
  688. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  689. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  690. {
  691. Serial.print("File size: "); Serial.println(fileSize);
  692. Serial.print("Image Offset: "); Serial.println(imageOffset);
  693. Serial.print("Header size: "); Serial.println(headerSize);
  694. Serial.print("Bit Depth: "); Serial.println(depth);
  695. Serial.print("Image size: ");
  696. Serial.print(width);
  697. Serial.print('x');
  698. Serial.println(abs(height));
  699. // BMP rows are padded (if needed) to 4-byte boundary
  700. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  701. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  702. if (height < 0)
  703. {
  704. height = -height;
  705. flip = false;
  706. }
  707. uint16_t w = width;
  708. uint16_t h = height;
  709. if ((x + w - 1) >= display.epd2.WIDTH) w = display.epd2.WIDTH - x;
  710. if ((y + h - 1) >= display.epd2.HEIGHT) h = display.epd2.HEIGHT - y;
  711. if (w <= max_row_width) // handle with direct drawing
  712. {
  713. valid = true;
  714. uint8_t bitmask = 0xFF;
  715. uint8_t bitshift = 8 - depth;
  716. uint16_t red, green, blue;
  717. bool whitish = false;
  718. bool colored = false;
  719. if (depth == 1) with_color = false;
  720. if (depth <= 8)
  721. {
  722. if (depth < 8) bitmask >>= depth;
  723. //bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  724. bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
  725. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  726. {
  727. blue = client.read();
  728. green = client.read();
  729. red = client.read();
  730. client.read();
  731. bytes_read += 4;
  732. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  733. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  734. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  735. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  736. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  737. color_palette_buffer[pn / 8] |= colored << pn % 8;
  738. }
  739. }
  740. display.clearScreen();
  741. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  742. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  743. bytes_read += skip(client, rowPosition - bytes_read);
  744. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  745. {
  746. if (!connection_ok || !(client.connected() || client.available())) break;
  747. delay(1); // yield() to avoid WDT
  748. uint32_t in_remain = rowSize;
  749. uint32_t in_idx = 0;
  750. uint32_t in_bytes = 0;
  751. uint8_t in_byte = 0; // for depth <= 8
  752. uint8_t in_bits = 0; // for depth <= 8
  753. uint8_t out_byte = 0xFF; // white (for w%8!=0 border)
  754. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 border)
  755. uint32_t out_idx = 0;
  756. for (uint16_t col = 0; col < w; col++) // for each pixel
  757. {
  758. yield();
  759. if (!connection_ok || !(client.connected() || client.available())) break;
  760. // Time to read more pixel data?
  761. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  762. {
  763. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  764. uint32_t got = read8n(client, input_buffer, get);
  765. while ((got < get) && connection_ok)
  766. {
  767. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  768. //if ((get - got) > client.available()) delay(200); // does improve? yes, if >= 200
  769. uint32_t gotmore = read8n(client, input_buffer + got, get - got);
  770. got += gotmore;
  771. connection_ok = gotmore > 0;
  772. }
  773. in_bytes = got;
  774. in_remain -= got;
  775. bytes_read += got;
  776. in_idx = 0;
  777. }
  778. if (!connection_ok)
  779. {
  780. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  781. break;
  782. }
  783. switch (depth)
  784. {
  785. case 32:
  786. blue = input_buffer[in_idx++];
  787. green = input_buffer[in_idx++];
  788. red = input_buffer[in_idx++];
  789. in_idx++; // skip alpha
  790. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  791. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  792. break;
  793. case 24:
  794. blue = input_buffer[in_idx++];
  795. green = input_buffer[in_idx++];
  796. red = input_buffer[in_idx++];
  797. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  798. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  799. break;
  800. case 16:
  801. {
  802. uint8_t lsb = input_buffer[in_idx++];
  803. uint8_t msb = input_buffer[in_idx++];
  804. if (format == 0) // 555
  805. {
  806. blue = (lsb & 0x1F) << 3;
  807. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  808. red = (msb & 0x7C) << 1;
  809. }
  810. else // 565
  811. {
  812. blue = (lsb & 0x1F) << 3;
  813. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  814. red = (msb & 0xF8);
  815. }
  816. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  817. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  818. }
  819. break;
  820. case 1:
  821. case 2:
  822. case 4:
  823. case 8:
  824. {
  825. if (0 == in_bits)
  826. {
  827. in_byte = input_buffer[in_idx++];
  828. in_bits = 8;
  829. }
  830. uint16_t pn = (in_byte >> bitshift) & bitmask;
  831. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  832. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  833. in_byte <<= depth;
  834. in_bits -= depth;
  835. }
  836. break;
  837. }
  838. if (whitish)
  839. {
  840. // keep white
  841. }
  842. else if (colored && with_color)
  843. {
  844. out_color_byte &= ~(0x80 >> col % 8); // colored
  845. }
  846. else
  847. {
  848. out_byte &= ~(0x80 >> col % 8); // black
  849. }
  850. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 border)
  851. {
  852. output_row_color_buffer[out_idx] = out_color_byte;
  853. output_row_mono_buffer[out_idx++] = out_byte;
  854. out_byte = 0xFF; // white (for w%8!=0 border)
  855. out_color_byte = 0xFF; // white (for w%8!=0 border)
  856. }
  857. } // end pixel
  858. int16_t yrow = y + (flip ? h - row - 1 : row);
  859. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  860. } // end line
  861. Serial.print("downloaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  862. Serial.print("bytes read "); Serial.println(bytes_read);
  863. display.refresh();
  864. }
  865. }
  866. }
  867. client.stop();
  868. if (!valid)
  869. {
  870. Serial.println("bitmap format not handled.");
  871. }
  872. }
  873. #include "GxEPD2_WiFi_Buffered.h"
  874. #include "GxEPD2_WiFi_Native4c.h"
  875. #include "GxEPD2_WiFi_Native7c.h"
  876. uint16_t read16(WiFiClient& client)
  877. {
  878. // BMP data is stored little-endian, same as Arduino.
  879. uint16_t result;
  880. ((uint8_t *)&result)[0] = client.read(); // LSB
  881. ((uint8_t *)&result)[1] = client.read(); // MSB
  882. return result;
  883. }
  884. uint32_t read32(WiFiClient& client)
  885. {
  886. // BMP data is stored little-endian, same as Arduino.
  887. uint32_t result;
  888. ((uint8_t *)&result)[0] = client.read(); // LSB
  889. ((uint8_t *)&result)[1] = client.read();
  890. ((uint8_t *)&result)[2] = client.read();
  891. ((uint8_t *)&result)[3] = client.read(); // MSB
  892. return result;
  893. }
  894. #if USE_BearSSL
  895. uint32_t skip(BearSSL::WiFiClientSecure& client, int32_t bytes)
  896. {
  897. int32_t remain = bytes;
  898. uint32_t start = millis();
  899. while ((client.connected() || client.available()) && (remain > 0))
  900. {
  901. if (client.available())
  902. {
  903. client.read();
  904. remain--;
  905. }
  906. else delay(10);
  907. if (millis() - start > 2000) break; // don't hang forever
  908. }
  909. return bytes - remain;
  910. }
  911. uint32_t read8n(BearSSL::WiFiClientSecure& client, uint8_t* buffer, int32_t bytes)
  912. {
  913. int32_t remain = bytes;
  914. uint32_t start = millis();
  915. while ((client.connected() || client.available()) && (remain > 0))
  916. {
  917. if (client.available())
  918. {
  919. int16_t v = client.read();
  920. *buffer++ = uint8_t(v);
  921. remain--;
  922. }
  923. else delay(10);
  924. if (millis() - start > 2000) break; // don't hang forever
  925. }
  926. return bytes - remain;
  927. }
  928. #endif
  929. uint32_t skip(WiFiClient& client, int32_t bytes)
  930. {
  931. int32_t remain = bytes;
  932. uint32_t start = millis();
  933. while ((client.connected() || client.available()) && (remain > 0))
  934. {
  935. if (client.available())
  936. {
  937. client.read();
  938. remain--;
  939. }
  940. else delay(1);
  941. if (millis() - start > 2000) break; // don't hang forever
  942. }
  943. return bytes - remain;
  944. }
  945. uint32_t read8n(WiFiClient& client, uint8_t* buffer, int32_t bytes)
  946. {
  947. int32_t remain = bytes;
  948. uint32_t start = millis();
  949. while ((client.connected() || client.available()) && (remain > 0))
  950. {
  951. if (client.available())
  952. {
  953. int16_t v = client.read();
  954. *buffer++ = uint8_t(v);
  955. remain--;
  956. }
  957. else delay(1);
  958. if (millis() - start > 2000) break; // don't hang forever
  959. }
  960. return bytes - remain;
  961. }
  962. // Set time via NTP, as required for x.509 validation
  963. void setClock()
  964. {
  965. configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
  966. Serial.print("Waiting for NTP time sync: ");
  967. time_t now = time(nullptr);
  968. while (now < 8 * 3600 * 2)
  969. {
  970. delay(500);
  971. Serial.print(".");
  972. now = time(nullptr);
  973. }
  974. Serial.println("");
  975. struct tm timeinfo;
  976. gmtime_r(&now, &timeinfo);
  977. Serial.print("Current time: ");
  978. Serial.print(asctime(&timeinfo));
  979. }