GxEPD2_WiFi_Example.ino 66 KB

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