GxEPD2_SD_Example.ino 29 KB

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