GxEPD2_WiFi_Example.ino 76 KB

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