GxEPD2_WiFi_Example.ino 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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 Display 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. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "output5.bmp", fp_rawcontent, 0, 0);
  235. delay(2000);
  236. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "output6.bmp", fp_rawcontent, 0, 0);
  237. delay(2000);
  238. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_1.bmp", fp_rawcontent, 0, 0);
  239. delay(2000);
  240. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_4.bmp", fp_rawcontent, 0, 0);
  241. delay(2000);
  242. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_8.bmp", fp_rawcontent, 0, 0);
  243. delay(2000);
  244. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_11.bmp", fp_rawcontent, 0, 0);
  245. delay(2000);
  246. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_44.bmp", fp_rawcontent, 0, 0);
  247. delay(2000);
  248. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_88.bmp", fp_rawcontent, 0, 0);
  249. delay(2000);
  250. }
  251. void drawBitmapsBuffered_200x200()
  252. {
  253. int16_t x = (display.width() - 200) / 2;
  254. int16_t y = (display.height() - 200) / 2;
  255. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "logo200x200.bmp", fp_rawcontent, x, y);
  256. delay(2000);
  257. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "first200x200.bmp", fp_rawcontent, x, y);
  258. delay(2000);
  259. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "second200x200.bmp", fp_rawcontent, x, y);
  260. delay(2000);
  261. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "third200x200.bmp", fp_rawcontent, x, y);
  262. delay(2000);
  263. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "fourth200x200.bmp", fp_rawcontent, x, y);
  264. delay(2000);
  265. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "fifth200x200.bmp", fp_rawcontent, x, y);
  266. delay(2000);
  267. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "sixth200x200.bmp", fp_rawcontent, x, y);
  268. delay(2000);
  269. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "seventh200x200.bmp", fp_rawcontent, x, y);
  270. delay(2000);
  271. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "eighth200x200.bmp", fp_rawcontent, x, y);
  272. delay(2000);
  273. }
  274. void drawBitmapsBuffered_other()
  275. {
  276. int16_t w2 = display.width() / 2;
  277. int16_t h2 = display.height() / 2;
  278. showBitmapFrom_HTTP_Buffered("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", w2 - 50, h2 - 50, false);
  279. delay(2000);
  280. //showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_1.bmp", fp_rawcontent, w2 - 100, h2 - 160);
  281. delay(2000);
  282. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  283. delay(2000);
  284. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "marilyn_240x240x8.bmp", fp_rawcontent, w2 - 120, h2 - 120);
  285. delay(2000);
  286. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "miniwoof.bmp", fp_rawcontent, w2 - 60, h2 - 80);
  287. delay(2000);
  288. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "test.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  289. delay(2000);
  290. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger.bmp", fp_rawcontent, w2 - 160, h2 - 120);
  291. delay(2000);
  292. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_178x160x4.bmp", fp_rawcontent, w2 - 89, h2 - 80);
  293. delay(2000);
  294. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_240x317x4.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  295. delay(2000);
  296. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_320x200x24.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  297. delay(2000);
  298. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger16T.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  299. delay(2000);
  300. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "woof.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  301. delay(2000);
  302. }
  303. void drawBitmapsBuffered_test()
  304. {
  305. int16_t w2 = display.width() / 2;
  306. int16_t h2 = display.height() / 2;
  307. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  308. delay(2000);
  309. }
  310. static const uint16_t input_buffer_pixels = 800; // may affect performance
  311. static const uint16_t max_row_width = 800; // for up to 7.5" display 800x480
  312. static const uint16_t max_palette_pixels = 256; // for depth <= 8
  313. uint8_t input_buffer[3 * input_buffer_pixels]; // up to depth 24
  314. uint8_t output_row_mono_buffer[max_row_width / 8]; // buffer for at least one row of b/w bits
  315. uint8_t output_row_color_buffer[max_row_width / 8]; // buffer for at least one row of color bits
  316. uint8_t mono_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 b/w
  317. uint8_t color_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 c/w
  318. void showBitmapFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  319. {
  320. WiFiClient client;
  321. bool connection_ok = false;
  322. bool valid = false; // valid format to be handled
  323. bool flip = true; // bitmap is stored bottom-to-top
  324. uint32_t startTime = millis();
  325. if ((x >= display.width()) || (y >= display.height())) return;
  326. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  327. Serial.print("connecting to "); Serial.println(host);
  328. if (!client.connect(host, httpPort))
  329. {
  330. Serial.println("connection failed");
  331. return;
  332. }
  333. Serial.print("requesting URL: ");
  334. Serial.println(String("http://") + host + path + filename);
  335. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  336. "Host: " + host + "\r\n" +
  337. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  338. "Connection: close\r\n\r\n");
  339. Serial.println("request sent");
  340. while (client.connected())
  341. {
  342. String line = client.readStringUntil('\n');
  343. if (!connection_ok)
  344. {
  345. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  346. if (connection_ok) Serial.println(line);
  347. //if (!connection_ok) Serial.println(line);
  348. }
  349. if (!connection_ok) Serial.println(line);
  350. //Serial.println(line);
  351. if (line == "\r")
  352. {
  353. Serial.println("headers received");
  354. break;
  355. }
  356. }
  357. if (!connection_ok) return;
  358. // Parse BMP header
  359. if (read16(client) == 0x4D42) // BMP signature
  360. {
  361. uint32_t fileSize = read32(client);
  362. uint32_t creatorBytes = read32(client);
  363. uint32_t imageOffset = read32(client); // Start of image data
  364. uint32_t headerSize = read32(client);
  365. uint32_t width = read32(client);
  366. uint32_t height = read32(client);
  367. uint16_t planes = read16(client);
  368. uint16_t depth = read16(client); // bits per pixel
  369. uint32_t format = read32(client);
  370. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  371. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  372. {
  373. Serial.print("File size: "); Serial.println(fileSize);
  374. Serial.print("Image Offset: "); Serial.println(imageOffset);
  375. Serial.print("Header size: "); Serial.println(headerSize);
  376. Serial.print("Bit Depth: "); Serial.println(depth);
  377. Serial.print("Image size: ");
  378. Serial.print(width);
  379. Serial.print('x');
  380. Serial.println(height);
  381. // BMP rows are padded (if needed) to 4-byte boundary
  382. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  383. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  384. if (height < 0)
  385. {
  386. height = -height;
  387. flip = false;
  388. }
  389. uint16_t w = width;
  390. uint16_t h = height;
  391. if ((x + w - 1) >= display.width()) w = display.width() - x;
  392. if ((y + h - 1) >= display.height()) h = display.height() - y;
  393. if (w <= max_row_width) // handle with direct drawing
  394. {
  395. valid = true;
  396. uint8_t bitmask = 0xFF;
  397. uint8_t bitshift = 8 - depth;
  398. uint16_t red, green, blue;
  399. bool whitish, colored;
  400. if (depth == 1) with_color = false;
  401. if (depth <= 8)
  402. {
  403. if (depth < 8) bitmask >>= depth;
  404. //bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  405. bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
  406. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  407. {
  408. blue = client.read();
  409. green = client.read();
  410. red = client.read();
  411. client.read();
  412. bytes_read += 4;
  413. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  414. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  415. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  416. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  417. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  418. color_palette_buffer[pn / 8] |= colored << pn % 8;
  419. }
  420. }
  421. display.clearScreen();
  422. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  423. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  424. bytes_read += skip(client, rowPosition - bytes_read);
  425. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  426. {
  427. if (!connection_ok || !(client.connected() || client.available())) break;
  428. delay(1); // yield() to avoid WDT
  429. uint32_t in_remain = rowSize;
  430. uint32_t in_idx = 0;
  431. uint32_t in_bytes = 0;
  432. uint8_t in_byte = 0; // for depth <= 8
  433. uint8_t in_bits = 0; // for depth <= 8
  434. uint8_t out_byte = 0xFF; // white (for w%8!=0 boarder)
  435. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 boarder)
  436. uint32_t out_idx = 0;
  437. for (uint16_t col = 0; col < w; col++) // for each pixel
  438. {
  439. yield();
  440. if (!connection_ok || !(client.connected() || client.available())) break;
  441. // Time to read more pixel data?
  442. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  443. {
  444. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  445. uint32_t got = read(client, input_buffer, get);
  446. while ((got < get) && connection_ok)
  447. {
  448. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  449. uint32_t gotmore = read(client, input_buffer + got, get - got);
  450. got += gotmore;
  451. connection_ok = gotmore > 0;
  452. }
  453. in_bytes = got;
  454. in_remain -= got;
  455. bytes_read += got;
  456. }
  457. if (!connection_ok)
  458. {
  459. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  460. break;
  461. }
  462. switch (depth)
  463. {
  464. case 24:
  465. blue = input_buffer[in_idx++];
  466. green = input_buffer[in_idx++];
  467. red = input_buffer[in_idx++];
  468. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  469. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  470. break;
  471. case 16:
  472. {
  473. uint8_t lsb = input_buffer[in_idx++];
  474. uint8_t msb = input_buffer[in_idx++];
  475. if (format == 0) // 555
  476. {
  477. blue = (lsb & 0x1F) << 3;
  478. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  479. red = (msb & 0x7C) << 1;
  480. }
  481. else // 565
  482. {
  483. blue = (lsb & 0x1F) << 3;
  484. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  485. red = (msb & 0xF8);
  486. }
  487. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  488. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  489. }
  490. break;
  491. case 1:
  492. case 4:
  493. case 8:
  494. {
  495. if (0 == in_bits)
  496. {
  497. in_byte = input_buffer[in_idx++];
  498. in_bits = 8;
  499. }
  500. uint16_t pn = (in_byte >> bitshift) & bitmask;
  501. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  502. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  503. in_byte <<= depth;
  504. in_bits -= depth;
  505. }
  506. break;
  507. }
  508. if (whitish)
  509. {
  510. // keep white
  511. }
  512. else if (colored && with_color)
  513. {
  514. out_color_byte &= ~(0x80 >> col % 8); // colored
  515. }
  516. else
  517. {
  518. out_byte &= ~(0x80 >> col % 8); // black
  519. }
  520. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 boarder)
  521. {
  522. output_row_color_buffer[out_idx] = out_color_byte;
  523. output_row_mono_buffer[out_idx++] = out_byte;
  524. out_byte = 0xFF; // white (for w%8!=0 boarder)
  525. out_color_byte = 0xFF; // white (for w%8!=0 boarder)
  526. }
  527. } // end pixel
  528. int16_t yrow = y + (flip ? h - row - 1 : row);
  529. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  530. } // end line
  531. Serial.print("downloaded in ");
  532. Serial.print(millis() - startTime);
  533. Serial.println(" ms");
  534. display.refresh();
  535. }
  536. Serial.print("bytes read "); Serial.println(bytes_read);
  537. }
  538. }
  539. if (!valid)
  540. {
  541. Serial.println("bitmap format not handled.");
  542. }
  543. }
  544. void drawBitmapFrom_HTTP_ToBuffer(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  545. {
  546. WiFiClient client;
  547. bool connection_ok = false;
  548. bool valid = false; // valid format to be handled
  549. bool flip = true; // bitmap is stored bottom-to-top
  550. uint32_t startTime = millis();
  551. if ((x >= display.width()) || (y >= display.height())) return;
  552. display.fillScreen(GxEPD_WHITE);
  553. Serial.print("connecting to "); Serial.println(host);
  554. if (!client.connect(host, httpPort))
  555. {
  556. Serial.println("connection failed");
  557. return;
  558. }
  559. Serial.print("requesting URL: ");
  560. Serial.println(String("http://") + host + path + filename);
  561. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  562. "Host: " + host + "\r\n" +
  563. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  564. "Connection: close\r\n\r\n");
  565. Serial.println("request sent");
  566. while (client.connected())
  567. {
  568. String line = client.readStringUntil('\n');
  569. if (!connection_ok)
  570. {
  571. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  572. if (connection_ok) Serial.println(line);
  573. //if (!connection_ok) Serial.println(line);
  574. }
  575. if (!connection_ok) Serial.println(line);
  576. //Serial.println(line);
  577. if (line == "\r")
  578. {
  579. Serial.println("headers received");
  580. break;
  581. }
  582. }
  583. if (!connection_ok) return;
  584. // Parse BMP header
  585. if (read16(client) == 0x4D42) // BMP signature
  586. {
  587. uint32_t fileSize = read32(client);
  588. uint32_t creatorBytes = read32(client);
  589. uint32_t imageOffset = read32(client); // Start of image data
  590. uint32_t headerSize = read32(client);
  591. uint32_t width = read32(client);
  592. uint32_t height = read32(client);
  593. uint16_t planes = read16(client);
  594. uint16_t depth = read16(client); // bits per pixel
  595. uint32_t format = read32(client);
  596. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  597. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  598. {
  599. Serial.print("File size: "); Serial.println(fileSize);
  600. Serial.print("Image Offset: "); Serial.println(imageOffset);
  601. Serial.print("Header size: "); Serial.println(headerSize);
  602. Serial.print("Bit Depth: "); Serial.println(depth);
  603. Serial.print("Image size: ");
  604. Serial.print(width);
  605. Serial.print('x');
  606. Serial.println(height);
  607. // BMP rows are padded (if needed) to 4-byte boundary
  608. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  609. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  610. if (height < 0)
  611. {
  612. height = -height;
  613. flip = false;
  614. }
  615. uint16_t w = width;
  616. uint16_t h = height;
  617. if ((x + w - 1) >= display.width()) w = display.width() - x;
  618. if ((y + h - 1) >= display.height()) h = display.height() - y;
  619. //if (w <= max_row_width) // handle with direct drawing
  620. {
  621. valid = true;
  622. uint8_t bitmask = 0xFF;
  623. uint8_t bitshift = 8 - depth;
  624. uint16_t red, green, blue;
  625. bool whitish, colored;
  626. if (depth == 1) with_color = false;
  627. if (depth <= 8)
  628. {
  629. if (depth < 8) bitmask >>= depth;
  630. //bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  631. bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
  632. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  633. {
  634. blue = client.read();
  635. green = client.read();
  636. red = client.read();
  637. client.read();
  638. bytes_read += 4;
  639. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  640. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  641. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  642. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  643. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  644. color_palette_buffer[pn / 8] |= colored << pn % 8;
  645. //Serial.print("0x00"); Serial.print(red, HEX); Serial.print(green, HEX); Serial.print(blue, HEX);
  646. //Serial.print(" : "); Serial.print(whitish); Serial.print(", "); Serial.println(colored);
  647. }
  648. }
  649. display.writeScreenBuffer();
  650. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  651. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  652. bytes_read += skip(client, rowPosition - bytes_read);
  653. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  654. {
  655. if (!connection_ok || !(client.connected() || client.available())) break;
  656. delay(1); // yield() to avoid WDT
  657. uint32_t in_remain = rowSize;
  658. uint32_t in_idx = 0;
  659. uint32_t in_bytes = 0;
  660. uint8_t in_byte = 0; // for depth <= 8
  661. uint8_t in_bits = 0; // for depth <= 8
  662. uint16_t color = GxEPD_WHITE;
  663. for (uint16_t col = 0; col < w; col++) // for each pixel
  664. {
  665. yield();
  666. if (!connection_ok || !(client.connected() || client.available())) break;
  667. // Time to read more pixel data?
  668. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  669. {
  670. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  671. uint32_t got = read(client, input_buffer, get);
  672. while ((got < get) && connection_ok)
  673. {
  674. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  675. uint32_t gotmore = read(client, input_buffer + got, get - got);
  676. got += gotmore;
  677. connection_ok = gotmore > 0;
  678. }
  679. in_bytes = got;
  680. in_remain -= got;
  681. bytes_read += got;
  682. }
  683. if (!connection_ok)
  684. {
  685. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  686. break;
  687. }
  688. switch (depth)
  689. {
  690. case 24:
  691. blue = input_buffer[in_idx++];
  692. green = input_buffer[in_idx++];
  693. red = input_buffer[in_idx++];
  694. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  695. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  696. break;
  697. case 16:
  698. {
  699. uint8_t lsb = input_buffer[in_idx++];
  700. uint8_t msb = input_buffer[in_idx++];
  701. if (format == 0) // 555
  702. {
  703. blue = (lsb & 0x1F) << 3;
  704. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  705. red = (msb & 0x7C) << 1;
  706. }
  707. else // 565
  708. {
  709. blue = (lsb & 0x1F) << 3;
  710. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  711. red = (msb & 0xF8);
  712. }
  713. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  714. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  715. }
  716. break;
  717. case 1:
  718. case 4:
  719. case 8:
  720. {
  721. if (0 == in_bits)
  722. {
  723. in_byte = input_buffer[in_idx++];
  724. in_bits = 8;
  725. }
  726. uint16_t pn = (in_byte >> bitshift) & bitmask;
  727. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  728. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  729. in_byte <<= depth;
  730. in_bits -= depth;
  731. }
  732. break;
  733. }
  734. if (whitish)
  735. {
  736. color = GxEPD_WHITE;
  737. }
  738. else if (colored && with_color)
  739. {
  740. color = GxEPD_COLORED;
  741. }
  742. else
  743. {
  744. color = GxEPD_BLACK;
  745. }
  746. uint16_t yrow = y + (flip ? h - row - 1 : row);
  747. display.drawPixel(x + col, yrow, color);
  748. } // end pixel
  749. } // end line
  750. }
  751. Serial.print("bytes read "); Serial.println(bytes_read);
  752. }
  753. }
  754. Serial.print("loaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  755. if (!valid)
  756. {
  757. Serial.println("bitmap format not handled.");
  758. }
  759. }
  760. void showBitmapFrom_HTTP_Buffered(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  761. {
  762. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  763. display.setFullWindow();
  764. display.firstPage();
  765. do
  766. {
  767. drawBitmapFrom_HTTP_ToBuffer(host, path, filename, x, y, with_color);
  768. }
  769. while (display.nextPage());
  770. }
  771. void showBitmapFrom_HTTPS(const char* host, const char* path, const char* filename, const char* fingerprint, int16_t x, int16_t y, bool with_color)
  772. {
  773. // Use WiFiClientSecure class to create TLS connection
  774. #if USE_BearSSL
  775. BearSSL::WiFiClientSecure client;
  776. #else
  777. WiFiClientSecure client;
  778. #endif
  779. bool connection_ok = false;
  780. bool valid = false; // valid format to be handled
  781. bool flip = true; // bitmap is stored bottom-to-top
  782. uint32_t startTime = millis();
  783. if ((x >= display.width()) || (y >= display.height())) return;
  784. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  785. Serial.print("connecting to "); Serial.println(host);
  786. #if USE_BearSSL
  787. if (fingerprint) client.setFingerprint((uint8_t*)fingerprint);
  788. #endif
  789. if (!client.connect(host, httpsPort))
  790. {
  791. Serial.println("connection failed");
  792. return;
  793. }
  794. #if defined (ESP8266) && !USE_BearSSL
  795. if (fingerprint)
  796. {
  797. if (client.verify(fingerprint, host))
  798. {
  799. Serial.println("certificate matches");
  800. }
  801. else
  802. {
  803. Serial.println("certificate doesn't match");
  804. return;
  805. }
  806. }
  807. #endif
  808. Serial.print("requesting URL: ");
  809. Serial.println(String("https://") + host + path + filename);
  810. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  811. "Host: " + host + "\r\n" +
  812. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  813. "Connection: close\r\n\r\n");
  814. Serial.println("request sent");
  815. while (client.connected())
  816. {
  817. String line = client.readStringUntil('\n');
  818. if (!connection_ok)
  819. {
  820. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  821. if (connection_ok) Serial.println(line);
  822. //if (!connection_ok) Serial.println(line);
  823. }
  824. if (!connection_ok) Serial.println(line);
  825. //Serial.println(line);
  826. if (line == "\r")
  827. {
  828. Serial.println("headers received");
  829. break;
  830. }
  831. }
  832. if (!connection_ok) return;
  833. // Parse BMP header
  834. if (read16(client) == 0x4D42) // BMP signature
  835. {
  836. uint32_t fileSize = read32(client);
  837. uint32_t creatorBytes = read32(client);
  838. uint32_t imageOffset = read32(client); // Start of image data
  839. uint32_t headerSize = read32(client);
  840. uint32_t width = read32(client);
  841. uint32_t height = read32(client);
  842. uint16_t planes = read16(client);
  843. uint16_t depth = read16(client); // bits per pixel
  844. uint32_t format = read32(client);
  845. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  846. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  847. {
  848. Serial.print("File size: "); Serial.println(fileSize);
  849. Serial.print("Image Offset: "); Serial.println(imageOffset);
  850. Serial.print("Header size: "); Serial.println(headerSize);
  851. Serial.print("Bit Depth: "); Serial.println(depth);
  852. Serial.print("Image size: ");
  853. Serial.print(width);
  854. Serial.print('x');
  855. Serial.println(height);
  856. // BMP rows are padded (if needed) to 4-byte boundary
  857. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  858. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  859. if (height < 0)
  860. {
  861. height = -height;
  862. flip = false;
  863. }
  864. uint16_t w = width;
  865. uint16_t h = height;
  866. if ((x + w - 1) >= display.width()) w = display.width() - x;
  867. if ((y + h - 1) >= display.height()) h = display.height() - y;
  868. if (w <= max_row_width) // handle with direct drawing
  869. {
  870. valid = true;
  871. uint8_t bitmask = 0xFF;
  872. uint8_t bitshift = 8 - depth;
  873. uint16_t red, green, blue;
  874. bool whitish, colored;
  875. if (depth == 1) with_color = false;
  876. if (depth <= 8)
  877. {
  878. if (depth < 8) bitmask >>= depth;
  879. //bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  880. bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
  881. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  882. {
  883. blue = client.read();
  884. green = client.read();
  885. red = client.read();
  886. client.read();
  887. bytes_read += 4;
  888. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  889. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  890. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  891. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  892. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  893. color_palette_buffer[pn / 8] |= colored << pn % 8;
  894. }
  895. }
  896. display.clearScreen();
  897. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  898. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  899. bytes_read += skip(client, rowPosition - bytes_read);
  900. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  901. {
  902. if (!connection_ok || !(client.connected() || client.available())) break;
  903. delay(1); // yield() to avoid WDT
  904. uint32_t in_remain = rowSize;
  905. uint32_t in_idx = 0;
  906. uint32_t in_bytes = 0;
  907. uint8_t in_byte = 0; // for depth <= 8
  908. uint8_t in_bits = 0; // for depth <= 8
  909. uint8_t out_byte = 0xFF; // white (for w%8!=0 boarder)
  910. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 boarder)
  911. uint32_t out_idx = 0;
  912. for (uint16_t col = 0; col < w; col++) // for each pixel
  913. {
  914. yield();
  915. if (!connection_ok || !(client.connected() || client.available())) break;
  916. // Time to read more pixel data?
  917. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  918. {
  919. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  920. uint32_t got = read(client, input_buffer, get);
  921. while ((got < get) && connection_ok)
  922. {
  923. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  924. //if ((get - got) > client.available()) delay(200); // does improve? yes, if >= 200
  925. uint32_t gotmore = read(client, input_buffer + got, get - got);
  926. got += gotmore;
  927. connection_ok = gotmore > 0;
  928. }
  929. in_bytes = got;
  930. in_remain -= got;
  931. bytes_read += got;
  932. }
  933. if (!connection_ok)
  934. {
  935. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  936. break;
  937. }
  938. switch (depth)
  939. {
  940. case 24:
  941. blue = input_buffer[in_idx++];
  942. green = input_buffer[in_idx++];
  943. red = input_buffer[in_idx++];
  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. break;
  947. case 16:
  948. {
  949. uint8_t lsb = input_buffer[in_idx++];
  950. uint8_t msb = input_buffer[in_idx++];
  951. if (format == 0) // 555
  952. {
  953. blue = (lsb & 0x1F) << 3;
  954. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  955. red = (msb & 0x7C) << 1;
  956. }
  957. else // 565
  958. {
  959. blue = (lsb & 0x1F) << 3;
  960. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  961. red = (msb & 0xF8);
  962. }
  963. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  964. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  965. }
  966. break;
  967. case 1:
  968. case 4:
  969. case 8:
  970. {
  971. if (0 == in_bits)
  972. {
  973. in_byte = input_buffer[in_idx++];
  974. in_bits = 8;
  975. }
  976. uint16_t pn = (in_byte >> bitshift) & bitmask;
  977. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  978. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  979. in_byte <<= depth;
  980. in_bits -= depth;
  981. }
  982. break;
  983. }
  984. if (whitish)
  985. {
  986. // keep white
  987. }
  988. else if (colored && with_color)
  989. {
  990. out_color_byte &= ~(0x80 >> col % 8); // colored
  991. }
  992. else
  993. {
  994. out_byte &= ~(0x80 >> col % 8); // black
  995. }
  996. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 boarder)
  997. {
  998. output_row_color_buffer[out_idx] = out_color_byte;
  999. output_row_mono_buffer[out_idx++] = out_byte;
  1000. out_byte = 0xFF; // white (for w%8!=0 boarder)
  1001. out_color_byte = 0xFF; // white (for w%8!=0 boarder)
  1002. }
  1003. } // end pixel
  1004. int16_t yrow = y + (flip ? h - row - 1 : row);
  1005. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  1006. } // end line
  1007. Serial.print("downloaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  1008. Serial.print("bytes read "); Serial.println(bytes_read);
  1009. display.refresh();
  1010. }
  1011. }
  1012. }
  1013. if (!valid)
  1014. {
  1015. Serial.println("bitmap format not handled.");
  1016. }
  1017. }
  1018. 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)
  1019. {
  1020. // Use WiFiClientSecure class to create TLS connection
  1021. #if USE_BearSSL
  1022. BearSSL::WiFiClientSecure client;
  1023. #else
  1024. WiFiClientSecure client;
  1025. #endif
  1026. bool connection_ok = false;
  1027. bool valid = false; // valid format to be handled
  1028. bool flip = true; // bitmap is stored bottom-to-top
  1029. uint32_t startTime = millis();
  1030. if ((x >= display.width()) || (y >= display.height())) return;
  1031. display.fillScreen(GxEPD_WHITE);
  1032. Serial.print("connecting to "); Serial.println(host);
  1033. #if USE_BearSSL
  1034. if (fingerprint) client.setFingerprint((uint8_t*)fingerprint);
  1035. #endif
  1036. if (!client.connect(host, httpsPort))
  1037. {
  1038. Serial.println("connection failed");
  1039. return;
  1040. }
  1041. #if defined (ESP8266) && !USE_BearSSL
  1042. if (fingerprint)
  1043. {
  1044. if (client.verify(fingerprint, host))
  1045. {
  1046. Serial.println("certificate matches");
  1047. }
  1048. else
  1049. {
  1050. Serial.println("certificate doesn't match");
  1051. return;
  1052. }
  1053. }
  1054. #endif
  1055. Serial.print("requesting URL: ");
  1056. Serial.println(String("https://") + host + path + filename);
  1057. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  1058. "Host: " + host + "\r\n" +
  1059. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  1060. "Connection: close\r\n\r\n");
  1061. Serial.println("request sent");
  1062. while (client.connected())
  1063. {
  1064. String line = client.readStringUntil('\n');
  1065. if (!connection_ok)
  1066. {
  1067. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  1068. if (connection_ok) Serial.println(line);
  1069. //if (!connection_ok) Serial.println(line);
  1070. }
  1071. if (!connection_ok) Serial.println(line);
  1072. //Serial.println(line);
  1073. if (line == "\r")
  1074. {
  1075. Serial.println("headers received");
  1076. break;
  1077. }
  1078. }
  1079. if (!connection_ok) return;
  1080. // Parse BMP header
  1081. if (read16(client) == 0x4D42) // BMP signature
  1082. {
  1083. uint32_t fileSize = read32(client);
  1084. uint32_t creatorBytes = read32(client);
  1085. uint32_t imageOffset = read32(client); // Start of image data
  1086. uint32_t headerSize = read32(client);
  1087. uint32_t width = read32(client);
  1088. uint32_t height = read32(client);
  1089. uint16_t planes = read16(client);
  1090. uint16_t depth = read16(client); // bits per pixel
  1091. uint32_t format = read32(client);
  1092. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  1093. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  1094. {
  1095. Serial.print("File size: "); Serial.println(fileSize);
  1096. Serial.print("Image Offset: "); Serial.println(imageOffset);
  1097. Serial.print("Header size: "); Serial.println(headerSize);
  1098. Serial.print("Bit Depth: "); Serial.println(depth);
  1099. Serial.print("Image size: ");
  1100. Serial.print(width);
  1101. Serial.print('x');
  1102. Serial.println(height);
  1103. // BMP rows are padded (if needed) to 4-byte boundary
  1104. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  1105. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  1106. if (height < 0)
  1107. {
  1108. height = -height;
  1109. flip = false;
  1110. }
  1111. uint16_t w = width;
  1112. uint16_t h = height;
  1113. if ((x + w - 1) >= display.width()) w = display.width() - x;
  1114. if ((y + h - 1) >= display.height()) h = display.height() - y;
  1115. //if (w <= max_row_width) // handle with direct drawing
  1116. {
  1117. valid = true;
  1118. uint8_t bitmask = 0xFF;
  1119. uint8_t bitshift = 8 - depth;
  1120. uint16_t red, green, blue;
  1121. bool whitish, colored;
  1122. if (depth == 1) with_color = false;
  1123. if (depth <= 8)
  1124. {
  1125. if (depth < 8) bitmask >>= depth;
  1126. //bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  1127. bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
  1128. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  1129. {
  1130. blue = client.read();
  1131. green = client.read();
  1132. red = client.read();
  1133. client.read();
  1134. bytes_read += 4;
  1135. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1136. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1137. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  1138. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  1139. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  1140. color_palette_buffer[pn / 8] |= colored << pn % 8;
  1141. //Serial.print("0x00"); Serial.print(red, HEX); Serial.print(green, HEX); Serial.print(blue, HEX);
  1142. //Serial.print(" : "); Serial.print(whitish); Serial.print(", "); Serial.println(colored);
  1143. }
  1144. }
  1145. display.writeScreenBuffer();
  1146. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  1147. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  1148. bytes_read += skip(client, rowPosition - bytes_read);
  1149. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  1150. {
  1151. if (!connection_ok || !(client.connected() || client.available())) break;
  1152. delay(1); // yield() to avoid WDT
  1153. uint32_t in_remain = rowSize;
  1154. uint32_t in_idx = 0;
  1155. uint32_t in_bytes = 0;
  1156. uint8_t in_byte = 0; // for depth <= 8
  1157. uint8_t in_bits = 0; // for depth <= 8
  1158. uint16_t color = GxEPD_WHITE;
  1159. for (uint16_t col = 0; col < w; col++) // for each pixel
  1160. {
  1161. yield();
  1162. if (!connection_ok || !(client.connected() || client.available())) break;
  1163. // Time to read more pixel data?
  1164. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  1165. {
  1166. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  1167. uint32_t got = read(client, input_buffer, get);
  1168. while ((got < get) && connection_ok)
  1169. {
  1170. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  1171. uint32_t gotmore = read(client, input_buffer + got, get - got);
  1172. got += gotmore;
  1173. connection_ok = gotmore > 0;
  1174. }
  1175. in_bytes = got;
  1176. in_remain -= got;
  1177. bytes_read += got;
  1178. }
  1179. if (!connection_ok)
  1180. {
  1181. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  1182. break;
  1183. }
  1184. switch (depth)
  1185. {
  1186. case 24:
  1187. blue = input_buffer[in_idx++];
  1188. green = input_buffer[in_idx++];
  1189. red = input_buffer[in_idx++];
  1190. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1191. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1192. break;
  1193. case 16:
  1194. {
  1195. uint8_t lsb = input_buffer[in_idx++];
  1196. uint8_t msb = input_buffer[in_idx++];
  1197. if (format == 0) // 555
  1198. {
  1199. blue = (lsb & 0x1F) << 3;
  1200. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  1201. red = (msb & 0x7C) << 1;
  1202. }
  1203. else // 565
  1204. {
  1205. blue = (lsb & 0x1F) << 3;
  1206. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  1207. red = (msb & 0xF8);
  1208. }
  1209. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1210. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1211. }
  1212. break;
  1213. case 1:
  1214. case 4:
  1215. case 8:
  1216. {
  1217. if (0 == in_bits)
  1218. {
  1219. in_byte = input_buffer[in_idx++];
  1220. in_bits = 8;
  1221. }
  1222. uint16_t pn = (in_byte >> bitshift) & bitmask;
  1223. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  1224. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  1225. in_byte <<= depth;
  1226. in_bits -= depth;
  1227. }
  1228. break;
  1229. }
  1230. if (whitish)
  1231. {
  1232. color = GxEPD_WHITE;
  1233. }
  1234. else if (colored && with_color)
  1235. {
  1236. color = GxEPD_COLORED;
  1237. }
  1238. else
  1239. {
  1240. color = GxEPD_BLACK;
  1241. }
  1242. uint16_t yrow = y + (flip ? h - row - 1 : row);
  1243. display.drawPixel(x + col, yrow, color);
  1244. } // end pixel
  1245. } // end line
  1246. }
  1247. Serial.print("bytes read "); Serial.println(bytes_read);
  1248. }
  1249. }
  1250. Serial.print("loaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  1251. if (!valid)
  1252. {
  1253. Serial.println("bitmap format not handled.");
  1254. }
  1255. }
  1256. 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)
  1257. {
  1258. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  1259. display.setFullWindow();
  1260. display.firstPage();
  1261. do
  1262. {
  1263. drawBitmapFrom_HTTPS_ToBuffer(host, path, filename, fingerprint, x, y, with_color);
  1264. }
  1265. while (display.nextPage());
  1266. }
  1267. uint16_t read16(WiFiClient& client)
  1268. {
  1269. // BMP data is stored little-endian, same as Arduino.
  1270. uint16_t result;
  1271. ((uint8_t *)&result)[0] = client.read(); // LSB
  1272. ((uint8_t *)&result)[1] = client.read(); // MSB
  1273. return result;
  1274. }
  1275. uint32_t read32(WiFiClient& client)
  1276. {
  1277. // BMP data is stored little-endian, same as Arduino.
  1278. uint32_t result;
  1279. ((uint8_t *)&result)[0] = client.read(); // LSB
  1280. ((uint8_t *)&result)[1] = client.read();
  1281. ((uint8_t *)&result)[2] = client.read();
  1282. ((uint8_t *)&result)[3] = client.read(); // MSB
  1283. return result;
  1284. }
  1285. #if USE_BearSSL
  1286. uint32_t skip(BearSSL::WiFiClientSecure& client, int32_t bytes)
  1287. {
  1288. int32_t remain = bytes;
  1289. uint32_t start = millis();
  1290. while ((client.connected() || client.available()) && (remain > 0))
  1291. {
  1292. if (client.available())
  1293. {
  1294. int16_t v = client.read();
  1295. remain--;
  1296. }
  1297. else delay(1);
  1298. if (millis() - start > 2000) break; // don't hang forever
  1299. }
  1300. return bytes - remain;
  1301. }
  1302. uint32_t read(BearSSL::WiFiClientSecure& client, uint8_t* buffer, int32_t bytes)
  1303. {
  1304. int32_t remain = bytes;
  1305. uint32_t start = millis();
  1306. while ((client.connected() || client.available()) && (remain > 0))
  1307. {
  1308. if (client.available())
  1309. {
  1310. int16_t v = client.read();
  1311. *buffer++ = uint8_t(v);
  1312. remain--;
  1313. }
  1314. else delay(1);
  1315. if (millis() - start > 2000) break; // don't hang forever
  1316. }
  1317. return bytes - remain;
  1318. }
  1319. #endif
  1320. uint32_t skip(WiFiClient& client, int32_t bytes)
  1321. {
  1322. int32_t remain = bytes;
  1323. uint32_t start = millis();
  1324. while ((client.connected() || client.available()) && (remain > 0))
  1325. {
  1326. if (client.available())
  1327. {
  1328. int16_t v = client.read();
  1329. remain--;
  1330. }
  1331. else delay(1);
  1332. if (millis() - start > 2000) break; // don't hang forever
  1333. }
  1334. return bytes - remain;
  1335. }
  1336. uint32_t read(WiFiClient& client, uint8_t* buffer, int32_t bytes)
  1337. {
  1338. int32_t remain = bytes;
  1339. uint32_t start = millis();
  1340. while ((client.connected() || client.available()) && (remain > 0))
  1341. {
  1342. if (client.available())
  1343. {
  1344. int16_t v = client.read();
  1345. *buffer++ = uint8_t(v);
  1346. remain--;
  1347. }
  1348. else delay(1);
  1349. if (millis() - start > 2000) break; // don't hang forever
  1350. }
  1351. return bytes - remain;
  1352. }