GxEPD2_SerialFlash_Example.ino 35 KB

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