GxEPD2_SerialFlash_Example.ino 25 KB

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