GxEPD2_WiFi_Example.ino 66 KB

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