GxEPD2_WiFi_Example.ino 71 KB

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