GxEPD2_SerialFlash_Example.ino 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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(115200); // default 10ms reset pulse, e.g. for bare panels with DESPI-C02
  78. display.init(115200, true, 2, false); // USE THIS for Waveshare boards with "clever" reset circuit, 2ms reset pulse
  79. if (!SerialFlash.begin(FlashChipSelect))
  80. {
  81. Serial.println("Unable to access SPI Flash chip");
  82. return;
  83. }
  84. Serial.println("SerialFlash started");
  85. listFiles();
  86. if ((display.epd2.panel == GxEPD2::GDEW0154Z04) || (display.epd2.panel == GxEPD2::ACeP565) || (display.epd2.panel == GxEPD2::GDEY073D46) || false)
  87. {
  88. //drawBitmapsBuffered_200x200();
  89. //drawBitmapsBuffered_other();
  90. drawBitmapsBuffered_test();
  91. }
  92. else
  93. {
  94. drawBitmaps_200x200();
  95. drawBitmaps_other();
  96. //drawBitmaps_test();
  97. }
  98. Serial.println("GxEPD2_SerialFlash_Example done");
  99. }
  100. void loop(void)
  101. {
  102. }
  103. void listFiles()
  104. {
  105. Serial.println("All Files on SPI Flash chip:");
  106. SerialFlash.opendir();
  107. while (1)
  108. {
  109. char filename[64];
  110. uint32_t filesize;
  111. if (SerialFlash.readdir(filename, sizeof(filename), filesize))
  112. {
  113. Serial.print(" ");
  114. Serial.print(filename);
  115. spaces(20 - strlen(filename));
  116. Serial.print(" ");
  117. Serial.print(filesize);
  118. Serial.print(" bytes");
  119. Serial.println();
  120. }
  121. else
  122. {
  123. Serial.println("no more files...");
  124. break; // no more files
  125. }
  126. }
  127. }
  128. void spaces(int num)
  129. {
  130. for (int i = 0; i < num; i++)
  131. {
  132. Serial.print(" ");
  133. }
  134. }
  135. void drawBitmaps_200x200()
  136. {
  137. int16_t x = (display.width() - 200) / 2;
  138. int16_t y = (display.height() - 200) / 2;
  139. drawBitmapFromSerialFlash("logo200x200.bmp", x, y);
  140. delay(2000);
  141. drawBitmapFromSerialFlash("first200x200.bmp", x, y);
  142. delay(2000);
  143. drawBitmapFromSerialFlash("second200x200.bmp", x, y);
  144. delay(2000);
  145. drawBitmapFromSerialFlash("third200x200.bmp", x, y);
  146. delay(2000);
  147. drawBitmapFromSerialFlash("fourth200x200.bmp", x, y);
  148. delay(2000);
  149. drawBitmapFromSerialFlash("fifth200x200.bmp", x, y);
  150. delay(2000);
  151. drawBitmapFromSerialFlash("sixth200x200.bmp", x, y);
  152. delay(2000);
  153. drawBitmapFromSerialFlash("seventh200x200.bmp", x, y);
  154. delay(2000);
  155. drawBitmapFromSerialFlash("eighth200x200.bmp", x, y);
  156. delay(2000);
  157. }
  158. void drawBitmaps_other()
  159. {
  160. int16_t w2 = display.width() / 2;
  161. int16_t h2 = display.height() / 2;
  162. drawBitmapFromSerialFlash("chanceflurries.bmp", w2 - 50, h2 - 50, false);
  163. delay(2000);
  164. drawBitmapFromSerialFlash("betty_1.bmp", w2 - 100, h2 - 160);
  165. delay(2000);
  166. drawBitmapFromSerialFlash("betty_4.bmp", w2 - 102, h2 - 126);
  167. delay(2000);
  168. drawBitmapFromSerialFlash("marilyn_240x240x8.bmp", w2 - 120, h2 - 120);
  169. delay(2000);
  170. drawBitmapFromSerialFlash("miniwoof.bmp", w2 - 60, h2 - 80);
  171. delay(2000);
  172. drawBitmapFromSerialFlash("tiger.bmp", w2 - 160, h2 - 120);
  173. delay(2000);
  174. drawBitmapFromSerialFlash("tiger_178x160x4.bmp", w2 - 89, h2 - 80);
  175. delay(2000);
  176. drawBitmapFromSerialFlash("tiger_240x317x4.bmp", w2 - 120, h2 - 160);
  177. delay(2000);
  178. drawBitmapFromSerialFlash("tiger_320x200x24.bmp", w2 - 160, h2 - 100);
  179. delay(2000);
  180. drawBitmapFromSerialFlash("woof.bmp", w2 - 120, h2 - 160);
  181. delay(2000);
  182. }
  183. void drawBitmaps_test()
  184. {
  185. drawBitmapFromSerialFlash("output5.bmp", 0, 0);
  186. delay(2000);
  187. drawBitmapFromSerialFlash("output6.bmp", 0, 0);
  188. delay(2000);
  189. drawBitmapFromSerialFlash("tractor_1.bmp", 0, 0);
  190. delay(2000);
  191. drawBitmapFromSerialFlash("tractor_4.bmp", 0, 0);
  192. delay(2000);
  193. //drawBitmapFromSerialFlash("tractor_8.bmp", 0, 0); // format 1: BI_RLE8 is not supported
  194. //delay(2000);
  195. drawBitmapFromSerialFlash("tractor_11.bmp", 0, 0);
  196. delay(2000);
  197. drawBitmapFromSerialFlash("tractor_44.bmp", 0, 0);
  198. delay(2000);
  199. drawBitmapFromSerialFlash("tractor_88.bmp", 0, 0);
  200. delay(2000);
  201. }
  202. void drawBitmapsBuffered_200x200()
  203. {
  204. int16_t x = (display.width() - 200) / 2;
  205. int16_t y = (display.height() - 200) / 2;
  206. drawBitmapFromSerialFlash_Buffered("logo200x200.bmp", x, y);
  207. delay(2000);
  208. drawBitmapFromSerialFlash_Buffered("first200x200.bmp", x, y);
  209. delay(2000);
  210. drawBitmapFromSerialFlash_Buffered("second200x200.bmp", x, y);
  211. delay(2000);
  212. drawBitmapFromSerialFlash_Buffered("third200x200.bmp", x, y);
  213. delay(2000);
  214. drawBitmapFromSerialFlash_Buffered("fourth200x200.bmp", x, y);
  215. delay(2000);
  216. drawBitmapFromSerialFlash_Buffered("fifth200x200.bmp", x, y);
  217. delay(2000);
  218. drawBitmapFromSerialFlash_Buffered("sixth200x200.bmp", x, y);
  219. delay(2000);
  220. drawBitmapFromSerialFlash_Buffered("seventh200x200.bmp", x, y);
  221. delay(2000);
  222. drawBitmapFromSerialFlash_Buffered("eighth200x200.bmp", x, y);
  223. delay(2000);
  224. }
  225. void drawBitmapsBuffered_other()
  226. {
  227. int16_t w2 = display.width() / 2;
  228. int16_t h2 = display.height() / 2;
  229. drawBitmapFromSerialFlash_Buffered("chanceflurries.bmp", w2 - 50, h2 - 50, false);
  230. delay(2000);
  231. drawBitmapFromSerialFlash_Buffered("betty_1.bmp", w2 - 100, h2 - 160);
  232. delay(2000);
  233. drawBitmapFromSerialFlash_Buffered("betty_4.bmp", w2 - 102, h2 - 126);
  234. delay(2000);
  235. drawBitmapFromSerialFlash_Buffered("marilyn_240x240x8.bmp", w2 - 120, h2 - 120);
  236. delay(2000);
  237. drawBitmapFromSerialFlash_Buffered("miniwoof.bmp", w2 - 60, h2 - 80);
  238. delay(2000);
  239. drawBitmapFromSerialFlash_Buffered("tiger.bmp", w2 - 160, h2 - 120);
  240. delay(2000);
  241. drawBitmapFromSerialFlash_Buffered("tiger_178x160x4.bmp", w2 - 89, h2 - 80);
  242. delay(2000);
  243. drawBitmapFromSerialFlash_Buffered("tiger_240x317x4.bmp", w2 - 120, h2 - 160);
  244. delay(2000);
  245. drawBitmapFromSerialFlash_Buffered("tiger_320x200x24.bmp", w2 - 160, h2 - 100);
  246. delay(2000);
  247. drawBitmapFromSerialFlash_Buffered("woof.bmp", w2 - 120, h2 - 160);
  248. delay(2000);
  249. }
  250. void drawBitmapsBuffered_test()
  251. {
  252. int16_t w2 = display.width() / 2;
  253. int16_t h2 = display.height() / 2;
  254. drawBitmapFromSerialFlash_Buffered("betty_4.bmp", w2 - 102, h2 - 126);
  255. delay(2000);
  256. drawBitmapFromSerialFlash_Buffered("bb4.bmp", 0, 0, false, true, true);
  257. delay(2000);
  258. drawBitmapFromSerialFlash_Buffered("rgb32.bmp", 0, 0);
  259. delay(2000);
  260. drawBitmapFromSerialFlash_Buffered("parrot.bmp", 0, 0);
  261. delay(2000);
  262. drawBitmapFromSerialFlash_Buffered("5in65f3.bmp", 0, 0);
  263. delay(2000);
  264. }
  265. static const uint16_t input_buffer_pixels = 800; // may affect performance
  266. static const uint16_t max_row_width = 1448; // for up to 6" display 1448x1072
  267. static const uint16_t max_palette_pixels = 256; // for depth <= 8
  268. uint8_t input_buffer[3 * input_buffer_pixels]; // up to depth 24
  269. uint8_t output_row_mono_buffer[max_row_width / 8]; // buffer for at least one row of b/w bits
  270. uint8_t output_row_color_buffer[max_row_width / 8]; // buffer for at least one row of color bits
  271. uint8_t mono_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 b/w
  272. uint8_t color_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 c/w
  273. uint16_t rgb_palette_buffer[max_palette_pixels]; // palette buffer for depth <= 8 for buffered graphics, needed for 7-color display
  274. void drawBitmapFromSerialFlash(const char *filename, int16_t x, int16_t y, bool with_color)
  275. {
  276. bool valid = false; // valid format to be handled
  277. bool flip = true; // bitmap is stored bottom-to-top
  278. uint32_t startTime = millis();
  279. if ((x >= display.epd2.WIDTH) || (y >= display.epd2.HEIGHT)) return;
  280. Serial.println();
  281. Serial.print("Loading image '");
  282. Serial.print(filename);
  283. Serial.println('\'');
  284. SerialFlashFile file = SerialFlash.open(filename);
  285. if (!file)
  286. {
  287. Serial.print("File not found");
  288. return;
  289. }
  290. // Parse BMP header
  291. if (read16(file) == 0x4D42) // BMP signature
  292. {
  293. uint32_t fileSize = read32(file);
  294. uint32_t creatorBytes = read32(file); (void)creatorBytes; //unused
  295. uint32_t imageOffset = read32(file); // Start of image data
  296. uint32_t headerSize = read32(file);
  297. uint32_t width = read32(file);
  298. int32_t height = (int32_t) read32(file);
  299. uint16_t planes = read16(file);
  300. uint16_t depth = read16(file); // bits per pixel
  301. uint32_t format = read32(file);
  302. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  303. {
  304. Serial.print("File size: "); Serial.println(fileSize);
  305. Serial.print("Image Offset: "); Serial.println(imageOffset);
  306. Serial.print("Header size: "); Serial.println(headerSize);
  307. Serial.print("Bit Depth: "); Serial.println(depth);
  308. Serial.print("Image size: ");
  309. Serial.print(width);
  310. Serial.print('x');
  311. Serial.println(height);
  312. // BMP rows are padded (if needed) to 4-byte boundary
  313. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  314. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  315. if (height < 0)
  316. {
  317. height = -height;
  318. flip = false;
  319. }
  320. uint16_t w = width;
  321. uint16_t h = height;
  322. if ((x + w - 1) >= display.epd2.WIDTH) w = display.epd2.WIDTH - x;
  323. if ((y + h - 1) >= display.epd2.HEIGHT) h = display.epd2.HEIGHT - y;
  324. if (w <= max_row_width) // handle with direct drawing
  325. {
  326. valid = true;
  327. uint8_t bitmask = 0xFF;
  328. uint8_t bitshift = 8 - depth;
  329. uint16_t red, green, blue;
  330. bool whitish = false;
  331. bool colored = false;
  332. if (depth == 1) with_color = false;
  333. if (depth <= 8)
  334. {
  335. if (depth < 8) bitmask >>= depth;
  336. //file.seek(54); //palette is always @ 54
  337. file.seek(imageOffset - (4 << depth)); // 54 for regular, diff for colorsimportant
  338. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  339. {
  340. blue = read8(file);
  341. green = read8(file);
  342. red = read8(file);
  343. read8(file);
  344. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  345. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  346. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  347. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  348. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  349. color_palette_buffer[pn / 8] |= colored << pn % 8;
  350. }
  351. }
  352. display.clearScreen();
  353. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  354. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  355. {
  356. uint32_t in_remain = rowSize;
  357. uint32_t in_idx = 0;
  358. uint32_t in_bytes = 0;
  359. uint8_t in_byte = 0; // for depth <= 8
  360. uint8_t in_bits = 0; // for depth <= 8
  361. uint8_t out_byte = 0xFF; // white (for w%8!=0 border)
  362. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 border)
  363. uint32_t out_idx = 0;
  364. file.seek(rowPosition);
  365. for (uint16_t col = 0; col < w; col++) // for each pixel
  366. {
  367. // Time to read more pixel data?
  368. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  369. {
  370. in_bytes = file.read(input_buffer, in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain);
  371. in_remain -= in_bytes;
  372. in_idx = 0;
  373. }
  374. switch (depth)
  375. {
  376. case 32:
  377. blue = input_buffer[in_idx++];
  378. green = input_buffer[in_idx++];
  379. red = input_buffer[in_idx++];
  380. in_idx++; // skip alpha
  381. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  382. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  383. break;
  384. case 24:
  385. blue = input_buffer[in_idx++];
  386. green = input_buffer[in_idx++];
  387. red = input_buffer[in_idx++];
  388. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  389. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  390. break;
  391. case 16:
  392. {
  393. uint8_t lsb = input_buffer[in_idx++];
  394. uint8_t msb = input_buffer[in_idx++];
  395. if (format == 0) // 555
  396. {
  397. blue = (lsb & 0x1F) << 3;
  398. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  399. red = (msb & 0x7C) << 1;
  400. }
  401. else // 565
  402. {
  403. blue = (lsb & 0x1F) << 3;
  404. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  405. red = (msb & 0xF8);
  406. }
  407. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  408. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  409. }
  410. break;
  411. case 1:
  412. case 2:
  413. case 4:
  414. case 8:
  415. {
  416. if (0 == in_bits)
  417. {
  418. in_byte = input_buffer[in_idx++];
  419. in_bits = 8;
  420. }
  421. uint16_t pn = (in_byte >> bitshift) & bitmask;
  422. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  423. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  424. in_byte <<= depth;
  425. in_bits -= depth;
  426. }
  427. break;
  428. }
  429. if (whitish)
  430. {
  431. // keep white
  432. }
  433. else if (colored && with_color)
  434. {
  435. out_color_byte &= ~(0x80 >> col % 8); // colored
  436. }
  437. else
  438. {
  439. out_byte &= ~(0x80 >> col % 8); // black
  440. }
  441. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 border)
  442. {
  443. output_row_color_buffer[out_idx] = out_color_byte;
  444. output_row_mono_buffer[out_idx++] = out_byte;
  445. out_byte = 0xFF; // white (for w%8!=0 border)
  446. out_color_byte = 0xFF; // white (for w%8!=0 border)
  447. }
  448. } // end pixel
  449. uint16_t yrow = y + (flip ? h - row - 1 : row);
  450. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  451. } // end line
  452. Serial.print("loaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  453. display.refresh();
  454. }
  455. }
  456. }
  457. file.close();
  458. if (!valid)
  459. {
  460. Serial.println("bitmap format not handled.");
  461. }
  462. }
  463. void drawBitmapFromSerialFlash_Buffered(const char *filename, int16_t x, int16_t y, bool with_color, bool partial_update, bool overwrite)
  464. {
  465. bool valid = false; // valid format to be handled
  466. bool flip = true; // bitmap is stored bottom-to-top
  467. bool has_multicolors = (display.epd2.panel == GxEPD2::ACeP565) || (display.epd2.panel == GxEPD2::GDEY073D46);
  468. uint32_t startTime = millis();
  469. if ((x >= display.width()) || (y >= display.height())) return;
  470. Serial.println();
  471. Serial.print("Loading image '");
  472. Serial.print(filename);
  473. Serial.println('\'');
  474. SerialFlashFile file = SerialFlash.open(filename);
  475. if (!file)
  476. {
  477. Serial.print("File not found");
  478. return;
  479. }
  480. // Parse BMP header
  481. if (read16(file) == 0x4D42) // BMP signature
  482. {
  483. uint32_t fileSize = read32(file);
  484. uint32_t creatorBytes = read32(file); (void)creatorBytes; //unused
  485. uint32_t imageOffset = read32(file); // Start of image data
  486. uint32_t headerSize = read32(file);
  487. uint32_t width = read32(file);
  488. int32_t height = (int32_t) read32(file);
  489. uint16_t planes = read16(file);
  490. uint16_t depth = read16(file); // bits per pixel
  491. uint32_t format = read32(file);
  492. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  493. {
  494. Serial.print("File size: "); Serial.println(fileSize);
  495. Serial.print("Image Offset: "); Serial.println(imageOffset);
  496. Serial.print("Header size: "); Serial.println(headerSize);
  497. Serial.print("Bit Depth: "); Serial.println(depth);
  498. Serial.print("Image size: ");
  499. Serial.print(width);
  500. Serial.print('x');
  501. Serial.println(height);
  502. // BMP rows are padded (if needed) to 4-byte boundary
  503. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  504. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  505. if (height < 0)
  506. {
  507. height = -height;
  508. flip = false;
  509. }
  510. uint16_t w = width;
  511. uint16_t h = height;
  512. if ((x + w - 1) >= display.width()) w = display.width() - x;
  513. if ((y + h - 1) >= display.height()) h = display.height() - y;
  514. //if (w <= max_row_width) // handle with direct drawing
  515. {
  516. valid = true;
  517. uint8_t bitmask = 0xFF;
  518. uint8_t bitshift = 8 - depth;
  519. uint16_t red, green, blue;
  520. bool whitish = false;
  521. bool colored = false;
  522. if (depth == 1) with_color = false;
  523. if (depth <= 8)
  524. {
  525. if (depth < 8) bitmask >>= depth;
  526. //file.seek(54); //palette is always @ 54
  527. file.seek(imageOffset - (4 << depth)); // 54 for regular, diff for colorsimportant
  528. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  529. {
  530. blue = read8(file);
  531. green = read8(file);
  532. red = read8(file);
  533. read8(file);
  534. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  535. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  536. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  537. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  538. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  539. color_palette_buffer[pn / 8] |= colored << pn % 8;
  540. rgb_palette_buffer[pn] = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  541. }
  542. }
  543. if (partial_update) display.setPartialWindow(x, y, w, h);
  544. else display.setFullWindow();
  545. display.firstPage();
  546. do
  547. {
  548. if (!overwrite) display.fillScreen(GxEPD_WHITE);
  549. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  550. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  551. {
  552. uint32_t in_remain = rowSize;
  553. uint32_t in_idx = 0;
  554. uint32_t in_bytes = 0;
  555. uint8_t in_byte = 0; // for depth <= 8
  556. uint8_t in_bits = 0; // for depth <= 8
  557. uint16_t color = GxEPD_WHITE;
  558. file.seek(rowPosition);
  559. for (uint16_t col = 0; col < w; col++) // for each pixel
  560. {
  561. // Time to read more pixel data?
  562. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  563. {
  564. in_bytes = file.read(input_buffer, in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain);
  565. in_remain -= in_bytes;
  566. in_idx = 0;
  567. }
  568. switch (depth)
  569. {
  570. case 32:
  571. blue = input_buffer[in_idx++];
  572. green = input_buffer[in_idx++];
  573. red = input_buffer[in_idx++];
  574. in_idx++; // skip alpha
  575. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  576. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  577. color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  578. break;
  579. case 24:
  580. blue = input_buffer[in_idx++];
  581. green = input_buffer[in_idx++];
  582. red = input_buffer[in_idx++];
  583. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  584. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  585. color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  586. break;
  587. case 16:
  588. {
  589. uint8_t lsb = input_buffer[in_idx++];
  590. uint8_t msb = input_buffer[in_idx++];
  591. if (format == 0) // 555
  592. {
  593. blue = (lsb & 0x1F) << 3;
  594. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  595. red = (msb & 0x7C) << 1;
  596. color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  597. }
  598. else // 565
  599. {
  600. blue = (lsb & 0x1F) << 3;
  601. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  602. red = (msb & 0xF8);
  603. color = (msb << 8) | lsb;
  604. }
  605. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  606. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  607. }
  608. break;
  609. case 1:
  610. case 2:
  611. case 4:
  612. case 8:
  613. {
  614. if (0 == in_bits)
  615. {
  616. in_byte = input_buffer[in_idx++];
  617. in_bits = 8;
  618. }
  619. uint16_t pn = (in_byte >> bitshift) & bitmask;
  620. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  621. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  622. in_byte <<= depth;
  623. in_bits -= depth;
  624. color = rgb_palette_buffer[pn];
  625. }
  626. break;
  627. }
  628. if (with_color && has_multicolors)
  629. {
  630. // keep color
  631. }
  632. else if (whitish)
  633. {
  634. color = GxEPD_WHITE;
  635. }
  636. else if (colored && with_color)
  637. {
  638. color = GxEPD_COLORED;
  639. }
  640. else
  641. {
  642. color = GxEPD_BLACK;
  643. }
  644. uint16_t yrow = y + (flip ? h - row - 1 : row);
  645. display.drawPixel(x + col, yrow, color);
  646. } // end pixel
  647. } // end line
  648. }
  649. while (display.nextPage());
  650. Serial.print("loaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  651. }
  652. }
  653. }
  654. file.close();
  655. if (!valid)
  656. {
  657. Serial.println("bitmap format not handled.");
  658. }
  659. }
  660. uint8_t read8(SerialFlashFile& f)
  661. {
  662. uint8_t result;
  663. f.read((void*)&result, 1);
  664. return result;
  665. }
  666. uint16_t read16(SerialFlashFile& f)
  667. {
  668. // BMP data is stored little-endian, same as Arduino.
  669. uint16_t result;
  670. f.read((void*)&result, 2);
  671. return result;
  672. }
  673. uint32_t read32(SerialFlashFile& f)
  674. {
  675. // BMP data is stored little-endian, same as Arduino.
  676. uint32_t result;
  677. f.read((void*)&result, 4);
  678. return result;
  679. }