GxEPD2_WiFi_Example.ino 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  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: these e-papers require 3.3V supply AND data lines!
  3. //
  4. // Display Library based on Demo Example from Good Display: http://www.good-display.com/download_list/downloadcategoryid=34&isMode=false.html
  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. // Supporting Arduino Forum Topics:
  14. // Waveshare e-paper displays with SPI: http://forum.arduino.cc/index.php?topic=487007.0
  15. // Good Dispay ePaper for Arduino : https://forum.arduino.cc/index.php?topic=436411.0
  16. // mapping suggestion from Waveshare SPI e-Paper to Wemos D1 mini
  17. // BUSY -> D2, RST -> D4, DC -> D3, CS -> D8, CLK -> D5, DIN -> D7, GND -> GND, 3.3V -> 3.3V
  18. // mapping suggestion from Waveshare SPI e-Paper to generic ESP8266
  19. // BUSY -> GPIO4, RST -> GPIO2, DC -> GPIO0, CS -> GPIO15, CLK -> GPIO14, DIN -> GPIO13, GND -> GND, 3.3V -> 3.3V
  20. // mapping of Waveshare e-Paper ESP8266 Driver Board
  21. // BUSY -> GPIO16, RST -> GPIO5, DC -> GPIO4, CS -> GPIO15, CLK -> GPIO14, DIN -> GPIO13, GND -> GND, 3.3V -> 3.3V
  22. // mapping suggestion for ESP32, e.g. LOLIN32, see .../variants/.../pins_arduino.h for your board
  23. // NOTE: there are variants with different pins for SPI ! CHECK SPI PINS OF YOUR BOARD
  24. // BUSY -> 4, RST -> 16, DC -> 17, CS -> SS(5), CLK -> SCK(18), DIN -> MOSI(23), GND -> GND, 3.3V -> 3.3V
  25. // new mapping suggestion for STM32F1, e.g. STM32F103C8T6 "BluePill"
  26. // BUSY -> A1, RST -> A2, DC -> A3, CS-> A4, CLK -> A5, DIN -> A7
  27. // mapping suggestion for AVR, UNO, NANO etc.
  28. // BUSY -> 7, RST -> 9, DC -> 8, CS-> 10, CLK -> 13, DIN -> 11
  29. //
  30. #include <GxEPD2_BW.h>
  31. #include <GxEPD2_3C.h>
  32. #define EPD_CS SS
  33. #if defined (ESP8266)
  34. // select one and adapt to your mapping, can use full buffer size (full HEIGHT)
  35. //GxEPD2_BW<GxEPD2_154, GxEPD2_154::HEIGHT> display(GxEPD2_154(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  36. //GxEPD2_BW<GxEPD2_213, GxEPD2_213::HEIGHT> display(GxEPD2_213(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  37. //GxEPD2_BW<GxEPD2_213_flex, GxEPD2_213_flex::HEIGHT> display(GxEPD2_213_flex(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  38. //GxEPD2_BW<GxEPD2_290, GxEPD2_290::HEIGHT> display(GxEPD2_290(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  39. //GxEPD2_BW<GxEPD2_270, GxEPD2_270::HEIGHT> display(GxEPD2_270(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  40. //GxEPD2_BW<GxEPD2_420, GxEPD2_420::HEIGHT> display(GxEPD2_420(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  41. // can use only half buffer size
  42. //GxEPD2_BW < GxEPD2_583, GxEPD2_583::HEIGHT / 2 > display(GxEPD2_583(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  43. //GxEPD2_BW < GxEPD2_750, GxEPD2_750::HEIGHT / 2 > display(GxEPD2_750(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  44. // 3-color e-papers
  45. //GxEPD2_3C<GxEPD2_154c, GxEPD2_154c::HEIGHT> display(GxEPD2_154c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  46. //GxEPD2_3C<GxEPD2_213c, GxEPD2_213c::HEIGHT> display(GxEPD2_213c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  47. //GxEPD2_3C<GxEPD2_290c, GxEPD2_290c::HEIGHT> display(GxEPD2_290c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  48. //GxEPD2_3C<GxEPD2_270c, GxEPD2_270c::HEIGHT> display(GxEPD2_270c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  49. // can use only half buffer size
  50. //GxEPD2_3C<GxEPD2_420c, GxEPD2_420c::HEIGHT / 2> display(GxEPD2_420c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  51. // can use only quarter buffer size
  52. //GxEPD2_3C<GxEPD2_583c, GxEPD2_583c::HEIGHT / 4> display(GxEPD2_583c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  53. //GxEPD2_3C<GxEPD2_750c, GxEPD2_750c::HEIGHT / 4> display(GxEPD2_750c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  54. // ***** for mapping of Waveshare e-Paper ESP8266 Driver Board *****
  55. // select one , can use full buffer size (full HEIGHT)
  56. //GxEPD2_BW<GxEPD2_154, GxEPD2_154::HEIGHT> display(GxEPD2_154(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  57. //GxEPD2_BW<GxEPD2_213, GxEPD2_213::HEIGHT> display(GxEPD2_213(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  58. //GxEPD2_BW<GxEPD2_213_flex, GxEPD2_213_flex::HEIGHT> display(GxEPD2_213_flex(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  59. //GxEPD2_BW<GxEPD2_290, GxEPD2_290::HEIGHT> display(GxEPD2_290(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  60. //GxEPD2_BW<GxEPD2_270, GxEPD2_270::HEIGHT> display(GxEPD2_270(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  61. //GxEPD2_BW<GxEPD2_420, GxEPD2_420::HEIGHT> display(GxEPD2_420(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  62. // can use only half buffer size
  63. //GxEPD2_BW < GxEPD2_583, GxEPD2_583::HEIGHT / 2 > display(GxEPD2_583(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  64. //GxEPD2_BW < GxEPD2_750, GxEPD2_750::HEIGHT / 2 > display(GxEPD2_750(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  65. // 3-color e-papers
  66. //GxEPD2_3C<GxEPD2_154c, GxEPD2_154c::HEIGHT> display(GxEPD2_154c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  67. //GxEPD2_3C<GxEPD2_213c, GxEPD2_213c::HEIGHT> display(GxEPD2_213c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  68. //GxEPD2_3C<GxEPD2_290c, GxEPD2_290c::HEIGHT> display(GxEPD2_290c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  69. //GxEPD2_3C<GxEPD2_270c, GxEPD2_270c::HEIGHT> display(GxEPD2_270c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  70. // can use only half buffer size
  71. //GxEPD2_3C<GxEPD2_420c, GxEPD2_420c::HEIGHT / 2> display(GxEPD2_420c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  72. // can use only quarter buffer size
  73. //GxEPD2_3C<GxEPD2_583c, GxEPD2_583c::HEIGHT / 4> display(GxEPD2_583c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  74. //GxEPD2_3C<GxEPD2_750c, GxEPD2_750c::HEIGHT / 4> display(GxEPD2_750c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  75. #endif
  76. #if defined(ESP32)
  77. // select one and adapt to your mapping, can use full buffer size (full HEIGHT)
  78. //GxEPD2_BW<GxEPD2_154, GxEPD2_154::HEIGHT> display(GxEPD2_154(/*CS=5*/ EPD_CS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  79. //GxEPD2_BW<GxEPD2_213, GxEPD2_213::HEIGHT> display(GxEPD2_213(/*CS=5*/ EPD_CS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  80. //GxEPD2_BW<GxEPD2_213_flex, GxEPD2_213_flex::HEIGHT> display(GxEPD2_213_flex(/*CS=5*/ EPD_CS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  81. //GxEPD2_BW<GxEPD2_290, GxEPD2_290::HEIGHT> display(GxEPD2_290(/*CS=5*/ EPD_CS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  82. //GxEPD2_BW<GxEPD2_270, GxEPD2_270::HEIGHT> display(GxEPD2_270(/*CS=5*/ EPD_CS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  83. //GxEPD2_BW<GxEPD2_420, GxEPD2_420::HEIGHT> display(GxEPD2_420(/*CS=5*/ EPD_CS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  84. //GxEPD2_BW<GxEPD2_750, GxEPD2_750::HEIGHT> display(GxEPD2_750(/*CS=5*/ EPD_CS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  85. // 3-color e-papers
  86. //GxEPD2_3C<GxEPD2_154c, GxEPD2_154c::HEIGHT> display(GxEPD2_154c(/*CS=5*/ EPD_CS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  87. //GxEPD2_3C<GxEPD2_213c, GxEPD2_213c::HEIGHT> display(GxEPD2_213c(/*CS=5*/ EPD_CS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  88. //GxEPD2_3C<GxEPD2_290c, GxEPD2_290c::HEIGHT> display(GxEPD2_290c(/*CS=5*/ EPD_CS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  89. //GxEPD2_3C<GxEPD2_270c, GxEPD2_270c::HEIGHT> display(GxEPD2_270c(/*CS=5*/ EPD_CS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  90. //GxEPD2_3C<GxEPD2_420c, GxEPD2_420c::HEIGHT> display(GxEPD2_420c(/*CS=5*/ EPD_CS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  91. //GxEPD2_3C<GxEPD2_750c, GxEPD2_750c::HEIGHT> display(GxEPD2_750c(/*CS=5*/ EPD_CS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  92. #endif
  93. #if defined (ESP8266)
  94. #include <ESP8266WiFi.h>
  95. #define USE_BearSSL true
  96. #endif
  97. #include <WiFiClient.h>
  98. #include <WiFiClientSecure.h>
  99. const char* ssid = "........";
  100. const char* password = "........";
  101. const int httpPort = 80;
  102. const int httpsPort = 443;
  103. const char* fp_api_github_com = "35 85 74 EF 67 35 A7 CE 40 69 50 F3 C0 F6 80 CF 80 3B 2E 19";
  104. const char* fp_github_com = "ca 06 f5 6b 25 8b 7a 0d 4f 2b 05 47 09 39 47 86 51 15 19 84";
  105. #if USE_BearSSL
  106. const char fp_rawcontent[20] = {0xcc, 0xaa, 0x48, 0x48, 0x66, 0x46, 0x0e, 0x91, 0x53, 0x2c, 0x9c, 0x7c, 0x23, 0x2a, 0xb1, 0x74, 0x4d, 0x29, 0x9d, 0x33};
  107. #else
  108. const char* fp_rawcontent = "cc aa 48 48 66 46 0e 91 53 2c 9c 7c 23 2a b1 74 4d 29 9d 33";
  109. #endif
  110. const char* host_rawcontent = "raw.githubusercontent.com";
  111. const char* path_rawcontent = "/ZinggJM/GxEPD2/master/extras/bitmaps/";
  112. const char* path_prenticedavid = "/prenticedavid/MCUFRIEND_kbv/master/extras/bitmaps/";
  113. void showBitmapFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color = true);
  114. 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);
  115. void showBitmapFrom_HTTP_Buffered(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color = true);
  116. 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);
  117. void setup()
  118. {
  119. Serial.begin(115200);
  120. Serial.println();
  121. Serial.println("GxEPD2_WiFi_Example");
  122. display.init(115200);
  123. #ifdef RE_INIT_NEEDED
  124. WiFi.persistent(true);
  125. WiFi.mode(WIFI_STA); // switch off AP
  126. WiFi.setAutoConnect(true);
  127. WiFi.setAutoReconnect(true);
  128. WiFi.disconnect();
  129. #endif
  130. if (!WiFi.getAutoConnect() || ( WiFi.getMode() != WIFI_STA) || ((WiFi.SSID() != ssid) && String(ssid) != "........"))
  131. {
  132. Serial.println();
  133. Serial.print("WiFi.getAutoConnect()=");
  134. Serial.println(WiFi.getAutoConnect());
  135. Serial.print("WiFi.SSID()=");
  136. Serial.println(WiFi.SSID());
  137. WiFi.mode(WIFI_STA); // switch off AP
  138. Serial.print("Connecting to ");
  139. Serial.println(ssid);
  140. WiFi.begin(ssid, password);
  141. }
  142. int ConnectTimeout = 30; // 15 seconds
  143. while (WiFi.status() != WL_CONNECTED)
  144. {
  145. delay(500);
  146. Serial.print(".");
  147. Serial.print(WiFi.status());
  148. if (--ConnectTimeout <= 0)
  149. {
  150. Serial.println();
  151. Serial.println("WiFi connect timeout");
  152. return;
  153. }
  154. }
  155. Serial.println();
  156. Serial.println("WiFi connected");
  157. // Print the IP address
  158. Serial.println(WiFi.localIP());
  159. if ((display.epd2.panel == GxEPD2::GDEW0154Z04) || false)
  160. {
  161. drawBitmapsBuffered_200x200();
  162. drawBitmapsBuffered_other();
  163. }
  164. else
  165. {
  166. drawBitmaps_200x200();
  167. drawBitmaps_other();
  168. }
  169. //drawBitmaps_test();
  170. //drawBitmapsBuffered_test();
  171. Serial.println("GxEPD2_WiFi_Example done");
  172. }
  173. void loop(void)
  174. {
  175. }
  176. void drawBitmaps_200x200()
  177. {
  178. int16_t x = (display.width() - 200) / 2;
  179. int16_t y = (display.height() - 200) / 2;
  180. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "logo200x200.bmp", fp_rawcontent, x, y);
  181. delay(2000);
  182. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "first200x200.bmp", fp_rawcontent, x, y);
  183. delay(2000);
  184. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "second200x200.bmp", fp_rawcontent, x, y);
  185. delay(2000);
  186. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "third200x200.bmp", fp_rawcontent, x, y);
  187. delay(2000);
  188. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "fourth200x200.bmp", fp_rawcontent, x, y);
  189. delay(2000);
  190. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "fifth200x200.bmp", fp_rawcontent, x, y);
  191. delay(2000);
  192. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "sixth200x200.bmp", fp_rawcontent, x, y);
  193. delay(2000);
  194. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "seventh200x200.bmp", fp_rawcontent, x, y);
  195. delay(2000);
  196. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "eighth200x200.bmp", fp_rawcontent, x, y);
  197. delay(2000);
  198. }
  199. void drawBitmaps_other()
  200. {
  201. int16_t w2 = display.width() / 2;
  202. int16_t h2 = display.height() / 2;
  203. showBitmapFrom_HTTP("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", w2 - 50, h2 - 50, false);
  204. delay(2000);
  205. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_1.bmp", fp_rawcontent, w2 - 100, h2 - 160);
  206. delay(2000);
  207. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  208. delay(2000);
  209. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "marilyn_240x240x8.bmp", fp_rawcontent, w2 - 120, h2 - 120);
  210. delay(2000);
  211. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "miniwoof.bmp", fp_rawcontent, w2 - 60, h2 - 80);
  212. delay(2000);
  213. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "test.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  214. delay(2000);
  215. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger.bmp", fp_rawcontent, w2 - 160, h2 - 120);
  216. delay(2000);
  217. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_178x160x4.bmp", fp_rawcontent, w2 - 89, h2 - 80);
  218. delay(2000);
  219. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_240x317x4.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  220. delay(2000);
  221. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_320x200x24.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  222. delay(2000);
  223. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger16T.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  224. delay(2000);
  225. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "woof.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  226. delay(2000);
  227. }
  228. void drawBitmaps_test()
  229. {
  230. int16_t w2 = display.width() / 2;
  231. int16_t h2 = display.height() / 2;
  232. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  233. delay(2000);
  234. }
  235. void drawBitmapsBuffered_200x200()
  236. {
  237. int16_t x = (display.width() - 200) / 2;
  238. int16_t y = (display.height() - 200) / 2;
  239. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "logo200x200.bmp", fp_rawcontent, x, y);
  240. delay(2000);
  241. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "first200x200.bmp", fp_rawcontent, x, y);
  242. delay(2000);
  243. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "second200x200.bmp", fp_rawcontent, x, y);
  244. delay(2000);
  245. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "third200x200.bmp", fp_rawcontent, x, y);
  246. delay(2000);
  247. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "fourth200x200.bmp", fp_rawcontent, x, y);
  248. delay(2000);
  249. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "fifth200x200.bmp", fp_rawcontent, x, y);
  250. delay(2000);
  251. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "sixth200x200.bmp", fp_rawcontent, x, y);
  252. delay(2000);
  253. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "seventh200x200.bmp", fp_rawcontent, x, y);
  254. delay(2000);
  255. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "eighth200x200.bmp", fp_rawcontent, x, y);
  256. delay(2000);
  257. }
  258. void drawBitmapsBuffered_other()
  259. {
  260. int16_t w2 = display.width() / 2;
  261. int16_t h2 = display.height() / 2;
  262. showBitmapFrom_HTTP_Buffered("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", w2 - 50, h2 - 50, false);
  263. delay(2000);
  264. //showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_1.bmp", fp_rawcontent, w2 - 100, h2 - 160);
  265. delay(2000);
  266. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  267. delay(2000);
  268. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "marilyn_240x240x8.bmp", fp_rawcontent, w2 - 120, h2 - 120);
  269. delay(2000);
  270. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "miniwoof.bmp", fp_rawcontent, w2 - 60, h2 - 80);
  271. delay(2000);
  272. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "test.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  273. delay(2000);
  274. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger.bmp", fp_rawcontent, w2 - 160, h2 - 120);
  275. delay(2000);
  276. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_178x160x4.bmp", fp_rawcontent, w2 - 89, h2 - 80);
  277. delay(2000);
  278. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_240x317x4.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  279. delay(2000);
  280. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_320x200x24.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  281. delay(2000);
  282. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger16T.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  283. delay(2000);
  284. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "woof.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  285. delay(2000);
  286. }
  287. void drawBitmapsBuffered_test()
  288. {
  289. int16_t w2 = display.width() / 2;
  290. int16_t h2 = display.height() / 2;
  291. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  292. delay(2000);
  293. }
  294. static const uint16_t input_buffer_pixels = 640; // may affect performance
  295. static const uint16_t max_row_width = 640; // for up to 7.5" display
  296. static const uint16_t max_palette_pixels = 256; // for depth <= 8
  297. uint8_t input_buffer[3 * input_buffer_pixels]; // up to depth 24
  298. uint8_t output_row_mono_buffer[max_row_width / 8]; // buffer for at least one row of b/w bits
  299. uint8_t output_row_color_buffer[max_row_width / 8]; // buffer for at least one row of color bits
  300. uint8_t mono_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 b/w
  301. uint8_t color_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 c/w
  302. void showBitmapFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  303. {
  304. WiFiClient client;
  305. bool connection_ok = false;
  306. bool valid = false; // valid format to be handled
  307. bool flip = true; // bitmap is stored bottom-to-top
  308. uint32_t startTime = millis();
  309. if ((x >= display.width()) || (y >= display.height())) return;
  310. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  311. Serial.print("connecting to "); Serial.println(host);
  312. if (!client.connect(host, httpPort))
  313. {
  314. Serial.println("connection failed");
  315. return;
  316. }
  317. Serial.print("requesting URL: ");
  318. Serial.println(String("http://") + host + path + filename);
  319. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  320. "Host: " + host + "\r\n" +
  321. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  322. "Connection: close\r\n\r\n");
  323. Serial.println("request sent");
  324. while (client.connected())
  325. {
  326. String line = client.readStringUntil('\n');
  327. if (!connection_ok)
  328. {
  329. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  330. if (connection_ok) Serial.println(line);
  331. //if (!connection_ok) Serial.println(line);
  332. }
  333. if (!connection_ok) Serial.println(line);
  334. //Serial.println(line);
  335. if (line == "\r")
  336. {
  337. Serial.println("headers received");
  338. break;
  339. }
  340. }
  341. if (!connection_ok) return;
  342. // Parse BMP header
  343. if (read16(client) == 0x4D42) // BMP signature
  344. {
  345. uint32_t fileSize = read32(client);
  346. uint32_t creatorBytes = read32(client);
  347. uint32_t imageOffset = read32(client); // Start of image data
  348. uint32_t headerSize = read32(client);
  349. uint32_t width = read32(client);
  350. uint32_t height = read32(client);
  351. uint16_t planes = read16(client);
  352. uint16_t depth = read16(client); // bits per pixel
  353. uint32_t format = read32(client);
  354. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  355. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  356. {
  357. Serial.print("File size: "); Serial.println(fileSize);
  358. Serial.print("Image Offset: "); Serial.println(imageOffset);
  359. Serial.print("Header size: "); Serial.println(headerSize);
  360. Serial.print("Bit Depth: "); Serial.println(depth);
  361. Serial.print("Image size: ");
  362. Serial.print(width);
  363. Serial.print('x');
  364. Serial.println(height);
  365. // BMP rows are padded (if needed) to 4-byte boundary
  366. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  367. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  368. if (height < 0)
  369. {
  370. height = -height;
  371. flip = false;
  372. }
  373. uint16_t w = width;
  374. uint16_t h = height;
  375. if ((x + w - 1) >= display.width()) w = display.width() - x;
  376. if ((y + h - 1) >= display.height()) h = display.height() - y;
  377. if (w <= max_row_width) // handle with direct drawing
  378. {
  379. valid = true;
  380. uint8_t bitmask = 0xFF;
  381. uint8_t bitshift = 8 - depth;
  382. uint16_t red, green, blue;
  383. bool whitish, colored;
  384. if (depth == 1) with_color = false;
  385. if (depth <= 8)
  386. {
  387. if (depth < 8) bitmask >>= depth;
  388. bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  389. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  390. {
  391. blue = client.read();
  392. green = client.read();
  393. red = client.read();
  394. client.read();
  395. bytes_read += 4;
  396. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  397. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  398. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  399. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  400. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  401. color_palette_buffer[pn / 8] |= colored << pn % 8;
  402. }
  403. }
  404. display.clearScreen();
  405. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  406. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  407. bytes_read += skip(client, rowPosition - bytes_read);
  408. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  409. {
  410. if (!connection_ok || !(client.connected() || client.available())) break;
  411. delay(1); // yield() to avoid WDT
  412. uint32_t in_remain = rowSize;
  413. uint32_t in_idx = 0;
  414. uint32_t in_bytes = 0;
  415. uint8_t in_byte = 0; // for depth <= 8
  416. uint8_t in_bits = 0; // for depth <= 8
  417. uint8_t out_byte = 0xFF; // white (for w%8!=0 boarder)
  418. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 boarder)
  419. uint32_t out_idx = 0;
  420. for (uint16_t col = 0; col < w; col++) // for each pixel
  421. {
  422. yield();
  423. if (!connection_ok || !(client.connected() || client.available())) break;
  424. // Time to read more pixel data?
  425. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  426. {
  427. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  428. uint32_t got = read(client, input_buffer, get);
  429. while ((got < get) && connection_ok)
  430. {
  431. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  432. uint32_t gotmore = read(client, input_buffer + got, get - got);
  433. got += gotmore;
  434. connection_ok = gotmore > 0;
  435. }
  436. in_bytes = got;
  437. in_remain -= got;
  438. bytes_read += got;
  439. }
  440. if (!connection_ok)
  441. {
  442. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  443. break;
  444. }
  445. switch (depth)
  446. {
  447. case 24:
  448. blue = input_buffer[in_idx++];
  449. green = input_buffer[in_idx++];
  450. red = input_buffer[in_idx++];
  451. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  452. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  453. break;
  454. case 16:
  455. {
  456. uint8_t lsb = input_buffer[in_idx++];
  457. uint8_t msb = input_buffer[in_idx++];
  458. if (format == 0) // 555
  459. {
  460. blue = (lsb & 0x1F) << 3;
  461. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  462. red = (msb & 0x7C) << 1;
  463. }
  464. else // 565
  465. {
  466. blue = (lsb & 0x1F) << 3;
  467. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  468. red = (msb & 0xF8);
  469. }
  470. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  471. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  472. }
  473. break;
  474. case 1:
  475. case 4:
  476. case 8:
  477. {
  478. if (0 == in_bits)
  479. {
  480. in_byte = input_buffer[in_idx++];
  481. in_bits = 8;
  482. }
  483. uint16_t pn = (in_byte >> bitshift) & bitmask;
  484. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  485. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  486. in_byte <<= depth;
  487. in_bits -= depth;
  488. }
  489. break;
  490. }
  491. if (whitish)
  492. {
  493. // keep white
  494. }
  495. else if (colored && with_color)
  496. {
  497. out_color_byte &= ~(0x80 >> col % 8); // colored
  498. }
  499. else
  500. {
  501. out_byte &= ~(0x80 >> col % 8); // black
  502. }
  503. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 boarder)
  504. {
  505. output_row_color_buffer[out_idx] = out_color_byte;
  506. output_row_mono_buffer[out_idx++] = out_byte;
  507. out_byte = 0xFF; // white (for w%8!=0 boarder)
  508. out_color_byte = 0xFF; // white (for w%8!=0 boarder)
  509. }
  510. } // end pixel
  511. int16_t yrow = y + (flip ? h - row - 1 : row);
  512. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  513. } // end line
  514. Serial.print("downloaded in ");
  515. Serial.print(millis() - startTime);
  516. Serial.println(" ms");
  517. display.refresh();
  518. }
  519. Serial.print("bytes read "); Serial.println(bytes_read);
  520. }
  521. }
  522. if (!valid)
  523. {
  524. Serial.println("bitmap format not handled.");
  525. }
  526. }
  527. void drawBitmapFrom_HTTP_ToBuffer(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  528. {
  529. WiFiClient client;
  530. bool connection_ok = false;
  531. bool valid = false; // valid format to be handled
  532. bool flip = true; // bitmap is stored bottom-to-top
  533. uint32_t startTime = millis();
  534. if ((x >= display.width()) || (y >= display.height())) return;
  535. display.fillScreen(GxEPD_WHITE);
  536. Serial.print("connecting to "); Serial.println(host);
  537. if (!client.connect(host, httpPort))
  538. {
  539. Serial.println("connection failed");
  540. return;
  541. }
  542. Serial.print("requesting URL: ");
  543. Serial.println(String("http://") + host + path + filename);
  544. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  545. "Host: " + host + "\r\n" +
  546. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  547. "Connection: close\r\n\r\n");
  548. Serial.println("request sent");
  549. while (client.connected())
  550. {
  551. String line = client.readStringUntil('\n');
  552. if (!connection_ok)
  553. {
  554. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  555. if (connection_ok) Serial.println(line);
  556. //if (!connection_ok) Serial.println(line);
  557. }
  558. if (!connection_ok) Serial.println(line);
  559. //Serial.println(line);
  560. if (line == "\r")
  561. {
  562. Serial.println("headers received");
  563. break;
  564. }
  565. }
  566. if (!connection_ok) return;
  567. // Parse BMP header
  568. if (read16(client) == 0x4D42) // BMP signature
  569. {
  570. uint32_t fileSize = read32(client);
  571. uint32_t creatorBytes = read32(client);
  572. uint32_t imageOffset = read32(client); // Start of image data
  573. uint32_t headerSize = read32(client);
  574. uint32_t width = read32(client);
  575. uint32_t height = read32(client);
  576. uint16_t planes = read16(client);
  577. uint16_t depth = read16(client); // bits per pixel
  578. uint32_t format = read32(client);
  579. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  580. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  581. {
  582. Serial.print("File size: "); Serial.println(fileSize);
  583. Serial.print("Image Offset: "); Serial.println(imageOffset);
  584. Serial.print("Header size: "); Serial.println(headerSize);
  585. Serial.print("Bit Depth: "); Serial.println(depth);
  586. Serial.print("Image size: ");
  587. Serial.print(width);
  588. Serial.print('x');
  589. Serial.println(height);
  590. // BMP rows are padded (if needed) to 4-byte boundary
  591. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  592. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  593. if (height < 0)
  594. {
  595. height = -height;
  596. flip = false;
  597. }
  598. uint16_t w = width;
  599. uint16_t h = height;
  600. if ((x + w - 1) >= display.width()) w = display.width() - x;
  601. if ((y + h - 1) >= display.height()) h = display.height() - y;
  602. //if (w <= max_row_width) // handle with direct drawing
  603. {
  604. valid = true;
  605. uint8_t bitmask = 0xFF;
  606. uint8_t bitshift = 8 - depth;
  607. uint16_t red, green, blue;
  608. bool whitish, colored;
  609. if (depth == 1) with_color = false;
  610. if (depth <= 8)
  611. {
  612. if (depth < 8) bitmask >>= depth;
  613. bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  614. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  615. {
  616. blue = client.read();
  617. green = client.read();
  618. red = client.read();
  619. client.read();
  620. bytes_read += 4;
  621. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  622. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  623. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  624. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  625. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  626. color_palette_buffer[pn / 8] |= colored << pn % 8;
  627. //Serial.print("0x00"); Serial.print(red, HEX); Serial.print(green, HEX); Serial.print(blue, HEX);
  628. //Serial.print(" : "); Serial.print(whitish); Serial.print(", "); Serial.println(colored);
  629. }
  630. }
  631. display.writeScreenBuffer();
  632. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  633. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  634. bytes_read += skip(client, rowPosition - bytes_read);
  635. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  636. {
  637. if (!connection_ok || !(client.connected() || client.available())) break;
  638. delay(1); // yield() to avoid WDT
  639. uint32_t in_remain = rowSize;
  640. uint32_t in_idx = 0;
  641. uint32_t in_bytes = 0;
  642. uint8_t in_byte = 0; // for depth <= 8
  643. uint8_t in_bits = 0; // for depth <= 8
  644. uint16_t color = GxEPD_WHITE;
  645. for (uint16_t col = 0; col < w; col++) // for each pixel
  646. {
  647. yield();
  648. if (!connection_ok || !(client.connected() || client.available())) break;
  649. // Time to read more pixel data?
  650. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  651. {
  652. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  653. uint32_t got = read(client, input_buffer, get);
  654. while ((got < get) && connection_ok)
  655. {
  656. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  657. uint32_t gotmore = read(client, input_buffer + got, get - got);
  658. got += gotmore;
  659. connection_ok = gotmore > 0;
  660. }
  661. in_bytes = got;
  662. in_remain -= got;
  663. bytes_read += got;
  664. }
  665. if (!connection_ok)
  666. {
  667. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  668. break;
  669. }
  670. switch (depth)
  671. {
  672. case 24:
  673. blue = input_buffer[in_idx++];
  674. green = input_buffer[in_idx++];
  675. red = input_buffer[in_idx++];
  676. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  677. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  678. break;
  679. case 16:
  680. {
  681. uint8_t lsb = input_buffer[in_idx++];
  682. uint8_t msb = input_buffer[in_idx++];
  683. if (format == 0) // 555
  684. {
  685. blue = (lsb & 0x1F) << 3;
  686. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  687. red = (msb & 0x7C) << 1;
  688. }
  689. else // 565
  690. {
  691. blue = (lsb & 0x1F) << 3;
  692. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  693. red = (msb & 0xF8);
  694. }
  695. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  696. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  697. }
  698. break;
  699. case 1:
  700. case 4:
  701. case 8:
  702. {
  703. if (0 == in_bits)
  704. {
  705. in_byte = input_buffer[in_idx++];
  706. in_bits = 8;
  707. }
  708. uint16_t pn = (in_byte >> bitshift) & bitmask;
  709. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  710. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  711. in_byte <<= depth;
  712. in_bits -= depth;
  713. }
  714. break;
  715. }
  716. if (whitish)
  717. {
  718. color = GxEPD_WHITE;
  719. }
  720. else if (colored && with_color)
  721. {
  722. color = GxEPD_COLORED;
  723. }
  724. else
  725. {
  726. color = GxEPD_BLACK;
  727. }
  728. uint16_t yrow = y + (flip ? h - row - 1 : row);
  729. display.drawPixel(x + col, yrow, color);
  730. } // end pixel
  731. } // end line
  732. }
  733. Serial.print("bytes read "); Serial.println(bytes_read);
  734. }
  735. }
  736. Serial.print("loaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  737. if (!valid)
  738. {
  739. Serial.println("bitmap format not handled.");
  740. }
  741. }
  742. void showBitmapFrom_HTTP_Buffered(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  743. {
  744. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  745. display.setFullWindow();
  746. display.firstPage();
  747. do
  748. {
  749. drawBitmapFrom_HTTP_ToBuffer(host, path, filename, x, y, with_color);
  750. }
  751. while (display.nextPage());
  752. }
  753. void showBitmapFrom_HTTPS(const char* host, const char* path, const char* filename, const char* fingerprint, int16_t x, int16_t y, bool with_color)
  754. {
  755. // Use WiFiClientSecure class to create TLS connection
  756. #if USE_BearSSL
  757. BearSSL::WiFiClientSecure client;
  758. #else
  759. WiFiClientSecure client;
  760. #endif
  761. bool connection_ok = false;
  762. bool valid = false; // valid format to be handled
  763. bool flip = true; // bitmap is stored bottom-to-top
  764. uint32_t startTime = millis();
  765. if ((x >= display.width()) || (y >= display.height())) return;
  766. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  767. Serial.print("connecting to "); Serial.println(host);
  768. #if USE_BearSSL
  769. if (fingerprint) client.setFingerprint((uint8_t*)fingerprint);
  770. #endif
  771. if (!client.connect(host, httpsPort))
  772. {
  773. Serial.println("connection failed");
  774. return;
  775. }
  776. #if defined (ESP8266) && !USE_BearSSL
  777. if (fingerprint)
  778. {
  779. if (client.verify(fingerprint, host))
  780. {
  781. Serial.println("certificate matches");
  782. }
  783. else
  784. {
  785. Serial.println("certificate doesn't match");
  786. return;
  787. }
  788. }
  789. #endif
  790. Serial.print("requesting URL: ");
  791. Serial.println(String("https://") + host + path + filename);
  792. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  793. "Host: " + host + "\r\n" +
  794. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  795. "Connection: close\r\n\r\n");
  796. Serial.println("request sent");
  797. while (client.connected())
  798. {
  799. String line = client.readStringUntil('\n');
  800. if (!connection_ok)
  801. {
  802. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  803. if (connection_ok) Serial.println(line);
  804. //if (!connection_ok) Serial.println(line);
  805. }
  806. if (!connection_ok) Serial.println(line);
  807. //Serial.println(line);
  808. if (line == "\r")
  809. {
  810. Serial.println("headers received");
  811. break;
  812. }
  813. }
  814. if (!connection_ok) return;
  815. // Parse BMP header
  816. if (read16(client) == 0x4D42) // BMP signature
  817. {
  818. uint32_t fileSize = read32(client);
  819. uint32_t creatorBytes = read32(client);
  820. uint32_t imageOffset = read32(client); // Start of image data
  821. uint32_t headerSize = read32(client);
  822. uint32_t width = read32(client);
  823. uint32_t height = read32(client);
  824. uint16_t planes = read16(client);
  825. uint16_t depth = read16(client); // bits per pixel
  826. uint32_t format = read32(client);
  827. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  828. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  829. {
  830. Serial.print("File size: "); Serial.println(fileSize);
  831. Serial.print("Image Offset: "); Serial.println(imageOffset);
  832. Serial.print("Header size: "); Serial.println(headerSize);
  833. Serial.print("Bit Depth: "); Serial.println(depth);
  834. Serial.print("Image size: ");
  835. Serial.print(width);
  836. Serial.print('x');
  837. Serial.println(height);
  838. // BMP rows are padded (if needed) to 4-byte boundary
  839. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  840. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  841. if (height < 0)
  842. {
  843. height = -height;
  844. flip = false;
  845. }
  846. uint16_t w = width;
  847. uint16_t h = height;
  848. if ((x + w - 1) >= display.width()) w = display.width() - x;
  849. if ((y + h - 1) >= display.height()) h = display.height() - y;
  850. if (w <= max_row_width) // handle with direct drawing
  851. {
  852. valid = true;
  853. uint8_t bitmask = 0xFF;
  854. uint8_t bitshift = 8 - depth;
  855. uint16_t red, green, blue;
  856. bool whitish, colored;
  857. if (depth == 1) with_color = false;
  858. if (depth <= 8)
  859. {
  860. if (depth < 8) bitmask >>= depth;
  861. bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  862. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  863. {
  864. blue = client.read();
  865. green = client.read();
  866. red = client.read();
  867. client.read();
  868. bytes_read += 4;
  869. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  870. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  871. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  872. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  873. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  874. color_palette_buffer[pn / 8] |= colored << pn % 8;
  875. }
  876. }
  877. display.clearScreen();
  878. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  879. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  880. bytes_read += skip(client, rowPosition - bytes_read);
  881. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  882. {
  883. if (!connection_ok || !(client.connected() || client.available())) break;
  884. delay(1); // yield() to avoid WDT
  885. uint32_t in_remain = rowSize;
  886. uint32_t in_idx = 0;
  887. uint32_t in_bytes = 0;
  888. uint8_t in_byte = 0; // for depth <= 8
  889. uint8_t in_bits = 0; // for depth <= 8
  890. uint8_t out_byte = 0xFF; // white (for w%8!=0 boarder)
  891. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 boarder)
  892. uint32_t out_idx = 0;
  893. for (uint16_t col = 0; col < w; col++) // for each pixel
  894. {
  895. yield();
  896. if (!connection_ok || !(client.connected() || client.available())) break;
  897. // Time to read more pixel data?
  898. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  899. {
  900. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  901. uint32_t got = read(client, input_buffer, get);
  902. while ((got < get) && connection_ok)
  903. {
  904. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  905. //if ((get - got) > client.available()) delay(200); // does improve? yes, if >= 200
  906. uint32_t gotmore = read(client, input_buffer + got, get - got);
  907. got += gotmore;
  908. connection_ok = gotmore > 0;
  909. }
  910. in_bytes = got;
  911. in_remain -= got;
  912. bytes_read += got;
  913. }
  914. if (!connection_ok)
  915. {
  916. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  917. break;
  918. }
  919. switch (depth)
  920. {
  921. case 24:
  922. blue = input_buffer[in_idx++];
  923. green = input_buffer[in_idx++];
  924. red = input_buffer[in_idx++];
  925. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  926. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  927. break;
  928. case 16:
  929. {
  930. uint8_t lsb = input_buffer[in_idx++];
  931. uint8_t msb = input_buffer[in_idx++];
  932. if (format == 0) // 555
  933. {
  934. blue = (lsb & 0x1F) << 3;
  935. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  936. red = (msb & 0x7C) << 1;
  937. }
  938. else // 565
  939. {
  940. blue = (lsb & 0x1F) << 3;
  941. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  942. red = (msb & 0xF8);
  943. }
  944. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  945. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  946. }
  947. break;
  948. case 1:
  949. case 4:
  950. case 8:
  951. {
  952. if (0 == in_bits)
  953. {
  954. in_byte = input_buffer[in_idx++];
  955. in_bits = 8;
  956. }
  957. uint16_t pn = (in_byte >> bitshift) & bitmask;
  958. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  959. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  960. in_byte <<= depth;
  961. in_bits -= depth;
  962. }
  963. break;
  964. }
  965. if (whitish)
  966. {
  967. // keep white
  968. }
  969. else if (colored && with_color)
  970. {
  971. out_color_byte &= ~(0x80 >> col % 8); // colored
  972. }
  973. else
  974. {
  975. out_byte &= ~(0x80 >> col % 8); // black
  976. }
  977. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 boarder)
  978. {
  979. output_row_color_buffer[out_idx] = out_color_byte;
  980. output_row_mono_buffer[out_idx++] = out_byte;
  981. out_byte = 0xFF; // white (for w%8!=0 boarder)
  982. out_color_byte = 0xFF; // white (for w%8!=0 boarder)
  983. }
  984. } // end pixel
  985. int16_t yrow = y + (flip ? h - row - 1 : row);
  986. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  987. } // end line
  988. Serial.print("downloaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  989. Serial.print("bytes read "); Serial.println(bytes_read);
  990. display.refresh();
  991. }
  992. }
  993. }
  994. if (!valid)
  995. {
  996. Serial.println("bitmap format not handled.");
  997. }
  998. }
  999. 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)
  1000. {
  1001. // Use WiFiClientSecure class to create TLS connection
  1002. #if USE_BearSSL
  1003. BearSSL::WiFiClientSecure client;
  1004. #else
  1005. WiFiClientSecure client;
  1006. #endif
  1007. bool connection_ok = false;
  1008. bool valid = false; // valid format to be handled
  1009. bool flip = true; // bitmap is stored bottom-to-top
  1010. uint32_t startTime = millis();
  1011. if ((x >= display.width()) || (y >= display.height())) return;
  1012. display.fillScreen(GxEPD_WHITE);
  1013. Serial.print("connecting to "); Serial.println(host);
  1014. #if USE_BearSSL
  1015. if (fingerprint) client.setFingerprint((uint8_t*)fingerprint);
  1016. #endif
  1017. if (!client.connect(host, httpsPort))
  1018. {
  1019. Serial.println("connection failed");
  1020. return;
  1021. }
  1022. #if defined (ESP8266) && !USE_BearSSL
  1023. if (fingerprint)
  1024. {
  1025. if (client.verify(fingerprint, host))
  1026. {
  1027. Serial.println("certificate matches");
  1028. }
  1029. else
  1030. {
  1031. Serial.println("certificate doesn't match");
  1032. return;
  1033. }
  1034. }
  1035. #endif
  1036. Serial.print("requesting URL: ");
  1037. Serial.println(String("https://") + host + path + filename);
  1038. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  1039. "Host: " + host + "\r\n" +
  1040. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  1041. "Connection: close\r\n\r\n");
  1042. Serial.println("request sent");
  1043. while (client.connected())
  1044. {
  1045. String line = client.readStringUntil('\n');
  1046. if (!connection_ok)
  1047. {
  1048. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  1049. if (connection_ok) Serial.println(line);
  1050. //if (!connection_ok) Serial.println(line);
  1051. }
  1052. if (!connection_ok) Serial.println(line);
  1053. //Serial.println(line);
  1054. if (line == "\r")
  1055. {
  1056. Serial.println("headers received");
  1057. break;
  1058. }
  1059. }
  1060. if (!connection_ok) return;
  1061. // Parse BMP header
  1062. if (read16(client) == 0x4D42) // BMP signature
  1063. {
  1064. uint32_t fileSize = read32(client);
  1065. uint32_t creatorBytes = read32(client);
  1066. uint32_t imageOffset = read32(client); // Start of image data
  1067. uint32_t headerSize = read32(client);
  1068. uint32_t width = read32(client);
  1069. uint32_t height = read32(client);
  1070. uint16_t planes = read16(client);
  1071. uint16_t depth = read16(client); // bits per pixel
  1072. uint32_t format = read32(client);
  1073. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  1074. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  1075. {
  1076. Serial.print("File size: "); Serial.println(fileSize);
  1077. Serial.print("Image Offset: "); Serial.println(imageOffset);
  1078. Serial.print("Header size: "); Serial.println(headerSize);
  1079. Serial.print("Bit Depth: "); Serial.println(depth);
  1080. Serial.print("Image size: ");
  1081. Serial.print(width);
  1082. Serial.print('x');
  1083. Serial.println(height);
  1084. // BMP rows are padded (if needed) to 4-byte boundary
  1085. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  1086. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  1087. if (height < 0)
  1088. {
  1089. height = -height;
  1090. flip = false;
  1091. }
  1092. uint16_t w = width;
  1093. uint16_t h = height;
  1094. if ((x + w - 1) >= display.width()) w = display.width() - x;
  1095. if ((y + h - 1) >= display.height()) h = display.height() - y;
  1096. //if (w <= max_row_width) // handle with direct drawing
  1097. {
  1098. valid = true;
  1099. uint8_t bitmask = 0xFF;
  1100. uint8_t bitshift = 8 - depth;
  1101. uint16_t red, green, blue;
  1102. bool whitish, colored;
  1103. if (depth == 1) with_color = false;
  1104. if (depth <= 8)
  1105. {
  1106. if (depth < 8) bitmask >>= depth;
  1107. bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  1108. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  1109. {
  1110. blue = client.read();
  1111. green = client.read();
  1112. red = client.read();
  1113. client.read();
  1114. bytes_read += 4;
  1115. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1116. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1117. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  1118. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  1119. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  1120. color_palette_buffer[pn / 8] |= colored << pn % 8;
  1121. //Serial.print("0x00"); Serial.print(red, HEX); Serial.print(green, HEX); Serial.print(blue, HEX);
  1122. //Serial.print(" : "); Serial.print(whitish); Serial.print(", "); Serial.println(colored);
  1123. }
  1124. }
  1125. display.writeScreenBuffer();
  1126. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  1127. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  1128. bytes_read += skip(client, rowPosition - bytes_read);
  1129. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  1130. {
  1131. if (!connection_ok || !(client.connected() || client.available())) break;
  1132. delay(1); // yield() to avoid WDT
  1133. uint32_t in_remain = rowSize;
  1134. uint32_t in_idx = 0;
  1135. uint32_t in_bytes = 0;
  1136. uint8_t in_byte = 0; // for depth <= 8
  1137. uint8_t in_bits = 0; // for depth <= 8
  1138. uint16_t color = GxEPD_WHITE;
  1139. for (uint16_t col = 0; col < w; col++) // for each pixel
  1140. {
  1141. yield();
  1142. if (!connection_ok || !(client.connected() || client.available())) break;
  1143. // Time to read more pixel data?
  1144. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  1145. {
  1146. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  1147. uint32_t got = read(client, input_buffer, get);
  1148. while ((got < get) && connection_ok)
  1149. {
  1150. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  1151. uint32_t gotmore = read(client, input_buffer + got, get - got);
  1152. got += gotmore;
  1153. connection_ok = gotmore > 0;
  1154. }
  1155. in_bytes = got;
  1156. in_remain -= got;
  1157. bytes_read += got;
  1158. }
  1159. if (!connection_ok)
  1160. {
  1161. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  1162. break;
  1163. }
  1164. switch (depth)
  1165. {
  1166. case 24:
  1167. blue = input_buffer[in_idx++];
  1168. green = input_buffer[in_idx++];
  1169. red = input_buffer[in_idx++];
  1170. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1171. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1172. break;
  1173. case 16:
  1174. {
  1175. uint8_t lsb = input_buffer[in_idx++];
  1176. uint8_t msb = input_buffer[in_idx++];
  1177. if (format == 0) // 555
  1178. {
  1179. blue = (lsb & 0x1F) << 3;
  1180. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  1181. red = (msb & 0x7C) << 1;
  1182. }
  1183. else // 565
  1184. {
  1185. blue = (lsb & 0x1F) << 3;
  1186. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  1187. red = (msb & 0xF8);
  1188. }
  1189. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1190. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1191. }
  1192. break;
  1193. case 1:
  1194. case 4:
  1195. case 8:
  1196. {
  1197. if (0 == in_bits)
  1198. {
  1199. in_byte = input_buffer[in_idx++];
  1200. in_bits = 8;
  1201. }
  1202. uint16_t pn = (in_byte >> bitshift) & bitmask;
  1203. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  1204. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  1205. in_byte <<= depth;
  1206. in_bits -= depth;
  1207. }
  1208. break;
  1209. }
  1210. if (whitish)
  1211. {
  1212. color = GxEPD_WHITE;
  1213. }
  1214. else if (colored && with_color)
  1215. {
  1216. color = GxEPD_COLORED;
  1217. }
  1218. else
  1219. {
  1220. color = GxEPD_BLACK;
  1221. }
  1222. uint16_t yrow = y + (flip ? h - row - 1 : row);
  1223. display.drawPixel(x + col, yrow, color);
  1224. } // end pixel
  1225. } // end line
  1226. }
  1227. Serial.print("bytes read "); Serial.println(bytes_read);
  1228. }
  1229. }
  1230. Serial.print("loaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  1231. if (!valid)
  1232. {
  1233. Serial.println("bitmap format not handled.");
  1234. }
  1235. }
  1236. 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)
  1237. {
  1238. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  1239. display.setFullWindow();
  1240. display.firstPage();
  1241. do
  1242. {
  1243. drawBitmapFrom_HTTPS_ToBuffer(host, path, filename, fingerprint, x, y, with_color);
  1244. }
  1245. while (display.nextPage());
  1246. }
  1247. uint16_t read16(WiFiClient& client)
  1248. {
  1249. // BMP data is stored little-endian, same as Arduino.
  1250. uint16_t result;
  1251. ((uint8_t *)&result)[0] = client.read(); // LSB
  1252. ((uint8_t *)&result)[1] = client.read(); // MSB
  1253. return result;
  1254. }
  1255. uint32_t read32(WiFiClient& client)
  1256. {
  1257. // BMP data is stored little-endian, same as Arduino.
  1258. uint32_t result;
  1259. ((uint8_t *)&result)[0] = client.read(); // LSB
  1260. ((uint8_t *)&result)[1] = client.read();
  1261. ((uint8_t *)&result)[2] = client.read();
  1262. ((uint8_t *)&result)[3] = client.read(); // MSB
  1263. return result;
  1264. }
  1265. #if USE_BearSSL
  1266. uint32_t skip(BearSSL::WiFiClientSecure& client, int32_t bytes)
  1267. {
  1268. int32_t remain = bytes;
  1269. uint32_t start = millis();
  1270. while ((client.connected() || client.available()) && (remain > 0))
  1271. {
  1272. if (client.available())
  1273. {
  1274. int16_t v = client.read();
  1275. remain--;
  1276. }
  1277. else delay(1);
  1278. if (millis() - start > 2000) break; // don't hang forever
  1279. }
  1280. return bytes - remain;
  1281. }
  1282. uint32_t read(BearSSL::WiFiClientSecure& client, uint8_t* buffer, int32_t bytes)
  1283. {
  1284. int32_t remain = bytes;
  1285. uint32_t start = millis();
  1286. while ((client.connected() || client.available()) && (remain > 0))
  1287. {
  1288. if (client.available())
  1289. {
  1290. int16_t v = client.read();
  1291. *buffer++ = uint8_t(v);
  1292. remain--;
  1293. }
  1294. else delay(1);
  1295. if (millis() - start > 2000) break; // don't hang forever
  1296. }
  1297. return bytes - remain;
  1298. }
  1299. #endif
  1300. uint32_t skip(WiFiClient& client, int32_t bytes)
  1301. {
  1302. int32_t remain = bytes;
  1303. uint32_t start = millis();
  1304. while ((client.connected() || client.available()) && (remain > 0))
  1305. {
  1306. if (client.available())
  1307. {
  1308. int16_t v = client.read();
  1309. remain--;
  1310. }
  1311. else delay(1);
  1312. if (millis() - start > 2000) break; // don't hang forever
  1313. }
  1314. return bytes - remain;
  1315. }
  1316. uint32_t read(WiFiClient& client, uint8_t* buffer, int32_t bytes)
  1317. {
  1318. int32_t remain = bytes;
  1319. uint32_t start = millis();
  1320. while ((client.connected() || client.available()) && (remain > 0))
  1321. {
  1322. if (client.available())
  1323. {
  1324. int16_t v = client.read();
  1325. *buffer++ = uint8_t(v);
  1326. remain--;
  1327. }
  1328. else delay(1);
  1329. if (millis() - start > 2000) break; // don't hang forever
  1330. }
  1331. return bytes - remain;
  1332. }