GxEPD2_WiFi_Example.ino 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  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: the e-paper panels require 3.3V supply AND data lines!
  3. //
  4. // Display Library based on Demo Example from Good Display: http://www.e-paper-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. //
  14. // note that BMP bitmaps are drawn at physical position in physical orientation of the screen
  15. // Supporting Arduino Forum Topics:
  16. // Waveshare e-paper displays with SPI: http://forum.arduino.cc/index.php?topic=487007.0
  17. // Good Display ePaper for Arduino: https://forum.arduino.cc/index.php?topic=436411.0
  18. // see GxEPD2_wiring_examples.h for wiring suggestions and examples
  19. // base class GxEPD2_GFX can be used to pass references or pointers to the display instance as parameter, uses ~1.2k more code
  20. // enable or disable GxEPD2_GFX base class
  21. #define ENABLE_GxEPD2_GFX 0
  22. // uncomment next line to use class GFX of library GFX_Root instead of Adafruit_GFX
  23. //#include <GFX.h>
  24. // Note: if you use this with ENABLE_GxEPD2_GFX 1:
  25. // uncomment it in GxEPD2_GFX.h too, or add #include <GFX.h> before any #include <GxEPD2_GFX.h>
  26. #include <GxEPD2_BW.h>
  27. #include <GxEPD2_3C.h>
  28. #include <GxEPD2_7C.h>
  29. // select the display class (only one), matching the kind of display panel
  30. #define GxEPD2_DISPLAY_CLASS GxEPD2_BW
  31. //#define GxEPD2_DISPLAY_CLASS GxEPD2_3C
  32. //#define GxEPD2_DISPLAY_CLASS GxEPD2_7C
  33. /// select the display driver class (only one) for your panel
  34. //#define GxEPD2_DRIVER_CLASS GxEPD2_154 // GDEP015OC1 200x200, no longer available
  35. //#define GxEPD2_DRIVER_CLASS GxEPD2_154_D67 // GDEH0154D67 200x200
  36. //#define GxEPD2_DRIVER_CLASS GxEPD2_154_T8 // GDEW0154T8 152x152
  37. //#define GxEPD2_DRIVER_CLASS GxEPD2_154_M09 // GDEW0154M09 200x200
  38. //#define GxEPD2_DRIVER_CLASS GxEPD2_154_M10 // GDEW0154M10 152x152
  39. //#define GxEPD2_DRIVER_CLASS GxEPD2_213 // GDE0213B1 128x250, phased out
  40. //#define GxEPD2_DRIVER_CLASS GxEPD2_213_B72 // GDEH0213B72 128x250
  41. //#define GxEPD2_DRIVER_CLASS GxEPD2_213_B73 // GDEH0213B73 128x250
  42. //#define GxEPD2_DRIVER_CLASS GxEPD2_213_flex // GDEW0213I5F 128x250
  43. //#define GxEPD2_DRIVER_CLASS GxEPD2_213_M21 // GDEW0213M21 128x250
  44. //#define GxEPD2_DRIVER_CLASS GxEPD2_290 // GDEH029A1 128x296
  45. //#define GxEPD2_DRIVER_CLASS GxEPD2_290_T5 // GDEW029T5 128x296
  46. //#define GxEPD2_DRIVER_CLASS GxEPD2_290_T94 // GDEM029T94 128x296
  47. //#define GxEPD2_DRIVER_CLASS GxEPD2_290_M06 // GDEW029M06 128x296
  48. //#define GxEPD2_DRIVER_CLASS GxEPD2_260 // GDEW026T0 152x296
  49. //#define GxEPD2_DRIVER_CLASS GxEPD2_260_M01 // GDEW026M01 152x296
  50. //#define GxEPD2_DRIVER_CLASS GxEPD2_270 // GDEW027W3 176x264
  51. //#define GxEPD2_DRIVER_CLASS GxEPD2_371 // GDEW0371W7 240x416
  52. //#define GxEPD2_DRIVER_CLASS GxEPD2_420 // GDEW042T2 400x300
  53. //#define GxEPD2_DRIVER_CLASS GxEPD2_420_M01 // GDEW042M01 400x300
  54. //#define GxEPD2_DRIVER_CLASS GxEPD2_583 // GDEW0583T7 600x448
  55. //#define GxEPD2_DRIVER_CLASS GxEPD2_583_T8 // GDEW0583T8 648x480
  56. //#define GxEPD2_DRIVER_CLASS GxEPD2_750 // GDEW075T8 640x384
  57. //#define GxEPD2_DRIVER_CLASS GxEPD2_750_T7 // GDEW075T7 800x480
  58. // 3-color e-papers
  59. //#define GxEPD2_DRIVER_CLASS GxEPD2_154c // GDEW0154Z04 200x200, no longer available
  60. //#define GxEPD2_DRIVER_CLASS GxEPD2_154_Z90c // GDEH0154Z90 200x200
  61. //#define GxEPD2_DRIVER_CLASS GxEPD2_213c // GDEW0213Z16 104x212
  62. //#define GxEPD2_DRIVER_CLASS GxEPD2_290c // GDEW029Z10 128x296
  63. //#define GxEPD2_DRIVER_CLASS GxEPD2_270c // GDEW027C44 176x264
  64. //#define GxEPD2_DRIVER_CLASS GxEPD2_420c // GDEW042Z15 400x300
  65. //#define GxEPD2_DRIVER_CLASS GxEPD2_583c // GDEW0583Z21 600x448
  66. //#define GxEPD2_DRIVER_CLASS GxEPD2_750c // GDEW075Z09 600x384
  67. //#define GxEPD2_DRIVER_CLASS GxEPD2_750c_Z08 // GDEW075Z08 800x480
  68. //#define GxEPD2_DRIVER_CLASS GxEPD2_750c_Z90 // GDEH075Z90 880x528
  69. //#define GxEPD2_DRIVER_CLASS GxEPD2_1248 // GDEW1248T3 1303x984
  70. // 7-color e-paper
  71. //#define GxEPD2_DRIVER_CLASS GxEPD2_565c // Waveshare 5.65" 7-color (3C graphics)
  72. // grey levels parallel IF e-papers on Waveshare e-Paper IT8951 Driver HAT
  73. //#define GxEPD2_DRIVER_CLASS GxEPD2_it60 // ED060SCT 800x600
  74. //#define GxEPD2_DRIVER_CLASS GxEPD2_it60_1448x1072 // ED060KC1 1448x1072
  75. // SS is usually used for CS. define here for easy change
  76. #ifndef EPD_CS
  77. #define EPD_CS SS
  78. #endif
  79. #if defined (ESP8266)
  80. #define MAX_DISPLAY_BUFFER_SIZE (81920ul-34000ul-34000ul) // ~34000 base use, WiFiClientSecure seems to need about 34k more to work
  81. #if GxEPD2_DISPLAY_CLASS == GxEPD2_BW
  82. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= MAX_DISPLAY_BUFFER_SIZE / (EPD::WIDTH / 8) ? EPD::HEIGHT : MAX_DISPLAY_BUFFER_SIZE / (EPD::WIDTH / 8))
  83. #elif GxEPD2_DISPLAY_CLASS == GxEPD2_3C
  84. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= (MAX_DISPLAY_BUFFER_SIZE / 2) / (EPD::WIDTH / 8) ? EPD::HEIGHT : (MAX_DISPLAY_BUFFER_SIZE / 2) / (EPD::WIDTH / 8))
  85. #elif GxEPD2_DISPLAY_CLASS == GxEPD2_7C
  86. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= (MAX_DISPLAY_BUFFER_SIZE) / (EPD::WIDTH / 2) ? EPD::HEIGHT : (MAX_DISPLAY_BUFFER_SIZE) / (EPD::WIDTH / 2))
  87. #endif
  88. // adapt the constructor parameters to your wiring
  89. GxEPD2_DISPLAY_CLASS<GxEPD2_DRIVER_CLASS, MAX_HEIGHT(GxEPD2_DRIVER_CLASS)> display(GxEPD2_DRIVER_CLASS(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  90. // mapping of Waveshare e-Paper ESP8266 Driver Board, new version
  91. //GxEPD2_DISPLAY_CLASS<GxEPD2_DRIVER_CLASS, MAX_HEIGHT(GxEPD2_DRIVER_CLASS)> display(GxEPD2_DRIVER_CLASS(/*CS=15*/ EPD_CS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  92. // mapping of Waveshare e-Paper ESP8266 Driver Board, old version
  93. //GxEPD2_DISPLAY_CLASS<GxEPD2_DRIVER_CLASS, MAX_HEIGHT(GxEPD2_DRIVER_CLASS)> display(GxEPD2_DRIVER_CLASS(/*CS=15*/ EPD_CS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  94. #endif
  95. #if defined(ESP32)
  96. #define MAX_DISPLAY_BUFFER_SIZE 65536ul // e.g.
  97. #if GxEPD2_DISPLAY_CLASS == GxEPD2_BW
  98. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= MAX_DISPLAY_BUFFER_SIZE / (EPD::WIDTH / 8) ? EPD::HEIGHT : MAX_DISPLAY_BUFFER_SIZE / (EPD::WIDTH / 8))
  99. #elif GxEPD2_DISPLAY_CLASS == GxEPD2_3C
  100. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= (MAX_DISPLAY_BUFFER_SIZE / 2) / (EPD::WIDTH / 8) ? EPD::HEIGHT : (MAX_DISPLAY_BUFFER_SIZE / 2) / (EPD::WIDTH / 8))
  101. #elif GxEPD2_DISPLAY_CLASS == GxEPD2_7C
  102. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= (MAX_DISPLAY_BUFFER_SIZE) / (EPD::WIDTH / 2) ? EPD::HEIGHT : (MAX_DISPLAY_BUFFER_SIZE) / (EPD::WIDTH / 2))
  103. #endif
  104. // adapt the constructor parameters to your wiring
  105. #if (GxEPD2_DRIVER_CLASS != GxEPD2_1248)
  106. #if defined(ARDUINO_LOLIN_D32_PRO)
  107. GxEPD2_DISPLAY_CLASS<GxEPD2_DRIVER_CLASS, MAX_HEIGHT(GxEPD2_DRIVER_CLASS)> display(GxEPD2_DRIVER_CLASS(/*CS=5*/ EPD_CS, /*DC=*/ 0, /*RST=*/ 2, /*BUSY=*/ 15));
  108. #elif defined(ARDUINO_ESP32_DEV) // e.g. TTGO T8 ESP32-WROVER
  109. GxEPD2_DISPLAY_CLASS<GxEPD2_DRIVER_CLASS, MAX_HEIGHT(GxEPD2_DRIVER_CLASS)> display(GxEPD2_DRIVER_CLASS(/*CS=5*/ EPD_CS, /*DC=*/ 2, /*RST=*/ 0, /*BUSY=*/ 4));
  110. #else
  111. GxEPD2_DISPLAY_CLASS<GxEPD2_DRIVER_CLASS, MAX_HEIGHT(GxEPD2_DRIVER_CLASS)> display(GxEPD2_DRIVER_CLASS(/*CS=5*/ EPD_CS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  112. #endif
  113. #else // GxEPD2_1248
  114. // Waveshare 12.48 b/w SPI display board and frame or Good Display 12.48 b/w panel GDEW1248T3
  115. // general constructor for use with all parameters, e.g. for Waveshare ESP32 driver board mounted on connection board
  116. GxEPD2_BW < GxEPD2_1248, GxEPD2_1248::HEIGHT / 4 > display(GxEPD2_1248(/*sck=*/ 13, /*miso=*/ 12, /*mosi=*/ 14,
  117. /*cs_m1=*/ 23, /*cs_s1=*/ 22, /*cs_m2=*/ 16, /*cs_s2=*/ 19,
  118. /*dc1=*/ 25, /*dc2=*/ 17, /*rst1=*/ 33, /*rst2=*/ 5,
  119. /*busy_m1=*/ 32, /*busy_s1=*/ 26, /*busy_m2=*/ 18, /*busy_s2=*/ 4));
  120. #endif
  121. #endif
  122. #if defined (ESP8266)
  123. #include <ESP8266WiFi.h>
  124. #define USE_BearSSL true
  125. #endif
  126. #include <WiFiClient.h>
  127. #include <WiFiClientSecure.h>
  128. const char* ssid = "........";
  129. const char* password = "........";
  130. const int httpPort = 80;
  131. const int httpsPort = 443;
  132. const char* fp_api_github_com = "df b2 29 c6 a6 38 1a 59 9d c9 ad 92 2d 26 f5 3c 83 8f a5 87"; // as of 25.11.2020
  133. const char* fp_github_com = "5f 3f 7a c2 56 9f 50 a4 66 76 47 c6 a1 8c a0 07 aa ed bb 8e"; // as of 25.11.2020
  134. const char* fp_rawcontent = "70 94 de dd e6 c4 69 48 3a 92 70 a1 48 56 78 2d 18 64 e0 b7"; // as of 25.11.2020
  135. // how to find the certificate was not easy. finally I found it using Mozilla Firefox.
  136. // opened one of the bitmaps, e.g. https://raw.githubusercontent.com/ZinggJM/GxEPD2/master/extras/bitmaps/logo200x200.bmp
  137. // clicked the lock symbol, Connection secure clicked >, show connection details, clicked More Information, clicked View Certificate, clicked Download PEM (chain),
  138. // selected Open with Notepad. Copied the middle certificate and editted to the following format:
  139. // the number of characters per line is of no importance, but the 3 \n are needed
  140. const char* certificate_rawcontent =
  141. "-----BEGIN CERTIFICATE-----\n"
  142. "MIIEsTCCA5mgAwIBAgIQBOHnpNxc8vNtwCtCuF0VnzANBgkqhkiG9w0BAQsFADBsMQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGln"
  143. "aUNlcnQgSW5jMRkwFwYDVQQLExB3d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5jZSBFViBS"
  144. "b290IENBMB4XDTEzMTAyMjEyMDAwMFoXDTI4MTAyMjEyMDAwMFowcDELMAkGA1UEBhMCVVMxFTATBgNVBAoTDERpZ2lDZXJ0IElu"
  145. "YzEZMBcGA1UECxMQd3d3LmRpZ2ljZXJ0LmNvbTEvMC0GA1UEAxMmRGlnaUNlcnQgU0hBMiBIaWdoIEFzc3VyYW5jZSBTZXJ2ZXIg"
  146. "Q0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC24C/CJAbIbQRf1+8KZAayfSImZRauQkCbztyfn3YHPsMwVYcZuU+U"
  147. "DlqUH1VWtMICKq/QmO4LQNfE0DtyyBSe75CxEamu0si4QzrZCwvV1ZX1QK/IHe1NnF9Xt4ZQaJn1itrSxwUfqJfJ3KSxgoQtxq2l"
  148. "nMcZgqaFD15EWCo3j/018QsIJzJa9buLnqS9UdAn4t07QjOjBSjEuyjMmqwrIw14xnvmXnG3Sj4I+4G3FhahnSMSTeXXkgisdaSc"
  149. "us0Xsh5ENWV/UyU50RwKmmMbGZJ0aAo3wsJSSMs5WqK24V3B3aAguCGikyZvFEohQcftbZvySC/zA/WiaJJTL17jAgMBAAGjggFJ"
  150. "MIIBRTASBgNVHRMBAf8ECDAGAQH/AgEAMA4GA1UdDwEB/wQEAwIBhjAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwNAYI"
  151. "KwYBBQUHAQEEKDAmMCQGCCsGAQUFBzABhhhodHRwOi8vb2NzcC5kaWdpY2VydC5jb20wSwYDVR0fBEQwQjBAoD6gPIY6aHR0cDov"
  152. "L2NybDQuZGlnaWNlcnQuY29tL0RpZ2lDZXJ0SGlnaEFzc3VyYW5jZUVWUm9vdENBLmNybDA9BgNVHSAENjA0MDIGBFUdIAAwKjAo"
  153. "BggrBgEFBQcCARYcaHR0cHM6Ly93d3cuZGlnaWNlcnQuY29tL0NQUzAdBgNVHQ4EFgQUUWj/kK8CB3U8zNllZGKiErhZcjswHwYD"
  154. "VR0jBBgwFoAUsT7DaQP4v0cB1JgmGggC72NkK8MwDQYJKoZIhvcNAQELBQADggEBABiKlYkD5m3fXPwdaOpKj4PWUS+Na0QWnqxj"
  155. "9dJubISZi6qBcYRb7TROsLd5kinMLYBq8I4g4Xmk/gNHE+r1hspZcX30BJZr01lYPf7TMSVcGDiEo+afgv2MW5gxTs14nhr9hctJ"
  156. "qvIni5ly/D6q1UEL2tU2ob8cbkdJf17ZSHwD2f2LSaCYJkJA69aSEaRkCldUxPUd1gJea6zuxICaEnL6VpPX/78whQYwvwt/Tv9X"
  157. "BZ0k7YXDK/umdaisLRbvfXknsuvCnQsH6qqF0wGjIChBWUMo0oHjqvbsezt3tkBigAVBRQHvFwY+3sAzm2fTYS5yh+Rp/BIAV0AecPUeybQ=\n"
  158. "-----END CERTIFICATE-----\n";
  159. const char* host_rawcontent = "raw.githubusercontent.com";
  160. const char* path_rawcontent = "/ZinggJM/GxEPD2/master/extras/bitmaps/";
  161. const char* path_prenticedavid = "/prenticedavid/MCUFRIEND_kbv/master/extras/bitmaps/";
  162. const char* path_waveshare_c = "/waveshare/e-Paper/master/RaspberryPi_JetsonNano/c/pic/";
  163. const char* path_waveshare_py = "/waveshare/e-Paper/master/RaspberryPi_JetsonNano/python/pic/";
  164. // note that BMP bitmaps are drawn at physical position in physical orientation of the screen
  165. void showBitmapFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color = true);
  166. 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,
  167. const char* certificate = certificate_rawcontent);
  168. // draws BMP bitmap according to set orientation
  169. void showBitmapFrom_HTTP_Buffered(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color = true);
  170. 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,
  171. const char* certificate = certificate_rawcontent);
  172. void setup()
  173. {
  174. Serial.begin(115200);
  175. Serial.println();
  176. Serial.println("GxEPD2_WiFi_Example");
  177. display.init(115200);
  178. #ifdef REMAP_SPI_FOR_WAVESHARE_ESP32_DRIVER_BOARD
  179. SPI.end(); // release standard SPI pins, e.g. SCK(18), MISO(19), MOSI(23), SS(5)
  180. //SPI: void begin(int8_t sck=-1, int8_t miso=-1, int8_t mosi=-1, int8_t ss=-1);
  181. SPI.begin(13, 12, 14, 15); // map and init SPI pins SCK(13), MISO(12), MOSI(14), SS(15)
  182. #endif
  183. #ifdef RE_INIT_NEEDED
  184. WiFi.persistent(true);
  185. WiFi.mode(WIFI_STA); // switch off AP
  186. WiFi.setAutoConnect(true);
  187. WiFi.setAutoReconnect(true);
  188. WiFi.disconnect();
  189. #endif
  190. if (!WiFi.getAutoConnect() || ( WiFi.getMode() != WIFI_STA) || ((WiFi.SSID() != ssid) && String(ssid) != "........"))
  191. {
  192. Serial.println();
  193. Serial.print("WiFi.getAutoConnect()=");
  194. Serial.println(WiFi.getAutoConnect());
  195. Serial.print("WiFi.SSID()=");
  196. Serial.println(WiFi.SSID());
  197. WiFi.mode(WIFI_STA); // switch off AP
  198. Serial.print("Connecting to ");
  199. Serial.println(ssid);
  200. WiFi.begin(ssid, password);
  201. }
  202. int ConnectTimeout = 30; // 15 seconds
  203. while (WiFi.status() != WL_CONNECTED)
  204. {
  205. delay(500);
  206. Serial.print(".");
  207. Serial.print(WiFi.status());
  208. if (--ConnectTimeout <= 0)
  209. {
  210. Serial.println();
  211. Serial.println("WiFi connect timeout");
  212. return;
  213. }
  214. }
  215. Serial.println();
  216. Serial.println("WiFi connected");
  217. // Print the IP address
  218. Serial.println(WiFi.localIP());
  219. if ((display.epd2.panel == GxEPD2::GDEW0154Z04) || (display.epd2.panel == GxEPD2::ACeP565) || false)
  220. {
  221. drawBitmapsBuffered_200x200();
  222. drawBitmapsBuffered_other();
  223. drawBitmapsBuffered_7C();
  224. }
  225. else
  226. {
  227. drawBitmaps_200x200();
  228. drawBitmaps_other();
  229. }
  230. //drawBitmaps_test();
  231. //drawBitmapsBuffered_test();
  232. Serial.println("GxEPD2_WiFi_Example done");
  233. }
  234. void loop(void)
  235. {
  236. }
  237. void drawBitmaps_200x200()
  238. {
  239. int16_t x = (display.width() - 200) / 2;
  240. int16_t y = (display.height() - 200) / 2;
  241. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "logo200x200.bmp", fp_rawcontent, x, y);
  242. delay(2000);
  243. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "first200x200.bmp", fp_rawcontent, x, y);
  244. delay(2000);
  245. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "second200x200.bmp", fp_rawcontent, x, y);
  246. delay(2000);
  247. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "third200x200.bmp", fp_rawcontent, x, y);
  248. delay(2000);
  249. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "fourth200x200.bmp", fp_rawcontent, x, y);
  250. delay(2000);
  251. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "fifth200x200.bmp", fp_rawcontent, x, y);
  252. delay(2000);
  253. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "sixth200x200.bmp", fp_rawcontent, x, y);
  254. delay(2000);
  255. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "seventh200x200.bmp", fp_rawcontent, x, y);
  256. delay(2000);
  257. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "eighth200x200.bmp", fp_rawcontent, x, y);
  258. delay(2000);
  259. }
  260. void drawBitmaps_other()
  261. {
  262. int16_t w2 = display.width() / 2;
  263. int16_t h2 = display.height() / 2;
  264. showBitmapFrom_HTTP("www.packescape.com", "/img/assets/", "IniciMenusTV2.bmp", w2 - 200, h2 - 150, false);
  265. delay(2000);
  266. showBitmapFrom_HTTP("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", w2 - 50, h2 - 50, false);
  267. delay(2000);
  268. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_1.bmp", fp_rawcontent, w2 - 100, h2 - 160);
  269. delay(2000);
  270. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  271. delay(2000);
  272. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "marilyn_240x240x8.bmp", fp_rawcontent, w2 - 120, h2 - 120);
  273. delay(2000);
  274. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "miniwoof.bmp", fp_rawcontent, w2 - 60, h2 - 80);
  275. delay(2000);
  276. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "test.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  277. delay(2000);
  278. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger.bmp", fp_rawcontent, w2 - 160, h2 - 120);
  279. delay(2000);
  280. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_178x160x4.bmp", fp_rawcontent, w2 - 89, h2 - 80);
  281. delay(2000);
  282. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_240x317x4.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  283. delay(2000);
  284. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_320x200x24.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  285. delay(2000);
  286. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger16T.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  287. delay(2000);
  288. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "woof.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  289. delay(2000);
  290. }
  291. void drawBitmaps_test()
  292. {
  293. int16_t w2 = display.width() / 2;
  294. int16_t h2 = display.height() / 2;
  295. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  296. delay(2000);
  297. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "output5.bmp", fp_rawcontent, 0, 0);
  298. delay(2000);
  299. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "output6.bmp", fp_rawcontent, 0, 0);
  300. delay(2000);
  301. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_1.bmp", fp_rawcontent, 0, 0);
  302. delay(2000);
  303. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_4.bmp", fp_rawcontent, 0, 0);
  304. delay(2000);
  305. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_8.bmp", fp_rawcontent, 0, 0);
  306. delay(2000);
  307. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_11.bmp", fp_rawcontent, 0, 0);
  308. delay(2000);
  309. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_44.bmp", fp_rawcontent, 0, 0);
  310. delay(2000);
  311. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_88.bmp", fp_rawcontent, 0, 0);
  312. delay(2000);
  313. //showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "liberato640x384.bmp", fp_rawcontent, 0, 0);
  314. //delay(2000);
  315. }
  316. void drawBitmapsBuffered_200x200()
  317. {
  318. int16_t x = (display.width() - 200) / 2;
  319. int16_t y = (display.height() - 200) / 2;
  320. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "logo200x200.bmp", fp_rawcontent, x, y);
  321. delay(2000);
  322. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "first200x200.bmp", fp_rawcontent, x, y);
  323. delay(2000);
  324. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "second200x200.bmp", fp_rawcontent, x, y);
  325. delay(2000);
  326. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "third200x200.bmp", fp_rawcontent, x, y);
  327. delay(2000);
  328. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "fourth200x200.bmp", fp_rawcontent, x, y);
  329. delay(2000);
  330. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "fifth200x200.bmp", fp_rawcontent, x, y);
  331. delay(2000);
  332. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "sixth200x200.bmp", fp_rawcontent, x, y);
  333. delay(2000);
  334. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "seventh200x200.bmp", fp_rawcontent, x, y);
  335. delay(2000);
  336. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "eighth200x200.bmp", fp_rawcontent, x, y);
  337. delay(2000);
  338. }
  339. void drawBitmapsBuffered_other()
  340. {
  341. int16_t w2 = display.width() / 2;
  342. int16_t h2 = display.height() / 2;
  343. showBitmapFrom_HTTP_Buffered("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", w2 - 50, h2 - 50, false);
  344. delay(2000);
  345. //showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_1.bmp", fp_rawcontent, w2 - 100, h2 - 160);
  346. delay(2000);
  347. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  348. delay(2000);
  349. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "marilyn_240x240x8.bmp", fp_rawcontent, w2 - 120, h2 - 120);
  350. delay(2000);
  351. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "miniwoof.bmp", fp_rawcontent, w2 - 60, h2 - 80);
  352. delay(2000);
  353. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "test.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  354. delay(2000);
  355. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger.bmp", fp_rawcontent, w2 - 160, h2 - 120);
  356. delay(2000);
  357. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_178x160x4.bmp", fp_rawcontent, w2 - 89, h2 - 80);
  358. delay(2000);
  359. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_240x317x4.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  360. delay(2000);
  361. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_320x200x24.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  362. delay(2000);
  363. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger16T.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  364. delay(2000);
  365. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "woof.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  366. delay(2000);
  367. }
  368. void drawBitmapsBuffered_7C()
  369. {
  370. if (display.epd2.panel == GxEPD2::ACeP565)
  371. {
  372. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f.bmp", fp_rawcontent, 0, 0);
  373. delay(2000);
  374. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f2.bmp", fp_rawcontent, 0, 0);
  375. delay(2000);
  376. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f3.bmp", fp_rawcontent, 0, 0);
  377. delay(2000);
  378. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f4.bmp", fp_rawcontent, 0, 0);
  379. delay(2000);
  380. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_py, "5in65f4.bmp", fp_rawcontent, 0, 0);
  381. delay(2000);
  382. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_py, "N-Color1.bmp", fp_rawcontent, 0, 0);
  383. delay(2000);
  384. }
  385. }
  386. void drawBitmapsBuffered_test()
  387. {
  388. int16_t w2 = display.width() / 2;
  389. int16_t h2 = display.height() / 2;
  390. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  391. delay(2000);
  392. }
  393. static const uint16_t input_buffer_pixels = 800; // may affect performance
  394. static const uint16_t max_row_width = 1448; // for up to 6" display 1448x1072
  395. static const uint16_t max_palette_pixels = 256; // for depth <= 8
  396. uint8_t input_buffer[3 * input_buffer_pixels]; // up to depth 24
  397. uint8_t output_row_mono_buffer[max_row_width / 8]; // buffer for at least one row of b/w bits
  398. uint8_t output_row_color_buffer[max_row_width / 8]; // buffer for at least one row of color bits
  399. uint8_t mono_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 b/w
  400. uint8_t color_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 c/w
  401. uint16_t rgb_palette_buffer[max_palette_pixels]; // palette buffer for depth <= 8 for buffered graphics, needed for 7-color display
  402. void showBitmapFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  403. {
  404. WiFiClient client;
  405. bool connection_ok = false;
  406. bool valid = false; // valid format to be handled
  407. bool flip = true; // bitmap is stored bottom-to-top
  408. uint32_t startTime = millis();
  409. if ((x >= display.epd2.WIDTH) || (y >= display.epd2.HEIGHT)) return;
  410. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  411. Serial.print("connecting to "); Serial.println(host);
  412. if (!client.connect(host, httpPort))
  413. {
  414. Serial.println("connection failed");
  415. return;
  416. }
  417. Serial.print("requesting URL: ");
  418. Serial.println(String("http://") + host + path + filename);
  419. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  420. "Host: " + host + "\r\n" +
  421. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  422. "Connection: close\r\n\r\n");
  423. Serial.println("request sent");
  424. while (client.connected())
  425. {
  426. String line = client.readStringUntil('\n');
  427. if (!connection_ok)
  428. {
  429. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  430. if (connection_ok) Serial.println(line);
  431. //if (!connection_ok) Serial.println(line);
  432. }
  433. if (!connection_ok) Serial.println(line);
  434. //Serial.println(line);
  435. if (line == "\r")
  436. {
  437. Serial.println("headers received");
  438. break;
  439. }
  440. }
  441. if (!connection_ok) return;
  442. // Parse BMP header
  443. if (read16(client) == 0x4D42) // BMP signature
  444. {
  445. uint32_t fileSize = read32(client);
  446. uint32_t creatorBytes = read32(client);
  447. uint32_t imageOffset = read32(client); // Start of image data
  448. uint32_t headerSize = read32(client);
  449. uint32_t width = read32(client);
  450. uint32_t height = read32(client);
  451. uint16_t planes = read16(client);
  452. uint16_t depth = read16(client); // bits per pixel
  453. uint32_t format = read32(client);
  454. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  455. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  456. {
  457. Serial.print("File size: "); Serial.println(fileSize);
  458. Serial.print("Image Offset: "); Serial.println(imageOffset);
  459. Serial.print("Header size: "); Serial.println(headerSize);
  460. Serial.print("Bit Depth: "); Serial.println(depth);
  461. Serial.print("Image size: ");
  462. Serial.print(width);
  463. Serial.print('x');
  464. Serial.println(height);
  465. // BMP rows are padded (if needed) to 4-byte boundary
  466. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  467. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  468. if (height < 0)
  469. {
  470. height = -height;
  471. flip = false;
  472. }
  473. uint16_t w = width;
  474. uint16_t h = height;
  475. if ((x + w - 1) >= display.epd2.WIDTH) w = display.epd2.WIDTH - x;
  476. if ((y + h - 1) >= display.epd2.HEIGHT) h = display.epd2.HEIGHT - y;
  477. if (w <= max_row_width) // handle with direct drawing
  478. {
  479. valid = true;
  480. uint8_t bitmask = 0xFF;
  481. uint8_t bitshift = 8 - depth;
  482. uint16_t red, green, blue;
  483. bool whitish, colored;
  484. if (depth == 1) with_color = false;
  485. if (depth <= 8)
  486. {
  487. if (depth < 8) bitmask >>= depth;
  488. //bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  489. bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
  490. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  491. {
  492. blue = client.read();
  493. green = client.read();
  494. red = client.read();
  495. client.read();
  496. bytes_read += 4;
  497. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  498. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  499. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  500. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  501. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  502. color_palette_buffer[pn / 8] |= colored << pn % 8;
  503. }
  504. }
  505. display.clearScreen();
  506. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  507. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  508. bytes_read += skip(client, rowPosition - bytes_read);
  509. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  510. {
  511. if (!connection_ok || !(client.connected() || client.available())) break;
  512. delay(1); // yield() to avoid WDT
  513. uint32_t in_remain = rowSize;
  514. uint32_t in_idx = 0;
  515. uint32_t in_bytes = 0;
  516. uint8_t in_byte = 0; // for depth <= 8
  517. uint8_t in_bits = 0; // for depth <= 8
  518. uint8_t out_byte = 0xFF; // white (for w%8!=0 border)
  519. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 border)
  520. uint32_t out_idx = 0;
  521. for (uint16_t col = 0; col < w; col++) // for each pixel
  522. {
  523. yield();
  524. if (!connection_ok || !(client.connected() || client.available())) break;
  525. // Time to read more pixel data?
  526. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  527. {
  528. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  529. uint32_t got = read8n(client, input_buffer, get);
  530. while ((got < get) && connection_ok)
  531. {
  532. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  533. uint32_t gotmore = read8n(client, input_buffer + got, get - got);
  534. got += gotmore;
  535. connection_ok = gotmore > 0;
  536. }
  537. in_bytes = got;
  538. in_remain -= got;
  539. bytes_read += got;
  540. }
  541. if (!connection_ok)
  542. {
  543. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  544. break;
  545. }
  546. switch (depth)
  547. {
  548. case 24:
  549. blue = input_buffer[in_idx++];
  550. green = input_buffer[in_idx++];
  551. red = input_buffer[in_idx++];
  552. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  553. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  554. break;
  555. case 16:
  556. {
  557. uint8_t lsb = input_buffer[in_idx++];
  558. uint8_t msb = input_buffer[in_idx++];
  559. if (format == 0) // 555
  560. {
  561. blue = (lsb & 0x1F) << 3;
  562. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  563. red = (msb & 0x7C) << 1;
  564. }
  565. else // 565
  566. {
  567. blue = (lsb & 0x1F) << 3;
  568. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  569. red = (msb & 0xF8);
  570. }
  571. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  572. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  573. }
  574. break;
  575. case 1:
  576. case 4:
  577. case 8:
  578. {
  579. if (0 == in_bits)
  580. {
  581. in_byte = input_buffer[in_idx++];
  582. in_bits = 8;
  583. }
  584. uint16_t pn = (in_byte >> bitshift) & bitmask;
  585. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  586. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  587. in_byte <<= depth;
  588. in_bits -= depth;
  589. }
  590. break;
  591. }
  592. if (whitish)
  593. {
  594. // keep white
  595. }
  596. else if (colored && with_color)
  597. {
  598. out_color_byte &= ~(0x80 >> col % 8); // colored
  599. }
  600. else
  601. {
  602. out_byte &= ~(0x80 >> col % 8); // black
  603. }
  604. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 border)
  605. {
  606. output_row_color_buffer[out_idx] = out_color_byte;
  607. output_row_mono_buffer[out_idx++] = out_byte;
  608. out_byte = 0xFF; // white (for w%8!=0 border)
  609. out_color_byte = 0xFF; // white (for w%8!=0 border)
  610. }
  611. } // end pixel
  612. int16_t yrow = y + (flip ? h - row - 1 : row);
  613. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  614. } // end line
  615. Serial.print("downloaded in ");
  616. Serial.print(millis() - startTime);
  617. Serial.println(" ms");
  618. display.refresh();
  619. }
  620. Serial.print("bytes read "); Serial.println(bytes_read);
  621. }
  622. }
  623. client.stop();
  624. if (!valid)
  625. {
  626. Serial.println("bitmap format not handled.");
  627. }
  628. }
  629. void drawBitmapFrom_HTTP_ToBuffer(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  630. {
  631. WiFiClient client;
  632. bool connection_ok = false;
  633. bool valid = false; // valid format to be handled
  634. bool flip = true; // bitmap is stored bottom-to-top
  635. bool has_multicolors = display.epd2.panel == GxEPD2::ACeP565;
  636. uint32_t startTime = millis();
  637. if ((x >= display.width()) || (y >= display.height())) return;
  638. display.fillScreen(GxEPD_WHITE);
  639. Serial.print("connecting to "); Serial.println(host);
  640. if (!client.connect(host, httpPort))
  641. {
  642. Serial.println("connection failed");
  643. return;
  644. }
  645. Serial.print("requesting URL: ");
  646. Serial.println(String("http://") + host + path + filename);
  647. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  648. "Host: " + host + "\r\n" +
  649. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  650. "Connection: close\r\n\r\n");
  651. Serial.println("request sent");
  652. while (client.connected())
  653. {
  654. String line = client.readStringUntil('\n');
  655. if (!connection_ok)
  656. {
  657. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  658. if (connection_ok) Serial.println(line);
  659. //if (!connection_ok) Serial.println(line);
  660. }
  661. if (!connection_ok) Serial.println(line);
  662. //Serial.println(line);
  663. if (line == "\r")
  664. {
  665. Serial.println("headers received");
  666. break;
  667. }
  668. }
  669. if (!connection_ok) return;
  670. // Parse BMP header
  671. if (read16(client) == 0x4D42) // BMP signature
  672. {
  673. uint32_t fileSize = read32(client);
  674. uint32_t creatorBytes = read32(client);
  675. uint32_t imageOffset = read32(client); // Start of image data
  676. uint32_t headerSize = read32(client);
  677. uint32_t width = read32(client);
  678. uint32_t height = read32(client);
  679. uint16_t planes = read16(client);
  680. uint16_t depth = read16(client); // bits per pixel
  681. uint32_t format = read32(client);
  682. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  683. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  684. {
  685. Serial.print("File size: "); Serial.println(fileSize);
  686. Serial.print("Image Offset: "); Serial.println(imageOffset);
  687. Serial.print("Header size: "); Serial.println(headerSize);
  688. Serial.print("Bit Depth: "); Serial.println(depth);
  689. Serial.print("Image size: ");
  690. Serial.print(width);
  691. Serial.print('x');
  692. Serial.println(height);
  693. // BMP rows are padded (if needed) to 4-byte boundary
  694. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  695. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  696. if (height < 0)
  697. {
  698. height = -height;
  699. flip = false;
  700. }
  701. uint16_t w = width;
  702. uint16_t h = height;
  703. if ((x + w - 1) >= display.width()) w = display.width() - x;
  704. if ((y + h - 1) >= display.height()) h = display.height() - y;
  705. //if (w <= max_row_width) // handle with direct drawing
  706. {
  707. valid = true;
  708. uint8_t bitmask = 0xFF;
  709. uint8_t bitshift = 8 - depth;
  710. uint16_t red, green, blue;
  711. bool whitish, colored;
  712. if (depth == 1) with_color = false;
  713. if (depth <= 8)
  714. {
  715. if (depth < 8) bitmask >>= depth;
  716. //bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  717. bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
  718. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  719. {
  720. blue = client.read();
  721. green = client.read();
  722. red = client.read();
  723. client.read();
  724. bytes_read += 4;
  725. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  726. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  727. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  728. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  729. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  730. color_palette_buffer[pn / 8] |= colored << pn % 8;
  731. //Serial.print("0x00"); Serial.print(red, HEX); Serial.print(green, HEX); Serial.print(blue, HEX);
  732. //Serial.print(" : "); Serial.print(whitish); Serial.print(", "); Serial.println(colored);
  733. rgb_palette_buffer[pn] = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  734. }
  735. }
  736. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  737. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  738. bytes_read += skip(client, rowPosition - bytes_read);
  739. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  740. {
  741. if (!connection_ok || !(client.connected() || client.available())) break;
  742. delay(1); // yield() to avoid WDT
  743. uint32_t in_remain = rowSize;
  744. uint32_t in_idx = 0;
  745. uint32_t in_bytes = 0;
  746. uint8_t in_byte = 0; // for depth <= 8
  747. uint8_t in_bits = 0; // for depth <= 8
  748. uint16_t color = GxEPD_WHITE;
  749. for (uint16_t col = 0; col < w; col++) // for each pixel
  750. {
  751. yield();
  752. if (!connection_ok || !(client.connected() || client.available())) break;
  753. // Time to read more pixel data?
  754. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  755. {
  756. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  757. uint32_t got = read8n(client, input_buffer, get);
  758. while ((got < get) && connection_ok)
  759. {
  760. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  761. uint32_t gotmore = read8n(client, input_buffer + got, get - got);
  762. got += gotmore;
  763. connection_ok = gotmore > 0;
  764. }
  765. in_bytes = got;
  766. in_remain -= got;
  767. bytes_read += got;
  768. }
  769. if (!connection_ok)
  770. {
  771. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  772. break;
  773. }
  774. switch (depth)
  775. {
  776. case 24:
  777. blue = input_buffer[in_idx++];
  778. green = input_buffer[in_idx++];
  779. red = input_buffer[in_idx++];
  780. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  781. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  782. color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  783. break;
  784. case 16:
  785. {
  786. uint8_t lsb = input_buffer[in_idx++];
  787. uint8_t msb = input_buffer[in_idx++];
  788. if (format == 0) // 555
  789. {
  790. blue = (lsb & 0x1F) << 3;
  791. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  792. red = (msb & 0x7C) << 1;
  793. color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  794. }
  795. else // 565
  796. {
  797. blue = (lsb & 0x1F) << 3;
  798. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  799. red = (msb & 0xF8);
  800. color = (msb << 8) | lsb;
  801. }
  802. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  803. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  804. }
  805. break;
  806. case 1:
  807. case 4:
  808. case 8:
  809. {
  810. if (0 == in_bits)
  811. {
  812. in_byte = input_buffer[in_idx++];
  813. in_bits = 8;
  814. }
  815. uint16_t pn = (in_byte >> bitshift) & bitmask;
  816. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  817. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  818. in_byte <<= depth;
  819. in_bits -= depth;
  820. color = rgb_palette_buffer[pn];
  821. }
  822. break;
  823. }
  824. if (with_color && has_multicolors)
  825. {
  826. // keep color
  827. }
  828. else if (whitish)
  829. {
  830. color = GxEPD_WHITE;
  831. }
  832. else if (colored && with_color)
  833. {
  834. color = GxEPD_COLORED;
  835. }
  836. else
  837. {
  838. color = GxEPD_BLACK;
  839. }
  840. uint16_t yrow = y + (flip ? h - row - 1 : row);
  841. display.drawPixel(x + col, yrow, color);
  842. } // end pixel
  843. } // end line
  844. }
  845. Serial.print("bytes read "); Serial.println(bytes_read);
  846. }
  847. }
  848. Serial.print("loaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  849. client.stop();
  850. if (!valid)
  851. {
  852. Serial.println("bitmap format not handled.");
  853. }
  854. }
  855. void showBitmapFrom_HTTP_Buffered(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  856. {
  857. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  858. display.setFullWindow();
  859. display.firstPage();
  860. do
  861. {
  862. drawBitmapFrom_HTTP_ToBuffer(host, path, filename, x, y, with_color);
  863. }
  864. while (display.nextPage());
  865. }
  866. void showBitmapFrom_HTTPS(const char* host, const char* path, const char* filename, const char* fingerprint, int16_t x, int16_t y, bool with_color, const char* certificate)
  867. {
  868. // Use WiFiClientSecure class to create TLS connection
  869. #if USE_BearSSL
  870. BearSSL::WiFiClientSecure client;
  871. #else
  872. WiFiClientSecure client;
  873. #endif
  874. bool connection_ok = false;
  875. bool valid = false; // valid format to be handled
  876. bool flip = true; // bitmap is stored bottom-to-top
  877. uint32_t startTime = millis();
  878. if ((x >= display.epd2.WIDTH) || (y >= display.epd2.HEIGHT)) return;
  879. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  880. Serial.print("connecting to "); Serial.println(host);
  881. #if defined (ESP8266)
  882. client.setBufferSizes(4096, 4096); // doesn't help
  883. if (fingerprint) client.setFingerprint(fingerprint);
  884. #elif defined (ESP32)
  885. if (certificate) client.setCACert(certificate);
  886. #endif
  887. if (!client.connect(host, httpsPort))
  888. {
  889. Serial.println("connection failed");
  890. return;
  891. }
  892. Serial.print("requesting URL: ");
  893. Serial.println(String("https://") + host + path + filename);
  894. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  895. "Host: " + host + "\r\n" +
  896. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  897. "Connection: close\r\n\r\n");
  898. Serial.println("request sent");
  899. while (client.connected())
  900. {
  901. String line = client.readStringUntil('\n');
  902. if (!connection_ok)
  903. {
  904. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  905. if (connection_ok) Serial.println(line);
  906. //if (!connection_ok) Serial.println(line);
  907. }
  908. if (!connection_ok) Serial.println(line);
  909. //Serial.println(line);
  910. if (line == "\r")
  911. {
  912. Serial.println("headers received");
  913. break;
  914. }
  915. }
  916. if (!connection_ok) return;
  917. // Parse BMP header
  918. if (read16(client) == 0x4D42) // BMP signature
  919. {
  920. uint32_t fileSize = read32(client);
  921. uint32_t creatorBytes = read32(client);
  922. uint32_t imageOffset = read32(client); // Start of image data
  923. uint32_t headerSize = read32(client);
  924. uint32_t width = read32(client);
  925. uint32_t height = read32(client);
  926. uint16_t planes = read16(client);
  927. uint16_t depth = read16(client); // bits per pixel
  928. uint32_t format = read32(client);
  929. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  930. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  931. {
  932. Serial.print("File size: "); Serial.println(fileSize);
  933. Serial.print("Image Offset: "); Serial.println(imageOffset);
  934. Serial.print("Header size: "); Serial.println(headerSize);
  935. Serial.print("Bit Depth: "); Serial.println(depth);
  936. Serial.print("Image size: ");
  937. Serial.print(width);
  938. Serial.print('x');
  939. Serial.println(height);
  940. // BMP rows are padded (if needed) to 4-byte boundary
  941. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  942. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  943. if (height < 0)
  944. {
  945. height = -height;
  946. flip = false;
  947. }
  948. uint16_t w = width;
  949. uint16_t h = height;
  950. if ((x + w - 1) >= display.epd2.WIDTH) w = display.epd2.WIDTH - x;
  951. if ((y + h - 1) >= display.epd2.HEIGHT) h = display.epd2.HEIGHT - y;
  952. if (w <= max_row_width) // handle with direct drawing
  953. {
  954. valid = true;
  955. uint8_t bitmask = 0xFF;
  956. uint8_t bitshift = 8 - depth;
  957. uint16_t red, green, blue;
  958. bool whitish, colored;
  959. if (depth == 1) with_color = false;
  960. if (depth <= 8)
  961. {
  962. if (depth < 8) bitmask >>= depth;
  963. //bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  964. bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
  965. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  966. {
  967. blue = client.read();
  968. green = client.read();
  969. red = client.read();
  970. client.read();
  971. bytes_read += 4;
  972. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  973. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  974. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  975. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  976. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  977. color_palette_buffer[pn / 8] |= colored << pn % 8;
  978. }
  979. }
  980. display.clearScreen();
  981. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  982. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  983. bytes_read += skip(client, rowPosition - bytes_read);
  984. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  985. {
  986. if (!connection_ok || !(client.connected() || client.available())) break;
  987. delay(1); // yield() to avoid WDT
  988. uint32_t in_remain = rowSize;
  989. uint32_t in_idx = 0;
  990. uint32_t in_bytes = 0;
  991. uint8_t in_byte = 0; // for depth <= 8
  992. uint8_t in_bits = 0; // for depth <= 8
  993. uint8_t out_byte = 0xFF; // white (for w%8!=0 border)
  994. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 border)
  995. uint32_t out_idx = 0;
  996. for (uint16_t col = 0; col < w; col++) // for each pixel
  997. {
  998. yield();
  999. if (!connection_ok || !(client.connected() || client.available())) break;
  1000. // Time to read more pixel data?
  1001. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  1002. {
  1003. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  1004. uint32_t got = read8n(client, input_buffer, get);
  1005. while ((got < get) && connection_ok)
  1006. {
  1007. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  1008. //if ((get - got) > client.available()) delay(200); // does improve? yes, if >= 200
  1009. uint32_t gotmore = read8n(client, input_buffer + got, get - got);
  1010. got += gotmore;
  1011. connection_ok = gotmore > 0;
  1012. }
  1013. in_bytes = got;
  1014. in_remain -= got;
  1015. bytes_read += got;
  1016. }
  1017. if (!connection_ok)
  1018. {
  1019. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  1020. break;
  1021. }
  1022. switch (depth)
  1023. {
  1024. case 24:
  1025. blue = input_buffer[in_idx++];
  1026. green = input_buffer[in_idx++];
  1027. red = input_buffer[in_idx++];
  1028. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1029. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1030. break;
  1031. case 16:
  1032. {
  1033. uint8_t lsb = input_buffer[in_idx++];
  1034. uint8_t msb = input_buffer[in_idx++];
  1035. if (format == 0) // 555
  1036. {
  1037. blue = (lsb & 0x1F) << 3;
  1038. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  1039. red = (msb & 0x7C) << 1;
  1040. }
  1041. else // 565
  1042. {
  1043. blue = (lsb & 0x1F) << 3;
  1044. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  1045. red = (msb & 0xF8);
  1046. }
  1047. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1048. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1049. }
  1050. break;
  1051. case 1:
  1052. case 4:
  1053. case 8:
  1054. {
  1055. if (0 == in_bits)
  1056. {
  1057. in_byte = input_buffer[in_idx++];
  1058. in_bits = 8;
  1059. }
  1060. uint16_t pn = (in_byte >> bitshift) & bitmask;
  1061. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  1062. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  1063. in_byte <<= depth;
  1064. in_bits -= depth;
  1065. }
  1066. break;
  1067. }
  1068. if (whitish)
  1069. {
  1070. // keep white
  1071. }
  1072. else if (colored && with_color)
  1073. {
  1074. out_color_byte &= ~(0x80 >> col % 8); // colored
  1075. }
  1076. else
  1077. {
  1078. out_byte &= ~(0x80 >> col % 8); // black
  1079. }
  1080. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 border)
  1081. {
  1082. output_row_color_buffer[out_idx] = out_color_byte;
  1083. output_row_mono_buffer[out_idx++] = out_byte;
  1084. out_byte = 0xFF; // white (for w%8!=0 border)
  1085. out_color_byte = 0xFF; // white (for w%8!=0 border)
  1086. }
  1087. } // end pixel
  1088. int16_t yrow = y + (flip ? h - row - 1 : row);
  1089. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  1090. } // end line
  1091. Serial.print("downloaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  1092. Serial.print("bytes read "); Serial.println(bytes_read);
  1093. display.refresh();
  1094. }
  1095. }
  1096. }
  1097. client.stop();
  1098. if (!valid)
  1099. {
  1100. Serial.println("bitmap format not handled.");
  1101. }
  1102. }
  1103. 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, const char* certificate)
  1104. {
  1105. // Use WiFiClientSecure class to create TLS connection
  1106. #if USE_BearSSL
  1107. BearSSL::WiFiClientSecure client;
  1108. #else
  1109. WiFiClientSecure client;
  1110. #endif
  1111. bool connection_ok = false;
  1112. bool valid = false; // valid format to be handled
  1113. bool flip = true; // bitmap is stored bottom-to-top
  1114. bool has_multicolors = display.epd2.panel == GxEPD2::ACeP565;
  1115. uint32_t startTime = millis();
  1116. if ((x >= display.width()) || (y >= display.height())) return;
  1117. display.fillScreen(GxEPD_WHITE);
  1118. Serial.print("connecting to "); Serial.println(host);
  1119. #if defined (ESP8266)
  1120. client.setBufferSizes(4096, 4096); // doesn't help
  1121. if (fingerprint) client.setFingerprint(fingerprint);
  1122. #elif defined (ESP32)
  1123. if (certificate) client.setCACert(certificate);
  1124. #endif
  1125. if (!client.connect(host, httpsPort))
  1126. {
  1127. Serial.println("connection failed");
  1128. return;
  1129. }
  1130. Serial.print("requesting URL: ");
  1131. Serial.println(String("https://") + host + path + filename);
  1132. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  1133. "Host: " + host + "\r\n" +
  1134. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  1135. "Connection: close\r\n\r\n");
  1136. Serial.println("request sent");
  1137. while (client.connected())
  1138. {
  1139. String line = client.readStringUntil('\n');
  1140. if (!connection_ok)
  1141. {
  1142. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  1143. if (connection_ok) Serial.println(line);
  1144. //if (!connection_ok) Serial.println(line);
  1145. }
  1146. if (!connection_ok) Serial.println(line);
  1147. //Serial.println(line);
  1148. if (line == "\r")
  1149. {
  1150. Serial.println("headers received");
  1151. break;
  1152. }
  1153. }
  1154. if (!connection_ok) return;
  1155. // Parse BMP header
  1156. if (read16(client) == 0x4D42) // BMP signature
  1157. {
  1158. uint32_t fileSize = read32(client);
  1159. uint32_t creatorBytes = read32(client);
  1160. uint32_t imageOffset = read32(client); // Start of image data
  1161. uint32_t headerSize = read32(client);
  1162. uint32_t width = read32(client);
  1163. uint32_t height = read32(client);
  1164. uint16_t planes = read16(client);
  1165. uint16_t depth = read16(client); // bits per pixel
  1166. uint32_t format = read32(client);
  1167. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  1168. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  1169. {
  1170. Serial.print("File size: "); Serial.println(fileSize);
  1171. Serial.print("Image Offset: "); Serial.println(imageOffset);
  1172. Serial.print("Header size: "); Serial.println(headerSize);
  1173. Serial.print("Bit Depth: "); Serial.println(depth);
  1174. Serial.print("Image size: ");
  1175. Serial.print(width);
  1176. Serial.print('x');
  1177. Serial.println(height);
  1178. // BMP rows are padded (if needed) to 4-byte boundary
  1179. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  1180. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  1181. if (height < 0)
  1182. {
  1183. height = -height;
  1184. flip = false;
  1185. }
  1186. uint16_t w = width;
  1187. uint16_t h = height;
  1188. if ((x + w - 1) >= display.width()) w = display.width() - x;
  1189. if ((y + h - 1) >= display.height()) h = display.height() - y;
  1190. //if (w <= max_row_width) // handle with direct drawing
  1191. {
  1192. valid = true;
  1193. uint8_t bitmask = 0xFF;
  1194. uint8_t bitshift = 8 - depth;
  1195. uint16_t red, green, blue;
  1196. bool whitish, colored;
  1197. if (depth == 1) with_color = false;
  1198. if (depth <= 8)
  1199. {
  1200. if (depth < 8) bitmask >>= depth;
  1201. //bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  1202. bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
  1203. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  1204. {
  1205. blue = client.read();
  1206. green = client.read();
  1207. red = client.read();
  1208. client.read();
  1209. bytes_read += 4;
  1210. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1211. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1212. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  1213. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  1214. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  1215. color_palette_buffer[pn / 8] |= colored << pn % 8;
  1216. //Serial.print("0x00"); Serial.print(red, HEX); Serial.print(green, HEX); Serial.print(blue, HEX);
  1217. //Serial.print(" : "); Serial.print(whitish); Serial.print(", "); Serial.println(colored);
  1218. rgb_palette_buffer[pn] = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  1219. }
  1220. }
  1221. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  1222. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  1223. bytes_read += skip(client, rowPosition - bytes_read);
  1224. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  1225. {
  1226. if (!connection_ok || !(client.connected() || client.available())) break;
  1227. delay(1); // yield() to avoid WDT
  1228. uint32_t in_remain = rowSize;
  1229. uint32_t in_idx = 0;
  1230. uint32_t in_bytes = 0;
  1231. uint8_t in_byte = 0; // for depth <= 8
  1232. uint8_t in_bits = 0; // for depth <= 8
  1233. uint16_t color = GxEPD_WHITE;
  1234. for (uint16_t col = 0; col < w; col++) // for each pixel
  1235. {
  1236. yield();
  1237. if (!connection_ok || !(client.connected() || client.available())) break;
  1238. // Time to read more pixel data?
  1239. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  1240. {
  1241. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  1242. uint32_t got = read8n(client, input_buffer, get);
  1243. while ((got < get) && connection_ok)
  1244. {
  1245. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  1246. uint32_t gotmore = read8n(client, input_buffer + got, get - got);
  1247. got += gotmore;
  1248. connection_ok = gotmore > 0;
  1249. }
  1250. in_bytes = got;
  1251. in_remain -= got;
  1252. bytes_read += got;
  1253. }
  1254. if (!connection_ok)
  1255. {
  1256. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  1257. break;
  1258. }
  1259. switch (depth)
  1260. {
  1261. case 24:
  1262. blue = input_buffer[in_idx++];
  1263. green = input_buffer[in_idx++];
  1264. red = input_buffer[in_idx++];
  1265. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1266. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1267. color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  1268. break;
  1269. case 16:
  1270. {
  1271. uint8_t lsb = input_buffer[in_idx++];
  1272. uint8_t msb = input_buffer[in_idx++];
  1273. if (format == 0) // 555
  1274. {
  1275. blue = (lsb & 0x1F) << 3;
  1276. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  1277. red = (msb & 0x7C) << 1;
  1278. color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  1279. }
  1280. else // 565
  1281. {
  1282. blue = (lsb & 0x1F) << 3;
  1283. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  1284. red = (msb & 0xF8);
  1285. color = (msb << 8) | lsb;
  1286. }
  1287. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1288. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1289. }
  1290. break;
  1291. case 1:
  1292. case 4:
  1293. case 8:
  1294. {
  1295. if (0 == in_bits)
  1296. {
  1297. in_byte = input_buffer[in_idx++];
  1298. in_bits = 8;
  1299. }
  1300. uint16_t pn = (in_byte >> bitshift) & bitmask;
  1301. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  1302. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  1303. in_byte <<= depth;
  1304. in_bits -= depth;
  1305. color = rgb_palette_buffer[pn];
  1306. }
  1307. break;
  1308. }
  1309. if (with_color && has_multicolors)
  1310. {
  1311. // keep color
  1312. }
  1313. else if (whitish)
  1314. {
  1315. color = GxEPD_WHITE;
  1316. }
  1317. else if (colored && with_color)
  1318. {
  1319. color = GxEPD_COLORED;
  1320. }
  1321. else
  1322. {
  1323. color = GxEPD_BLACK;
  1324. }
  1325. uint16_t yrow = y + (flip ? h - row - 1 : row);
  1326. display.drawPixel(x + col, yrow, color);
  1327. } // end pixel
  1328. } // end line
  1329. }
  1330. Serial.print("bytes read "); Serial.println(bytes_read);
  1331. }
  1332. }
  1333. Serial.print("loaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  1334. client.stop();
  1335. if (!valid)
  1336. {
  1337. Serial.println("bitmap format not handled.");
  1338. }
  1339. }
  1340. 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, const char* certificate)
  1341. {
  1342. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  1343. display.setFullWindow();
  1344. display.firstPage();
  1345. do
  1346. {
  1347. drawBitmapFrom_HTTPS_ToBuffer(host, path, filename, fingerprint, x, y, with_color, certificate);
  1348. }
  1349. while (display.nextPage());
  1350. }
  1351. uint16_t read16(WiFiClient& client)
  1352. {
  1353. // BMP data is stored little-endian, same as Arduino.
  1354. uint16_t result;
  1355. ((uint8_t *)&result)[0] = client.read(); // LSB
  1356. ((uint8_t *)&result)[1] = client.read(); // MSB
  1357. return result;
  1358. }
  1359. uint32_t read32(WiFiClient& client)
  1360. {
  1361. // BMP data is stored little-endian, same as Arduino.
  1362. uint32_t result;
  1363. ((uint8_t *)&result)[0] = client.read(); // LSB
  1364. ((uint8_t *)&result)[1] = client.read();
  1365. ((uint8_t *)&result)[2] = client.read();
  1366. ((uint8_t *)&result)[3] = client.read(); // MSB
  1367. return result;
  1368. }
  1369. #if USE_BearSSL
  1370. uint32_t skip(BearSSL::WiFiClientSecure& client, int32_t bytes)
  1371. {
  1372. int32_t remain = bytes;
  1373. uint32_t start = millis();
  1374. while ((client.connected() || client.available()) && (remain > 0))
  1375. {
  1376. if (client.available())
  1377. {
  1378. int16_t v = client.read();
  1379. remain--;
  1380. }
  1381. else delay(1);
  1382. if (millis() - start > 2000) break; // don't hang forever
  1383. }
  1384. return bytes - remain;
  1385. }
  1386. uint32_t read8n(BearSSL::WiFiClientSecure& client, uint8_t* buffer, int32_t bytes)
  1387. {
  1388. int32_t remain = bytes;
  1389. uint32_t start = millis();
  1390. while ((client.connected() || client.available()) && (remain > 0))
  1391. {
  1392. if (client.available())
  1393. {
  1394. int16_t v = client.read();
  1395. *buffer++ = uint8_t(v);
  1396. remain--;
  1397. }
  1398. else delay(1);
  1399. if (millis() - start > 2000) break; // don't hang forever
  1400. }
  1401. return bytes - remain;
  1402. }
  1403. #endif
  1404. uint32_t skip(WiFiClient& client, int32_t bytes)
  1405. {
  1406. int32_t remain = bytes;
  1407. uint32_t start = millis();
  1408. while ((client.connected() || client.available()) && (remain > 0))
  1409. {
  1410. if (client.available())
  1411. {
  1412. int16_t v = client.read();
  1413. remain--;
  1414. }
  1415. else delay(1);
  1416. if (millis() - start > 2000) break; // don't hang forever
  1417. }
  1418. return bytes - remain;
  1419. }
  1420. uint32_t read8n(WiFiClient& client, uint8_t* buffer, int32_t bytes)
  1421. {
  1422. int32_t remain = bytes;
  1423. uint32_t start = millis();
  1424. while ((client.connected() || client.available()) && (remain > 0))
  1425. {
  1426. if (client.available())
  1427. {
  1428. int16_t v = client.read();
  1429. *buffer++ = uint8_t(v);
  1430. remain--;
  1431. }
  1432. else delay(1);
  1433. if (millis() - start > 2000) break; // don't hang forever
  1434. }
  1435. return bytes - remain;
  1436. }