GxEPD2_SD_AVR_Example.ino 15 KB

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