GxEPD2_WiFi_Example.ino 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689
  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: https://www.good-display.com/companyfile/32/
  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 (closed, read only):
  16. // Good Display ePaper for Arduino: https://forum.arduino.cc/t/good-display-epaper-for-arduino/419657
  17. // Waveshare e-paper displays with SPI: https://forum.arduino.cc/t/waveshare-e-paper-displays-with-spi/467865
  18. //
  19. // Add new topics in https://forum.arduino.cc/c/using-arduino/displays/23 for new questions and issues
  20. // see GxEPD2_wiring_examples.h for wiring suggestions and examples
  21. // base class GxEPD2_GFX can be used to pass references or pointers to the display instance as parameter, uses ~1.2k more code
  22. // enable or disable GxEPD2_GFX base class
  23. #define ENABLE_GxEPD2_GFX 0
  24. // uncomment next line to use class GFX of library GFX_Root instead of Adafruit_GFX
  25. //#include <GFX.h>
  26. // Note: if you use this with ENABLE_GxEPD2_GFX 1:
  27. // uncomment it in GxEPD2_GFX.h too, or add #include <GFX.h> before any #include <GxEPD2_GFX.h>
  28. #include <GxEPD2_BW.h>
  29. #include <GxEPD2_3C.h>
  30. #include <GxEPD2_7C.h>
  31. // NOTE: you may need to adapt or select for your wiring in the processor specific conditional compile sections below
  32. // select the display class (only one), matching the kind of display panel
  33. #define GxEPD2_DISPLAY_CLASS GxEPD2_BW
  34. //#define GxEPD2_DISPLAY_CLASS GxEPD2_3C
  35. //#define GxEPD2_DISPLAY_CLASS GxEPD2_4C
  36. //#define GxEPD2_DISPLAY_CLASS GxEPD2_7C
  37. // select the display driver class (only one) for your panel
  38. //#define GxEPD2_DRIVER_CLASS GxEPD2_102 // GDEW0102T4 80x128, UC8175, (WFT0102CZA2)
  39. //#define GxEPD2_DRIVER_CLASS GxEPD2_150_BN // DEPG0150BN 200x200, SSD1681, (FPC8101), TTGO T5 V2.4.1
  40. //#define GxEPD2_DRIVER_CLASS GxEPD2_154 // GDEP015OC1 200x200, IL3829, (WFC0000CZ07), no longer available
  41. //#define GxEPD2_DRIVER_CLASS GxEPD2_154_D67 // GDEH0154D67 200x200, SSD1681, (HINK-E154A07-A1)
  42. //#define GxEPD2_DRIVER_CLASS GxEPD2_154_T8 // GDEW0154T8 152x152, UC8151 (IL0373), (WFT0154CZ17)
  43. //#define GxEPD2_DRIVER_CLASS GxEPD2_154_M09 // GDEW0154M09 200x200, JD79653A, (WFT0154CZB3)
  44. //#define GxEPD2_DRIVER_CLASS GxEPD2_154_M10 // GDEW0154M10 152x152, UC8151D, (WFT0154CZ17)
  45. //#define GxEPD2_DRIVER_CLASS GxEPD2_154_GDEY0154D67 // GDEY0154D67 200x200, SSD1681, (FPC-B001 20.05.21)
  46. //#define GxEPD2_DRIVER_CLASS GxEPD2_213 // GDE0213B1 122x250, IL3895, (HINK-E0213-G01), phased out
  47. //#define GxEPD2_DRIVER_CLASS GxEPD2_213_B72 // GDEH0213B72 122x250, SSD1675A (IL3897), (HINK-E0213A22-A0 SLH1852)
  48. //#define GxEPD2_DRIVER_CLASS GxEPD2_213_B73 // GDEH0213B73 122x250, SSD1675B, (HINK-E0213A22-A0 SLH1914)
  49. //#define GxEPD2_DRIVER_CLASS GxEPD2_213_B74 // GDEM0213B74 122x250, SSD1680, FPC-7528B)
  50. //#define GxEPD2_DRIVER_CLASS GxEPD2_213_flex // GDEW0213I5F 104x212, UC8151 (IL0373), (WFT0213CZ16)
  51. //#define GxEPD2_DRIVER_CLASS GxEPD2_213_M21 // GDEW0213M21 104x212, UC8151 (IL0373), (WFT0213CZ16)
  52. //#define GxEPD2_DRIVER_CLASS GxEPD2_213_T5D // GDEW0213T5D 104x212, UC8151D, (WFT0213CZ16)
  53. //#define GxEPD2_DRIVER_CLASS GxEPD2_213_BN // DEPG0213BN 122x250, SSD1680, (FPC-7528B), TTGO T5 V2.4.1, V2.3.1
  54. //#define GxEPD2_DRIVER_CLASS GxEPD2_213_GDEY0213B74 // GDEY0213B74 122x250, SSD1680, (FPC-A002 20.04.08)
  55. //#define GxEPD2_DRIVER_CLASS GxEPD2_260 // GDEW026T0 152x296, UC8151 (IL0373), (WFT0154CZ17)
  56. //#define GxEPD2_DRIVER_CLASS GxEPD2_260_M01 // GDEW026M01 152x296, UC8151 (IL0373), (WFT0260CZB2)
  57. //#define GxEPD2_DRIVER_CLASS GxEPD2_266_BN // DEPG0266BN 152x296, SSD1680, (FPC7510), TTGO T5 V2.66, TTGO T5 V2.4.1
  58. //#define GxEPD2_DRIVER_CLASS GxEPD2_266_GDEY0266T90 // GDEY0266T90 152x296, SSD1680, (FPC-A003 HB)
  59. //#define GxEPD2_DRIVER_CLASS GxEPD2_270 // GDEW027W3 176x264, EK79652 (IL91874), (WFI0190CZ22)
  60. //#define GxEPD2_DRIVER_CLASS GxEPD2_270_GDEY027T91 // GDEY027T91 176x264, SSD1680, (FB)
  61. //#define GxEPD2_DRIVER_CLASS GxEPD2_290 // GDEH029A1 128x296, SSD1608 (IL3820), (E029A01-FPC-A1 SYX1553)
  62. //#define GxEPD2_DRIVER_CLASS GxEPD2_290_T5 // GDEW029T5 128x296, UC8151 (IL0373), (WFT0290CZ10)
  63. //#define GxEPD2_DRIVER_CLASS GxEPD2_290_T5D // GDEW029T5D 128x296, UC8151D, (WFT0290CZ10)
  64. //#define GxEPD2_DRIVER_CLASS GxEPD2_290_I6FD // GDEW029I6FD 128x296, UC8151D, (WFT0290CZ10)
  65. //#define GxEPD2_DRIVER_CLASS GxEPD2_290_T94 // GDEM029T94 128x296, SSD1680, (FPC-7519 rev.b)
  66. //#define GxEPD2_DRIVER_CLASS GxEPD2_290_T94_V2 // GDEM029T94 128x296, SSD1680, (FPC-7519 rev.b), Waveshare 2.9" V2 variant
  67. //#define GxEPD2_DRIVER_CLASS GxEPD2_290_BS // DEPG0290BS 128x296, SSD1680, (FPC-7519 rev.b)
  68. //#define GxEPD2_DRIVER_CLASS GxEPD2_290_M06 // GDEW029M06 128x296, UC8151D, (WFT0290CZ10)
  69. //#define GxEPD2_DRIVER_CLASS GxEPD2_290_GDEY029T94 // GDEY029T94 128x296, SSD1680, (FPC-A005 20.06.15)
  70. #define GxEPD2_DRIVER_CLASS GxEPD2_310_GDEQ031T10 // GDEQ031T10 240x320, UC8253, (no inking, backside mark KEGMO 3100)
  71. //#define GxEPD2_DRIVER_CLASS GxEPD2_371 // GDEW0371W7 240x416, UC8171 (IL0324), (missing)
  72. //#define GxEPD2_DRIVER_CLASS GxEPD2_370_TC1 // ED037TC1 280x480, SSD1677, (ICA-FU-20 ichia 2029), Waveshare 3.7"
  73. //#define GxEPD2_DRIVER_CLASS GxEPD2_420 // GDEW042T2 400x300, UC8176 (IL0398), (WFT042CZ15)
  74. //#define GxEPD2_DRIVER_CLASS GxEPD2_420_M01 // GDEW042M01 400x300, UC8176 (IL0398), (WFT042CZ15)
  75. //#define GxEPD2_DRIVER_CLASS GxEPD2_420_GDEY042T81 // GDEY042T81 400x300, SSD1683 (no inking)
  76. //#define GxEPD2_DRIVER_CLASS GxEPD2_426_GDEQ0426T82 // GDEQ0426T82 480x800, SSD1677 (P426010-MF1-A)
  77. //#define GxEPD2_DRIVER_CLASS GxEPD2_583 // GDEW0583T7 600x448, UC8159c (IL0371), (missing)
  78. //#define GxEPD2_DRIVER_CLASS GxEPD2_583_T8 // GDEW0583T8 648x480, EK79655 (GD7965), (WFT0583CZ61)
  79. //#define GxEPD2_DRIVER_CLASS GxEPD2_583_GDEQ0583T31 // GDEQ0583T31 648x480, UC8179, (P583010-MF1-B)
  80. //#define GxEPD2_DRIVER_CLASS GxEPD2_750 // GDEW075T8 640x384, UC8159c (IL0371), (WF0583CZ09)
  81. //#define GxEPD2_DRIVER_CLASS GxEPD2_750_T7 // GDEW075T7 800x480, EK79655 (GD7965), (WFT0583CZ61)
  82. //#define GxEPD2_DRIVER_CLASS GxEPD2_750_YT7 // GDEY075T7 800x480, UC8179 (GD7965), (FPC-C001 20.8.20)
  83. //#define GxEPD2_DRIVER_CLASS GxEPD2_1160_T91 // GDEH116T91 960x640, SSD1677, (none or hidden)
  84. //#define GxEPD2_DRIVER_CLASS GxEPD2_1248 // GDEW1248T3 1304x984, UC8179, (WFT1248BZ23,WFT1248BZ24)
  85. // 3-color e-papers
  86. //#define GxEPD2_DRIVER_CLASS GxEPD2_154c // GDEW0154Z04 200x200, IL0376F, (WFT0000CZ04), no longer available
  87. //#define GxEPD2_DRIVER_CLASS GxEPD2_154_Z90c // GDEH0154Z90 200x200, SSD1681, (HINK-E154A07-A1)
  88. //#define GxEPD2_DRIVER_CLASS GxEPD2_213c // GDEW0213Z16 104x212, UC8151 (IL0373), (WFT0213CZ16)
  89. //#define GxEPD2_DRIVER_CLASS GxEPD2_213_Z19c // GDEH0213Z19 104x212, UC8151D, (HINK-E0213A20-A2 2020-11-19)
  90. //#define GxEPD2_DRIVER_CLASS GxEPD2_213_Z98c // GDEY0213Z98 122x250, SSD1680, (FPC-A002 20.04.08)
  91. //#define GxEPD2_DRIVER_CLASS GxEPD2_266c // GDEY0266Z90 152x296, SSD1680, (FPC-7510)
  92. //#define GxEPD2_DRIVER_CLASS GxEPD2_270c // GDEW027C44 176x264, IL91874, (WFI0190CZ22)
  93. //#define GxEPD2_DRIVER_CLASS GxEPD2_290c // GDEW029Z10 128x296, UC8151 (IL0373), (WFT0290CZ10)
  94. //#define GxEPD2_DRIVER_CLASS GxEPD2_290_Z13c // GDEH029Z13 128x296, UC8151D, (HINK-E029A10-A3 20160809)
  95. //#define GxEPD2_DRIVER_CLASS GxEPD2_290_C90c // GDEM029C90 128x296, SSD1680, (FPC-7519 rev.b)
  96. //#define GxEPD2_DRIVER_CLASS GxEPD2_420c // GDEW042Z15 400x300, UC8176 (IL0398), (WFT0420CZ15)
  97. //#define GxEPD2_DRIVER_CLASS GxEPD2_420c_Z21 // GDEQ042Z21 400x300, UC8276, (hidden)
  98. //#define GxEPD2_DRIVER_CLASS GxEPD2_583c // GDEW0583Z21 600x448, UC8159c (IL0371), (missing)
  99. //#define GxEPD2_DRIVER_CLASS GxEPD2_583c_Z83 // GDEW0583Z83 648x480, EK79655 (GD7965), (WFT0583CZ61)
  100. //#define GxEPD2_DRIVER_CLASS GxEPD2_750c // GDEW075Z09 640x384, UC8159c (IL0371), (WF0583CZ09)
  101. //#define GxEPD2_DRIVER_CLASS GxEPD2_750c_Z08 // GDEW075Z08 800x480, EK79655 (GD7965), (WFT0583CZ61)
  102. //#define GxEPD2_DRIVER_CLASS GxEPD2_750c_Z90 // GDEH075Z90 880x528, SSD1677, (HINK-E075A07-A0)
  103. //#define GxEPD2_DRIVER_CLASS GxEPD2_1248c // GDEY1248Z51 1304x984, UC8179, (WFT1248BZ23,WFT1248BZ24)
  104. // 4-color e-paper
  105. //#define GxEPD2_DRIVER_CLASS GxEPD2_266c_GDEY0266F51H // GDEY0266F51H 184x360, JD79667 (FPC-H006 22.04.02)
  106. //#define GxEPD2_DRIVER_CLASS GxEPD2_290c_GDEY029F51H // GDEY029F51H 168x384, JD79667 (FPC-H004 22.03.24)
  107. //#define GxEPD2_DRIVER_CLASS GxEPD2_300c // Waveshare 3.00" 4-color
  108. //#define GxEPD2_DRIVER_CLASS GxEPD2_420c_GDEY0420F51 // GDEY0420F51 400x300, HX8717 (no inking)
  109. //#define GxEPD2_DRIVER_CLASS GxEPD2_437c // Waveshare 4.37" 4-color
  110. // 7-color e-paper
  111. //#define GxEPD2_DRIVER_CLASS GxEPD2_565c // Waveshare 5.65" 7-color
  112. //#define GxEPD2_DRIVER_CLASS GxEPD2_730c_GDEY073D46 // GDEY073D46 800x480 7-color, (N-FPC-001 2021.11.26)
  113. // grey levels parallel IF e-papers on Waveshare e-Paper IT8951 Driver HAT
  114. //#define GxEPD2_DRIVER_CLASS GxEPD2_it60 // ED060SCT 800x600
  115. //#define GxEPD2_DRIVER_CLASS GxEPD2_it60_1448x1072 // ED060KC1 1448x1072
  116. //#define GxEPD2_DRIVER_CLASS GxEPD2_it78_1872x1404 // ED078KC2 1872x1404
  117. //#define GxEPD2_DRIVER_CLASS GxEPD2_it103_1872x1404 // ES103TC1 1872x1404
  118. // SS is usually used for CS. define here for easy change
  119. #ifndef EPD_CS
  120. #define EPD_CS SS
  121. #endif
  122. // somehow there should be an easier way to do this
  123. #define GxEPD2_BW_IS_GxEPD2_BW true
  124. #define GxEPD2_3C_IS_GxEPD2_3C true
  125. #define GxEPD2_4C_IS_GxEPD2_4C true
  126. #define GxEPD2_7C_IS_GxEPD2_7C true
  127. #define GxEPD2_1248_IS_GxEPD2_1248 true
  128. #define GxEPD2_1248c_IS_GxEPD2_1248c true
  129. #define IS_GxEPD(c, x) (c##x)
  130. #define IS_GxEPD2_BW(x) IS_GxEPD(GxEPD2_BW_IS_, x)
  131. #define IS_GxEPD2_3C(x) IS_GxEPD(GxEPD2_3C_IS_, x)
  132. #define IS_GxEPD2_4C(x) IS_GxEPD(GxEPD2_4C_IS_, x)
  133. #define IS_GxEPD2_7C(x) IS_GxEPD(GxEPD2_7C_IS_, x)
  134. #define IS_GxEPD2_1248(x) IS_GxEPD(GxEPD2_1248_IS_, x)
  135. #define IS_GxEPD2_1248c(x) IS_GxEPD(GxEPD2_1248c_IS_, x)
  136. #if defined (ESP8266)
  137. #define MAX_DISPLAY_BUFFER_SIZE (81920ul-34000ul-36000ul) // ~34000 base use, WiFiClientSecure seems to need about 36k more to work (with certificates)
  138. #if IS_GxEPD2_BW(GxEPD2_DISPLAY_CLASS)
  139. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= MAX_DISPLAY_BUFFER_SIZE / (EPD::WIDTH / 8) ? EPD::HEIGHT : MAX_DISPLAY_BUFFER_SIZE / (EPD::WIDTH / 8))
  140. #elif IS_GxEPD2_3C(GxEPD2_DISPLAY_CLASS) || IS_GxEPD2_4C(GxEPD2_DISPLAY_CLASS)
  141. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= (MAX_DISPLAY_BUFFER_SIZE / 2) / (EPD::WIDTH / 8) ? EPD::HEIGHT : (MAX_DISPLAY_BUFFER_SIZE / 2) / (EPD::WIDTH / 8))
  142. #elif IS_GxEPD2_7C(GxEPD2_DISPLAY_CLASS)
  143. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= (MAX_DISPLAY_BUFFER_SIZE) / (EPD::WIDTH / 2) ? EPD::HEIGHT : (MAX_DISPLAY_BUFFER_SIZE) / (EPD::WIDTH / 2))
  144. #endif
  145. // adapt the constructor parameters to your wiring
  146. 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));
  147. // mapping of Waveshare e-Paper ESP8266 Driver Board, new version
  148. //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));
  149. // mapping of Waveshare e-Paper ESP8266 Driver Board, old version
  150. //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));
  151. #undef MAX_DISPLAY_BUFFER_SIZE
  152. #undef MAX_HEIGHT
  153. #endif
  154. #if defined(ESP32)
  155. #define MAX_DISPLAY_BUFFER_SIZE 65536ul // e.g.
  156. #if IS_GxEPD2_BW(GxEPD2_DISPLAY_CLASS)
  157. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= MAX_DISPLAY_BUFFER_SIZE / (EPD::WIDTH / 8) ? EPD::HEIGHT : MAX_DISPLAY_BUFFER_SIZE / (EPD::WIDTH / 8))
  158. #elif IS_GxEPD2_3C(GxEPD2_DISPLAY_CLASS) || IS_GxEPD2_4C(GxEPD2_DISPLAY_CLASS)
  159. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= (MAX_DISPLAY_BUFFER_SIZE / 2) / (EPD::WIDTH / 8) ? EPD::HEIGHT : (MAX_DISPLAY_BUFFER_SIZE / 2) / (EPD::WIDTH / 8))
  160. #elif IS_GxEPD2_7C(GxEPD2_DISPLAY_CLASS)
  161. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= (MAX_DISPLAY_BUFFER_SIZE) / (EPD::WIDTH / 2) ? EPD::HEIGHT : (MAX_DISPLAY_BUFFER_SIZE) / (EPD::WIDTH / 2))
  162. #endif
  163. // adapt the constructor parameters to your wiring
  164. #if !IS_GxEPD2_1248(GxEPD2_DRIVER_CLASS) && !IS_GxEPD2_1248c(GxEPD2_DRIVER_CLASS)
  165. #if defined(ARDUINO_LOLIN_D32_PRO)
  166. 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)); // my LOLIN_D32_PRO proto board
  167. #else
  168. 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)); // my suggested wiring and proto board
  169. //GxEPD2_DISPLAY_CLASS<GxEPD2_DRIVER_CLASS, MAX_HEIGHT(GxEPD2_DRIVER_CLASS)> display(GxEPD2_DRIVER_CLASS(/*CS=5*/ 5, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // LILYGO_T5_V2.4.1
  170. //GxEPD2_DISPLAY_CLASS<GxEPD2_DRIVER_CLASS, MAX_HEIGHT(GxEPD2_DRIVER_CLASS)> display(GxEPD2_DRIVER_CLASS(/*CS=5*/ EPD_CS, /*DC=*/ 19, /*RST=*/ 4, /*BUSY=*/ 34)); // LILYGO® TTGO T5 2.66
  171. //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)); // e.g. TTGO T8 ESP32-WROVER
  172. //GxEPD2_DISPLAY_CLASS<GxEPD2_DRIVER_CLASS, MAX_HEIGHT(GxEPD2_DRIVER_CLASS)> display(GxEPD2_DRIVER_CLASS(/*CS=*/ 15, /*DC=*/ 27, /*RST=*/ 26, /*BUSY=*/ 25)); // Waveshare ESP32 Driver Board
  173. #endif
  174. #else // GxEPD2_1248 or GxEPD2_1248c
  175. // Waveshare 12.48 b/w or b/w/r SPI display board and frame or Good Display 12.48 b/w panel GDEW1248T3 or b/w/r panel GDEY1248Z51
  176. // general constructor for use with all parameters, e.g. for Waveshare ESP32 driver board mounted on connection board
  177. GxEPD2_DISPLAY_CLASS < GxEPD2_DRIVER_CLASS, MAX_HEIGHT(GxEPD2_DRIVER_CLASS) > display(GxEPD2_DRIVER_CLASS(/*sck=*/ 13, /*miso=*/ 12, /*mosi=*/ 14,
  178. /*cs_m1=*/ 23, /*cs_s1=*/ 22, /*cs_m2=*/ 16, /*cs_s2=*/ 19,
  179. /*dc1=*/ 25, /*dc2=*/ 17, /*rst1=*/ 33, /*rst2=*/ 5,
  180. /*busy_m1=*/ 32, /*busy_s1=*/ 26, /*busy_m2=*/ 18, /*busy_s2=*/ 4));
  181. #endif
  182. #undef MAX_DISPLAY_BUFFER_SIZE
  183. #undef MAX_HEIGHT
  184. #endif
  185. #if defined(ARDUINO_UNOR4_WIFI)
  186. #define MAX_DISPLAY_BUFFER_SIZE 16384ul // e.g. half of available RAM
  187. #if IS_GxEPD2_BW(GxEPD2_DISPLAY_CLASS)
  188. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= MAX_DISPLAY_BUFFER_SIZE / (EPD::WIDTH / 8) ? EPD::HEIGHT : MAX_DISPLAY_BUFFER_SIZE / (EPD::WIDTH / 8))
  189. #elif IS_GxEPD2_3C(GxEPD2_DISPLAY_CLASS) || IS_GxEPD2_4C(GxEPD2_DISPLAY_CLASS)
  190. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= (MAX_DISPLAY_BUFFER_SIZE / 2) / (EPD::WIDTH / 8) ? EPD::HEIGHT : (MAX_DISPLAY_BUFFER_SIZE / 2) / (EPD::WIDTH / 8))
  191. #elif IS_GxEPD2_7C(GxEPD2_DISPLAY_CLASS)
  192. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= (MAX_DISPLAY_BUFFER_SIZE) / (EPD::WIDTH / 2) ? EPD::HEIGHT : (MAX_DISPLAY_BUFFER_SIZE) / (EPD::WIDTH / 2))
  193. #endif
  194. // adapt the constructor parameters to your wiring
  195. GxEPD2_DISPLAY_CLASS<GxEPD2_DRIVER_CLASS, MAX_HEIGHT(GxEPD2_DRIVER_CLASS)> display(GxEPD2_DRIVER_CLASS(/*CS=*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  196. #endif
  197. #if defined(ARDUINO_ARCH_RP2040)
  198. #define MAX_DISPLAY_BUFFER_SIZE 131072ul // e.g. half of available ram
  199. #if IS_GxEPD2_BW(GxEPD2_DISPLAY_CLASS)
  200. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= MAX_DISPLAY_BUFFER_SIZE / (EPD::WIDTH / 8) ? EPD::HEIGHT : MAX_DISPLAY_BUFFER_SIZE / (EPD::WIDTH / 8))
  201. #elif IS_GxEPD2_3C(GxEPD2_DISPLAY_CLASS) || IS_GxEPD2_4C(GxEPD2_DISPLAY_CLASS)
  202. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= (MAX_DISPLAY_BUFFER_SIZE / 2) / (EPD::WIDTH / 8) ? EPD::HEIGHT : (MAX_DISPLAY_BUFFER_SIZE / 2) / (EPD::WIDTH / 8))
  203. #elif IS_GxEPD2_7C(GxEPD2_DISPLAY_CLASS)
  204. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= (MAX_DISPLAY_BUFFER_SIZE) / (EPD::WIDTH / 2) ? EPD::HEIGHT : (MAX_DISPLAY_BUFFER_SIZE) / (EPD::WIDTH / 2))
  205. #endif
  206. #if defined(ARDUINO_NANO_RP2040_CONNECT)
  207. // adapt the constructor parameters to your wiring
  208. GxEPD2_DISPLAY_CLASS<GxEPD2_DRIVER_CLASS, MAX_HEIGHT(GxEPD2_DRIVER_CLASS)> display(GxEPD2_DRIVER_CLASS(/*CS=*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  209. #endif
  210. #if defined(ARDUINO_RASPBERRY_PI_PICO_W)
  211. GxEPD2_DISPLAY_CLASS<GxEPD2_DRIVER_CLASS, MAX_HEIGHT(GxEPD2_DRIVER_CLASS)> display(GxEPD2_DRIVER_CLASS(/*CS=*/ 9, /*DC=*/ 8, /*RST=*/ 12, /*BUSY=*/ 13)); // Waveshare Pico-ePaper-2.9
  212. #endif
  213. #undef MAX_DISPLAY_BUFFER_SIZE
  214. #undef MAX_HEIGHT
  215. #endif
  216. #if defined (ESP8266)
  217. #include <ESP8266WiFi.h>
  218. #endif
  219. #include <WiFiClient.h>
  220. #include <WiFiClientSecure.h>
  221. const char* ssid = "........";
  222. const char* password = "........";
  223. const int httpPort = 80;
  224. const int httpsPort = 443;
  225. // note: the certificates have been moved to a separate header file, as R"CERT( destroys IDE Auto Format capability
  226. #include "GxEPD2_github_raw_certs.h"
  227. const char* certificate_rawcontent = cert_DigiCert_TLS_RSA_SHA256_2020_CA1; // ok, should work until 2031-04-13 23:59:59
  228. //const char* certificate_rawcontent = github_io_chain_pem_first; // ok, should work until Tue, 21 Mar 2023 23:59:59 GMT
  229. //const char* certificate_rawcontent = github_io_chain_pem_second; // ok, should work until Tue, 21 Mar 2023 23:59:59 GMT
  230. //const char* certificate_rawcontent = github_io_chain_pem_third; // ok, should work until Tue, 21 Mar 2023 23:59:59 GMT
  231. const char* host_rawcontent = "raw.githubusercontent.com";
  232. const char* path_rawcontent = "/ZinggJM/GxEPD2/master/extras/bitmaps/";
  233. const char* path_prenticedavid = "/prenticedavid/MCUFRIEND_kbv/master/extras/bitmaps/";
  234. const char* path_waveshare_c = "/waveshare/e-Paper/master/RaspberryPi_JetsonNano/c/pic/";
  235. const char* path_waveshare_py = "/waveshare/e-Paper/master/RaspberryPi_JetsonNano/python/pic/";
  236. const char* fp_rawcontent = "8F 0E 79 24 71 C5 A7 D2 A7 46 76 30 C1 3C B7 2A 13 B0 01 B2"; // as of 29.7.2022
  237. // note that BMP bitmaps are drawn at physical position in physical orientation of the screen
  238. void showBitmapFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color = true);
  239. 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,
  240. const char* certificate = certificate_rawcontent);
  241. // draws BMP bitmap according to set orientation
  242. void showBitmapFrom_HTTP_Buffered(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color = true);
  243. 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,
  244. const char* certificate = certificate_rawcontent);
  245. #if defined(ESP32)
  246. // uncomment next line to use HSPI for EPD (and VSPI for SD), e.g. with Waveshare ESP32 Driver Board
  247. //#define USE_HSPI_FOR_EPD
  248. #endif
  249. #if defined(ARDUINO_RASPBERRY_PI_PICO_W)
  250. // SPIClassRP2040(spi_inst_t *spi, pin_size_t rx, pin_size_t cs, pin_size_t sck, pin_size_t tx);
  251. // uncomment next line for use with Waveshare Pico-ePaper-2.9.
  252. SPIClassRP2040 SPIn(spi1, 12, 13, 10, 11); // need be valid pins for same SPI channel, else fails blinking 4 long 4 short
  253. #endif
  254. #if defined(ESP32) && defined(USE_HSPI_FOR_EPD)
  255. SPIClass hspi(HSPI);
  256. #endif
  257. void setup()
  258. {
  259. #if defined(ESP32) && defined(USE_HSPI_FOR_EPD)
  260. hspi.begin(13, 12, 14, 15); // remap hspi for EPD (swap pins)
  261. display.epd2.selectSPI(hspi, SPISettings(4000000, MSBFIRST, SPI_MODE0));
  262. #endif
  263. #if defined(ARDUINO_RASPBERRY_PI_PICO_W)
  264. delay(2000); // USB serial available
  265. display.epd2.selectSPI(SPIn, SPISettings(4000000, MSBFIRST, SPI_MODE0));
  266. #endif
  267. Serial.begin(115200);
  268. Serial.println();
  269. Serial.println("GxEPD2_WiFi_Example");
  270. //display.init(115200); // default 10ms reset pulse, e.g. for bare panels with DESPI-C02
  271. display.init(115200, true, 2, false); // USE THIS for Waveshare boards with "clever" reset circuit, 2ms reset pulse
  272. #if defined (ESP8266) || defined (ESP32)
  273. #ifdef RE_INIT_NEEDED
  274. WiFi.persistent(true);
  275. WiFi.mode(WIFI_STA); // switch off AP
  276. WiFi.setAutoConnect(true);
  277. WiFi.setAutoReconnect(true);
  278. WiFi.disconnect();
  279. #endif
  280. if (!WiFi.getAutoConnect() || ( WiFi.getMode() != WIFI_STA) || ((WiFi.SSID() != ssid) && String(ssid) != "........"))
  281. {
  282. Serial.println();
  283. Serial.print("WiFi.getAutoConnect() = ");
  284. Serial.println(WiFi.getAutoConnect());
  285. Serial.print("WiFi.SSID() = ");
  286. Serial.println(WiFi.SSID());
  287. WiFi.mode(WIFI_STA); // switch off AP
  288. Serial.print("Connecting to ");
  289. Serial.println(ssid);
  290. WiFi.begin(ssid, password);
  291. }
  292. #else
  293. Serial.print("Connecting to ");
  294. Serial.println(ssid);
  295. WiFi.begin(ssid, password);
  296. #endif
  297. int ConnectTimeout = 60; // 30 seconds
  298. while (WiFi.status() != WL_CONNECTED)
  299. {
  300. delay(500);
  301. Serial.print(".");
  302. Serial.print(WiFi.status());
  303. if (--ConnectTimeout <= 0)
  304. {
  305. Serial.println();
  306. Serial.println("WiFi connect timeout");
  307. return;
  308. }
  309. }
  310. Serial.println();
  311. Serial.println("WiFi connected");
  312. // Print the IP address
  313. Serial.println(WiFi.localIP());
  314. setClock();
  315. if ((display.epd2.panel == GxEPD2::GDEW0154Z04) || (display.epd2.panel == GxEPD2::ACeP565) || (display.epd2.panel == GxEPD2::GDEY073D46) || false)
  316. {
  317. //drawBitmapsBuffered_200x200();
  318. //drawBitmapsBuffered_other();
  319. drawBitmapsBuffered_7C();
  320. }
  321. else
  322. {
  323. drawBitmaps_200x200();
  324. drawBitmaps_other();
  325. }
  326. //drawBitmaps_test();
  327. //drawBitmapsBuffered_test();
  328. Serial.println("GxEPD2_WiFi_Example done");
  329. }
  330. void loop(void)
  331. {
  332. }
  333. void drawBitmaps_200x200()
  334. {
  335. int16_t x = (display.width() - 200) / 2;
  336. int16_t y = (display.height() - 200) / 2;
  337. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "logo200x200.bmp", fp_rawcontent, x, y);
  338. delay(2000);
  339. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "first200x200.bmp", fp_rawcontent, x, y);
  340. delay(2000);
  341. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "second200x200.bmp", fp_rawcontent, x, y);
  342. delay(2000);
  343. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "third200x200.bmp", fp_rawcontent, x, y);
  344. delay(2000);
  345. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "fourth200x200.bmp", fp_rawcontent, x, y);
  346. delay(2000);
  347. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "fifth200x200.bmp", fp_rawcontent, x, y);
  348. delay(2000);
  349. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "sixth200x200.bmp", fp_rawcontent, x, y);
  350. delay(2000);
  351. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "seventh200x200.bmp", fp_rawcontent, x, y);
  352. delay(2000);
  353. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "eighth200x200.bmp", fp_rawcontent, x, y);
  354. delay(2000);
  355. }
  356. void drawBitmaps_other()
  357. {
  358. int16_t w2 = display.width() / 2;
  359. int16_t h2 = display.height() / 2;
  360. //showBitmapFrom_HTTP("www.packescape.com", "/img/assets/", "IniciMenusTV2.bmp", w2 - 200, h2 - 150, false);
  361. //delay(2000);
  362. showBitmapFrom_HTTP("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", w2 - 50, h2 - 50, false);
  363. delay(2000);
  364. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_1.bmp", fp_rawcontent, w2 - 100, h2 - 160);
  365. delay(2000);
  366. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  367. delay(2000);
  368. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "marilyn_240x240x8.bmp", fp_rawcontent, w2 - 120, h2 - 120);
  369. delay(2000);
  370. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "miniwoof.bmp", fp_rawcontent, w2 - 60, h2 - 80);
  371. delay(2000);
  372. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "test.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  373. delay(2000);
  374. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger.bmp", fp_rawcontent, w2 - 160, h2 - 120);
  375. delay(2000);
  376. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_178x160x4.bmp", fp_rawcontent, w2 - 89, h2 - 80);
  377. delay(2000);
  378. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_240x317x4.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  379. delay(2000);
  380. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_320x200x24.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  381. delay(2000);
  382. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger16T.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  383. delay(2000);
  384. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "woof.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  385. delay(2000);
  386. }
  387. void drawBitmaps_test()
  388. {
  389. int16_t w2 = display.width() / 2;
  390. int16_t h2 = display.height() / 2;
  391. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  392. delay(2000);
  393. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "output5.bmp", fp_rawcontent, 0, 0);
  394. delay(2000);
  395. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "output6.bmp", fp_rawcontent, 0, 0);
  396. delay(2000);
  397. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_1.bmp", fp_rawcontent, 0, 0);
  398. delay(2000);
  399. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_4.bmp", fp_rawcontent, 0, 0);
  400. delay(2000);
  401. //showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_8.bmp", fp_rawcontent, 0, 0); // format 1: BI_RLE8 is not supported
  402. //delay(2000);
  403. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_11.bmp", fp_rawcontent, 0, 0);
  404. delay(2000);
  405. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_44.bmp", fp_rawcontent, 0, 0);
  406. delay(2000);
  407. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_88.bmp", fp_rawcontent, 0, 0);
  408. delay(2000);
  409. //showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "liberato640x384.bmp", fp_rawcontent, 0, 0);
  410. //delay(2000);
  411. }
  412. void drawBitmapsBuffered_200x200()
  413. {
  414. int16_t x = (display.width() - 200) / 2;
  415. int16_t y = (display.height() - 200) / 2;
  416. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "logo200x200.bmp", fp_rawcontent, x, y);
  417. delay(2000);
  418. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "first200x200.bmp", fp_rawcontent, x, y);
  419. delay(2000);
  420. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "second200x200.bmp", fp_rawcontent, x, y);
  421. delay(2000);
  422. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "third200x200.bmp", fp_rawcontent, x, y);
  423. delay(2000);
  424. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "fourth200x200.bmp", fp_rawcontent, x, y);
  425. delay(2000);
  426. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "fifth200x200.bmp", fp_rawcontent, x, y);
  427. delay(2000);
  428. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "sixth200x200.bmp", fp_rawcontent, x, y);
  429. delay(2000);
  430. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "seventh200x200.bmp", fp_rawcontent, x, y);
  431. delay(2000);
  432. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "eighth200x200.bmp", fp_rawcontent, x, y);
  433. delay(2000);
  434. }
  435. void drawBitmapsBuffered_other()
  436. {
  437. int16_t w2 = display.width() / 2;
  438. int16_t h2 = display.height() / 2;
  439. showBitmapFrom_HTTP_Buffered("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", w2 - 50, h2 - 50, false);
  440. delay(2000);
  441. //showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_1.bmp", fp_rawcontent, w2 - 100, h2 - 160);
  442. delay(2000);
  443. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  444. delay(2000);
  445. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "marilyn_240x240x8.bmp", fp_rawcontent, w2 - 120, h2 - 120);
  446. delay(2000);
  447. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "miniwoof.bmp", fp_rawcontent, w2 - 60, h2 - 80);
  448. delay(2000);
  449. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "test.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  450. delay(2000);
  451. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger.bmp", fp_rawcontent, w2 - 160, h2 - 120);
  452. delay(2000);
  453. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_178x160x4.bmp", fp_rawcontent, w2 - 89, h2 - 80);
  454. delay(2000);
  455. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_240x317x4.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  456. delay(2000);
  457. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_320x200x24.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  458. delay(2000);
  459. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger16T.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  460. delay(2000);
  461. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "woof.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  462. delay(2000);
  463. }
  464. void drawBitmapsBuffered_7C()
  465. {
  466. if ((display.epd2.panel == GxEPD2::ACeP565) || (display.epd2.panel == GxEPD2::GDEY073D46))
  467. {
  468. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f.bmp", fp_rawcontent, 0, 0);
  469. delay(2000);
  470. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f2.bmp", fp_rawcontent, 0, 0);
  471. delay(2000);
  472. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f3.bmp", fp_rawcontent, 0, 0);
  473. delay(2000);
  474. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f4.bmp", fp_rawcontent, 0, 0);
  475. delay(2000);
  476. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_py, "5in65f4.bmp", fp_rawcontent, 0, 0);
  477. delay(2000);
  478. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_py, "N-Color1.bmp", fp_rawcontent, 0, 0);
  479. delay(2000);
  480. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "displayed_bmp_small_but_padded.bmp", fp_rawcontent, 0, 0);
  481. delay(2000);
  482. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "displayed_bmp_large.bmp", fp_rawcontent, 0, 0);
  483. delay(2000);
  484. }
  485. }
  486. void drawBitmapsBuffered_test()
  487. {
  488. int16_t w2 = display.width() / 2;
  489. int16_t h2 = display.height() / 2;
  490. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  491. delay(2000);
  492. }
  493. static const uint16_t input_buffer_pixels = 800; // may affect performance
  494. static const uint16_t max_row_width = 1872; // for up to 7.8" display 1872x1404
  495. static const uint16_t max_palette_pixels = 256; // for depth <= 8
  496. uint8_t input_buffer[3 * input_buffer_pixels]; // up to depth 24
  497. uint8_t output_row_mono_buffer[max_row_width / 8]; // buffer for at least one row of b/w bits
  498. uint8_t output_row_color_buffer[max_row_width / 8]; // buffer for at least one row of color bits
  499. uint8_t mono_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 b/w
  500. uint8_t color_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 c/w
  501. uint16_t rgb_palette_buffer[max_palette_pixels]; // palette buffer for depth <= 8 for buffered graphics, needed for 7-color display
  502. void showBitmapFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  503. {
  504. WiFiClient client;
  505. bool connection_ok = false;
  506. bool valid = false; // valid format to be handled
  507. bool flip = true; // bitmap is stored bottom-to-top
  508. uint32_t startTime = millis();
  509. if ((x >= display.epd2.WIDTH) || (y >= display.epd2.HEIGHT)) return;
  510. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  511. Serial.print("connecting to "); Serial.println(host);
  512. if (!client.connect(host, httpPort))
  513. {
  514. Serial.println("connection failed");
  515. return;
  516. }
  517. Serial.print("requesting URL: ");
  518. Serial.println(String("http://") + host + path + filename);
  519. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  520. "Host: " + host + "\r\n" +
  521. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  522. "Connection: close\r\n\r\n");
  523. Serial.println("request sent");
  524. while (client.connected())
  525. {
  526. String line = client.readStringUntil('\n');
  527. if (!connection_ok)
  528. {
  529. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  530. if (connection_ok) Serial.println(line);
  531. //if (!connection_ok) Serial.println(line);
  532. }
  533. if (!connection_ok) Serial.println(line);
  534. //Serial.println(line);
  535. if (line == "\r")
  536. {
  537. Serial.println("headers received");
  538. break;
  539. }
  540. }
  541. if (!connection_ok) return;
  542. // Parse BMP header
  543. if (read16(client) == 0x4D42) // BMP signature
  544. {
  545. uint32_t fileSize = read32(client);
  546. uint32_t creatorBytes = read32(client); (void)creatorBytes; //unused
  547. uint32_t imageOffset = read32(client); // Start of image data
  548. uint32_t headerSize = read32(client);
  549. uint32_t width = read32(client);
  550. int32_t height = (int32_t) read32(client);
  551. uint16_t planes = read16(client);
  552. uint16_t depth = read16(client); // bits per pixel
  553. uint32_t format = read32(client);
  554. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  555. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  556. {
  557. Serial.print("File size: "); Serial.println(fileSize);
  558. Serial.print("Image Offset: "); Serial.println(imageOffset);
  559. Serial.print("Header size: "); Serial.println(headerSize);
  560. Serial.print("Bit Depth: "); Serial.println(depth);
  561. Serial.print("Image size: ");
  562. Serial.print(width);
  563. Serial.print('x');
  564. Serial.println(height);
  565. // BMP rows are padded (if needed) to 4-byte boundary
  566. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  567. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  568. if (height < 0)
  569. {
  570. height = -height;
  571. flip = false;
  572. }
  573. uint16_t w = width;
  574. uint16_t h = height;
  575. if ((x + w - 1) >= display.epd2.WIDTH) w = display.epd2.WIDTH - x;
  576. if ((y + h - 1) >= display.epd2.HEIGHT) h = display.epd2.HEIGHT - y;
  577. if (w <= max_row_width) // handle with direct drawing
  578. {
  579. valid = true;
  580. uint8_t bitmask = 0xFF;
  581. uint8_t bitshift = 8 - depth;
  582. uint16_t red, green, blue;
  583. bool whitish = false;
  584. bool colored = false;
  585. if (depth == 1) with_color = false;
  586. if (depth <= 8)
  587. {
  588. if (depth < 8) bitmask >>= depth;
  589. //bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  590. bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
  591. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  592. {
  593. blue = client.read();
  594. green = client.read();
  595. red = client.read();
  596. client.read();
  597. bytes_read += 4;
  598. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  599. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  600. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  601. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  602. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  603. color_palette_buffer[pn / 8] |= colored << pn % 8;
  604. }
  605. }
  606. display.clearScreen();
  607. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  608. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  609. bytes_read += skip(client, rowPosition - bytes_read);
  610. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  611. {
  612. if (!connection_ok || !(client.connected() || client.available())) break;
  613. delay(1); // yield() to avoid WDT
  614. uint32_t in_remain = rowSize;
  615. uint32_t in_idx = 0;
  616. uint32_t in_bytes = 0;
  617. uint8_t in_byte = 0; // for depth <= 8
  618. uint8_t in_bits = 0; // for depth <= 8
  619. uint8_t out_byte = 0xFF; // white (for w%8!=0 border)
  620. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 border)
  621. uint32_t out_idx = 0;
  622. for (uint16_t col = 0; col < w; col++) // for each pixel
  623. {
  624. yield();
  625. if (!connection_ok || !(client.connected() || client.available())) break;
  626. // Time to read more pixel data?
  627. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  628. {
  629. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  630. uint32_t got = read8n(client, input_buffer, get);
  631. while ((got < get) && connection_ok)
  632. {
  633. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  634. uint32_t gotmore = read8n(client, input_buffer + got, get - got);
  635. got += gotmore;
  636. connection_ok = gotmore > 0;
  637. }
  638. in_bytes = got;
  639. in_remain -= got;
  640. bytes_read += got;
  641. }
  642. if (!connection_ok)
  643. {
  644. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  645. break;
  646. }
  647. switch (depth)
  648. {
  649. case 32:
  650. blue = input_buffer[in_idx++];
  651. green = input_buffer[in_idx++];
  652. red = input_buffer[in_idx++];
  653. in_idx++; // skip alpha
  654. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  655. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  656. break;
  657. case 24:
  658. blue = input_buffer[in_idx++];
  659. green = input_buffer[in_idx++];
  660. red = input_buffer[in_idx++];
  661. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  662. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  663. break;
  664. case 16:
  665. {
  666. uint8_t lsb = input_buffer[in_idx++];
  667. uint8_t msb = input_buffer[in_idx++];
  668. if (format == 0) // 555
  669. {
  670. blue = (lsb & 0x1F) << 3;
  671. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  672. red = (msb & 0x7C) << 1;
  673. }
  674. else // 565
  675. {
  676. blue = (lsb & 0x1F) << 3;
  677. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  678. red = (msb & 0xF8);
  679. }
  680. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  681. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  682. }
  683. break;
  684. case 1:
  685. case 2:
  686. case 4:
  687. case 8:
  688. {
  689. if (0 == in_bits)
  690. {
  691. in_byte = input_buffer[in_idx++];
  692. in_bits = 8;
  693. }
  694. uint16_t pn = (in_byte >> bitshift) & bitmask;
  695. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  696. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  697. in_byte <<= depth;
  698. in_bits -= depth;
  699. }
  700. break;
  701. }
  702. if (whitish)
  703. {
  704. // keep white
  705. }
  706. else if (colored && with_color)
  707. {
  708. out_color_byte &= ~(0x80 >> col % 8); // colored
  709. }
  710. else
  711. {
  712. out_byte &= ~(0x80 >> col % 8); // black
  713. }
  714. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 border)
  715. {
  716. output_row_color_buffer[out_idx] = out_color_byte;
  717. output_row_mono_buffer[out_idx++] = out_byte;
  718. out_byte = 0xFF; // white (for w%8!=0 border)
  719. out_color_byte = 0xFF; // white (for w%8!=0 border)
  720. }
  721. } // end pixel
  722. int16_t yrow = y + (flip ? h - row - 1 : row);
  723. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  724. } // end line
  725. Serial.print("downloaded in ");
  726. Serial.print(millis() - startTime);
  727. Serial.println(" ms");
  728. display.refresh();
  729. }
  730. Serial.print("bytes read "); Serial.println(bytes_read);
  731. }
  732. }
  733. client.stop();
  734. if (!valid)
  735. {
  736. Serial.println("bitmap format not handled.");
  737. }
  738. }
  739. void drawBitmapFrom_HTTP_ToBuffer(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  740. {
  741. WiFiClient client;
  742. bool connection_ok = false;
  743. bool valid = false; // valid format to be handled
  744. bool flip = true; // bitmap is stored bottom-to-top
  745. bool has_multicolors = (display.epd2.panel == GxEPD2::ACeP565) || (display.epd2.panel == GxEPD2::GDEY073D46);
  746. uint32_t startTime = millis();
  747. if ((x >= display.width()) || (y >= display.height())) return;
  748. display.fillScreen(GxEPD_WHITE);
  749. Serial.print("connecting to "); Serial.println(host);
  750. if (!client.connect(host, httpPort))
  751. {
  752. Serial.println("connection failed");
  753. return;
  754. }
  755. Serial.print("requesting URL: ");
  756. Serial.println(String("http://") + host + path + filename);
  757. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  758. "Host: " + host + "\r\n" +
  759. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  760. "Connection: close\r\n\r\n");
  761. Serial.println("request sent");
  762. while (client.connected())
  763. {
  764. String line = client.readStringUntil('\n');
  765. if (!connection_ok)
  766. {
  767. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  768. if (connection_ok) Serial.println(line);
  769. //if (!connection_ok) Serial.println(line);
  770. }
  771. if (!connection_ok) Serial.println(line);
  772. //Serial.println(line);
  773. if (line == "\r")
  774. {
  775. Serial.println("headers received");
  776. break;
  777. }
  778. }
  779. if (!connection_ok) return;
  780. // Parse BMP header
  781. if (read16(client) == 0x4D42) // BMP signature
  782. {
  783. uint32_t fileSize = read32(client);
  784. uint32_t creatorBytes = read32(client); (void)creatorBytes; //unused
  785. uint32_t imageOffset = read32(client); // Start of image data
  786. uint32_t headerSize = read32(client);
  787. uint32_t width = read32(client);
  788. int32_t height = (int32_t) read32(client);
  789. uint16_t planes = read16(client);
  790. uint16_t depth = read16(client); // bits per pixel
  791. uint32_t format = read32(client);
  792. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  793. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  794. {
  795. Serial.print("File size: "); Serial.println(fileSize);
  796. Serial.print("Image Offset: "); Serial.println(imageOffset);
  797. Serial.print("Header size: "); Serial.println(headerSize);
  798. Serial.print("Bit Depth: "); Serial.println(depth);
  799. Serial.print("Image size: ");
  800. Serial.print(width);
  801. Serial.print('x');
  802. Serial.println(height);
  803. // BMP rows are padded (if needed) to 4-byte boundary
  804. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  805. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  806. if (height < 0)
  807. {
  808. height = -height;
  809. flip = false;
  810. }
  811. uint16_t w = width;
  812. uint16_t h = height;
  813. if ((x + w - 1) >= display.width()) w = display.width() - x;
  814. if ((y + h - 1) >= display.height()) h = display.height() - y;
  815. //if (w <= max_row_width) // handle with direct drawing
  816. {
  817. valid = true;
  818. uint8_t bitmask = 0xFF;
  819. uint8_t bitshift = 8 - depth;
  820. uint16_t red, green, blue;
  821. bool whitish = false;
  822. bool colored = false;
  823. if (depth == 1) with_color = false;
  824. if (depth <= 8)
  825. {
  826. if (depth < 8) bitmask >>= depth;
  827. //bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  828. bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
  829. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  830. {
  831. blue = client.read();
  832. green = client.read();
  833. red = client.read();
  834. client.read();
  835. bytes_read += 4;
  836. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  837. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  838. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  839. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  840. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  841. color_palette_buffer[pn / 8] |= colored << pn % 8;
  842. //Serial.print("0x00"); Serial.print(red, HEX); Serial.print(green, HEX); Serial.print(blue, HEX);
  843. //Serial.print(" : "); Serial.print(whitish); Serial.print(", "); Serial.println(colored);
  844. rgb_palette_buffer[pn] = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  845. }
  846. }
  847. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  848. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  849. bytes_read += skip(client, rowPosition - bytes_read);
  850. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  851. {
  852. if (!connection_ok || !(client.connected() || client.available())) break;
  853. delay(1); // yield() to avoid WDT
  854. uint32_t in_remain = rowSize;
  855. uint32_t in_idx = 0;
  856. uint32_t in_bytes = 0;
  857. uint8_t in_byte = 0; // for depth <= 8
  858. uint8_t in_bits = 0; // for depth <= 8
  859. uint16_t color = GxEPD_WHITE;
  860. for (uint16_t col = 0; col < w; col++) // for each pixel
  861. {
  862. yield();
  863. if (!connection_ok || !(client.connected() || client.available())) break;
  864. // Time to read more pixel data?
  865. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  866. {
  867. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  868. uint32_t got = read8n(client, input_buffer, get);
  869. while ((got < get) && connection_ok)
  870. {
  871. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  872. uint32_t gotmore = read8n(client, input_buffer + got, get - got);
  873. got += gotmore;
  874. connection_ok = gotmore > 0;
  875. }
  876. in_bytes = got;
  877. in_remain -= got;
  878. bytes_read += got;
  879. }
  880. if (!connection_ok)
  881. {
  882. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  883. break;
  884. }
  885. switch (depth)
  886. {
  887. case 32:
  888. blue = input_buffer[in_idx++];
  889. green = input_buffer[in_idx++];
  890. red = input_buffer[in_idx++];
  891. in_idx++; // skip alpha
  892. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  893. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  894. color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  895. break;
  896. case 24:
  897. blue = input_buffer[in_idx++];
  898. green = input_buffer[in_idx++];
  899. red = input_buffer[in_idx++];
  900. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  901. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  902. color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  903. break;
  904. case 16:
  905. {
  906. uint8_t lsb = input_buffer[in_idx++];
  907. uint8_t msb = input_buffer[in_idx++];
  908. if (format == 0) // 555
  909. {
  910. blue = (lsb & 0x1F) << 3;
  911. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  912. red = (msb & 0x7C) << 1;
  913. color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  914. }
  915. else // 565
  916. {
  917. blue = (lsb & 0x1F) << 3;
  918. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  919. red = (msb & 0xF8);
  920. color = (msb << 8) | lsb;
  921. }
  922. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  923. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  924. }
  925. break;
  926. case 1:
  927. case 2:
  928. case 4:
  929. case 8:
  930. {
  931. if (0 == in_bits)
  932. {
  933. in_byte = input_buffer[in_idx++];
  934. in_bits = 8;
  935. }
  936. uint16_t pn = (in_byte >> bitshift) & bitmask;
  937. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  938. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  939. in_byte <<= depth;
  940. in_bits -= depth;
  941. color = rgb_palette_buffer[pn];
  942. }
  943. break;
  944. }
  945. if (with_color && has_multicolors)
  946. {
  947. // keep color
  948. }
  949. else if (whitish)
  950. {
  951. color = GxEPD_WHITE;
  952. }
  953. else if (colored && with_color)
  954. {
  955. color = GxEPD_COLORED;
  956. }
  957. else
  958. {
  959. color = GxEPD_BLACK;
  960. }
  961. uint16_t yrow = y + (flip ? h - row - 1 : row);
  962. display.drawPixel(x + col, yrow, color);
  963. } // end pixel
  964. } // end line
  965. }
  966. Serial.print("bytes read "); Serial.println(bytes_read);
  967. }
  968. }
  969. Serial.print("loaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  970. client.stop();
  971. if (!valid)
  972. {
  973. Serial.println("bitmap format not handled.");
  974. }
  975. }
  976. void showBitmapFrom_HTTP_Buffered(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  977. {
  978. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  979. display.setFullWindow();
  980. display.firstPage();
  981. do
  982. {
  983. drawBitmapFrom_HTTP_ToBuffer(host, path, filename, x, y, with_color);
  984. }
  985. while (display.nextPage());
  986. }
  987. 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)
  988. {
  989. // Use WiFiClientSecure class to create TLS connection
  990. #if defined (ESP8266) || defined(ARDUINO_RASPBERRY_PI_PICO_W)
  991. BearSSL::WiFiClientSecure client;
  992. BearSSL::X509List cert(certificate ? certificate : certificate_rawcontent);
  993. #else
  994. WiFiClientSecure client;
  995. #endif
  996. bool connection_ok = false;
  997. bool valid = false; // valid format to be handled
  998. bool flip = true; // bitmap is stored bottom-to-top
  999. uint32_t startTime = millis();
  1000. if ((x >= display.epd2.WIDTH) || (y >= display.epd2.HEIGHT)) return;
  1001. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  1002. Serial.print("connecting to "); Serial.println(host);
  1003. #if defined (ESP8266) || defined(ARDUINO_RASPBERRY_PI_PICO_W)
  1004. client.setBufferSizes(4096, 4096); // required
  1005. //client.setBufferSizes(8192, 4096); // may help for some sites
  1006. if (certificate) client.setTrustAnchors(&cert);
  1007. else if (fingerprint) client.setFingerprint(fingerprint);
  1008. else client.setInsecure();
  1009. #elif defined (ESP32)
  1010. if (certificate) client.setCACert(certificate);
  1011. #endif
  1012. if (!client.connect(host, httpsPort))
  1013. {
  1014. Serial.println("connection failed");
  1015. return;
  1016. }
  1017. Serial.print("requesting URL: ");
  1018. Serial.println(String("https://") + host + path + filename);
  1019. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  1020. "Host: " + host + "\r\n" +
  1021. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  1022. "Connection: close\r\n\r\n");
  1023. Serial.println("request sent");
  1024. while (client.connected())
  1025. {
  1026. String line = client.readStringUntil('\n');
  1027. if (!connection_ok)
  1028. {
  1029. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  1030. if (connection_ok) Serial.println(line);
  1031. //if (!connection_ok) Serial.println(line);
  1032. }
  1033. if (!connection_ok) Serial.println(line);
  1034. //Serial.println(line);
  1035. if (line == "\r")
  1036. {
  1037. Serial.println("headers received");
  1038. break;
  1039. }
  1040. }
  1041. if (!connection_ok) return;
  1042. // Parse BMP header
  1043. //if (read16(client) == 0x4D42) // BMP signature
  1044. uint16_t signature = 0;
  1045. for (int16_t i = 0; i < 50; i++)
  1046. {
  1047. if (!client.available()) delay(100);
  1048. else signature = read16(client);
  1049. //Serial.print("signature: 0x"); Serial.println(signature, HEX);
  1050. if (signature == 0x4D42) break;
  1051. }
  1052. if (signature == 0x4D42) // BMP signature
  1053. {
  1054. uint32_t fileSize = read32(client);
  1055. uint32_t creatorBytes = read32(client); (void)creatorBytes; //unused
  1056. uint32_t imageOffset = read32(client); // Start of image data
  1057. uint32_t headerSize = read32(client);
  1058. uint32_t width = read32(client);
  1059. int32_t height = (int32_t) read32(client);
  1060. uint16_t planes = read16(client);
  1061. uint16_t depth = read16(client); // bits per pixel
  1062. uint32_t format = read32(client);
  1063. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  1064. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  1065. {
  1066. Serial.print("File size: "); Serial.println(fileSize);
  1067. Serial.print("Image Offset: "); Serial.println(imageOffset);
  1068. Serial.print("Header size: "); Serial.println(headerSize);
  1069. Serial.print("Bit Depth: "); Serial.println(depth);
  1070. Serial.print("Image size: ");
  1071. Serial.print(width);
  1072. Serial.print('x');
  1073. Serial.println(height);
  1074. // BMP rows are padded (if needed) to 4-byte boundary
  1075. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  1076. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  1077. if (height < 0)
  1078. {
  1079. height = -height;
  1080. flip = false;
  1081. }
  1082. uint16_t w = width;
  1083. uint16_t h = height;
  1084. if ((x + w - 1) >= display.epd2.WIDTH) w = display.epd2.WIDTH - x;
  1085. if ((y + h - 1) >= display.epd2.HEIGHT) h = display.epd2.HEIGHT - y;
  1086. if (w <= max_row_width) // handle with direct drawing
  1087. {
  1088. valid = true;
  1089. uint8_t bitmask = 0xFF;
  1090. uint8_t bitshift = 8 - depth;
  1091. uint16_t red, green, blue;
  1092. bool whitish = false;
  1093. bool colored = false;
  1094. if (depth == 1) with_color = false;
  1095. if (depth <= 8)
  1096. {
  1097. if (depth < 8) bitmask >>= depth;
  1098. //bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  1099. bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
  1100. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  1101. {
  1102. blue = client.read();
  1103. green = client.read();
  1104. red = client.read();
  1105. client.read();
  1106. bytes_read += 4;
  1107. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1108. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1109. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  1110. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  1111. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  1112. color_palette_buffer[pn / 8] |= colored << pn % 8;
  1113. }
  1114. }
  1115. display.clearScreen();
  1116. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  1117. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  1118. bytes_read += skip(client, rowPosition - bytes_read);
  1119. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  1120. {
  1121. if (!connection_ok || !(client.connected() || client.available())) break;
  1122. delay(1); // yield() to avoid WDT
  1123. uint32_t in_remain = rowSize;
  1124. uint32_t in_idx = 0;
  1125. uint32_t in_bytes = 0;
  1126. uint8_t in_byte = 0; // for depth <= 8
  1127. uint8_t in_bits = 0; // for depth <= 8
  1128. uint8_t out_byte = 0xFF; // white (for w%8!=0 border)
  1129. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 border)
  1130. uint32_t out_idx = 0;
  1131. for (uint16_t col = 0; col < w; col++) // for each pixel
  1132. {
  1133. yield();
  1134. if (!connection_ok || !(client.connected() || client.available())) break;
  1135. // Time to read more pixel data?
  1136. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  1137. {
  1138. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  1139. uint32_t got = read8n(client, input_buffer, get);
  1140. while ((got < get) && connection_ok)
  1141. {
  1142. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  1143. //if ((get - got) > client.available()) delay(200); // does improve? yes, if >= 200
  1144. uint32_t gotmore = read8n(client, input_buffer + got, get - got);
  1145. got += gotmore;
  1146. connection_ok = gotmore > 0;
  1147. }
  1148. in_bytes = got;
  1149. in_remain -= got;
  1150. bytes_read += got;
  1151. }
  1152. if (!connection_ok)
  1153. {
  1154. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  1155. break;
  1156. }
  1157. switch (depth)
  1158. {
  1159. case 32:
  1160. blue = input_buffer[in_idx++];
  1161. green = input_buffer[in_idx++];
  1162. red = input_buffer[in_idx++];
  1163. in_idx++; // skip alpha
  1164. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1165. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1166. break;
  1167. case 24:
  1168. blue = input_buffer[in_idx++];
  1169. green = input_buffer[in_idx++];
  1170. red = input_buffer[in_idx++];
  1171. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1172. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1173. break;
  1174. case 16:
  1175. {
  1176. uint8_t lsb = input_buffer[in_idx++];
  1177. uint8_t msb = input_buffer[in_idx++];
  1178. if (format == 0) // 555
  1179. {
  1180. blue = (lsb & 0x1F) << 3;
  1181. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  1182. red = (msb & 0x7C) << 1;
  1183. }
  1184. else // 565
  1185. {
  1186. blue = (lsb & 0x1F) << 3;
  1187. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  1188. red = (msb & 0xF8);
  1189. }
  1190. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1191. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1192. }
  1193. break;
  1194. case 1:
  1195. case 2:
  1196. case 4:
  1197. case 8:
  1198. {
  1199. if (0 == in_bits)
  1200. {
  1201. in_byte = input_buffer[in_idx++];
  1202. in_bits = 8;
  1203. }
  1204. uint16_t pn = (in_byte >> bitshift) & bitmask;
  1205. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  1206. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  1207. in_byte <<= depth;
  1208. in_bits -= depth;
  1209. }
  1210. break;
  1211. }
  1212. if (whitish)
  1213. {
  1214. // keep white
  1215. }
  1216. else if (colored && with_color)
  1217. {
  1218. out_color_byte &= ~(0x80 >> col % 8); // colored
  1219. }
  1220. else
  1221. {
  1222. out_byte &= ~(0x80 >> col % 8); // black
  1223. }
  1224. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 border)
  1225. {
  1226. output_row_color_buffer[out_idx] = out_color_byte;
  1227. output_row_mono_buffer[out_idx++] = out_byte;
  1228. out_byte = 0xFF; // white (for w%8!=0 border)
  1229. out_color_byte = 0xFF; // white (for w%8!=0 border)
  1230. }
  1231. } // end pixel
  1232. int16_t yrow = y + (flip ? h - row - 1 : row);
  1233. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  1234. } // end line
  1235. Serial.print("downloaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  1236. Serial.print("bytes read "); Serial.println(bytes_read);
  1237. display.refresh();
  1238. }
  1239. }
  1240. }
  1241. client.stop();
  1242. if (!valid)
  1243. {
  1244. Serial.println("bitmap format not handled.");
  1245. }
  1246. }
  1247. 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)
  1248. {
  1249. // Use WiFiClientSecure class to create TLS connection
  1250. #if defined (ESP8266)
  1251. BearSSL::WiFiClientSecure client;
  1252. BearSSL::X509List cert(certificate ? certificate : certificate_rawcontent);
  1253. #else
  1254. WiFiClientSecure client;
  1255. #endif
  1256. bool connection_ok = false;
  1257. bool valid = false; // valid format to be handled
  1258. bool flip = true; // bitmap is stored bottom-to-top
  1259. bool has_multicolors = (display.epd2.panel == GxEPD2::ACeP565) || (display.epd2.panel == GxEPD2::GDEY073D46);
  1260. uint32_t startTime = millis();
  1261. if ((x >= display.width()) || (y >= display.height())) return;
  1262. display.fillScreen(GxEPD_WHITE);
  1263. Serial.print("connecting to "); Serial.println(host);
  1264. #if defined (ESP8266)
  1265. client.setBufferSizes(4096, 4096); // required
  1266. //client.setBufferSizes(8192, 4096); // may help for some sites
  1267. if (certificate) client.setTrustAnchors(&cert);
  1268. else if (fingerprint) client.setFingerprint(fingerprint);
  1269. else client.setInsecure();
  1270. #elif defined (ESP32)
  1271. if (certificate) client.setCACert(certificate);
  1272. #endif
  1273. if (!client.connect(host, httpsPort))
  1274. {
  1275. Serial.println("connection failed");
  1276. return;
  1277. }
  1278. Serial.print("requesting URL: ");
  1279. Serial.println(String("https://") + host + path + filename);
  1280. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  1281. "Host: " + host + "\r\n" +
  1282. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  1283. "Connection: close\r\n\r\n");
  1284. Serial.println("request sent");
  1285. while (client.connected())
  1286. {
  1287. String line = client.readStringUntil('\n');
  1288. if (!connection_ok)
  1289. {
  1290. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  1291. if (connection_ok) Serial.println(line);
  1292. //if (!connection_ok) Serial.println(line);
  1293. }
  1294. if (!connection_ok) Serial.println(line);
  1295. //Serial.println(line);
  1296. if (line == "\r")
  1297. {
  1298. Serial.println("headers received");
  1299. break;
  1300. }
  1301. }
  1302. if (!connection_ok) return;
  1303. // Parse BMP header
  1304. //if (read16(client) == 0x4D42) // BMP signature
  1305. uint16_t signature = 0;
  1306. for (int16_t i = 0; i < 50; i++)
  1307. {
  1308. if (!client.available()) delay(100);
  1309. else signature = read16(client);
  1310. if (signature == 0x4D42)
  1311. {
  1312. //Serial.print("signature wait loops: "); Serial.println(i);
  1313. break;
  1314. }
  1315. }
  1316. if (signature == 0x4D42) // BMP signature
  1317. {
  1318. uint32_t fileSize = read32(client);
  1319. uint32_t creatorBytes = read32(client); (void)creatorBytes; //unused
  1320. uint32_t imageOffset = read32(client); // Start of image data
  1321. uint32_t headerSize = read32(client);
  1322. uint32_t width = read32(client);
  1323. int32_t height = (int32_t) read32(client);
  1324. uint16_t planes = read16(client);
  1325. uint16_t depth = read16(client); // bits per pixel
  1326. uint32_t format = read32(client);
  1327. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  1328. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  1329. {
  1330. Serial.print("File size: "); Serial.println(fileSize);
  1331. Serial.print("Image Offset: "); Serial.println(imageOffset);
  1332. Serial.print("Header size: "); Serial.println(headerSize);
  1333. Serial.print("Bit Depth: "); Serial.println(depth);
  1334. Serial.print("Image size: ");
  1335. Serial.print(width);
  1336. Serial.print('x');
  1337. Serial.println(height);
  1338. // BMP rows are padded (if needed) to 4-byte boundary
  1339. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  1340. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  1341. if (height < 0)
  1342. {
  1343. height = -height;
  1344. flip = false;
  1345. }
  1346. uint16_t w = width;
  1347. uint16_t h = height;
  1348. if ((x + w - 1) >= display.width()) w = display.width() - x;
  1349. if ((y + h - 1) >= display.height()) h = display.height() - y;
  1350. //if (w <= max_row_width) // handle with direct drawing
  1351. {
  1352. valid = true;
  1353. uint8_t bitmask = 0xFF;
  1354. uint8_t bitshift = 8 - depth;
  1355. uint16_t red, green, blue;
  1356. bool whitish = false;
  1357. bool colored = false;
  1358. if (depth == 1) with_color = false;
  1359. if (depth <= 8)
  1360. {
  1361. if (depth < 8) bitmask >>= depth;
  1362. //bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  1363. bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
  1364. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  1365. {
  1366. blue = client.read();
  1367. green = client.read();
  1368. red = client.read();
  1369. client.read();
  1370. bytes_read += 4;
  1371. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1372. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1373. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  1374. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  1375. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  1376. color_palette_buffer[pn / 8] |= colored << pn % 8;
  1377. //Serial.print("0x00"); Serial.print(red, HEX); Serial.print(green, HEX); Serial.print(blue, HEX);
  1378. //Serial.print(" : "); Serial.print(whitish); Serial.print(", "); Serial.println(colored);
  1379. rgb_palette_buffer[pn] = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  1380. }
  1381. }
  1382. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  1383. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  1384. bytes_read += skip(client, rowPosition - bytes_read);
  1385. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  1386. {
  1387. if (!connection_ok || !(client.connected() || client.available())) break;
  1388. delay(1); // yield() to avoid WDT
  1389. uint32_t in_remain = rowSize;
  1390. uint32_t in_idx = 0;
  1391. uint32_t in_bytes = 0;
  1392. uint8_t in_byte = 0; // for depth <= 8
  1393. uint8_t in_bits = 0; // for depth <= 8
  1394. uint16_t color = GxEPD_WHITE;
  1395. for (uint16_t col = 0; col < w; col++) // for each pixel
  1396. {
  1397. yield();
  1398. if (!connection_ok || !(client.connected() || client.available())) break;
  1399. // Time to read more pixel data?
  1400. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  1401. {
  1402. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  1403. uint32_t got = read8n(client, input_buffer, get);
  1404. while ((got < get) && connection_ok)
  1405. {
  1406. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  1407. uint32_t gotmore = read8n(client, input_buffer + got, get - got);
  1408. got += gotmore;
  1409. connection_ok = gotmore > 0;
  1410. }
  1411. in_bytes = got;
  1412. in_remain -= got;
  1413. bytes_read += got;
  1414. }
  1415. if (!connection_ok)
  1416. {
  1417. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  1418. break;
  1419. }
  1420. switch (depth)
  1421. {
  1422. case 32:
  1423. blue = input_buffer[in_idx++];
  1424. green = input_buffer[in_idx++];
  1425. red = input_buffer[in_idx++];
  1426. in_idx++; // skip alpha
  1427. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1428. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1429. color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  1430. break;
  1431. case 24:
  1432. blue = input_buffer[in_idx++];
  1433. green = input_buffer[in_idx++];
  1434. red = input_buffer[in_idx++];
  1435. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1436. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1437. color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  1438. break;
  1439. case 16:
  1440. {
  1441. uint8_t lsb = input_buffer[in_idx++];
  1442. uint8_t msb = input_buffer[in_idx++];
  1443. if (format == 0) // 555
  1444. {
  1445. blue = (lsb & 0x1F) << 3;
  1446. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  1447. red = (msb & 0x7C) << 1;
  1448. color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  1449. }
  1450. else // 565
  1451. {
  1452. blue = (lsb & 0x1F) << 3;
  1453. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  1454. red = (msb & 0xF8);
  1455. color = (msb << 8) | lsb;
  1456. }
  1457. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1458. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1459. }
  1460. break;
  1461. case 1:
  1462. case 2:
  1463. case 4:
  1464. case 8:
  1465. {
  1466. if (0 == in_bits)
  1467. {
  1468. in_byte = input_buffer[in_idx++];
  1469. in_bits = 8;
  1470. }
  1471. uint16_t pn = (in_byte >> bitshift) & bitmask;
  1472. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  1473. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  1474. in_byte <<= depth;
  1475. in_bits -= depth;
  1476. color = rgb_palette_buffer[pn];
  1477. }
  1478. break;
  1479. }
  1480. if (with_color && has_multicolors)
  1481. {
  1482. // keep color
  1483. }
  1484. else if (whitish)
  1485. {
  1486. color = GxEPD_WHITE;
  1487. }
  1488. else if (colored && with_color)
  1489. {
  1490. color = GxEPD_COLORED;
  1491. }
  1492. else
  1493. {
  1494. color = GxEPD_BLACK;
  1495. }
  1496. uint16_t yrow = y + (flip ? h - row - 1 : row);
  1497. display.drawPixel(x + col, yrow, color);
  1498. } // end pixel
  1499. } // end line
  1500. }
  1501. Serial.print("bytes read "); Serial.println(bytes_read);
  1502. }
  1503. }
  1504. Serial.print("loaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  1505. client.stop();
  1506. if (!valid)
  1507. {
  1508. Serial.println("bitmap format not handled.");
  1509. }
  1510. }
  1511. 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)
  1512. {
  1513. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  1514. display.setFullWindow();
  1515. display.firstPage();
  1516. do
  1517. {
  1518. drawBitmapFrom_HTTPS_ToBuffer(host, path, filename, fingerprint, x, y, with_color, certificate);
  1519. }
  1520. while (display.nextPage());
  1521. }
  1522. uint16_t read16(WiFiClient& client)
  1523. {
  1524. // BMP data is stored little-endian, same as Arduino.
  1525. uint16_t result;
  1526. ((uint8_t *)&result)[0] = client.read(); // LSB
  1527. ((uint8_t *)&result)[1] = client.read(); // MSB
  1528. return result;
  1529. }
  1530. uint32_t read32(WiFiClient& client)
  1531. {
  1532. // BMP data is stored little-endian, same as Arduino.
  1533. uint32_t result;
  1534. ((uint8_t *)&result)[0] = client.read(); // LSB
  1535. ((uint8_t *)&result)[1] = client.read();
  1536. ((uint8_t *)&result)[2] = client.read();
  1537. ((uint8_t *)&result)[3] = client.read(); // MSB
  1538. return result;
  1539. }
  1540. #if USE_BearSSL
  1541. uint32_t skip(BearSSL::WiFiClientSecure& client, int32_t bytes)
  1542. {
  1543. int32_t remain = bytes;
  1544. uint32_t start = millis();
  1545. while ((client.connected() || client.available()) && (remain > 0))
  1546. {
  1547. if (client.available())
  1548. {
  1549. client.read();
  1550. remain--;
  1551. }
  1552. else delay(1);
  1553. if (millis() - start > 2000) break; // don't hang forever
  1554. }
  1555. return bytes - remain;
  1556. }
  1557. uint32_t read8n(BearSSL::WiFiClientSecure& client, uint8_t* buffer, int32_t bytes)
  1558. {
  1559. int32_t remain = bytes;
  1560. uint32_t start = millis();
  1561. while ((client.connected() || client.available()) && (remain > 0))
  1562. {
  1563. if (client.available())
  1564. {
  1565. int16_t v = client.read();
  1566. *buffer++ = uint8_t(v);
  1567. remain--;
  1568. }
  1569. else delay(1);
  1570. if (millis() - start > 2000) break; // don't hang forever
  1571. }
  1572. return bytes - remain;
  1573. }
  1574. #endif
  1575. uint32_t skip(WiFiClient& client, int32_t bytes)
  1576. {
  1577. int32_t remain = bytes;
  1578. uint32_t start = millis();
  1579. while ((client.connected() || client.available()) && (remain > 0))
  1580. {
  1581. if (client.available())
  1582. {
  1583. client.read();
  1584. remain--;
  1585. }
  1586. else delay(1);
  1587. if (millis() - start > 2000) break; // don't hang forever
  1588. }
  1589. return bytes - remain;
  1590. }
  1591. uint32_t read8n(WiFiClient& client, uint8_t* buffer, int32_t bytes)
  1592. {
  1593. int32_t remain = bytes;
  1594. uint32_t start = millis();
  1595. while ((client.connected() || client.available()) && (remain > 0))
  1596. {
  1597. if (client.available())
  1598. {
  1599. int16_t v = client.read();
  1600. *buffer++ = uint8_t(v);
  1601. remain--;
  1602. }
  1603. else delay(1);
  1604. if (millis() - start > 2000) break; // don't hang forever
  1605. }
  1606. return bytes - remain;
  1607. }
  1608. // Set time via NTP, as required for x.509 validation
  1609. void setClock()
  1610. {
  1611. configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
  1612. Serial.print("Waiting for NTP time sync: ");
  1613. time_t now = time(nullptr);
  1614. while (now < 8 * 3600 * 2)
  1615. {
  1616. delay(500);
  1617. Serial.print(".");
  1618. now = time(nullptr);
  1619. }
  1620. Serial.println("");
  1621. struct tm timeinfo;
  1622. gmtime_r(&now, &timeinfo);
  1623. Serial.print("Current time: ");
  1624. Serial.print(asctime(&timeinfo));
  1625. }