GxEPD2_SD_AVR_Example.ino 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. // mapping suggestion from Waveshare SPI e-Paper to Wemos D1 mini
  17. // BUSY -> D2, RST -> D4, DC -> D3, CS -> D8, CLK -> D5, DIN -> D7, GND -> GND, 3.3V -> 3.3V
  18. // mapping suggestion from Waveshare SPI e-Paper to generic ESP8266
  19. // BUSY -> GPIO4, RST -> GPIO2, DC -> GPIO0, CS -> GPIO15, CLK -> GPIO14, DIN -> GPIO13, GND -> GND, 3.3V -> 3.3V
  20. // mapping of Waveshare e-Paper ESP8266 Driver Board
  21. // BUSY -> GPIO16, RST -> GPIO5, DC -> GPIO4, CS -> GPIO15, CLK -> GPIO14, DIN -> GPIO13, GND -> GND, 3.3V -> 3.3V
  22. // mapping suggestion for ESP32, e.g. LOLIN32, see .../variants/.../pins_arduino.h for your board
  23. // NOTE: there are variants with different pins for SPI ! CHECK SPI PINS OF YOUR BOARD
  24. // BUSY -> 4, RST -> 16, DC -> 17, CS -> SS(5), CLK -> SCK(18), DIN -> MOSI(23), GND -> GND, 3.3V -> 3.3V
  25. // new mapping suggestion for STM32F1, e.g. STM32F103C8T6 "BluePill"
  26. // BUSY -> A1, RST -> A2, DC -> A3, CS-> A4, CLK -> A5, DIN -> A7
  27. // mapping suggestion for AVR, UNO, NANO etc.
  28. // BUSY -> 7, RST -> 9, DC -> 8, CS-> 10, CLK -> 13, DIN -> 11
  29. //
  30. // **** NOTE that the mapping suggestion may need modification depending on SD board used! ****
  31. // ********************************************************************************************
  32. //
  33. //#define SD_CS SS // e.g. for RobotDyn Wemos D1 mini SD board
  34. //#define EPD_CS D1 // alternative I use with RobotDyn Wemos D1 mini SD board
  35. #include <GxEPD2_BW.h>
  36. #include <GxEPD2_3C.h>
  37. #include <Fonts/FreeMonoBold9pt7b.h>
  38. #if defined(ESP32)
  39. // has support for FAT32 support with long filenames
  40. #include "FS.h"
  41. #include "SD.h"
  42. #include "SPI.h"
  43. #define SdFile File
  44. #define seekSet seek
  45. #else
  46. // include SdFat for FAT32 support with long filenames; available through Library Manager
  47. #include <SdFat.h>
  48. SdFat SD;
  49. #endif
  50. #if defined(__AVR)
  51. #define SD_CS 6 // adapt to your wiring
  52. #define EPD_CS SS // adapt to your wiring
  53. // select one and adapt to your mapping
  54. //GxEPD2_154 display(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7); // GDEP015OC1 no longer available
  55. //GxEPD2_154_D67 display(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7); // GDEH0154D67
  56. //GxEPD2_213 display(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7);
  57. //GxEPD2_290 display(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7);
  58. //GxEPD2_270 display(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7);
  59. //GxEPD2_420 display(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7);
  60. //GxEPD2_750 display(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7);
  61. // 3-color e-papers
  62. // !!! can't be used for this //GxEPD2_154c display(GxEPD2_154c(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7);
  63. //GxEPD2_213c display(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7);
  64. //GxEPD2_290c display(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7);
  65. //GxEPD2_270c display(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7);
  66. //GxEPD2_420c display(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7);
  67. //GxEPD2_750c display(/*CS=10*/ EPD_CS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7);
  68. #endif
  69. // non-AVR board can also be used with GxEPD2 base display classes, e.g. for SD bitmap drawing
  70. // uncomment your display for your board in the following included header file and adapt to your mapping
  71. #include "GxEPD2_SD_AVR_boards_added.h"
  72. // function declaration with default parameter
  73. void drawBitmapFromSD(const char *filename, int16_t x, int16_t y, bool with_color = true);
  74. void setup()
  75. {
  76. Serial.begin(115200);
  77. Serial.println();
  78. Serial.println(F("setup"));
  79. display.init(115200);
  80. Serial.print(F("Initializing SD card..."));
  81. if (!SD.begin(SD_CS))
  82. {
  83. Serial.println(F("SD failed!"));
  84. return;
  85. }
  86. Serial.println(F("SD OK!"));
  87. drawBitmaps_200x200();
  88. drawBitmaps_other();
  89. //drawBitmaps_test();
  90. }
  91. void loop(void)
  92. {
  93. }
  94. void drawBitmaps_200x200()
  95. {
  96. int16_t x = (int16_t(display.WIDTH) - 200) / 2;
  97. int16_t y = (int16_t(display.HEIGHT) - 200) / 2;
  98. drawBitmapFromSD("logo200x200.bmp", x, y);
  99. delay(2000);
  100. drawBitmapFromSD("first200x200.bmp", x, y);
  101. delay(2000);
  102. drawBitmapFromSD("second200x200.bmp", x, y);
  103. delay(2000);
  104. drawBitmapFromSD("third200x200.bmp", x, y);
  105. delay(2000);
  106. drawBitmapFromSD("fourth200x200.bmp", x, y);
  107. delay(2000);
  108. drawBitmapFromSD("fifth200x200.bmp", x, y);
  109. delay(2000);
  110. drawBitmapFromSD("sixth200x200.bmp", x, y);
  111. delay(2000);
  112. drawBitmapFromSD("seventh200x200.bmp", x, y);
  113. delay(2000);
  114. drawBitmapFromSD("eighth200x200.bmp", x, y);
  115. delay(2000);
  116. }
  117. void drawBitmaps_other()
  118. {
  119. int16_t w2 = display.WIDTH / 2;
  120. int16_t h2 = display.HEIGHT / 2;
  121. drawBitmapFromSD("parrot.bmp", w2 - 64, h2 - 80);
  122. delay(2000);
  123. drawBitmapFromSD("betty_1.bmp", w2 - 100, h2 - 160);
  124. delay(2000);
  125. drawBitmapFromSD("betty_4.bmp", w2 - 102, h2 - 126);
  126. delay(2000);
  127. drawBitmapFromSD("marilyn_240x240x8.bmp", w2 - 120, h2 - 120);
  128. delay(2000);
  129. drawBitmapFromSD("miniwoof.bmp", w2 - 60, h2 - 80);
  130. delay(2000);
  131. drawBitmapFromSD("t200x200.bmp", w2 - 100, h2 - 100);
  132. delay(2000);
  133. drawBitmapFromSD("test.bmp", w2 - 120, h2 - 160);
  134. delay(2000);
  135. drawBitmapFromSD("tiger.bmp", w2 - 160, h2 - 120);
  136. delay(2000);
  137. drawBitmapFromSD("tiger_178x160x4.bmp", w2 - 89, h2 - 80);
  138. delay(2000);
  139. drawBitmapFromSD("tiger_240x317x4.bmp", w2 - 120, h2 - 160);
  140. delay(2000);
  141. drawBitmapFromSD("tiger_320x200x24.bmp", w2 - 160, h2 - 100);
  142. delay(2000);
  143. drawBitmapFromSD("tiger16T.bmp", w2 - 160, h2 - 120);
  144. delay(2000);
  145. drawBitmapFromSD("woof.bmp", w2 - 120, h2 - 160);
  146. delay(2000);
  147. drawBitmapFromSD("bitmap640x384_1.bmp", 0, 0);
  148. delay(2000);
  149. }
  150. void drawBitmaps_test()
  151. {
  152. int16_t w2 = display.WIDTH / 2;
  153. int16_t h2 = display.HEIGHT / 2;
  154. drawBitmapFromSD("betty_4.bmp", w2 - 102, h2 - 126);
  155. delay(2000);
  156. }
  157. static const uint16_t input_buffer_pixels = 20; // may affect performance
  158. static const uint16_t max_row_width = 640; // for up to 7.5" display
  159. static const uint16_t max_palette_pixels = 256; // for depth <= 8
  160. uint8_t input_buffer[3 * input_buffer_pixels]; // up to depth 24
  161. uint8_t output_row_mono_buffer[max_row_width / 8]; // buffer for at least one row of b/w bits
  162. uint8_t output_row_color_buffer[max_row_width / 8]; // buffer for at least one row of color bits
  163. uint8_t mono_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 b/w
  164. uint8_t color_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 c/w
  165. void drawBitmapFromSD(const char *filename, int16_t x, int16_t y, bool with_color)
  166. {
  167. SdFile file;
  168. bool valid = false; // valid format to be handled
  169. bool flip = true; // bitmap is stored bottom-to-top
  170. uint32_t startTime = millis();
  171. //Serial.println(); Serial.print(x); Serial.print(" "); Serial.print(y); Serial.print(" "); Serial.println(filename);
  172. if ((x >= int16_t(display.WIDTH)) || (y >= int16_t(display.HEIGHT))) return;
  173. Serial.println();
  174. Serial.print(F("Loading image '"));
  175. Serial.print(filename);
  176. Serial.println('\'');
  177. #if defined(ESP32)
  178. file = SD.open(String("/") + filename, FILE_READ);
  179. if (!file)
  180. {
  181. Serial.print(F("File not found"));
  182. return;
  183. }
  184. #else
  185. if (!file.open(filename, FILE_READ))
  186. {
  187. Serial.print(F("File not found"));
  188. return;
  189. }
  190. #endif
  191. // Parse BMP header
  192. if (read16(file) == 0x4D42) // BMP signature
  193. {
  194. uint32_t fileSize = read32(file);
  195. uint32_t creatorBytes = read32(file);
  196. uint32_t imageOffset = read32(file); // Start of image data
  197. uint32_t headerSize = read32(file);
  198. uint32_t width = read32(file);
  199. uint32_t height = read32(file);
  200. uint16_t planes = read16(file);
  201. uint16_t depth = read16(file); // bits per pixel
  202. uint32_t format = read32(file);
  203. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  204. {
  205. Serial.print(F("File size: ")); Serial.println(fileSize);
  206. Serial.print(F("Image Offset: ")); Serial.println(imageOffset);
  207. Serial.print(F("Header size: ")); Serial.println(headerSize);
  208. Serial.print(F("Bit Depth: ")); Serial.println(depth);
  209. Serial.print(F("Image size: "));
  210. Serial.print(width);
  211. Serial.print('x');
  212. Serial.println(height);
  213. // BMP rows are padded (if needed) to 4-byte boundary
  214. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  215. if (height < 0)
  216. {
  217. height = -height;
  218. flip = false;
  219. }
  220. uint16_t w = width;
  221. uint16_t h = height;
  222. if ((x + w - 1) >= int16_t(display.WIDTH)) w = int16_t(display.WIDTH) - x;
  223. if ((y + h - 1) >= int16_t(display.HEIGHT)) h = int16_t(display.HEIGHT) - y;
  224. if (w <= max_row_width) // handle with direct drawing
  225. {
  226. valid = true;
  227. uint8_t bitmask = 0xFF;
  228. uint8_t bitshift = 8 - depth;
  229. uint16_t red, green, blue;
  230. bool whitish, colored;
  231. if (depth == 1) with_color = false;
  232. if (depth <= 8)
  233. {
  234. if (depth < 8) bitmask >>= depth;
  235. //file.seekSet(54); //palette is always @ 54
  236. file.seekSet(imageOffset - (4 << depth)); // 54 for regular, diff for colorsimportant
  237. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  238. {
  239. blue = file.read();
  240. green = file.read();
  241. red = file.read();
  242. file.read();
  243. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  244. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  245. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  246. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  247. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  248. color_palette_buffer[pn / 8] |= colored << pn % 8;
  249. }
  250. }
  251. display.clearScreen();
  252. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  253. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  254. {
  255. uint32_t in_remain = rowSize;
  256. uint32_t in_idx = 0;
  257. uint32_t in_bytes = 0;
  258. uint8_t in_byte = 0; // for depth <= 8
  259. uint8_t in_bits = 0; // for depth <= 8
  260. uint8_t out_byte = 0xFF; // white (for w%8!=0 border)
  261. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 border)
  262. uint32_t out_idx = 0;
  263. file.seekSet(rowPosition);
  264. for (uint16_t col = 0; col < w; col++) // for each pixel
  265. {
  266. // Time to read more pixel data?
  267. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  268. {
  269. in_bytes = file.read(input_buffer, in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain);
  270. in_remain -= in_bytes;
  271. in_idx = 0;
  272. }
  273. switch (depth)
  274. {
  275. case 24:
  276. blue = input_buffer[in_idx++];
  277. green = input_buffer[in_idx++];
  278. red = input_buffer[in_idx++];
  279. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  280. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  281. break;
  282. case 16:
  283. {
  284. uint8_t lsb = input_buffer[in_idx++];
  285. uint8_t msb = input_buffer[in_idx++];
  286. if (format == 0) // 555
  287. {
  288. blue = (lsb & 0x1F) << 3;
  289. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  290. red = (msb & 0x7C) << 1;
  291. }
  292. else // 565
  293. {
  294. blue = (lsb & 0x1F) << 3;
  295. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  296. red = (msb & 0xF8);
  297. }
  298. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  299. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  300. }
  301. break;
  302. case 1:
  303. case 4:
  304. case 8:
  305. {
  306. if (0 == in_bits)
  307. {
  308. in_byte = input_buffer[in_idx++];
  309. in_bits = 8;
  310. }
  311. uint16_t pn = (in_byte >> bitshift) & bitmask;
  312. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  313. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  314. in_byte <<= depth;
  315. in_bits -= depth;
  316. }
  317. break;
  318. }
  319. if (whitish)
  320. {
  321. //out_byte |= 0x80 >> col % 8; // not black
  322. //out_color_byte |= 0x80 >> col % 8; // not colored
  323. // keep white
  324. }
  325. else if (colored && with_color)
  326. {
  327. //out_byte |= 0x80 >> col % 8; // not black
  328. out_color_byte &= ~(0x80 >> col % 8); // colored
  329. }
  330. else
  331. {
  332. //out_color_byte |= 0x80 >> col % 8; // not colored
  333. out_byte &= ~(0x80 >> col % 8); // black
  334. }
  335. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 border)
  336. {
  337. output_row_color_buffer[out_idx] = out_color_byte;
  338. output_row_mono_buffer[out_idx++] = out_byte;
  339. out_byte = 0xFF; // white (for w%8!=0 border)
  340. out_color_byte = 0xFF; // white (for w%8!=0 border)
  341. }
  342. } // end pixel
  343. uint16_t yrow = y + (flip ? h - row - 1 : row);
  344. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  345. } // end line
  346. Serial.print(F("loaded in ")); Serial.print(millis() - startTime); Serial.println(F(" ms"));
  347. display.refresh();
  348. }
  349. }
  350. }
  351. //Serial.print(F("end curPosition ")); Serial.println(file.curPosition());
  352. file.close();
  353. if (!valid)
  354. {
  355. Serial.println(F("bitmap format not handled."));
  356. }
  357. }
  358. uint16_t read16(SdFile& f)
  359. {
  360. // BMP data is stored little-endian, same as Arduino.
  361. uint16_t result;
  362. ((uint8_t *)&result)[0] = f.read(); // LSB
  363. ((uint8_t *)&result)[1] = f.read(); // MSB
  364. return result;
  365. }
  366. uint32_t read32(SdFile& f)
  367. {
  368. // BMP data is stored little-endian, same as Arduino.
  369. uint32_t result;
  370. ((uint8_t *)&result)[0] = f.read(); // LSB
  371. ((uint8_t *)&result)[1] = f.read();
  372. ((uint8_t *)&result)[2] = f.read();
  373. ((uint8_t *)&result)[3] = f.read(); // MSB
  374. return result;
  375. }