GxEPD2_SerialFlash_Example.ino 41 KB

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