GxEPD2_WiFi_Example.ino 38 KB

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