GxEPD2_SerialFlash_Example.ino 42 KB

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