GxEPD2_Spiffs_Example.ino 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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 Dispay 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. }
  121. Serial.println("GxEPD2_Spiffs_Example done");
  122. }
  123. void loop(void)
  124. {
  125. }
  126. void drawBitmaps_200x200()
  127. {
  128. int16_t x = (display.width() - 200) / 2;
  129. int16_t y = (display.height() - 200) / 2;
  130. drawBitmapFromSpiffs("logo200x200.bmp", x, y);
  131. delay(2000);
  132. drawBitmapFromSpiffs("first200x200.bmp", x, y);
  133. delay(2000);
  134. drawBitmapFromSpiffs("second200x200.bmp", x, y);
  135. delay(2000);
  136. drawBitmapFromSpiffs("third200x200.bmp", x, y);
  137. delay(2000);
  138. drawBitmapFromSpiffs("fourth200x200.bmp", x, y);
  139. delay(2000);
  140. drawBitmapFromSpiffs("fifth200x200.bmp", x, y);
  141. delay(2000);
  142. drawBitmapFromSpiffs("sixth200x200.bmp", x, y);
  143. delay(2000);
  144. drawBitmapFromSpiffs("seventh200x200.bmp", x, y);
  145. delay(2000);
  146. drawBitmapFromSpiffs("eighth200x200.bmp", x, y);
  147. delay(2000);
  148. }
  149. void drawBitmaps_other()
  150. {
  151. int16_t w2 = display.width() / 2;
  152. int16_t h2 = display.height() / 2;
  153. drawBitmapFromSpiffs("chanceflurries.bmp", w2 - 50, h2 - 50, false);
  154. delay(2000);
  155. drawBitmapFromSpiffs("betty_1.bmp", w2 - 100, h2 - 160);
  156. delay(2000);
  157. drawBitmapFromSpiffs("betty_4.bmp", w2 - 102, h2 - 126);
  158. delay(2000);
  159. drawBitmapFromSpiffs("marilyn_240x240x8.bmp", w2 - 120, h2 - 120);
  160. delay(2000);
  161. drawBitmapFromSpiffs("miniwoof.bmp", w2 - 60, h2 - 80);
  162. delay(2000);
  163. drawBitmapFromSpiffs("tiger.bmp", w2 - 160, h2 - 120);
  164. delay(2000);
  165. drawBitmapFromSpiffs("tiger_178x160x4.bmp", w2 - 89, h2 - 80);
  166. delay(2000);
  167. drawBitmapFromSpiffs("tiger_240x317x4.bmp", w2 - 120, h2 - 160);
  168. delay(2000);
  169. drawBitmapFromSpiffs("tiger_320x200x24.bmp", w2 - 160, h2 - 100);
  170. delay(2000);
  171. drawBitmapFromSpiffs("woof.bmp", w2 - 120, h2 - 160);
  172. delay(2000);
  173. }
  174. void drawBitmapsBuffered_200x200()
  175. {
  176. int16_t x = (display.width() - 200) / 2;
  177. int16_t y = (display.height() - 200) / 2;
  178. drawBitmapFromSpiffs_Buffered("logo200x200.bmp", x, y);
  179. delay(2000);
  180. drawBitmapFromSpiffs_Buffered("first200x200.bmp", x, y);
  181. delay(2000);
  182. drawBitmapFromSpiffs_Buffered("second200x200.bmp", x, y);
  183. delay(2000);
  184. drawBitmapFromSpiffs_Buffered("third200x200.bmp", x, y);
  185. delay(2000);
  186. drawBitmapFromSpiffs_Buffered("fourth200x200.bmp", x, y);
  187. delay(2000);
  188. drawBitmapFromSpiffs_Buffered("fifth200x200.bmp", x, y);
  189. delay(2000);
  190. drawBitmapFromSpiffs_Buffered("sixth200x200.bmp", x, y);
  191. delay(2000);
  192. drawBitmapFromSpiffs_Buffered("seventh200x200.bmp", x, y);
  193. delay(2000);
  194. drawBitmapFromSpiffs_Buffered("eighth200x200.bmp", x, y);
  195. delay(2000);
  196. }
  197. void drawBitmapsBuffered_other()
  198. {
  199. int16_t w2 = display.width() / 2;
  200. int16_t h2 = display.height() / 2;
  201. drawBitmapFromSpiffs_Buffered("chanceflurries.bmp", w2 - 50, h2 - 50, false);
  202. delay(2000);
  203. drawBitmapFromSpiffs_Buffered("betty_1.bmp", w2 - 100, h2 - 160);
  204. delay(2000);
  205. drawBitmapFromSpiffs_Buffered("betty_4.bmp", w2 - 102, h2 - 126);
  206. delay(2000);
  207. drawBitmapFromSpiffs_Buffered("marilyn_240x240x8.bmp", w2 - 120, h2 - 120);
  208. delay(2000);
  209. drawBitmapFromSpiffs_Buffered("miniwoof.bmp", w2 - 60, h2 - 80);
  210. delay(2000);
  211. drawBitmapFromSpiffs_Buffered("tiger.bmp", w2 - 160, h2 - 120);
  212. delay(2000);
  213. drawBitmapFromSpiffs_Buffered("tiger_178x160x4.bmp", w2 - 89, h2 - 80);
  214. delay(2000);
  215. drawBitmapFromSpiffs_Buffered("tiger_240x317x4.bmp", w2 - 120, h2 - 160);
  216. delay(2000);
  217. drawBitmapFromSpiffs_Buffered("tiger_320x200x24.bmp", w2 - 160, h2 - 100);
  218. delay(2000);
  219. drawBitmapFromSpiffs_Buffered("woof.bmp", w2 - 120, h2 - 160);
  220. delay(2000);
  221. }
  222. static const uint16_t input_buffer_pixels = 640; // may affect performance
  223. static const uint16_t max_row_width = 640; // for up to 7.5" display
  224. static const uint16_t max_palette_pixels = 256; // for depth <= 8
  225. uint8_t input_buffer[3 * input_buffer_pixels]; // up to depth 24
  226. uint8_t output_row_mono_buffer[max_row_width / 8]; // buffer for at least one row of b/w bits
  227. uint8_t output_row_color_buffer[max_row_width / 8]; // buffer for at least one row of color bits
  228. uint8_t mono_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 b/w
  229. uint8_t color_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 c/w
  230. void drawBitmapFromSpiffs(const char *filename, int16_t x, int16_t y, bool with_color)
  231. {
  232. fs::File file;
  233. bool valid = false; // valid format to be handled
  234. bool flip = true; // bitmap is stored bottom-to-top
  235. uint32_t startTime = millis();
  236. if ((x >= display.width()) || (y >= display.height())) return;
  237. Serial.println();
  238. Serial.print("Loading image '");
  239. Serial.print(filename);
  240. Serial.println('\'');
  241. #if defined(ESP32)
  242. file = SPIFFS.open(String("/") + filename, "r");
  243. #else
  244. file = SPIFFS.open(filename, "r");
  245. #endif
  246. if (!file)
  247. {
  248. Serial.print("File not found");
  249. return;
  250. }
  251. // Parse BMP header
  252. if (read16(file) == 0x4D42) // BMP signature
  253. {
  254. uint32_t fileSize = read32(file);
  255. uint32_t creatorBytes = read32(file);
  256. uint32_t imageOffset = read32(file); // Start of image data
  257. uint32_t headerSize = read32(file);
  258. uint32_t width = read32(file);
  259. uint32_t height = read32(file);
  260. uint16_t planes = read16(file);
  261. uint16_t depth = read16(file); // bits per pixel
  262. uint32_t format = read32(file);
  263. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  264. {
  265. Serial.print("File size: "); Serial.println(fileSize);
  266. Serial.print("Image Offset: "); Serial.println(imageOffset);
  267. Serial.print("Header size: "); Serial.println(headerSize);
  268. Serial.print("Bit Depth: "); Serial.println(depth);
  269. Serial.print("Image size: ");
  270. Serial.print(width);
  271. Serial.print('x');
  272. Serial.println(height);
  273. // BMP rows are padded (if needed) to 4-byte boundary
  274. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  275. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  276. if (height < 0)
  277. {
  278. height = -height;
  279. flip = false;
  280. }
  281. uint16_t w = width;
  282. uint16_t h = height;
  283. if ((x + w - 1) >= display.width()) w = display.width() - x;
  284. if ((y + h - 1) >= display.height()) h = display.height() - y;
  285. if (w <= max_row_width) // handle with direct drawing
  286. {
  287. valid = true;
  288. uint8_t bitmask = 0xFF;
  289. uint8_t bitshift = 8 - depth;
  290. uint16_t red, green, blue;
  291. bool whitish, colored;
  292. if (depth == 1) with_color = false;
  293. if (depth <= 8)
  294. {
  295. if (depth < 8) bitmask >>= depth;
  296. file.seek(54); //palette is always @ 54
  297. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  298. {
  299. blue = file.read();
  300. green = file.read();
  301. red = file.read();
  302. file.read();
  303. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  304. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  305. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  306. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  307. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  308. color_palette_buffer[pn / 8] |= colored << pn % 8;
  309. }
  310. }
  311. display.clearScreen();
  312. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  313. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  314. {
  315. uint32_t in_remain = rowSize;
  316. uint32_t in_idx = 0;
  317. uint32_t in_bytes = 0;
  318. uint8_t in_byte = 0; // for depth <= 8
  319. uint8_t in_bits = 0; // for depth <= 8
  320. uint8_t out_byte = 0xFF; // white (for w%8!=0 boarder)
  321. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 boarder)
  322. uint32_t out_idx = 0;
  323. file.seek(rowPosition);
  324. for (uint16_t col = 0; col < w; col++) // for each pixel
  325. {
  326. // Time to read more pixel data?
  327. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  328. {
  329. in_bytes = file.read(input_buffer, in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain);
  330. in_remain -= in_bytes;
  331. in_idx = 0;
  332. }
  333. switch (depth)
  334. {
  335. case 24:
  336. blue = input_buffer[in_idx++];
  337. green = input_buffer[in_idx++];
  338. red = input_buffer[in_idx++];
  339. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  340. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  341. break;
  342. case 16:
  343. {
  344. uint8_t lsb = input_buffer[in_idx++];
  345. uint8_t msb = input_buffer[in_idx++];
  346. if (format == 0) // 555
  347. {
  348. blue = (lsb & 0x1F) << 3;
  349. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  350. red = (msb & 0x7C) << 1;
  351. }
  352. else // 565
  353. {
  354. blue = (lsb & 0x1F) << 3;
  355. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  356. red = (msb & 0xF8);
  357. }
  358. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  359. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  360. }
  361. break;
  362. case 1:
  363. case 4:
  364. case 8:
  365. {
  366. if (0 == in_bits)
  367. {
  368. in_byte = input_buffer[in_idx++];
  369. in_bits = 8;
  370. }
  371. uint16_t pn = (in_byte >> bitshift) & bitmask;
  372. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  373. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  374. in_byte <<= depth;
  375. in_bits -= depth;
  376. }
  377. break;
  378. }
  379. if (whitish)
  380. {
  381. // keep white
  382. }
  383. else if (colored && with_color)
  384. {
  385. out_color_byte &= ~(0x80 >> col % 8); // colored
  386. }
  387. else
  388. {
  389. out_byte &= ~(0x80 >> col % 8); // black
  390. }
  391. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 boarder)
  392. {
  393. output_row_color_buffer[out_idx] = out_color_byte;
  394. output_row_mono_buffer[out_idx++] = out_byte;
  395. out_byte = 0xFF; // white (for w%8!=0 boarder)
  396. out_color_byte = 0xFF; // white (for w%8!=0 boarder)
  397. }
  398. } // end pixel
  399. uint16_t yrow = y + (flip ? h - row - 1 : row);
  400. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  401. } // end line
  402. Serial.print("loaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  403. display.refresh();
  404. }
  405. }
  406. }
  407. file.close();
  408. if (!valid)
  409. {
  410. Serial.println("bitmap format not handled.");
  411. }
  412. }
  413. void drawBitmapFromSpiffs_Buffered(const char *filename, int16_t x, int16_t y, bool with_color, bool partial_update, bool overwrite)
  414. {
  415. fs::File file;
  416. bool valid = false; // valid format to be handled
  417. bool flip = true; // bitmap is stored bottom-to-top
  418. uint32_t startTime = millis();
  419. if ((x >= display.width()) || (y >= display.height())) return;
  420. Serial.println();
  421. Serial.print("Loading image '");
  422. Serial.print(filename);
  423. Serial.println('\'');
  424. #if defined(ESP32)
  425. file = SPIFFS.open(String("/") + filename, "r");
  426. #else
  427. file = SPIFFS.open(filename, "r");
  428. #endif
  429. if (!file)
  430. {
  431. Serial.print("File not found");
  432. return;
  433. }
  434. // Parse BMP header
  435. if (read16(file) == 0x4D42) // BMP signature
  436. {
  437. uint32_t fileSize = read32(file);
  438. uint32_t creatorBytes = read32(file);
  439. uint32_t imageOffset = read32(file); // Start of image data
  440. uint32_t headerSize = read32(file);
  441. uint32_t width = read32(file);
  442. uint32_t height = read32(file);
  443. uint16_t planes = read16(file);
  444. uint16_t depth = read16(file); // bits per pixel
  445. uint32_t format = read32(file);
  446. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  447. {
  448. Serial.print("File size: "); Serial.println(fileSize);
  449. Serial.print("Image Offset: "); Serial.println(imageOffset);
  450. Serial.print("Header size: "); Serial.println(headerSize);
  451. Serial.print("Bit Depth: "); Serial.println(depth);
  452. Serial.print("Image size: ");
  453. Serial.print(width);
  454. Serial.print('x');
  455. Serial.println(height);
  456. // BMP rows are padded (if needed) to 4-byte boundary
  457. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  458. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  459. if (height < 0)
  460. {
  461. height = -height;
  462. flip = false;
  463. }
  464. uint16_t w = width;
  465. uint16_t h = height;
  466. if ((x + w - 1) >= display.width()) w = display.width() - x;
  467. if ((y + h - 1) >= display.height()) h = display.height() - y;
  468. //if (w <= max_row_width) // handle with direct drawing
  469. {
  470. valid = true;
  471. uint8_t bitmask = 0xFF;
  472. uint8_t bitshift = 8 - depth;
  473. uint16_t red, green, blue;
  474. bool whitish, colored;
  475. if (depth == 1) with_color = false;
  476. if (depth <= 8)
  477. {
  478. if (depth < 8) bitmask >>= depth;
  479. file.seek(54); //palette is always @ 54
  480. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  481. {
  482. blue = file.read();
  483. green = file.read();
  484. red = file.read();
  485. file.read();
  486. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  487. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  488. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  489. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  490. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  491. color_palette_buffer[pn / 8] |= colored << pn % 8;
  492. }
  493. }
  494. if (partial_update) display.setPartialWindow(x, y, w, h);
  495. else display.setFullWindow();
  496. display.firstPage();
  497. do
  498. {
  499. if (!overwrite) display.fillScreen(GxEPD_WHITE);
  500. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  501. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  502. {
  503. uint32_t in_remain = rowSize;
  504. uint32_t in_idx = 0;
  505. uint32_t in_bytes = 0;
  506. uint8_t in_byte = 0; // for depth <= 8
  507. uint8_t in_bits = 0; // for depth <= 8
  508. uint16_t color = GxEPD_WHITE;
  509. file.seek(rowPosition);
  510. for (uint16_t col = 0; col < w; col++) // for each pixel
  511. {
  512. // Time to read more pixel data?
  513. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  514. {
  515. in_bytes = file.read(input_buffer, in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain);
  516. in_remain -= in_bytes;
  517. in_idx = 0;
  518. }
  519. switch (depth)
  520. {
  521. case 24:
  522. blue = input_buffer[in_idx++];
  523. green = input_buffer[in_idx++];
  524. red = input_buffer[in_idx++];
  525. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  526. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  527. break;
  528. case 16:
  529. {
  530. uint8_t lsb = input_buffer[in_idx++];
  531. uint8_t msb = input_buffer[in_idx++];
  532. if (format == 0) // 555
  533. {
  534. blue = (lsb & 0x1F) << 3;
  535. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  536. red = (msb & 0x7C) << 1;
  537. }
  538. else // 565
  539. {
  540. blue = (lsb & 0x1F) << 3;
  541. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  542. red = (msb & 0xF8);
  543. }
  544. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  545. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  546. }
  547. break;
  548. case 1:
  549. case 4:
  550. case 8:
  551. {
  552. if (0 == in_bits)
  553. {
  554. in_byte = input_buffer[in_idx++];
  555. in_bits = 8;
  556. }
  557. uint16_t pn = (in_byte >> bitshift) & bitmask;
  558. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  559. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  560. in_byte <<= depth;
  561. in_bits -= depth;
  562. }
  563. break;
  564. }
  565. if (whitish)
  566. {
  567. color = GxEPD_WHITE;
  568. }
  569. else if (colored && with_color)
  570. {
  571. color = GxEPD_COLORED;
  572. }
  573. else
  574. {
  575. color = GxEPD_BLACK;
  576. }
  577. uint16_t yrow = y + (flip ? h - row - 1 : row);
  578. display.drawPixel(x + col, yrow, color);
  579. } // end pixel
  580. } // end line
  581. }
  582. while (display.nextPage());
  583. Serial.print("loaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  584. }
  585. }
  586. }
  587. file.close();
  588. if (!valid)
  589. {
  590. Serial.println("bitmap format not handled.");
  591. }
  592. }
  593. uint16_t read16(fs::File& f)
  594. {
  595. // BMP data is stored little-endian, same as Arduino.
  596. uint16_t result;
  597. ((uint8_t *)&result)[0] = f.read(); // LSB
  598. ((uint8_t *)&result)[1] = f.read(); // MSB
  599. return result;
  600. }
  601. uint32_t read32(fs::File& f)
  602. {
  603. // BMP data is stored little-endian, same as Arduino.
  604. uint32_t result;
  605. ((uint8_t *)&result)[0] = f.read(); // LSB
  606. ((uint8_t *)&result)[1] = f.read();
  607. ((uint8_t *)&result)[2] = f.read();
  608. ((uint8_t *)&result)[3] = f.read(); // MSB
  609. return result;
  610. }