GxEPD2_WiFi_Example.ino 57 KB

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