GxEPD2_Spiffs_Example.ino 28 KB

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