GxEPD2_WiFi_Example.ino 64 KB

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