GxEPD2_WiFi_Example.ino 64 KB

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