GxEPD2_WiFi_Example.ino 64 KB

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