GxEPD2_SerialFlash_Example.ino 24 KB

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