GxEPD2_SD_Example.ino 25 KB

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