GxEPD2_SD_Example.ino 27 KB

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