GxEPD2_Spiffs_Example.ino 23 KB

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