GxEPD2_WiFi_Example.ino 62 KB

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