GxEPD2_Spiffs_Example.ino 28 KB

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