GxEPD2_SerialFlash_Example.ino 40 KB

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