GxEPD2_WiFi_Example.ino 76 KB

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