GxEPD2_Spiffs_Example.ino 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. // GxEPD2_Spiffs_Example : Display Library example for SPI e-paper panels from Dalian Good Display and boards from Waveshare.
  2. // Requires HW SPI and Adafruit_GFX. Caution: the e-paper panels require 3.3V supply AND data lines!
  3. //
  4. // Display Library based on Demo Example from Good Display: http://www.e-paper-display.com/download_list/downloadcategoryid=34&isMode=false.html
  5. //
  6. // BMP handling code extracts taken from: https://github.com/prenticedavid/MCUFRIEND_kbv/tree/master/examples/showBMP_kbv_Uno
  7. //
  8. // Author: Jean-Marc Zingg
  9. //
  10. // Version: see library.properties
  11. //
  12. // Library: https://github.com/ZinggJM/GxEPD2
  13. //
  14. // note that BMP bitmaps are drawn at physical position in physical orientation of the screen
  15. // Supporting Arduino Forum Topics:
  16. // Waveshare e-paper displays with SPI: http://forum.arduino.cc/index.php?topic=487007.0
  17. // Good Display ePaper for Arduino: https://forum.arduino.cc/index.php?topic=436411.0
  18. // mapping suggestion from Waveshare SPI e-Paper to Wemos D1 mini
  19. // BUSY -> D2, RST -> D4, DC -> D3, CS -> D8, CLK -> D5, DIN -> D7, GND -> GND, 3.3V -> 3.3V
  20. // NOTE: connect 3.3k pull-down from D8 to GND if your board or shield has level converters
  21. // NOTE for ESP8266: using SS (GPIO15) for CS may cause boot mode problems, use different pin in case, or 4k7 pull-down
  22. // mapping suggestion from Waveshare SPI e-Paper to generic ESP8266
  23. // BUSY -> GPIO4, RST -> GPIO2, DC -> GPIO0, CS -> GPIO15, CLK -> GPIO14, DIN -> GPIO13, GND -> GND, 3.3V -> 3.3V
  24. // NOTE: connect 3.3k pull-down from GPIO15 to GND if your board or shield has level converters
  25. // NOTE for ESP8266: using SS (GPIO15) for CS may cause boot mode problems, use different pin in case, or 3.3k pull-down
  26. // mapping of Waveshare e-Paper ESP8266 Driver Board, new version (see GxEPD2_boards_added.h for old version)
  27. // BUSY -> GPIO5, RST -> GPIO2, DC -> GPIO4, CS -> GPIO15, CLK -> GPIO14, DIN -> GPIO13, GND -> GND, 3.3V -> 3.3V
  28. // NOTE for ESP8266: using SS (GPIO15) for CS may cause boot mode problems, add a 3.3k pull-down in case
  29. // the e-Paper ESP8266 Driver Board should have no boot mode issue, as it doesn't use level converters
  30. // mapping suggestion for ESP32, e.g. LOLIN32, see .../variants/.../pins_arduino.h for your board
  31. // NOTE: there are variants with different pins for SPI ! CHECK SPI PINS OF YOUR BOARD
  32. // BUSY -> 4, RST -> 16, DC -> 17, CS -> SS(5), CLK -> SCK(18), DIN -> MOSI(23), GND -> GND, 3.3V -> 3.3V
  33. // mapping of Waveshare ESP32 Driver Board
  34. // BUSY -> 25, RST -> 26, DC -> 27, CS-> 15, CLK -> 13, DIN -> 14
  35. // NOTE: this board uses "unusual" SPI pins and requires re-mapping of HW SPI to these pins in SPIClass
  36. // see example GxEPD2_WS_ESP32_Driver.ino, it shows how this can be done easily
  37. // new mapping suggestion for STM32F1, e.g. STM32F103C8T6 "BluePill"
  38. // BUSY -> A1, RST -> A2, DC -> A3, CS-> A4, CLK -> A5, DIN -> A7
  39. // mapping suggestion for AVR, UNO, NANO etc.
  40. // BUSY -> 7, RST -> 9, DC -> 8, CS-> 10, CLK -> 13, DIN -> 11
  41. // mapping of Waveshare Universal e-Paper Raw Panel Driver Shield for Arduino / NUCLEO
  42. // BUSY -> 7, RST -> 8, DC -> 9, CS-> 10, CLK -> 13, DIN -> 11
  43. // mapping suggestion for Arduino MEGA
  44. // BUSY -> 7, RST -> 9, DC -> 8, CS-> 53, CLK -> 52, DIN -> 51
  45. // base class GxEPD2_GFX can be used to pass references or pointers to the display instance as parameter, uses ~1.2k more code
  46. // enable or disable GxEPD2_GFX base class
  47. #define ENABLE_GxEPD2_GFX 0
  48. // uncomment next line to use class GFX of library GFX_Root instead of Adafruit_GFX
  49. //#include <GFX.h>
  50. // Note: if you use this with ENABLE_GxEPD2_GFX 1:
  51. // uncomment it in GxEPD2_GFX.h too, or add #include <GFX.h> before any #include <GxEPD2_GFX.h>
  52. #include <GxEPD2_BW.h>
  53. #include <GxEPD2_3C.h>
  54. #include <GxEPD2_7C.h>
  55. #include <Fonts/FreeMonoBold9pt7b.h>
  56. #if defined(ESP32)
  57. #include "SPIFFS.h"
  58. #endif
  59. #include <FS.h>
  60. #define FileClass fs::File
  61. #if defined (ESP8266)
  62. // select one and adapt to your mapping, can use full buffer size (full HEIGHT)
  63. //GxEPD2_BW<GxEPD2_154, GxEPD2_154::HEIGHT> display(GxEPD2_154(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEP015OC1 no longer available
  64. //GxEPD2_BW<GxEPD2_154_D67, GxEPD2_154_D67::HEIGHT> display(GxEPD2_154_D67(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEH0154D67
  65. //GxEPD2_BW<GxEPD2_154_T8, GxEPD2_154_T8::HEIGHT> display(GxEPD2_154_T8(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEW0154T8 152x152
  66. //GxEPD2_BW<GxEPD2_154_M09, GxEPD2_154_M09::HEIGHT> display(GxEPD2_154_M09(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEW0154M09 200x200
  67. //GxEPD2_BW<GxEPD2_154_M10, GxEPD2_154_M10::HEIGHT> display(GxEPD2_154_M10(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEW0154M10 152x152
  68. //GxEPD2_BW<GxEPD2_213, GxEPD2_213::HEIGHT> display(GxEPD2_213(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDE0213B1, phased out
  69. //GxEPD2_BW<GxEPD2_213_B72, GxEPD2_213_B72::HEIGHT> display(GxEPD2_213_B72(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEH0213B72
  70. //GxEPD2_BW<GxEPD2_213_B73, GxEPD2_213_B73::HEIGHT> display(GxEPD2_213_B73(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEH0213B73
  71. //GxEPD2_BW<GxEPD2_213_flex, GxEPD2_213_flex::HEIGHT> display(GxEPD2_213_flex(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEW0213I5F
  72. //GxEPD2_BW<GxEPD2_213_M21, GxEPD2_213_M21::HEIGHT> display(GxEPD2_213_M21(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEW0213M21
  73. //GxEPD2_BW<GxEPD2_290, GxEPD2_290::HEIGHT> display(GxEPD2_290(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  74. //GxEPD2_BW<GxEPD2_290_T5, GxEPD2_290_T5::HEIGHT> display(GxEPD2_290_T5(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEW029T5
  75. //GxEPD2_BW<GxEPD2_290_M06, GxEPD2_290_M06::HEIGHT> display(GxEPD2_290_M06(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEW029M06
  76. //GxEPD2_BW<GxEPD2_260, GxEPD2_260::HEIGHT> display(GxEPD2_260(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  77. //GxEPD2_BW<GxEPD2_270, GxEPD2_270::HEIGHT> display(GxEPD2_270(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  78. //GxEPD2_BW<GxEPD2_371, GxEPD2_371::HEIGHT> display(GxEPD2_371(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  79. //GxEPD2_BW<GxEPD2_420, GxEPD2_420::HEIGHT> display(GxEPD2_420(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  80. //GxEPD2_BW<GxEPD2_420_M01, GxEPD2_420_M01::HEIGHT> display(GxEPD2_420_M01(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEW042M01
  81. // can use only half buffer size
  82. //GxEPD2_BW < GxEPD2_583, GxEPD2_583::HEIGHT / 2 > display(GxEPD2_583(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  83. //GxEPD2_BW < GxEPD2_583_T8, GxEPD2_583_T8::HEIGHT / 2 > display(GxEPD2_583_T8(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  84. //GxEPD2_BW < GxEPD2_750, GxEPD2_750::HEIGHT / 2 > display(GxEPD2_750(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  85. //GxEPD2_BW < GxEPD2_750_T7, GxEPD2_750_T7::HEIGHT / 2 > display(GxEPD2_750_T7(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEW075T7 800x480
  86. // 3-color e-papers
  87. //GxEPD2_3C<GxEPD2_154c, GxEPD2_154c::HEIGHT> display(GxEPD2_154c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEW0154Z04 no longer available
  88. //GxEPD2_3C<GxEPD2_154_Z90c, GxEPD2_154_Z90c::HEIGHT> display(GxEPD2_154_Z90c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEH0154Z90
  89. //GxEPD2_3C<GxEPD2_213c, GxEPD2_213c::HEIGHT> display(GxEPD2_213c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  90. //GxEPD2_3C<GxEPD2_290c, GxEPD2_290c::HEIGHT> display(GxEPD2_290c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  91. //GxEPD2_3C<GxEPD2_270c, GxEPD2_270c::HEIGHT> display(GxEPD2_270c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  92. // can use only half buffer size
  93. //GxEPD2_3C<GxEPD2_420c, GxEPD2_420c::HEIGHT / 2> display(GxEPD2_420c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  94. // can use only quarter buffer size
  95. //GxEPD2_3C < GxEPD2_583c, GxEPD2_583c::HEIGHT / 4 > display(GxEPD2_583c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  96. //GxEPD2_3C<GxEPD2_750c, GxEPD2_750c::HEIGHT / 4> display(GxEPD2_750c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  97. //GxEPD2_3C < GxEPD2_750c_Z08, GxEPD2_750c_Z08::HEIGHT / 4 > display(GxEPD2_750c_Z08(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEW075Z08 800x480
  98. //GxEPD2_3C < GxEPD2_750c_Z90, GxEPD2_750c_Z90::HEIGHT / 4 > display(GxEPD2_750c_Z90(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEH075Z90 880x528
  99. // 7-color e-paper
  100. //GxEPD2_3C < GxEPD2_565c, GxEPD2_565c::HEIGHT / 4 > display(GxEPD2_565c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // Waveshare 5.65" 7-color (3C graphics)
  101. //GxEPD2_7C < GxEPD2_565c, GxEPD2_565c::HEIGHT / 4 > display(GxEPD2_565c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // Waveshare 5.65" 7-color
  102. // grey levels parallel IF e-papers on Waveshare e-Paper IT8951 Driver HAT
  103. // HRDY -> 4, RST -> 2, CS -> SS(15), SCK -> SCK(14), MOSI -> MOSI(D7(13)), MISO -> MISO(D6(12)), GND -> GND, 5V -> 5V
  104. // note: 5V supply needs to be exact and strong; 5V pin of USB powered Wemos D1 mini doesn't work!
  105. //GxEPD2_BW<GxEPD2_it60, GxEPD2_it60::HEIGHT / 8> display(GxEPD2_it60(/*CS=5*/ SS, /*DC=*/ 0, /*RST=*/ 2, /*BUSY=*/ 4));
  106. //GxEPD2_BW < GxEPD2_it60_1448x1072, GxEPD2_it60_1448x1072::HEIGHT / 8 > display(GxEPD2_it60_1448x1072(/*CS=5*/ SS, /*DC=*/ 0, /*RST=*/ 2, /*BUSY=*/ 4));
  107. // ***** for mapping of Waveshare e-Paper ESP8266 Driver Board *****
  108. // select one , can use full buffer size (full HEIGHT)
  109. //GxEPD2_BW<GxEPD2_154, GxEPD2_154::HEIGHT> display(GxEPD2_154(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEP015OC1 no longer available
  110. //GxEPD2_BW<GxEPD2_154_D67, GxEPD2_154_D67::HEIGHT> display(GxEPD2_154_D67(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEH0154D67
  111. //GxEPD2_BW<GxEPD2_154_T8, GxEPD2_154_T8::HEIGHT> display(GxEPD2_154_T8(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEW0154T8 152x152
  112. //GxEPD2_BW<GxEPD2_154_M09, GxEPD2_154_M09::HEIGHT> display(GxEPD2_154_M09(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEW0154M09 200x200
  113. //GxEPD2_BW<GxEPD2_154_M10, GxEPD2_154_M10::HEIGHT> display(GxEPD2_154_M10(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEW0154M10 152x152
  114. //GxEPD2_BW<GxEPD2_213, GxEPD2_213::HEIGHT> display(GxEPD2_213(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDE0213B1, phased out
  115. //GxEPD2_BW<GxEPD2_213_B72, GxEPD2_213_B72::HEIGHT> display(GxEPD2_213_B72(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEH0213B72
  116. //GxEPD2_BW<GxEPD2_213_B73, GxEPD2_213_B73::HEIGHT> display(GxEPD2_213_B73(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEH0213B73
  117. //GxEPD2_BW<GxEPD2_213_flex, GxEPD2_213_flex::HEIGHT> display(GxEPD2_213_flex(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEW0213I5F
  118. //GxEPD2_BW<GxEPD2_213_M21, GxEPD2_213_M21::HEIGHT> display(GxEPD2_213_M21(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEW0213M21
  119. //GxEPD2_BW<GxEPD2_290, GxEPD2_290::HEIGHT> display(GxEPD2_290(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  120. //GxEPD2_BW<GxEPD2_290_T5, GxEPD2_290_T5::HEIGHT> display(GxEPD2_290_T5(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEW029T5
  121. //GxEPD2_BW<GxEPD2_290_M06, GxEPD2_290_M06::HEIGHT> display(GxEPD2_290_M06(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEW029M06
  122. //GxEPD2_BW<GxEPD2_260, GxEPD2_260::HEIGHT> display(GxEPD2_260(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  123. //GxEPD2_BW<GxEPD2_270, GxEPD2_270::HEIGHT> display(GxEPD2_270(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  124. //GxEPD2_BW<GxEPD2_371, GxEPD2_371::HEIGHT> display(GxEPD2_371(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  125. //GxEPD2_BW<GxEPD2_420, GxEPD2_420::HEIGHT> display(GxEPD2_420(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  126. //GxEPD2_BW<GxEPD2_420_M01, GxEPD2_420_M01::HEIGHT> display(GxEPD2_420_M01(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEW042M01
  127. // can use only half buffer size
  128. //GxEPD2_BW < GxEPD2_583, GxEPD2_583::HEIGHT / 2 > display(GxEPD2_583(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  129. //GxEPD2_BW < GxEPD2_583_T8, GxEPD2_583_T8::HEIGHT / 2 > display(GxEPD2_583_T8(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  130. //GxEPD2_BW < GxEPD2_750, GxEPD2_750::HEIGHT / 2 > display(GxEPD2_750(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  131. //GxEPD2_BW < GxEPD2_750_T7, GxEPD2_750_T7::HEIGHT / 2 > display(GxEPD2_750_T7(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEW075T7 800x480
  132. // 3-color e-papers
  133. //GxEPD2_3C<GxEPD2_154c, GxEPD2_154c::HEIGHT> display(GxEPD2_154c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEW0154Z04 no longer available
  134. //GxEPD2_3C<GxEPD2_154_Z90c, GxEPD2_154_Z90c::HEIGHT> display(GxEPD2_154_Z90c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEH0154Z90
  135. //GxEPD2_3C<GxEPD2_213c, GxEPD2_213c::HEIGHT> display(GxEPD2_213c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  136. //GxEPD2_3C<GxEPD2_290c, GxEPD2_290c::HEIGHT> display(GxEPD2_290c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  137. //GxEPD2_3C<GxEPD2_270c, GxEPD2_270c::HEIGHT> display(GxEPD2_270c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  138. // can use only half buffer size
  139. //GxEPD2_3C<GxEPD2_420c, GxEPD2_420c::HEIGHT / 2> display(GxEPD2_420c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  140. // can use only quarter buffer size
  141. //GxEPD2_3C<GxEPD2_583c, GxEPD2_583c::HEIGHT / 4> display(GxEPD2_583c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  142. //GxEPD2_3C<GxEPD2_750c, GxEPD2_750c::HEIGHT / 4> display(GxEPD2_750c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  143. //GxEPD2_3C<GxEPD2_750c_Z08, GxEPD2_750c_Z08::HEIGHT / 4> display(GxEPD2_750c_Z08(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEW075Z08 800x480
  144. //GxEPD2_3C<GxEPD2_750c_Z90, GxEPD2_750c_Z90::HEIGHT / 4> display(GxEPD2_750c_Z90(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEH075Z90 880x528
  145. // 7-color e-paper
  146. //GxEPD2_7C < GxEPD2_565c, GxEPD2_565c::HEIGHT / 4 > display(GxEPD2_565c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // Waveshare 5.65" 7-color
  147. #endif
  148. #if defined(ESP32)
  149. // select one and adapt to your mapping, can use full buffer size (full HEIGHT)
  150. //GxEPD2_BW<GxEPD2_154, GxEPD2_154::HEIGHT> display(GxEPD2_154(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEP015OC1 no longer available
  151. //GxEPD2_BW<GxEPD2_154_D67, GxEPD2_154_D67::HEIGHT> display(GxEPD2_154_D67(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEH0154D67
  152. //GxEPD2_BW<GxEPD2_154_T8, GxEPD2_154_T8::HEIGHT> display(GxEPD2_154_T8(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEW0154T8 152x152
  153. //GxEPD2_BW<GxEPD2_154_M09, GxEPD2_154_M09::HEIGHT> display(GxEPD2_154_M09(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEW0154M09 200x200
  154. //GxEPD2_BW<GxEPD2_154_M10, GxEPD2_154_M10::HEIGHT> display(GxEPD2_154_M10(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEW0154M10 152x152
  155. //GxEPD2_BW<GxEPD2_213, GxEPD2_213::HEIGHT> display(GxEPD2_213(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDE0213B1, phased out
  156. //GxEPD2_BW<GxEPD2_213_B72, GxEPD2_213_B72::HEIGHT> display(GxEPD2_213_B72(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEH0213B72
  157. //GxEPD2_BW<GxEPD2_213_B73, GxEPD2_213_B73::HEIGHT> display(GxEPD2_213_B73(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEH0213B73
  158. //GxEPD2_BW<GxEPD2_213_flex, GxEPD2_213_flex::HEIGHT> display(GxEPD2_213_flex(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEW0213I5F
  159. //GxEPD2_BW<GxEPD2_213_M21, GxEPD2_213_M21::HEIGHT> display(GxEPD2_213_M21(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEW0213M21
  160. //GxEPD2_BW<GxEPD2_290, GxEPD2_290::HEIGHT> display(GxEPD2_290(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  161. //GxEPD2_BW<GxEPD2_290_T5, GxEPD2_290_T5::HEIGHT> display(GxEPD2_290_T5(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEW029T5
  162. //GxEPD2_BW<GxEPD2_290_M06, GxEPD2_290_M06::HEIGHT> display(GxEPD2_290_M06(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEW029M06
  163. //GxEPD2_BW<GxEPD2_260, GxEPD2_260::HEIGHT> display(GxEPD2_260(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  164. //GxEPD2_BW<GxEPD2_270, GxEPD2_270::HEIGHT> display(GxEPD2_270(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  165. //GxEPD2_BW<GxEPD2_371, GxEPD2_371::HEIGHT> display(GxEPD2_371(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  166. //GxEPD2_BW<GxEPD2_420, GxEPD2_420::HEIGHT> display(GxEPD2_420(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  167. //GxEPD2_BW<GxEPD2_420_M01, GxEPD2_420_M01::HEIGHT> display(GxEPD2_420_M01(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEW042M01
  168. //GxEPD2_BW<GxEPD2_583, GxEPD2_583::HEIGHT> display(GxEPD2_583(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  169. //GxEPD2_BW<GxEPD2_583_T8, GxEPD2_583_T8::HEIGHT> display(GxEPD2_583_T8(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  170. //GxEPD2_BW<GxEPD2_750, GxEPD2_750::HEIGHT> display(GxEPD2_750(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  171. //GxEPD2_BW<GxEPD2_750_T7, GxEPD2_750_T7::HEIGHT> display(GxEPD2_750_T7(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEW075T7 800x480
  172. // 3-color e-papers
  173. //GxEPD2_3C<GxEPD2_154c, GxEPD2_154c::HEIGHT> display(GxEPD2_154c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEW0154Z04 no longer available
  174. //GxEPD2_3C<GxEPD2_154_Z90c, GxEPD2_154_Z90c::HEIGHT> display(GxEPD2_154_Z90c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEH0154Z90
  175. //GxEPD2_3C<GxEPD2_213c, GxEPD2_213c::HEIGHT> display(GxEPD2_213c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  176. //GxEPD2_3C<GxEPD2_290c, GxEPD2_290c::HEIGHT> display(GxEPD2_290c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  177. //GxEPD2_3C<GxEPD2_270c, GxEPD2_270c::HEIGHT> display(GxEPD2_270c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  178. //GxEPD2_3C<GxEPD2_420c, GxEPD2_420c::HEIGHT> display(GxEPD2_420c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  179. //GxEPD2_3C<GxEPD2_583c, GxEPD2_583c::HEIGHT> display(GxEPD2_583c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  180. //GxEPD2_3C<GxEPD2_750c, GxEPD2_750c::HEIGHT> display(GxEPD2_750c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  181. //GxEPD2_3C<GxEPD2_750c_Z08, GxEPD2_750c_Z08::HEIGHT> display(GxEPD2_750c_Z08(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEW075Z08 800x480
  182. //GxEPD2_3C<GxEPD2_750c_Z90, GxEPD2_750c_Z90::HEIGHT / 2> display(GxEPD2_750c_Z90(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEH075Z90 880x528
  183. // 7-color e-paper
  184. //GxEPD2_3C < GxEPD2_565c, GxEPD2_565c::HEIGHT / 2 > display(GxEPD2_565c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // Waveshare 5.65" 7-color (3C graphics)
  185. //GxEPD2_7C < GxEPD2_565c, GxEPD2_565c::HEIGHT / 2 > display(GxEPD2_565c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // Waveshare 5.65" 7-color
  186. // grey levels parallel IF e-papers on Waveshare e-Paper IT8951 Driver HAT
  187. // HRDY -> 4, RST -> 16, CS -> SS(5), SCK -> SCK(18), MOSI -> MOSI(23), MISO -> MISO(19), GND -> GND, 5V -> 5V
  188. // note: 5V supply needs to be exact and strong; 5V over diode from USB (e.g. Wemos D1 mini) doesn't work!
  189. //GxEPD2_BW<GxEPD2_it60, GxEPD2_it60::HEIGHT> display(GxEPD2_it60(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  190. //GxEPD2_BW < GxEPD2_it60_1448x1072, GxEPD2_it60_1448x1072::HEIGHT / 4 > display(GxEPD2_it60_1448x1072(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  191. // Waveshare 12.48 b/w SPI display board and frame or Good Display 12.48 b/w panel GDEW1248T3
  192. // general constructor for use with all parameters, e.g. for Waveshare ESP32 driver board mounted on connection board
  193. //GxEPD2_BW < GxEPD2_1248, GxEPD2_1248::HEIGHT / 4 >
  194. //display(GxEPD2_1248(/*sck=*/ 13, /*miso=*/ 12, /*mosi=*/ 14, /*cs_m1=*/ 23, /*cs_s1=*/ 22, /*cs_m2=*/ 16, /*cs_s2=*/ 19,
  195. // /*dc1=*/ 25, /*dc2=*/ 17, /*rst1=*/ 33, /*rst2=*/ 5, /*busy_m1=*/ 32, /*busy_s1=*/ 26, /*busy_m2=*/ 18, /*busy_s2=*/ 4));
  196. #endif
  197. // function declaration with default parameter
  198. // note that BMP bitmaps are drawn at physical position in physical orientation of the screen
  199. void drawBitmapFromSpiffs(const char *filename, int16_t x, int16_t y, bool with_color = true);
  200. // bitmap drawing using buffered graphics, e.g. for small bitmaps or for GxEPD2_154c
  201. // draws BMP bitmap according to set orientation
  202. // partial_update selects refresh mode (not effective for GxEPD2_154c)
  203. // overwrite = true does not clear buffer before drawing, use only if buffer is full height
  204. void drawBitmapFromSpiffs_Buffered(const char *filename, int16_t x, int16_t y, bool with_color = true, bool partial_update = false, bool overwrite = false);
  205. void setup()
  206. {
  207. Serial.begin(115200);
  208. Serial.println();
  209. Serial.println("GxEPD2_Spiffs_Example");
  210. display.init(115200);
  211. SPIFFS.begin();
  212. Serial.println("SPIFFS started");
  213. listFiles();
  214. if ((display.epd2.panel == GxEPD2::GDEW0154Z04) || (display.epd2.panel == GxEPD2::ACeP565) || false)
  215. {
  216. drawBitmapsBuffered_200x200();
  217. drawBitmapsBuffered_other();
  218. }
  219. else
  220. {
  221. drawBitmaps_200x200();
  222. drawBitmaps_other();
  223. //drawBitmaps_test();
  224. }
  225. Serial.println("GxEPD2_Spiffs_Example done");
  226. }
  227. void loop(void)
  228. {
  229. }
  230. void drawBitmaps_200x200()
  231. {
  232. int16_t x = (display.width() - 200) / 2;
  233. int16_t y = (display.height() - 200) / 2;
  234. drawBitmapFromSpiffs("logo200x200.bmp", x, y);
  235. delay(2000);
  236. drawBitmapFromSpiffs("first200x200.bmp", x, y);
  237. delay(2000);
  238. drawBitmapFromSpiffs("second200x200.bmp", x, y);
  239. delay(2000);
  240. drawBitmapFromSpiffs("third200x200.bmp", x, y);
  241. delay(2000);
  242. drawBitmapFromSpiffs("fourth200x200.bmp", x, y);
  243. delay(2000);
  244. drawBitmapFromSpiffs("fifth200x200.bmp", x, y);
  245. delay(2000);
  246. drawBitmapFromSpiffs("sixth200x200.bmp", x, y);
  247. delay(2000);
  248. drawBitmapFromSpiffs("seventh200x200.bmp", x, y);
  249. delay(2000);
  250. drawBitmapFromSpiffs("eighth200x200.bmp", x, y);
  251. delay(2000);
  252. }
  253. void drawBitmaps_other()
  254. {
  255. int16_t w2 = display.width() / 2;
  256. int16_t h2 = display.height() / 2;
  257. drawBitmapFromSpiffs("chanceflurries.bmp", w2 - 50, h2 - 50, false);
  258. delay(2000);
  259. drawBitmapFromSpiffs("betty_1.bmp", w2 - 100, h2 - 160);
  260. delay(2000);
  261. drawBitmapFromSpiffs("betty_4.bmp", w2 - 102, h2 - 126);
  262. delay(2000);
  263. drawBitmapFromSpiffs("marilyn_240x240x8.bmp", w2 - 120, h2 - 120);
  264. delay(2000);
  265. drawBitmapFromSpiffs("miniwoof.bmp", w2 - 60, h2 - 80);
  266. delay(2000);
  267. drawBitmapFromSpiffs("tiger.bmp", w2 - 160, h2 - 120);
  268. delay(2000);
  269. drawBitmapFromSpiffs("tiger_178x160x4.bmp", w2 - 89, h2 - 80);
  270. delay(2000);
  271. drawBitmapFromSpiffs("tiger_240x317x4.bmp", w2 - 120, h2 - 160);
  272. delay(2000);
  273. drawBitmapFromSpiffs("tiger_320x200x24.bmp", w2 - 160, h2 - 100);
  274. delay(2000);
  275. drawBitmapFromSpiffs("woof.bmp", w2 - 120, h2 - 160);
  276. delay(2000);
  277. }
  278. void drawBitmaps_test()
  279. {
  280. drawBitmapFromSpiffs("output5.bmp", 0, 0);
  281. delay(2000);
  282. drawBitmapFromSpiffs("output6.bmp", 0, 0);
  283. delay(2000);
  284. drawBitmapFromSpiffs("tractor_1.bmp", 0, 0);
  285. delay(2000);
  286. drawBitmapFromSpiffs("tractor_4.bmp", 0, 0);
  287. delay(2000);
  288. drawBitmapFromSpiffs("tractor_8.bmp", 0, 0);
  289. delay(2000);
  290. drawBitmapFromSpiffs("tractor_11.bmp", 0, 0);
  291. delay(2000);
  292. drawBitmapFromSpiffs("tractor_44.bmp", 0, 0);
  293. delay(2000);
  294. drawBitmapFromSpiffs("tractor_88.bmp", 0, 0);
  295. delay(2000);
  296. }
  297. void drawBitmapsBuffered_200x200()
  298. {
  299. int16_t x = (display.width() - 200) / 2;
  300. int16_t y = (display.height() - 200) / 2;
  301. drawBitmapFromSpiffs_Buffered("logo200x200.bmp", x, y);
  302. delay(2000);
  303. drawBitmapFromSpiffs_Buffered("first200x200.bmp", x, y);
  304. delay(2000);
  305. drawBitmapFromSpiffs_Buffered("second200x200.bmp", x, y);
  306. delay(2000);
  307. drawBitmapFromSpiffs_Buffered("third200x200.bmp", x, y);
  308. delay(2000);
  309. drawBitmapFromSpiffs_Buffered("fourth200x200.bmp", x, y);
  310. delay(2000);
  311. drawBitmapFromSpiffs_Buffered("fifth200x200.bmp", x, y);
  312. delay(2000);
  313. drawBitmapFromSpiffs_Buffered("sixth200x200.bmp", x, y);
  314. delay(2000);
  315. drawBitmapFromSpiffs_Buffered("seventh200x200.bmp", x, y);
  316. delay(2000);
  317. drawBitmapFromSpiffs_Buffered("eighth200x200.bmp", x, y);
  318. delay(2000);
  319. }
  320. void drawBitmapsBuffered_other()
  321. {
  322. int16_t w2 = display.width() / 2;
  323. int16_t h2 = display.height() / 2;
  324. drawBitmapFromSpiffs_Buffered("chanceflurries.bmp", w2 - 50, h2 - 50, false);
  325. delay(2000);
  326. drawBitmapFromSpiffs_Buffered("betty_1.bmp", w2 - 100, h2 - 160);
  327. delay(2000);
  328. drawBitmapFromSpiffs_Buffered("betty_4.bmp", w2 - 102, h2 - 126);
  329. delay(2000);
  330. drawBitmapFromSpiffs_Buffered("marilyn_240x240x8.bmp", w2 - 120, h2 - 120);
  331. delay(2000);
  332. drawBitmapFromSpiffs_Buffered("miniwoof.bmp", w2 - 60, h2 - 80);
  333. delay(2000);
  334. drawBitmapFromSpiffs_Buffered("tiger.bmp", w2 - 160, h2 - 120);
  335. delay(2000);
  336. drawBitmapFromSpiffs_Buffered("tiger_178x160x4.bmp", w2 - 89, h2 - 80);
  337. delay(2000);
  338. drawBitmapFromSpiffs_Buffered("tiger_240x317x4.bmp", w2 - 120, h2 - 160);
  339. delay(2000);
  340. drawBitmapFromSpiffs_Buffered("tiger_320x200x24.bmp", w2 - 160, h2 - 100);
  341. delay(2000);
  342. drawBitmapFromSpiffs_Buffered("woof.bmp", w2 - 120, h2 - 160);
  343. delay(2000);
  344. }
  345. static const uint16_t input_buffer_pixels = 800; // may affect performance
  346. static const uint16_t max_row_width = 1448; // for up to 6" display 1448x1072
  347. static const uint16_t max_palette_pixels = 256; // for depth <= 8
  348. uint8_t input_buffer[3 * input_buffer_pixels]; // up to depth 24
  349. uint8_t output_row_mono_buffer[max_row_width / 8]; // buffer for at least one row of b/w bits
  350. uint8_t output_row_color_buffer[max_row_width / 8]; // buffer for at least one row of color bits
  351. uint8_t mono_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 b/w
  352. uint8_t color_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 c/w
  353. uint16_t rgb_palette_buffer[max_palette_pixels]; // palette buffer for depth <= 8 for buffered graphics, needed for 7-color display
  354. void drawBitmapFromSpiffs(const char *filename, int16_t x, int16_t y, bool with_color)
  355. {
  356. fs::File file;
  357. bool valid = false; // valid format to be handled
  358. bool flip = true; // bitmap is stored bottom-to-top
  359. uint32_t startTime = millis();
  360. if ((x >= display.epd2.WIDTH) || (y >= display.epd2.HEIGHT)) return;
  361. Serial.println();
  362. Serial.print("Loading image '");
  363. Serial.print(filename);
  364. Serial.println('\'');
  365. #if defined(ESP32)
  366. file = SPIFFS.open(String("/") + filename, "r");
  367. #else
  368. file = SPIFFS.open(filename, "r");
  369. #endif
  370. if (!file)
  371. {
  372. Serial.print("File not found");
  373. return;
  374. }
  375. // Parse BMP header
  376. if (read16(file) == 0x4D42) // BMP signature
  377. {
  378. uint32_t fileSize = read32(file);
  379. uint32_t creatorBytes = read32(file);
  380. uint32_t imageOffset = read32(file); // Start of image data
  381. uint32_t headerSize = read32(file);
  382. uint32_t width = read32(file);
  383. uint32_t height = read32(file);
  384. uint16_t planes = read16(file);
  385. uint16_t depth = read16(file); // bits per pixel
  386. uint32_t format = read32(file);
  387. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  388. {
  389. Serial.print("File size: "); Serial.println(fileSize);
  390. Serial.print("Image Offset: "); Serial.println(imageOffset);
  391. Serial.print("Header size: "); Serial.println(headerSize);
  392. Serial.print("Bit Depth: "); Serial.println(depth);
  393. Serial.print("Image size: ");
  394. Serial.print(width);
  395. Serial.print('x');
  396. Serial.println(height);
  397. // BMP rows are padded (if needed) to 4-byte boundary
  398. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  399. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  400. if (height < 0)
  401. {
  402. height = -height;
  403. flip = false;
  404. }
  405. uint16_t w = width;
  406. uint16_t h = height;
  407. if ((x + w - 1) >= display.epd2.WIDTH) w = display.epd2.WIDTH - x;
  408. if ((y + h - 1) >= display.epd2.HEIGHT) h = display.epd2.HEIGHT - y;
  409. if (w <= max_row_width) // handle with direct drawing
  410. {
  411. valid = true;
  412. uint8_t bitmask = 0xFF;
  413. uint8_t bitshift = 8 - depth;
  414. uint16_t red, green, blue;
  415. bool whitish, colored;
  416. if (depth == 1) with_color = false;
  417. if (depth <= 8)
  418. {
  419. if (depth < 8) bitmask >>= depth;
  420. //file.seek(54); //palette is always @ 54
  421. file.seek(imageOffset - (4 << depth)); // 54 for regular, diff for colorsimportant
  422. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  423. {
  424. blue = file.read();
  425. green = file.read();
  426. red = file.read();
  427. file.read();
  428. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  429. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  430. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  431. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  432. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  433. color_palette_buffer[pn / 8] |= colored << pn % 8;
  434. }
  435. }
  436. display.clearScreen();
  437. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  438. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  439. {
  440. uint32_t in_remain = rowSize;
  441. uint32_t in_idx = 0;
  442. uint32_t in_bytes = 0;
  443. uint8_t in_byte = 0; // for depth <= 8
  444. uint8_t in_bits = 0; // for depth <= 8
  445. uint8_t out_byte = 0xFF; // white (for w%8!=0 border)
  446. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 border)
  447. uint32_t out_idx = 0;
  448. file.seek(rowPosition);
  449. for (uint16_t col = 0; col < w; col++) // for each pixel
  450. {
  451. // Time to read more pixel data?
  452. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  453. {
  454. in_bytes = file.read(input_buffer, in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain);
  455. in_remain -= in_bytes;
  456. in_idx = 0;
  457. }
  458. switch (depth)
  459. {
  460. case 24:
  461. blue = input_buffer[in_idx++];
  462. green = input_buffer[in_idx++];
  463. red = input_buffer[in_idx++];
  464. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  465. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  466. break;
  467. case 16:
  468. {
  469. uint8_t lsb = input_buffer[in_idx++];
  470. uint8_t msb = input_buffer[in_idx++];
  471. if (format == 0) // 555
  472. {
  473. blue = (lsb & 0x1F) << 3;
  474. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  475. red = (msb & 0x7C) << 1;
  476. }
  477. else // 565
  478. {
  479. blue = (lsb & 0x1F) << 3;
  480. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  481. red = (msb & 0xF8);
  482. }
  483. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  484. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  485. }
  486. break;
  487. case 1:
  488. case 4:
  489. case 8:
  490. {
  491. if (0 == in_bits)
  492. {
  493. in_byte = input_buffer[in_idx++];
  494. in_bits = 8;
  495. }
  496. uint16_t pn = (in_byte >> bitshift) & bitmask;
  497. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  498. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  499. in_byte <<= depth;
  500. in_bits -= depth;
  501. }
  502. break;
  503. }
  504. if (whitish)
  505. {
  506. // keep white
  507. }
  508. else if (colored && with_color)
  509. {
  510. out_color_byte &= ~(0x80 >> col % 8); // colored
  511. }
  512. else
  513. {
  514. out_byte &= ~(0x80 >> col % 8); // black
  515. }
  516. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 border)
  517. {
  518. output_row_color_buffer[out_idx] = out_color_byte;
  519. output_row_mono_buffer[out_idx++] = out_byte;
  520. out_byte = 0xFF; // white (for w%8!=0 border)
  521. out_color_byte = 0xFF; // white (for w%8!=0 border)
  522. }
  523. } // end pixel
  524. uint16_t yrow = y + (flip ? h - row - 1 : row);
  525. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  526. } // end line
  527. Serial.print("loaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  528. display.refresh();
  529. }
  530. }
  531. }
  532. file.close();
  533. if (!valid)
  534. {
  535. Serial.println("bitmap format not handled.");
  536. }
  537. }
  538. void drawBitmapFromSpiffs_Buffered(const char *filename, int16_t x, int16_t y, bool with_color, bool partial_update, bool overwrite)
  539. {
  540. fs::File file;
  541. bool valid = false; // valid format to be handled
  542. bool flip = true; // bitmap is stored bottom-to-top
  543. bool has_multicolors = display.epd2.panel == GxEPD2::ACeP565;
  544. uint32_t startTime = millis();
  545. if ((x >= display.width()) || (y >= display.height())) return;
  546. Serial.println();
  547. Serial.print("Loading image '");
  548. Serial.print(filename);
  549. Serial.println('\'');
  550. #if defined(ESP32)
  551. file = SPIFFS.open(String("/") + filename, "r");
  552. #else
  553. file = SPIFFS.open(filename, "r");
  554. #endif
  555. if (!file)
  556. {
  557. Serial.print("File not found");
  558. return;
  559. }
  560. // Parse BMP header
  561. if (read16(file) == 0x4D42) // BMP signature
  562. {
  563. uint32_t fileSize = read32(file);
  564. uint32_t creatorBytes = read32(file);
  565. uint32_t imageOffset = read32(file); // Start of image data
  566. uint32_t headerSize = read32(file);
  567. uint32_t width = read32(file);
  568. uint32_t height = read32(file);
  569. uint16_t planes = read16(file);
  570. uint16_t depth = read16(file); // bits per pixel
  571. uint32_t format = read32(file);
  572. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  573. {
  574. Serial.print("File size: "); Serial.println(fileSize);
  575. Serial.print("Image Offset: "); Serial.println(imageOffset);
  576. Serial.print("Header size: "); Serial.println(headerSize);
  577. Serial.print("Bit Depth: "); Serial.println(depth);
  578. Serial.print("Image size: ");
  579. Serial.print(width);
  580. Serial.print('x');
  581. Serial.println(height);
  582. // BMP rows are padded (if needed) to 4-byte boundary
  583. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  584. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  585. if (height < 0)
  586. {
  587. height = -height;
  588. flip = false;
  589. }
  590. uint16_t w = width;
  591. uint16_t h = height;
  592. if ((x + w - 1) >= display.width()) w = display.width() - x;
  593. if ((y + h - 1) >= display.height()) h = display.height() - y;
  594. //if (w <= max_row_width) // handle with direct drawing
  595. {
  596. valid = true;
  597. uint8_t bitmask = 0xFF;
  598. uint8_t bitshift = 8 - depth;
  599. uint16_t red, green, blue;
  600. bool whitish, colored;
  601. if (depth == 1) with_color = false;
  602. if (depth <= 8)
  603. {
  604. if (depth < 8) bitmask >>= depth;
  605. //file.seek(54); //palette is always @ 54
  606. file.seek(imageOffset - (4 << depth)); // 54 for regular, diff for colorsimportant
  607. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  608. {
  609. blue = file.read();
  610. green = file.read();
  611. red = file.read();
  612. file.read();
  613. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  614. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  615. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  616. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  617. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  618. color_palette_buffer[pn / 8] |= colored << pn % 8;
  619. rgb_palette_buffer[pn] = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  620. }
  621. }
  622. if (partial_update) display.setPartialWindow(x, y, w, h);
  623. else display.setFullWindow();
  624. display.firstPage();
  625. do
  626. {
  627. if (!overwrite) display.fillScreen(GxEPD_WHITE);
  628. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  629. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  630. {
  631. uint32_t in_remain = rowSize;
  632. uint32_t in_idx = 0;
  633. uint32_t in_bytes = 0;
  634. uint8_t in_byte = 0; // for depth <= 8
  635. uint8_t in_bits = 0; // for depth <= 8
  636. uint16_t color = GxEPD_WHITE;
  637. file.seek(rowPosition);
  638. for (uint16_t col = 0; col < w; col++) // for each pixel
  639. {
  640. // Time to read more pixel data?
  641. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  642. {
  643. in_bytes = file.read(input_buffer, in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain);
  644. in_remain -= in_bytes;
  645. in_idx = 0;
  646. }
  647. switch (depth)
  648. {
  649. case 24:
  650. blue = input_buffer[in_idx++];
  651. green = input_buffer[in_idx++];
  652. red = input_buffer[in_idx++];
  653. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  654. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  655. color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  656. break;
  657. case 16:
  658. {
  659. uint8_t lsb = input_buffer[in_idx++];
  660. uint8_t msb = input_buffer[in_idx++];
  661. if (format == 0) // 555
  662. {
  663. blue = (lsb & 0x1F) << 3;
  664. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  665. red = (msb & 0x7C) << 1;
  666. color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  667. }
  668. else // 565
  669. {
  670. blue = (lsb & 0x1F) << 3;
  671. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  672. red = (msb & 0xF8);
  673. color = (msb << 8) | lsb;
  674. }
  675. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  676. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  677. }
  678. break;
  679. case 1:
  680. case 4:
  681. case 8:
  682. {
  683. if (0 == in_bits)
  684. {
  685. in_byte = input_buffer[in_idx++];
  686. in_bits = 8;
  687. }
  688. uint16_t pn = (in_byte >> bitshift) & bitmask;
  689. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  690. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  691. in_byte <<= depth;
  692. in_bits -= depth;
  693. color = rgb_palette_buffer[pn];
  694. }
  695. break;
  696. }
  697. if (with_color && has_multicolors)
  698. {
  699. // keep color
  700. }
  701. else if (whitish)
  702. {
  703. color = GxEPD_WHITE;
  704. }
  705. else if (colored && with_color)
  706. {
  707. color = GxEPD_COLORED;
  708. }
  709. else
  710. {
  711. color = GxEPD_BLACK;
  712. }
  713. uint16_t yrow = y + (flip ? h - row - 1 : row);
  714. display.drawPixel(x + col, yrow, color);
  715. } // end pixel
  716. } // end line
  717. }
  718. while (display.nextPage());
  719. Serial.print("loaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  720. }
  721. }
  722. }
  723. file.close();
  724. if (!valid)
  725. {
  726. Serial.println("bitmap format not handled.");
  727. }
  728. }
  729. uint16_t read16(fs::File& f)
  730. {
  731. // BMP data is stored little-endian, same as Arduino.
  732. uint16_t result;
  733. ((uint8_t *)&result)[0] = f.read(); // LSB
  734. ((uint8_t *)&result)[1] = f.read(); // MSB
  735. return result;
  736. }
  737. uint32_t read32(fs::File& f)
  738. {
  739. // BMP data is stored little-endian, same as Arduino.
  740. uint32_t result;
  741. ((uint8_t *)&result)[0] = f.read(); // LSB
  742. ((uint8_t *)&result)[1] = f.read();
  743. ((uint8_t *)&result)[2] = f.read();
  744. ((uint8_t *)&result)[3] = f.read(); // MSB
  745. return result;
  746. }