GxEPD2_Spiffs_Example.ino 23 KB

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