GxEPD2_WiFi_Example.ino 67 KB

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