GxEPD2_WiFi_Example.ino 63 KB

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