GxEPD2_SD_Example.ino 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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: 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. // Author: Jean-Marc Zingg
  7. //
  8. // Version: see library.properties
  9. //
  10. // Library: https://github.com/ZinggJM/GxEPD2
  11. // Supporting Arduino Forum Topics:
  12. // Waveshare e-paper displays with SPI: http://forum.arduino.cc/index.php?topic=487007.0
  13. // Good Dispay ePaper for Arduino : https://forum.arduino.cc/index.php?topic=436411.0
  14. // mapping suggestion from Waveshare SPI e-Paper to Wemos D1 mini
  15. // BUSY -> D2, RST -> D4, DC -> D3, CS -> D8, CLK -> D5, DIN -> D7, GND -> GND, 3.3V -> 3.3V
  16. // mapping suggestion from Waveshare SPI e-Paper to generic ESP8266
  17. // BUSY -> GPIO4, RST -> GPIO2, DC -> GPIO0, CS -> GPIO15, CLK -> GPIO14, DIN -> GPIO13, GND -> GND, 3.3V -> 3.3V
  18. // mapping of Waveshare e-Paper ESP8266 Driver Board
  19. // BUSY -> GPIO16, RST -> GPIO5, DC -> GPIO4, CS -> GPIO15, CLK -> GPIO14, DIN -> GPIO13, GND -> GND, 3.3V -> 3.3V
  20. // mapping suggestion for ESP32, e.g. LOLIN32, see .../variants/.../pins_arduino.h for your board
  21. // NOTE: there are variants with different pins for SPI ! CHECK SPI PINS OF YOUR BOARD
  22. // BUSY -> 4, RST -> 16, DC -> 17, CS -> SS(5), CLK -> SCK(18), DIN -> MOSI(23), GND -> GND, 3.3V -> 3.3V
  23. // new mapping suggestion for STM32F1, e.g. STM32F103C8T6 "BluePill"
  24. // BUSY -> A1, RST -> A2, DC -> A3, CS-> A4, CLK -> A5, DIN -> A7
  25. // mapping suggestion for AVR, UNO, NANO etc.
  26. // BUSY -> 7, RST -> 9, DC -> 8, CS-> 10, CLK -> 13, DIN -> 11
  27. //
  28. // **** NOTE that the mapping suggestion may need modification depending on SD board used! ****
  29. // ********************************************************************************************
  30. //
  31. //#define SD_CS SS // e.g. for RobotDyn Wemos D1 mini SD board
  32. //#define EPD_CS D1 // alternative I use with RobotDyn Wemos D1 mini SD board
  33. #include <GxEPD2_BW.h>
  34. #include <GxEPD2_3C.h>
  35. #include <Fonts/FreeMonoBold9pt7b.h>
  36. // include SdFat for FAT32 support with long filenames; available through Library Manager
  37. #include <SdFat.h>
  38. #if defined(SdFat_h)
  39. SdFat SD;
  40. #define FileClass SdFile
  41. #define position curPosition
  42. #define seek seekSet
  43. #else
  44. #include <SD.h>
  45. #define FileClass File
  46. #undef SdFat_h
  47. #endif
  48. #if defined (ESP8266)
  49. #define SD_CS SS // e.g. for RobotDyn Wemos D1 mini SD board
  50. #define EPD_CS D1 // alternative I use with RobotDyn Wemos D1 mini SD board
  51. // select one and adapt to your mapping, can use full buffer size (full HEIGHT)
  52. //GxEPD2_BW<GxEPD2_154, GxEPD2_154::HEIGHT> display(GxEPD2_154(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  53. //GxEPD2_BW<GxEPD2_213, GxEPD2_213::HEIGHT> display(GxEPD2_213(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  54. //GxEPD2_BW<GxEPD2_290, GxEPD2_290::HEIGHT> display(GxEPD2_290(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  55. //GxEPD2_BW<GxEPD2_270, GxEPD2_270::HEIGHT> display(GxEPD2_270(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  56. //GxEPD2_BW<GxEPD2_420, GxEPD2_420::HEIGHT> display(GxEPD2_420(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  57. // can use only half buffer size
  58. //GxEPD2_BW < GxEPD2_583, GxEPD2_583::HEIGHT / 2 > display(GxEPD2_583(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  59. //GxEPD2_BW < GxEPD2_750, GxEPD2_750::HEIGHT / 2 > display(GxEPD2_750(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  60. // 3-color e-papers
  61. //GxEPD2_3C<GxEPD2_154c, GxEPD2_154c::HEIGHT> display(GxEPD2_154c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  62. //GxEPD2_3C<GxEPD2_213c, GxEPD2_213c::HEIGHT> display(GxEPD2_213c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  63. //GxEPD2_3C<GxEPD2_290c, GxEPD2_290c::HEIGHT> display(GxEPD2_290c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  64. //GxEPD2_3C<GxEPD2_270c, GxEPD2_270c::HEIGHT> display(GxEPD2_270c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  65. // can use only half buffer size
  66. //GxEPD2_3C<GxEPD2_420c, GxEPD2_420c::HEIGHT / 2> display(GxEPD2_420c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  67. // can use only quarter buffer size
  68. //GxEPD2_3C<GxEPD2_583c, GxEPD2_583c::HEIGHT / 4> display(GxEPD2_583c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  69. //GxEPD2_3C<GxEPD2_750c, GxEPD2_750c::HEIGHT / 4> display(GxEPD2_750c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  70. // ***** for mapping of Waveshare e-Paper ESP8266 Driver Board *****
  71. // select one , can use full buffer size (full HEIGHT)
  72. //GxEPD2_BW<GxEPD2_154, GxEPD2_154::HEIGHT> display(GxEPD2_154(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  73. //GxEPD2_BW<GxEPD2_213, GxEPD2_213::HEIGHT> display(GxEPD2_213(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  74. //GxEPD2_BW<GxEPD2_290, GxEPD2_290::HEIGHT> display(GxEPD2_290(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  75. //GxEPD2_BW<GxEPD2_270, GxEPD2_270::HEIGHT> display(GxEPD2_270(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  76. //GxEPD2_BW<GxEPD2_420, GxEPD2_420::HEIGHT> display(GxEPD2_420(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  77. // can use only half buffer size
  78. //GxEPD2_BW < GxEPD2_583, GxEPD2_583::HEIGHT / 2 > display(GxEPD2_583(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  79. //GxEPD2_BW < GxEPD2_750, GxEPD2_750::HEIGHT / 2 > display(GxEPD2_750(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  80. // 3-color e-papers
  81. //GxEPD2_3C<GxEPD2_154c, GxEPD2_154c::HEIGHT> display(GxEPD2_154c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  82. //GxEPD2_3C<GxEPD2_213c, GxEPD2_213c::HEIGHT> display(GxEPD2_213c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  83. //GxEPD2_3C<GxEPD2_290c, GxEPD2_290c::HEIGHT> display(GxEPD2_290c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  84. //GxEPD2_3C<GxEPD2_270c, GxEPD2_270c::HEIGHT> display(GxEPD2_270c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  85. // can use only half buffer size
  86. //GxEPD2_3C<GxEPD2_420c, GxEPD2_420c::HEIGHT / 2> display(GxEPD2_420c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  87. // can use only quarter buffer size
  88. //GxEPD2_3C<GxEPD2_583c, GxEPD2_583c::HEIGHT / 4> display(GxEPD2_583c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  89. //GxEPD2_3C<GxEPD2_750c, GxEPD2_750c::HEIGHT / 4> display(GxEPD2_750c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
  90. #endif
  91. #if defined(ESP32)
  92. // select one and adapt to your mapping, can use full buffer size (full HEIGHT)
  93. //GxEPD2_BW<GxEPD2_154, GxEPD2_154::HEIGHT> display(GxEPD2_154(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  94. //GxEPD2_BW<GxEPD2_213, GxEPD2_213::HEIGHT> display(GxEPD2_213(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  95. //GxEPD2_BW<GxEPD2_290, GxEPD2_290::HEIGHT> display(GxEPD2_290(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  96. //GxEPD2_BW<GxEPD2_270, GxEPD2_270::HEIGHT> display(GxEPD2_270(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  97. //GxEPD2_BW<GxEPD2_420, GxEPD2_420::HEIGHT> display(GxEPD2_420(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  98. //GxEPD2_BW<GxEPD2_750, GxEPD2_750::HEIGHT> display(GxEPD2_750(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  99. // 3-color e-papers
  100. //GxEPD2_3C<GxEPD2_154c, GxEPD2_154c::HEIGHT> display(GxEPD2_154c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  101. //GxEPD2_3C<GxEPD2_213c, GxEPD2_213c::HEIGHT> display(GxEPD2_213c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  102. //GxEPD2_3C<GxEPD2_290c, GxEPD2_290c::HEIGHT> display(GxEPD2_290c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  103. //GxEPD2_3C<GxEPD2_270c, GxEPD2_270c::HEIGHT> display(GxEPD2_270c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  104. //GxEPD2_3C<GxEPD2_420c, GxEPD2_420c::HEIGHT> display(GxEPD2_420c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  105. //GxEPD2_3C<GxEPD2_750c, GxEPD2_750c::HEIGHT> display(GxEPD2_750c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  106. #endif
  107. #if defined(_BOARD_GENERIC_STM32F103C_H_)
  108. #define MAX_DISPAY_BUFFER_SIZE (400 * 300) // ~5k is a good compromise
  109. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= MAX_DISPAY_BUFFER_SIZE / 8 / EPD::WIDTH ? EPD::HEIGHT : MAX_DISPAY_BUFFER_SIZE / 8 / EPD::WIDTH)
  110. // select one and adapt to your mapping
  111. //GxEPD2_BW<GxEPD2_154, MAX_HEIGHT(GxEPD2_154)> display(GxEPD2_154(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  112. //GxEPD2_BW<GxEPD2_213, MAX_HEIGHT(GxEPD2_213)> display(GxEPD2_213(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  113. //GxEPD2_BW<GxEPD2_290, MAX_HEIGHT(GxEPD2_290)> display(GxEPD2_290(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  114. //GxEPD2_BW<GxEPD2_270, MAX_HEIGHT(GxEPD2_270)> display(GxEPD2_270(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  115. //GxEPD2_BW<GxEPD2_420, MAX_HEIGHT(GxEPD2_420)> display(GxEPD2_420(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  116. //GxEPD2_BW<GxEPD2_750, MAX_HEIGHT(GxEPD2_750)> display(GxEPD2_750(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  117. // 3-color e-papers
  118. //GxEPD2_3C<GxEPD2_154c, MAX_HEIGHT(GxEPD2_154c)> display(GxEPD2_154c(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  119. //GxEPD2_3C<GxEPD2_213c, MAX_HEIGHT(GxEPD2_213c)> display(GxEPD2_213c(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  120. //GxEPD2_3C<GxEPD2_290c, MAX_HEIGHT(GxEPD2_290c)> display(GxEPD2_290c(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  121. //GxEPD2_3C<GxEPD2_270c, MAX_HEIGHT(GxEPD2_270c)> display(GxEPD2_270c(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  122. //GxEPD2_3C<GxEPD2_420c, MAX_HEIGHT(GxEPD2_420c)> display(GxEPD2_420c(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  123. //GxEPD2_3C<GxEPD2_750c, MAX_HEIGHT(GxEPD2_750c)> display(GxEPD2_750c(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
  124. #endif
  125. #if defined(__AVR)
  126. #define MAX_DISPAY_BUFFER_SIZE 1000 // half of available RAM as a compromise
  127. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= MAX_DISPAY_BUFFER_SIZE / 8 / EPD::WIDTH ? EPD::HEIGHT : MAX_DISPAY_BUFFER_SIZE / 8 / EPD::WIDTH)
  128. // select one and adapt to your mapping
  129. //GxEPD2_BW<GxEPD2_154, MAX_HEIGHT(GxEPD2_154)> display(GxEPD2_154(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  130. //GxEPD2_BW<GxEPD2_213, MAX_HEIGHT(GxEPD2_213)> display(GxEPD2_213(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  131. //GxEPD2_BW<GxEPD2_290, MAX_HEIGHT(GxEPD2_290)> display(GxEPD2_290(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  132. //GxEPD2_BW<GxEPD2_270, MAX_HEIGHT(GxEPD2_270)> display(GxEPD2_270(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  133. //GxEPD2_BW<GxEPD2_420, MAX_HEIGHT(GxEPD2_420)> display(GxEPD2_420(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  134. //GxEPD2_BW<GxEPD2_750, MAX_HEIGHT(GxEPD2_750)> display(GxEPD2_750(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  135. // 3-color e-papers
  136. //GxEPD2_3C<GxEPD2_154c, MAX_HEIGHT(GxEPD2_154c)> display(GxEPD2_154c(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  137. //GxEPD2_3C<GxEPD2_213c, MAX_HEIGHT(GxEPD2_213c)> display(GxEPD2_213c(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  138. //GxEPD2_3C<GxEPD2_290c, MAX_HEIGHT(GxEPD2_290c)> display(GxEPD2_290c(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  139. //GxEPD2_3C<GxEPD2_270c, MAX_HEIGHT(GxEPD2_270c)> display(GxEPD2_270c(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  140. //GxEPD2_3C<GxEPD2_420c, MAX_HEIGHT(GxEPD2_420c)> display(GxEPD2_420c(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  141. //GxEPD2_3C<GxEPD2_750c, MAX_HEIGHT(GxEPD2_750c)> display(GxEPD2_750c(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
  142. #endif
  143. void setup()
  144. {
  145. Serial.begin(115200);
  146. Serial.println();
  147. Serial.println("setup");
  148. display.init();
  149. Serial.print("Initializing SD card...");
  150. if (!SD.begin(SD_CS))
  151. {
  152. Serial.println("SD failed!");
  153. return;
  154. }
  155. Serial.println("SD OK!");
  156. drawBitmapFromSD("parrot.bmp", 0, 0);
  157. delay(2000);
  158. drawBitmapFromSD("betty_1.bmp", 0, 0);
  159. delay(2000);
  160. #if defined(SdFat_h)
  161. drawBitmapFromSD("logo200x200.bmp", 0, 0);
  162. delay(2000);
  163. drawBitmapFromSD("first200x200.bmp", 0, 0);
  164. delay(2000);
  165. drawBitmapFromSD("second200x200.bmp", 0, 0);
  166. delay(2000);
  167. drawBitmapFromSD("third200x200.bmp", 0, 0);
  168. delay(2000);
  169. drawBitmapFromSD("fourth200x200.bmp", 0, 0);
  170. delay(2000);
  171. drawBitmapFromSD("fifth200x200.bmp", 0, 0);
  172. delay(2000);
  173. drawBitmapFromSD("sixth200x200.bmp", 0, 0);
  174. delay(2000);
  175. drawBitmapFromSD("senventh200x200.bmp", 0, 0);
  176. delay(2000);
  177. drawBitmapFromSD("eighth200x200.bmp", 0, 0);
  178. delay(2000);
  179. #else
  180. drawBitmapFromSD("logo.bmp", 0, 0);
  181. delay(2000);
  182. #endif
  183. }
  184. void loop(void)
  185. {
  186. }
  187. #define SD_BUFFER_PIXELS 20
  188. void drawBitmapFromSD(const char *filename, uint8_t x, uint8_t y)
  189. {
  190. FileClass file;
  191. uint8_t buffer[3 * SD_BUFFER_PIXELS]; // pixel buffer, size for r,g,b
  192. bool valid = false; // valid format to be handled
  193. bool flip = true; // bitmap is stored bottom-to-top
  194. uint32_t pos = 0;
  195. uint32_t startTime = millis();
  196. if ((x >= display.width()) || (y >= display.height())) return;
  197. Serial.println();
  198. Serial.print("Loading image '");
  199. Serial.print(filename);
  200. Serial.println('\'');
  201. #if defined(SdFat_h)
  202. if (!file.open(filename, FILE_READ))
  203. {
  204. Serial.print("File not found");
  205. return;
  206. }
  207. #else
  208. file = SD.open(filename);
  209. if (!file)
  210. {
  211. Serial.print("File not found");
  212. return;
  213. }
  214. #endif
  215. // Parse BMP header
  216. if (read16(file) == 0x4D42) // BMP signature
  217. {
  218. uint32_t fileSize = read32(file);
  219. uint32_t creatorBytes = read32(file);
  220. uint32_t imageOffset = read32(file); // Start of image data
  221. uint32_t headerSize = read32(file);
  222. uint32_t width = read32(file);
  223. uint32_t height = read32(file);
  224. uint16_t planes = read16(file);
  225. uint16_t depth = read16(file); // bits per pixel
  226. uint32_t format = read32(file);
  227. if ((planes == 1) && (format == 0)) // uncompressed is handled
  228. {
  229. Serial.print("File size: "); Serial.println(fileSize);
  230. Serial.print("Image Offset: "); Serial.println(imageOffset);
  231. Serial.print("Header size: "); Serial.println(headerSize);
  232. Serial.print("Bit Depth: "); Serial.println(depth);
  233. Serial.print("Image size: ");
  234. Serial.print(width);
  235. Serial.print('x');
  236. Serial.println(height);
  237. // Serial.print("first position "); Serial.println(file.position());
  238. // for (uint32_t i = file.position(); i < imageOffset; i++)
  239. // {
  240. // uint8_t data = file.read();// remaining header bytes
  241. // Serial.print("0x"); Serial.print(data, HEX); Serial.print(", ");
  242. // }
  243. // Serial.println();
  244. // Serial.print("start position "); Serial.println(file.position());
  245. // BMP rows are padded (if needed) to 4-byte boundary
  246. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  247. if (height < 0)
  248. {
  249. height = -height;
  250. flip = false;
  251. }
  252. uint16_t w = width;
  253. uint16_t h = height;
  254. if ((x + w - 1) >= display.width()) w = display.width() - x;
  255. if ((y + h - 1) >= display.height()) h = display.height() - y;
  256. if ((depth == 1) && (rowSize < sizeof(buffer))) // handle with direct drawing
  257. {
  258. valid = true;
  259. display.writeScreenBuffer();
  260. if (flip) file.seek(imageOffset + (height - h) * rowSize);
  261. else file.seek(imageOffset);
  262. for (uint16_t row = 0; row < h; row++) // for each line
  263. {
  264. file.read(buffer, rowSize);
  265. uint16_t y = flip ? h - row - 1 : row;
  266. display.writeImage(buffer, 0, y, w, 1);
  267. }
  268. display.refresh();
  269. }
  270. else
  271. {
  272. display.firstPage();
  273. do
  274. {
  275. size_t buffidx = sizeof(buffer); // force buffer load
  276. for (uint16_t row = 0; row < h; row++) // for each line
  277. {
  278. if (flip) // Bitmap is stored bottom-to-top order (normal BMP)
  279. pos = imageOffset + (height - 1 - row) * rowSize;
  280. else // Bitmap is stored top-to-bottom
  281. pos = imageOffset + row * rowSize;
  282. if (file.position() != pos)
  283. { // Need seek?
  284. file.seek(pos);
  285. buffidx = sizeof(buffer); // force buffer reload
  286. }
  287. uint8_t bits;
  288. for (uint16_t col = 0; col < w; col++) // for each pixel
  289. {
  290. // Time to read more pixel data?
  291. if (buffidx >= sizeof(buffer))
  292. {
  293. file.read(buffer, sizeof(buffer));
  294. buffidx = 0; // Set index to beginning
  295. }
  296. switch (depth)
  297. {
  298. case 1: // one bit per pixel b/w format
  299. {
  300. valid = true;
  301. if (0 == col % 8)
  302. {
  303. bits = buffer[buffidx++];
  304. }
  305. uint16_t bw_color = bits & 0x80 ? GxEPD_WHITE : GxEPD_BLACK;
  306. display.drawPixel(col, row, bw_color);
  307. bits <<= 1;
  308. }
  309. break;
  310. case 24: // standard BMP format
  311. {
  312. valid = true;
  313. uint16_t b = buffer[buffidx++];
  314. uint16_t g = buffer[buffidx++];
  315. uint16_t r = buffer[buffidx++];
  316. uint16_t bw_color = ((r + g + b) / 3 > 0xFF / 2) ? GxEPD_WHITE : GxEPD_BLACK;
  317. display.drawPixel(col, row, bw_color);
  318. }
  319. break;
  320. }
  321. } // end pixel
  322. } // end line
  323. } // end do
  324. while (display.nextPage());
  325. }
  326. }
  327. }
  328. //Serial.print("end position "); Serial.println(file.position());
  329. file.close();
  330. if (valid)
  331. {
  332. Serial.print("loaded in ");
  333. Serial.print(millis() - startTime);
  334. Serial.println(" ms");
  335. }
  336. else
  337. {
  338. Serial.println("bitmap format not handled.");
  339. }
  340. }
  341. uint16_t read16(FileClass& f)
  342. {
  343. // BMP data is stored little-endian, same as Arduino.
  344. uint16_t result;
  345. ((uint8_t *)&result)[0] = f.read(); // LSB
  346. ((uint8_t *)&result)[1] = f.read(); // MSB
  347. return result;
  348. }
  349. uint32_t read32(FileClass& f)
  350. {
  351. // BMP data is stored little-endian, same as Arduino.
  352. uint32_t result;
  353. ((uint8_t *)&result)[0] = f.read(); // LSB
  354. ((uint8_t *)&result)[1] = f.read();
  355. ((uint8_t *)&result)[2] = f.read();
  356. ((uint8_t *)&result)[3] = f.read(); // MSB
  357. return result;
  358. }