GxEPD2_WiFi_Example.ino 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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(_BOARD_GENERIC_STM32F103C_H_)
  91. #define MAX_DISPAY_BUFFER_SIZE (400 * 300) // ~5k is a good compromise
  92. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= MAX_DISPAY_BUFFER_SIZE / 8 / EPD::WIDTH ? EPD::HEIGHT : MAX_DISPAY_BUFFER_SIZE / 8 / EPD::WIDTH)
  93. // select one and adapt to your mapping
  94. //GxEPD2_BW<GxEPD2_154, MAX_HEIGHT(GxEPD2_154)> display(GxEPD2_154(/*CS=4*/ EPD_CS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  95. //GxEPD2_BW<GxEPD2_213, MAX_HEIGHT(GxEPD2_213)> display(GxEPD2_213(/*CS=4*/ EPD_CS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  96. //GxEPD2_BW<GxEPD2_290, MAX_HEIGHT(GxEPD2_290)> display(GxEPD2_290(/*CS=4*/ EPD_CS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  97. //GxEPD2_BW<GxEPD2_270, MAX_HEIGHT(GxEPD2_270)> display(GxEPD2_270(/*CS=4*/ EPD_CS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  98. //GxEPD2_BW<GxEPD2_420, MAX_HEIGHT(GxEPD2_420)> display(GxEPD2_420(/*CS=4*/ EPD_CS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  99. //GxEPD2_BW<GxEPD2_750, MAX_HEIGHT(GxEPD2_750)> display(GxEPD2_750(/*CS=4*/ EPD_CS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  100. // 3-color e-papers
  101. //GxEPD2_3C<GxEPD2_154c, MAX_HEIGHT(GxEPD2_154c)> display(GxEPD2_154c(/*CS=4*/ EPD_CS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  102. //GxEPD2_3C<GxEPD2_213c, MAX_HEIGHT(GxEPD2_213c)> display(GxEPD2_213c(/*CS=4*/ EPD_CS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  103. //GxEPD2_3C<GxEPD2_290c, MAX_HEIGHT(GxEPD2_290c)> display(GxEPD2_290c(/*CS=4*/ EPD_CS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  104. //GxEPD2_3C<GxEPD2_270c, MAX_HEIGHT(GxEPD2_270c)> display(GxEPD2_270c(/*CS=4*/ EPD_CS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  105. //GxEPD2_3C<GxEPD2_420c, MAX_HEIGHT(GxEPD2_420c)> display(GxEPD2_420c(/*CS=4*/ EPD_CS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  106. //GxEPD2_3C<GxEPD2_750c, MAX_HEIGHT(GxEPD2_750c)> display(GxEPD2_750c(/*CS=4*/ EPD_CS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  107. #endif
  108. #if defined(__AVR)
  109. #define MAX_DISPAY_BUFFER_SIZE 1000 // half of available RAM as a compromise
  110. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= MAX_DISPAY_BUFFER_SIZE / 8 / EPD::WIDTH ? EPD::HEIGHT : MAX_DISPAY_BUFFER_SIZE / 8 / EPD::WIDTH)
  111. // select one and adapt to your mapping
  112. //GxEPD2_BW<GxEPD2_154, MAX_HEIGHT(GxEPD2_154)> display(GxEPD2_154(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  113. //GxEPD2_BW<GxEPD2_213, MAX_HEIGHT(GxEPD2_213)> display(GxEPD2_213(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  114. //GxEPD2_BW<GxEPD2_290, MAX_HEIGHT(GxEPD2_290)> display(GxEPD2_290(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  115. //GxEPD2_BW<GxEPD2_270, MAX_HEIGHT(GxEPD2_270)> display(GxEPD2_270(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  116. //GxEPD2_BW<GxEPD2_420, MAX_HEIGHT(GxEPD2_420)> display(GxEPD2_420(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  117. //GxEPD2_BW<GxEPD2_750, MAX_HEIGHT(GxEPD2_750)> display(GxEPD2_750(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  118. // 3-color e-papers
  119. //GxEPD2_3C<GxEPD2_154c, MAX_HEIGHT(GxEPD2_154c)> display(GxEPD2_154c(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  120. //GxEPD2_3C<GxEPD2_213c, MAX_HEIGHT(GxEPD2_213c)> display(GxEPD2_213c(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  121. //GxEPD2_3C<GxEPD2_290c, MAX_HEIGHT(GxEPD2_290c)> display(GxEPD2_290c(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  122. //GxEPD2_3C<GxEPD2_270c, MAX_HEIGHT(GxEPD2_270c)> display(GxEPD2_270c(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  123. //GxEPD2_3C<GxEPD2_420c, MAX_HEIGHT(GxEPD2_420c)> display(GxEPD2_420c(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  124. //GxEPD2_3C<GxEPD2_750c, MAX_HEIGHT(GxEPD2_750c)> display(GxEPD2_750c(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  125. #endif
  126. #include <ESP8266WiFi.h>
  127. #include <WiFiClient.h>
  128. #include <WiFiClientSecure.h>
  129. const char* ssid = "........";
  130. const char* password = "........";
  131. const int httpPort = 80;
  132. const int httpsPort = 443;
  133. 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";
  134. 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";
  135. const char* fp_rawcontent = "cc aa 48 48 66 46 0e 91 53 2c 9c 7c 23 2a b1 74 4d 29 9d 33";
  136. const char* host_rawcontent = "raw.githubusercontent.com";
  137. const char* path_rawcontent = "/ZinggJM/GxEPD2/master/extras/bitmaps/";
  138. const char* path_prenticedavid = "/prenticedavid/MCUFRIEND_kbv/master/extras/bitmaps/";
  139. void showBitmapFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color = true);
  140. 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);
  141. void setup()
  142. {
  143. Serial.begin(115200);
  144. Serial.println();
  145. Serial.println("GxEPD2_WiFi_Example");
  146. display.init();
  147. #ifdef RE_INIT_NEEDED
  148. WiFi.persistent(true);
  149. WiFi.mode(WIFI_STA); // switch off AP
  150. WiFi.setAutoConnect(true);
  151. WiFi.setAutoReconnect(true);
  152. WiFi.disconnect();
  153. #endif
  154. if (!WiFi.getAutoConnect() || ( WiFi.getMode() != WIFI_STA) || ((WiFi.SSID() != ssid) && String(ssid) != "........"))
  155. {
  156. Serial.println();
  157. Serial.print("WiFi.getAutoConnect()=");
  158. Serial.println(WiFi.getAutoConnect());
  159. Serial.print("WiFi.SSID()=");
  160. Serial.println(WiFi.SSID());
  161. WiFi.mode(WIFI_STA); // switch off AP
  162. Serial.print("Connecting to ");
  163. Serial.println(ssid);
  164. WiFi.begin(ssid, password);
  165. }
  166. int ConnectTimeout = 30; // 15 seconds
  167. while (WiFi.status() != WL_CONNECTED)
  168. {
  169. delay(500);
  170. Serial.print(".");
  171. Serial.print(WiFi.status());
  172. if (--ConnectTimeout <= 0)
  173. {
  174. Serial.println();
  175. Serial.println("WiFi connect timeout");
  176. return;
  177. }
  178. }
  179. Serial.println();
  180. Serial.println("WiFi connected");
  181. // Print the IP address
  182. Serial.println(WiFi.localIP());
  183. drawBitmaps_200x200();
  184. drawBitmaps_other();
  185. }
  186. void loop(void)
  187. {
  188. }
  189. void drawBitmaps_200x200()
  190. {
  191. int16_t x = (display.width() - 200) / 2;
  192. int16_t y = (display.height() - 200) / 2;
  193. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "logo200x200.bmp", fp_rawcontent, x, y);
  194. delay(2000);
  195. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "first200x200.bmp", fp_rawcontent, x, y);
  196. delay(2000);
  197. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "second200x200.bmp", fp_rawcontent, x, y);
  198. delay(2000);
  199. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "third200x200.bmp", fp_rawcontent, x, y);
  200. delay(2000);
  201. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "fourth200x200.bmp", fp_rawcontent, x, y);
  202. delay(2000);
  203. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "fifth200x200.bmp", fp_rawcontent, x, y);
  204. delay(2000);
  205. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "sixth200x200.bmp", fp_rawcontent, x, y);
  206. delay(2000);
  207. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "seventh200x200.bmp", fp_rawcontent, x, y);
  208. delay(2000);
  209. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "eighth200x200.bmp", fp_rawcontent, x, y);
  210. delay(2000);
  211. }
  212. void drawBitmaps_other()
  213. {
  214. int16_t w2 = display.width() / 2;
  215. int16_t h2 = display.height() / 2;
  216. showBitmapFrom_HTTP("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", w2 - 50, h2 - 50, false);
  217. delay(2000);
  218. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_1.bmp", fp_rawcontent, w2 - 100, h2 - 160);
  219. delay(2000);
  220. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  221. delay(2000);
  222. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "marilyn_240x240x8.bmp", fp_rawcontent, w2 - 120, h2 - 120);
  223. delay(2000);
  224. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "miniwoof.bmp", fp_rawcontent, w2 - 60, h2 - 80);
  225. delay(2000);
  226. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "test.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  227. delay(2000);
  228. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger.bmp", fp_rawcontent, w2 - 160, h2 - 120);
  229. delay(2000);
  230. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_178x160x4.bmp", fp_rawcontent, w2 - 89, h2 - 80);
  231. delay(2000);
  232. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_240x317x4.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  233. delay(2000);
  234. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_320x200x24.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  235. delay(2000);
  236. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger16T.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  237. delay(2000);
  238. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "woof.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  239. delay(2000);
  240. }
  241. static const uint16_t input_buffer_pixels = 640; // may affect performance
  242. static const uint16_t max_row_width = 640; // for up to 7.5" display
  243. static const uint16_t max_palette_pixels = 256; // for depth <= 8
  244. uint8_t input_buffer[3 * input_buffer_pixels]; // up to depth 24
  245. uint8_t output_row_mono_buffer[max_row_width / 8]; // buffer for at least one row of b/w bits
  246. uint8_t output_row_color_buffer[max_row_width / 8]; // buffer for at least one row of color bits
  247. uint8_t mono_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 b/w
  248. uint8_t color_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 c/w
  249. void showBitmapFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  250. {
  251. WiFiClient client;
  252. bool connection_ok = false;
  253. bool valid = false; // valid format to be handled
  254. bool flip = true; // bitmap is stored bottom-to-top
  255. uint32_t startTime = millis();
  256. if ((x >= display.width()) || (y >= display.height())) return;
  257. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  258. Serial.print("connecting to "); Serial.println(host);
  259. if (!client.connect(host, httpPort))
  260. {
  261. Serial.println("connection failed");
  262. return;
  263. }
  264. Serial.print("requesting URL: ");
  265. Serial.println(String("http://") + host + path + filename);
  266. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  267. "Host: " + host + "\r\n" +
  268. "User-Agent: GxEPD2_Spiffs_Loader\r\n" +
  269. "Connection: close\r\n\r\n");
  270. Serial.println("request sent");
  271. while (client.connected())
  272. {
  273. String line = client.readStringUntil('\n');
  274. if (!connection_ok)
  275. {
  276. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  277. if (connection_ok) Serial.println(line);
  278. //if (!connection_ok) Serial.println(line);
  279. }
  280. if (!connection_ok) Serial.println(line);
  281. //Serial.println(line);
  282. if (line == "\r")
  283. {
  284. Serial.println("headers received");
  285. break;
  286. }
  287. }
  288. if (!connection_ok) return;
  289. // Parse BMP header
  290. if (read16(client) == 0x4D42) // BMP signature
  291. {
  292. uint32_t fileSize = read32(client);
  293. uint32_t creatorBytes = read32(client);
  294. uint32_t imageOffset = read32(client); // Start of image data
  295. uint32_t headerSize = read32(client);
  296. uint32_t width = read32(client);
  297. uint32_t height = read32(client);
  298. uint16_t planes = read16(client);
  299. uint16_t depth = read16(client); // bits per pixel
  300. uint32_t format = read32(client);
  301. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  302. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  303. {
  304. Serial.print("File size: "); Serial.println(fileSize);
  305. Serial.print("Image Offset: "); Serial.println(imageOffset);
  306. Serial.print("Header size: "); Serial.println(headerSize);
  307. Serial.print("Bit Depth: "); Serial.println(depth);
  308. Serial.print("Image size: ");
  309. Serial.print(width);
  310. Serial.print('x');
  311. Serial.println(height);
  312. // BMP rows are padded (if needed) to 4-byte boundary
  313. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  314. if (height < 0)
  315. {
  316. height = -height;
  317. flip = false;
  318. }
  319. uint16_t w = width;
  320. uint16_t h = height;
  321. if ((x + w - 1) >= display.width()) w = display.width() - x;
  322. if ((y + h - 1) >= display.height()) h = display.height() - y;
  323. if (w < max_row_width) // handle with direct drawing
  324. {
  325. valid = true;
  326. uint8_t bitmask = 0xFF;
  327. uint8_t bitshift = 8 - depth;
  328. uint16_t red, green, blue;
  329. bool whitish, colored;
  330. if (depth == 1) with_color = false;
  331. if (depth <= 8)
  332. {
  333. if (depth < 8) bitmask >>= depth;
  334. bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  335. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  336. {
  337. blue = client.read();
  338. green = client.read();
  339. red = client.read();
  340. client.read();
  341. bytes_read += 4;
  342. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  343. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  344. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  345. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  346. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  347. color_palette_buffer[pn / 8] |= colored << pn % 8;
  348. //Serial.print("0x00"); Serial.print(red, HEX); Serial.print(green, HEX); Serial.print(blue, HEX);
  349. //Serial.print(" : "); Serial.print(whitish); Serial.print(", "); Serial.println(colored);
  350. }
  351. }
  352. display.writeScreenBuffer();
  353. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  354. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  355. bytes_read += skip(client, rowPosition - bytes_read);
  356. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  357. {
  358. if (!connection_ok || !client.connected()) break;
  359. delay(1); // yield() to avoid WDT
  360. uint32_t in_remain = rowSize;
  361. uint32_t in_idx = 0;
  362. uint32_t in_bytes = 0;
  363. uint8_t in_byte = 0; // for depth <= 8
  364. uint8_t in_bits = 0; // for depth <= 8
  365. uint8_t out_byte = 0;
  366. uint8_t out_color_byte = 0;
  367. uint32_t out_idx = 0;
  368. for (uint16_t col = 0; col < w; col++) // for each pixel
  369. {
  370. yield();
  371. if (!connection_ok || !client.connected()) break;
  372. // Time to read more pixel data?
  373. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  374. {
  375. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  376. //if (get > client.available()) delay(200); // does improve? yes, if >= 200
  377. // there seems an issue with long downloads on ESP8266
  378. tryToWaitForAvailable(client, get);
  379. uint32_t got = client.read(input_buffer, get);
  380. while ((got < get) && connection_ok)
  381. {
  382. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  383. //if ((get - got) > client.available()) delay(200); // does improve? yes, if >= 200
  384. // there seems an issue with long downloads on ESP8266
  385. tryToWaitForAvailable(client, get - got);
  386. uint32_t gotmore = client.read(input_buffer + got, get - got);
  387. got += gotmore;
  388. connection_ok = gotmore > 0;
  389. }
  390. in_bytes = got;
  391. in_remain -= got;
  392. bytes_read += got;
  393. }
  394. if (!connection_ok)
  395. {
  396. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  397. break;
  398. }
  399. switch (depth)
  400. {
  401. case 24:
  402. blue = input_buffer[in_idx++];
  403. green = input_buffer[in_idx++];
  404. red = input_buffer[in_idx++];
  405. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  406. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  407. break;
  408. case 16:
  409. {
  410. uint8_t lsb = input_buffer[in_idx++];
  411. uint8_t msb = input_buffer[in_idx++];
  412. if (format == 0) // 555
  413. {
  414. blue = (lsb & 0x1F) << 3;
  415. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  416. red = (msb & 0x7C) << 1;
  417. }
  418. else // 565
  419. {
  420. blue = (lsb & 0x1F) << 3;
  421. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  422. red = (msb & 0xF8);
  423. }
  424. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  425. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  426. }
  427. break;
  428. case 1:
  429. case 4:
  430. case 8:
  431. {
  432. if (0 == in_bits)
  433. {
  434. in_byte = input_buffer[in_idx++];
  435. in_bits = 8;
  436. }
  437. uint16_t pn = (in_byte >> bitshift) & bitmask;
  438. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  439. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  440. in_byte <<= depth;
  441. in_bits -= depth;
  442. }
  443. break;
  444. }
  445. if (whitish)
  446. {
  447. out_byte |= 0x80 >> col % 8; // not black
  448. out_color_byte |= 0x80 >> col % 8; // not colored
  449. }
  450. else if (colored && with_color)
  451. {
  452. out_byte |= 0x80 >> col % 8; // not black
  453. }
  454. else
  455. {
  456. out_color_byte |= 0x80 >> col % 8; // not colored
  457. }
  458. if (7 == col % 8)
  459. {
  460. output_row_color_buffer[out_idx] = out_color_byte;
  461. output_row_mono_buffer[out_idx++] = out_byte;
  462. out_byte = 0;
  463. out_color_byte = 0;
  464. }
  465. } // end pixel
  466. int16_t yrow = y + (flip ? h - row - 1 : row);
  467. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  468. } // end line
  469. Serial.print("downloaded in ");
  470. Serial.print(millis() - startTime);
  471. Serial.println(" ms");
  472. display.refresh();
  473. }
  474. Serial.print("bytes read "); Serial.println(bytes_read);
  475. }
  476. }
  477. if (!valid)
  478. {
  479. Serial.println("bitmap format not handled.");
  480. }
  481. }
  482. void showBitmapFrom_HTTPS(const char* host, const char* path, const char* filename, const char* fingerprint, int16_t x, int16_t y, bool with_color)
  483. {
  484. // Use WiFiClientSecure class to create TLS connection
  485. WiFiClientSecure client;
  486. bool connection_ok = false;
  487. bool valid = false; // valid format to be handled
  488. bool flip = true; // bitmap is stored bottom-to-top
  489. uint32_t startTime = millis();
  490. if ((x >= display.width()) || (y >= display.height())) return;
  491. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  492. Serial.print("connecting to "); Serial.println(host);
  493. if (!client.connect(host, httpsPort))
  494. {
  495. Serial.println("connection failed");
  496. return;
  497. }
  498. if (fingerprint)
  499. {
  500. if (client.verify(fingerprint, host))
  501. {
  502. Serial.println("certificate matches");
  503. }
  504. else
  505. {
  506. Serial.println("certificate doesn't match");
  507. return;
  508. }
  509. }
  510. Serial.print("requesting URL: ");
  511. Serial.println(String("https://") + host + path + filename);
  512. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  513. "Host: " + host + "\r\n" +
  514. "User-Agent: GxEPD2_Spiffs_Loader\r\n" +
  515. "Connection: close\r\n\r\n");
  516. Serial.println("request sent");
  517. while (client.connected())
  518. {
  519. String line = client.readStringUntil('\n');
  520. if (!connection_ok)
  521. {
  522. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  523. if (connection_ok) Serial.println(line);
  524. //if (!connection_ok) Serial.println(line);
  525. }
  526. if (!connection_ok) Serial.println(line);
  527. //Serial.println(line);
  528. if (line == "\r")
  529. {
  530. Serial.println("headers received");
  531. break;
  532. }
  533. }
  534. if (!connection_ok) return;
  535. // Parse BMP header
  536. if (read16(client) == 0x4D42) // BMP signature
  537. {
  538. uint32_t fileSize = read32(client);
  539. uint32_t creatorBytes = read32(client);
  540. uint32_t imageOffset = read32(client); // Start of image data
  541. uint32_t headerSize = read32(client);
  542. uint32_t width = read32(client);
  543. uint32_t height = read32(client);
  544. uint16_t planes = read16(client);
  545. uint16_t depth = read16(client); // bits per pixel
  546. uint32_t format = read32(client);
  547. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  548. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  549. {
  550. Serial.print("File size: "); Serial.println(fileSize);
  551. Serial.print("Image Offset: "); Serial.println(imageOffset);
  552. Serial.print("Header size: "); Serial.println(headerSize);
  553. Serial.print("Bit Depth: "); Serial.println(depth);
  554. Serial.print("Image size: ");
  555. Serial.print(width);
  556. Serial.print('x');
  557. Serial.println(height);
  558. // BMP rows are padded (if needed) to 4-byte boundary
  559. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  560. if (height < 0)
  561. {
  562. height = -height;
  563. flip = false;
  564. }
  565. uint16_t w = width;
  566. uint16_t h = height;
  567. if ((x + w - 1) >= display.width()) w = display.width() - x;
  568. if ((y + h - 1) >= display.height()) h = display.height() - y;
  569. if (w < max_row_width) // handle with direct drawing
  570. {
  571. valid = true;
  572. uint8_t bitmask = 0xFF;
  573. uint8_t bitshift = 8 - depth;
  574. uint16_t red, green, blue;
  575. bool whitish, colored;
  576. if (depth == 1) with_color = false;
  577. if (depth <= 8)
  578. {
  579. if (depth < 8) bitmask >>= depth;
  580. bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  581. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  582. {
  583. blue = client.read();
  584. green = client.read();
  585. red = client.read();
  586. client.read();
  587. bytes_read += 4;
  588. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  589. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  590. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  591. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  592. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  593. color_palette_buffer[pn / 8] |= colored << pn % 8;
  594. //Serial.print("0x00"); Serial.print(red, HEX); Serial.print(green, HEX); Serial.print(blue, HEX);
  595. //Serial.print(" : "); Serial.print(whitish); Serial.print(", "); Serial.println(colored);
  596. }
  597. }
  598. display.writeScreenBuffer();
  599. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  600. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  601. bytes_read += skip(client, rowPosition - bytes_read);
  602. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  603. {
  604. if (!connection_ok || !client.connected()) break;
  605. delay(1); // yield() to avoid WDT
  606. uint32_t in_remain = rowSize;
  607. uint32_t in_idx = 0;
  608. uint32_t in_bytes = 0;
  609. uint8_t in_byte = 0; // for depth <= 8
  610. uint8_t in_bits = 0; // for depth <= 8
  611. uint8_t out_byte = 0;
  612. uint8_t out_color_byte = 0;
  613. uint32_t out_idx = 0;
  614. for (uint16_t col = 0; col < w; col++) // for each pixel
  615. {
  616. yield();
  617. if (!connection_ok || !client.connected()) break;
  618. // Time to read more pixel data?
  619. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  620. {
  621. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  622. //if (get > client.available()) delay(200); // does improve? yes, if >= 200
  623. // there seems an issue with long downloads on ESP8266
  624. tryToWaitForAvailable(client, get);
  625. uint32_t got = client.read(input_buffer, get);
  626. while ((got < get) && connection_ok)
  627. {
  628. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  629. //if ((get - got) > client.available()) delay(200); // does improve? yes, if >= 200
  630. // there seems an issue with long downloads on ESP8266
  631. tryToWaitForAvailable(client, get - got);
  632. uint32_t gotmore = client.read(input_buffer + got, get - got);
  633. got += gotmore;
  634. connection_ok = gotmore > 0;
  635. }
  636. in_bytes = got;
  637. in_remain -= got;
  638. bytes_read += got;
  639. }
  640. if (!connection_ok)
  641. {
  642. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  643. break;
  644. }
  645. switch (depth)
  646. {
  647. case 24:
  648. blue = input_buffer[in_idx++];
  649. green = input_buffer[in_idx++];
  650. red = input_buffer[in_idx++];
  651. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  652. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  653. break;
  654. case 16:
  655. {
  656. uint8_t lsb = input_buffer[in_idx++];
  657. uint8_t msb = input_buffer[in_idx++];
  658. if (format == 0) // 555
  659. {
  660. blue = (lsb & 0x1F);
  661. green = ((msb & 0x03) << 3) | ((lsb & 0xE0) >> 5);
  662. red = (msb & 0x7C) >> 2;
  663. }
  664. else // 565
  665. {
  666. blue = (lsb & 0x1F);
  667. green = ((msb & 0x07) << 3) | ((lsb & 0xE0) >> 5);
  668. red = (msb & 0xF8) >> 3;
  669. }
  670. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  671. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  672. }
  673. break;
  674. case 1:
  675. case 4:
  676. case 8:
  677. {
  678. if (0 == in_bits)
  679. {
  680. in_byte = input_buffer[in_idx++];
  681. in_bits = 8;
  682. }
  683. uint16_t pn = (in_byte >> bitshift) & bitmask;
  684. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  685. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  686. in_byte <<= depth;
  687. in_bits -= depth;
  688. }
  689. break;
  690. }
  691. if (whitish)
  692. {
  693. out_byte |= 0x80 >> col % 8; // not black
  694. out_color_byte |= 0x80 >> col % 8; // not colored
  695. }
  696. else if (colored && with_color)
  697. {
  698. out_byte |= 0x80 >> col % 8; // not black
  699. }
  700. else
  701. {
  702. out_color_byte |= 0x80 >> col % 8; // not colored
  703. }
  704. if (7 == col % 8)
  705. {
  706. output_row_color_buffer[out_idx] = out_color_byte;
  707. output_row_mono_buffer[out_idx++] = out_byte;
  708. out_byte = 0;
  709. out_color_byte = 0;
  710. }
  711. } // end pixel
  712. int16_t yrow = y + (flip ? h - row - 1 : row);
  713. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  714. } // end line
  715. Serial.print("downloaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  716. Serial.print("bytes read "); Serial.println(bytes_read);
  717. display.refresh();
  718. }
  719. }
  720. }
  721. if (!valid)
  722. {
  723. Serial.println("bitmap format not handled.");
  724. }
  725. }
  726. uint16_t read16(WiFiClient& client)
  727. {
  728. // BMP data is stored little-endian, same as Arduino.
  729. uint16_t result;
  730. ((uint8_t *)&result)[0] = client.read(); // LSB
  731. ((uint8_t *)&result)[1] = client.read(); // MSB
  732. return result;
  733. }
  734. uint32_t read32(WiFiClient& client)
  735. {
  736. // BMP data is stored little-endian, same as Arduino.
  737. uint32_t result;
  738. ((uint8_t *)&result)[0] = client.read(); // LSB
  739. ((uint8_t *)&result)[1] = client.read();
  740. ((uint8_t *)&result)[2] = client.read();
  741. ((uint8_t *)&result)[3] = client.read(); // MSB
  742. return result;
  743. }
  744. uint32_t skip(WiFiClient& client, int32_t bytes)
  745. {
  746. int32_t remain = bytes;
  747. int16_t retries = 3;
  748. uint32_t chunk = 1024;
  749. if (chunk > sizeof(input_buffer)) chunk = sizeof(input_buffer);
  750. while (client.connected() && (remain > chunk))
  751. {
  752. // there seems an issue with long downloads on ESP8266
  753. tryToWaitForAvailable(client, chunk);
  754. uint32_t got = client.read(input_buffer, chunk);
  755. //Serial.print("skipped "); Serial.println(got);
  756. remain -= got;
  757. if ((0 == got) && (0 == --retries))
  758. {
  759. Serial.print("Error on skip, got 0, skipped "); Serial.print(bytes - remain); Serial.print(" of "); Serial.println(bytes);
  760. break; // don't hang forever (retries don't help)
  761. }
  762. }
  763. if (client.connected() && (remain > 0) && (remain <= chunk))
  764. {
  765. remain -= client.read(input_buffer, remain);
  766. }
  767. return bytes - remain; // bytes read and skipped
  768. }
  769. void tryToWaitForAvailable(WiFiClient& client, int32_t amount)
  770. {
  771. // this doesn't work as expected, but it helps for long downloads
  772. int32_t start = millis();
  773. for (int16_t t = 0, dly = 50; t < 20; t++, dly += 50)
  774. {
  775. if (client.available()) break; // read would not recover after having returned 0
  776. delay(dly);
  777. }
  778. for (int16_t t = 0, dly = 50; t < 3; t++, dly += 25)
  779. {
  780. if (amount <= client.available()) break;
  781. delay(dly);
  782. //Serial.print("available "); Serial.println(client.available()); // stays constant
  783. }
  784. int32_t elapsed = millis() - start;
  785. if (elapsed > 250)
  786. {
  787. Serial.print("waited for available "); Serial.print(millis() - start); Serial.println(" ms"); // usually 0 or 225ms
  788. }
  789. }