GxEPD2_WiFi_Example.ino 69 KB

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