GxEPD2_SD_AVR_Example.ino 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. // GxEPD2_SD_AVR_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. // Supporting Arduino Forum Topics:
  14. // Waveshare e-paper displays with SPI: http://forum.arduino.cc/index.php?topic=487007.0
  15. // Good Display ePaper for Arduino: https://forum.arduino.cc/index.php?topic=436411.0
  16. // see GxEPD2_wiring_examples.h for wiring suggestions and examples
  17. // **** NOTE that the mapping suggestion may need modification depending on SD board used! ****
  18. // ********************************************************************************************
  19. //
  20. //#define SD_CS SS // e.g. for RobotDyn Wemos D1 mini SD board
  21. //#define EPD_CS D1 // alternative I use with RobotDyn Wemos D1 mini SD board
  22. // uncomment next line to use class GFX of library GFX_Root instead of Adafruit_GFX
  23. //#include <GFX.h>
  24. // Note: if you use this with ENABLE_GxEPD2_GFX 1:
  25. // uncomment it in GxEPD2_GFX.h too, or add #include <GFX.h> before any #include <GxEPD2_GFX.h>
  26. #include <GxEPD2_BW.h>
  27. #include <GxEPD2_3C.h>
  28. #include <Fonts/FreeMonoBold9pt7b.h>
  29. #if defined(ESP32)
  30. // has support for FAT32 support with long filenames
  31. #include "FS.h"
  32. #include "SD.h"
  33. #include "SPI.h"
  34. #define SdFile File
  35. #define seekSet seek
  36. #else
  37. // include SdFat for FAT32 support with long filenames; available through Library Manager
  38. #include <SdFat.h>
  39. SdFat SD;
  40. #endif
  41. // select the display driver class (only one) for your panel
  42. //#define GxEPD2_DRIVER_CLASS GxEPD2_154 // GDEP015OC1 200x200, no longer available
  43. //#define GxEPD2_DRIVER_CLASS GxEPD2_154_D67 // GDEH0154D67 200x200
  44. //#define GxEPD2_DRIVER_CLASS GxEPD2_154_T8 // GDEW0154T8 152x152
  45. //#define GxEPD2_DRIVER_CLASS GxEPD2_154_M09 // GDEW0154M09 200x200
  46. //#define GxEPD2_DRIVER_CLASS GxEPD2_154_M10 // GDEW0154M10 152x152
  47. //#define GxEPD2_DRIVER_CLASS GxEPD2_213 // GDE0213B1 128x250, phased out
  48. //#define GxEPD2_DRIVER_CLASS GxEPD2_213_B72 // GDEH0213B72 128x250
  49. //#define GxEPD2_DRIVER_CLASS GxEPD2_213_B73 // GDEH0213B73 128x250
  50. //#define GxEPD2_DRIVER_CLASS GxEPD2_213_flex // GDEW0213I5F 128x250
  51. //#define GxEPD2_DRIVER_CLASS GxEPD2_213_M21 // GDEW0213M21 128x250
  52. //#define GxEPD2_DRIVER_CLASS GxEPD2_290 // GDEH029A1 128x296
  53. //#define GxEPD2_DRIVER_CLASS GxEPD2_290_T5 // GDEW029T5 128x296
  54. //#define GxEPD2_DRIVER_CLASS GxEPD2_290_M06 // GDEW029M06 128x296
  55. //#define GxEPD2_DRIVER_CLASS GxEPD2_290_T94 // GDEM029T94 128x296
  56. //#define GxEPD2_DRIVER_CLASS GxEPD2_260 // GDEW026T0 152x296
  57. //#define GxEPD2_DRIVER_CLASS GxEPD2_270 // GDEW027W3 176x264
  58. //#define GxEPD2_DRIVER_CLASS GxEPD2_371 // GDEW0371W7 240x416
  59. //#define GxEPD2_DRIVER_CLASS GxEPD2_420 // GDEW042T2 400x300
  60. //#define GxEPD2_DRIVER_CLASS GxEPD2_420_M01 // GDEW042M01 400x300
  61. //#define GxEPD2_DRIVER_CLASS GxEPD2_583 // GDEW0583T7 600x448
  62. //#define GxEPD2_DRIVER_CLASS GxEPD2_583_T8 // GDEW0583T8 648x480
  63. //#define GxEPD2_DRIVER_CLASS GxEPD2_750 // GDEW075T8 640x384
  64. //#define GxEPD2_DRIVER_CLASS GxEPD2_750_T7 // GDEW075T7 800x480
  65. // 3-color e-papers
  66. //#define GxEPD2_DRIVER_CLASS GxEPD2_154c // GDEW0154Z04 200x200, no longer available
  67. //#define GxEPD2_DRIVER_CLASS GxEPD2_154_Z90c // GDEH0154Z90 200x200
  68. //#define GxEPD2_DRIVER_CLASS GxEPD2_213c // GDEW0213Z16 104x212
  69. //#define GxEPD2_DRIVER_CLASS GxEPD2_290c // GDEW029Z10 128x296
  70. //#define GxEPD2_DRIVER_CLASS GxEPD2_270c // GDEW027C44 176x264
  71. //#define GxEPD2_DRIVER_CLASS GxEPD2_420c // GDEW042Z15 400x300
  72. //#define GxEPD2_DRIVER_CLASS GxEPD2_583c // GDEW0583Z21 600x448
  73. //#define GxEPD2_DRIVER_CLASS GxEPD2_750c // GDEW075Z09 600x384
  74. //#define GxEPD2_DRIVER_CLASS GxEPD2_750c_Z08 // GDEW075Z08 800x480
  75. //#define GxEPD2_DRIVER_CLASS GxEPD2_750c_Z90 // GDEH075Z90 880x528
  76. // 7-color e-paper
  77. //#define GxEPD2_DRIVER_CLASS GxEPD2_565c // Waveshare 5.65" 7-color (3C graphics)
  78. #if defined(__AVR)
  79. #define SD_CS 6 // adapt to your wiring
  80. #define EPD_CS SS // adapt to your wiring
  81. GxEPD2_DRIVER_CLASS display(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7);
  82. #endif
  83. // non-AVR board can also be used with GxEPD2 base display classes, e.g. for SD bitmap drawing
  84. // uncomment your display for your board in the following included header file and adapt to your mapping
  85. #include "GxEPD2_SD_AVR_boards_added.h"
  86. // function declaration with default parameter
  87. void drawBitmapFromSD(const char *filename, int16_t x, int16_t y, bool with_color = true);
  88. void setup()
  89. {
  90. Serial.begin(115200);
  91. Serial.println();
  92. Serial.println(F("setup"));
  93. display.init(115200);
  94. Serial.print(F("Initializing SD card..."));
  95. if (!SD.begin(SD_CS))
  96. {
  97. Serial.println(F("SD failed!"));
  98. return;
  99. }
  100. Serial.println(F("SD OK!"));
  101. drawBitmaps_200x200();
  102. drawBitmaps_other();
  103. //drawBitmaps_test();
  104. }
  105. void loop(void)
  106. {
  107. }
  108. void drawBitmaps_200x200()
  109. {
  110. int16_t x = (int16_t(display.WIDTH) - 200) / 2;
  111. int16_t y = (int16_t(display.HEIGHT) - 200) / 2;
  112. drawBitmapFromSD("logo200x200.bmp", x, y);
  113. delay(2000);
  114. drawBitmapFromSD("first200x200.bmp", x, y);
  115. delay(2000);
  116. drawBitmapFromSD("second200x200.bmp", x, y);
  117. delay(2000);
  118. drawBitmapFromSD("third200x200.bmp", x, y);
  119. delay(2000);
  120. drawBitmapFromSD("fourth200x200.bmp", x, y);
  121. delay(2000);
  122. drawBitmapFromSD("fifth200x200.bmp", x, y);
  123. delay(2000);
  124. drawBitmapFromSD("sixth200x200.bmp", x, y);
  125. delay(2000);
  126. drawBitmapFromSD("seventh200x200.bmp", x, y);
  127. delay(2000);
  128. drawBitmapFromSD("eighth200x200.bmp", x, y);
  129. delay(2000);
  130. }
  131. void drawBitmaps_other()
  132. {
  133. int16_t w2 = display.WIDTH / 2;
  134. int16_t h2 = display.HEIGHT / 2;
  135. drawBitmapFromSD("parrot.bmp", w2 - 64, h2 - 80);
  136. delay(2000);
  137. drawBitmapFromSD("betty_1.bmp", w2 - 100, h2 - 160);
  138. delay(2000);
  139. drawBitmapFromSD("betty_4.bmp", w2 - 102, h2 - 126);
  140. delay(2000);
  141. drawBitmapFromSD("marilyn_240x240x8.bmp", w2 - 120, h2 - 120);
  142. delay(2000);
  143. drawBitmapFromSD("miniwoof.bmp", w2 - 60, h2 - 80);
  144. delay(2000);
  145. drawBitmapFromSD("t200x200.bmp", w2 - 100, h2 - 100);
  146. delay(2000);
  147. drawBitmapFromSD("test.bmp", w2 - 120, h2 - 160);
  148. delay(2000);
  149. drawBitmapFromSD("tiger.bmp", w2 - 160, h2 - 120);
  150. delay(2000);
  151. drawBitmapFromSD("tiger_178x160x4.bmp", w2 - 89, h2 - 80);
  152. delay(2000);
  153. drawBitmapFromSD("tiger_240x317x4.bmp", w2 - 120, h2 - 160);
  154. delay(2000);
  155. drawBitmapFromSD("tiger_320x200x24.bmp", w2 - 160, h2 - 100);
  156. delay(2000);
  157. drawBitmapFromSD("tiger16T.bmp", w2 - 160, h2 - 120);
  158. delay(2000);
  159. drawBitmapFromSD("woof.bmp", w2 - 120, h2 - 160);
  160. delay(2000);
  161. drawBitmapFromSD("bitmap640x384_1.bmp", 0, 0);
  162. delay(2000);
  163. }
  164. void drawBitmaps_test()
  165. {
  166. int16_t w2 = display.WIDTH / 2;
  167. int16_t h2 = display.HEIGHT / 2;
  168. drawBitmapFromSD("betty_4.bmp", w2 - 102, h2 - 126);
  169. delay(2000);
  170. }
  171. static const uint16_t input_buffer_pixels = 20; // may affect performance
  172. static const uint16_t max_row_width = 640; // for up to 7.5" display
  173. static const uint16_t max_palette_pixels = 256; // for depth <= 8
  174. uint8_t input_buffer[3 * input_buffer_pixels]; // up to depth 24
  175. uint8_t output_row_mono_buffer[max_row_width / 8]; // buffer for at least one row of b/w bits
  176. uint8_t output_row_color_buffer[max_row_width / 8]; // buffer for at least one row of color bits
  177. uint8_t mono_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 b/w
  178. uint8_t color_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 c/w
  179. void drawBitmapFromSD(const char *filename, int16_t x, int16_t y, bool with_color)
  180. {
  181. SdFile file;
  182. bool valid = false; // valid format to be handled
  183. bool flip = true; // bitmap is stored bottom-to-top
  184. uint32_t startTime = millis();
  185. //Serial.println(); Serial.print(x); Serial.print(" "); Serial.print(y); Serial.print(" "); Serial.println(filename);
  186. if ((x >= int16_t(display.WIDTH)) || (y >= int16_t(display.HEIGHT))) return;
  187. Serial.println();
  188. Serial.print(F("Loading image '"));
  189. Serial.print(filename);
  190. Serial.println('\'');
  191. #if defined(ESP32)
  192. file = SD.open(String("/") + filename, FILE_READ);
  193. if (!file)
  194. {
  195. Serial.print(F("File not found"));
  196. return;
  197. }
  198. #else
  199. if (!file.open(filename, FILE_READ))
  200. {
  201. Serial.print(F("File not found"));
  202. return;
  203. }
  204. #endif
  205. // Parse BMP header
  206. if (read16(file) == 0x4D42) // BMP signature
  207. {
  208. uint32_t fileSize = read32(file);
  209. uint32_t creatorBytes = read32(file);
  210. uint32_t imageOffset = read32(file); // Start of image data
  211. uint32_t headerSize = read32(file);
  212. uint32_t width = read32(file);
  213. uint32_t height = read32(file);
  214. uint16_t planes = read16(file);
  215. uint16_t depth = read16(file); // bits per pixel
  216. uint32_t format = read32(file);
  217. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  218. {
  219. Serial.print(F("File size: ")); Serial.println(fileSize);
  220. Serial.print(F("Image Offset: ")); Serial.println(imageOffset);
  221. Serial.print(F("Header size: ")); Serial.println(headerSize);
  222. Serial.print(F("Bit Depth: ")); Serial.println(depth);
  223. Serial.print(F("Image size: "));
  224. Serial.print(width);
  225. Serial.print('x');
  226. Serial.println(height);
  227. // BMP rows are padded (if needed) to 4-byte boundary
  228. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  229. if (height < 0)
  230. {
  231. height = -height;
  232. flip = false;
  233. }
  234. uint16_t w = width;
  235. uint16_t h = height;
  236. if ((x + w - 1) >= int16_t(display.WIDTH)) w = int16_t(display.WIDTH) - x;
  237. if ((y + h - 1) >= int16_t(display.HEIGHT)) h = int16_t(display.HEIGHT) - y;
  238. if (w <= max_row_width) // handle with direct drawing
  239. {
  240. valid = true;
  241. uint8_t bitmask = 0xFF;
  242. uint8_t bitshift = 8 - depth;
  243. uint16_t red, green, blue;
  244. bool whitish, colored;
  245. if (depth == 1) with_color = false;
  246. if (depth <= 8)
  247. {
  248. if (depth < 8) bitmask >>= depth;
  249. //file.seekSet(54); //palette is always @ 54
  250. file.seekSet(imageOffset - (4 << depth)); // 54 for regular, diff for colorsimportant
  251. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  252. {
  253. blue = file.read();
  254. green = file.read();
  255. red = file.read();
  256. file.read();
  257. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  258. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  259. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  260. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  261. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  262. color_palette_buffer[pn / 8] |= colored << pn % 8;
  263. }
  264. }
  265. display.clearScreen();
  266. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  267. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  268. {
  269. uint32_t in_remain = rowSize;
  270. uint32_t in_idx = 0;
  271. uint32_t in_bytes = 0;
  272. uint8_t in_byte = 0; // for depth <= 8
  273. uint8_t in_bits = 0; // for depth <= 8
  274. uint8_t out_byte = 0xFF; // white (for w%8!=0 border)
  275. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 border)
  276. uint32_t out_idx = 0;
  277. file.seekSet(rowPosition);
  278. for (uint16_t col = 0; col < w; col++) // for each pixel
  279. {
  280. // Time to read more pixel data?
  281. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  282. {
  283. in_bytes = file.read(input_buffer, in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain);
  284. in_remain -= in_bytes;
  285. in_idx = 0;
  286. }
  287. switch (depth)
  288. {
  289. case 24:
  290. blue = input_buffer[in_idx++];
  291. green = input_buffer[in_idx++];
  292. red = input_buffer[in_idx++];
  293. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  294. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  295. break;
  296. case 16:
  297. {
  298. uint8_t lsb = input_buffer[in_idx++];
  299. uint8_t msb = input_buffer[in_idx++];
  300. if (format == 0) // 555
  301. {
  302. blue = (lsb & 0x1F) << 3;
  303. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  304. red = (msb & 0x7C) << 1;
  305. }
  306. else // 565
  307. {
  308. blue = (lsb & 0x1F) << 3;
  309. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  310. red = (msb & 0xF8);
  311. }
  312. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  313. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  314. }
  315. break;
  316. case 1:
  317. case 4:
  318. case 8:
  319. {
  320. if (0 == in_bits)
  321. {
  322. in_byte = input_buffer[in_idx++];
  323. in_bits = 8;
  324. }
  325. uint16_t pn = (in_byte >> bitshift) & bitmask;
  326. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  327. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  328. in_byte <<= depth;
  329. in_bits -= depth;
  330. }
  331. break;
  332. }
  333. if (whitish)
  334. {
  335. //out_byte |= 0x80 >> col % 8; // not black
  336. //out_color_byte |= 0x80 >> col % 8; // not colored
  337. // keep white
  338. }
  339. else if (colored && with_color)
  340. {
  341. //out_byte |= 0x80 >> col % 8; // not black
  342. out_color_byte &= ~(0x80 >> col % 8); // colored
  343. }
  344. else
  345. {
  346. //out_color_byte |= 0x80 >> col % 8; // not colored
  347. out_byte &= ~(0x80 >> col % 8); // black
  348. }
  349. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 border)
  350. {
  351. output_row_color_buffer[out_idx] = out_color_byte;
  352. output_row_mono_buffer[out_idx++] = out_byte;
  353. out_byte = 0xFF; // white (for w%8!=0 border)
  354. out_color_byte = 0xFF; // white (for w%8!=0 border)
  355. }
  356. } // end pixel
  357. uint16_t yrow = y + (flip ? h - row - 1 : row);
  358. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  359. } // end line
  360. Serial.print(F("loaded in ")); Serial.print(millis() - startTime); Serial.println(F(" ms"));
  361. display.refresh();
  362. }
  363. }
  364. }
  365. //Serial.print(F("end curPosition ")); Serial.println(file.curPosition());
  366. file.close();
  367. if (!valid)
  368. {
  369. Serial.println(F("bitmap format not handled."));
  370. }
  371. }
  372. uint16_t read16(SdFile& f)
  373. {
  374. // BMP data is stored little-endian, same as Arduino.
  375. uint16_t result;
  376. ((uint8_t *)&result)[0] = f.read(); // LSB
  377. ((uint8_t *)&result)[1] = f.read(); // MSB
  378. return result;
  379. }
  380. uint32_t read32(SdFile& f)
  381. {
  382. // BMP data is stored little-endian, same as Arduino.
  383. uint32_t result;
  384. ((uint8_t *)&result)[0] = f.read(); // LSB
  385. ((uint8_t *)&result)[1] = f.read();
  386. ((uint8_t *)&result)[2] = f.read();
  387. ((uint8_t *)&result)[3] = f.read(); // MSB
  388. return result;
  389. }