GxEPD2_GFX_Example.ino 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // 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. // Author: Jean-Marc Zingg
  7. //
  8. // Version: see library.properties
  9. //
  10. // Library: https://github.com/ZinggJM/GxEPD2
  11. //
  12. // Purpose: show uses of GxEPD2_GFX base class for references to a display instance
  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. // base class GxEPD2_GFX can be used to pass references or pointers to the display instance as parameter, uses ~1.2k more code
  18. // enable GxEPD2_GFX base class
  19. #define ENABLE_GxEPD2_GFX 1
  20. // uncomment next line to use class GFX of library GFX_Root instead of Adafruit_GFX
  21. //#include <GFX.h>
  22. // Note: if you use this with ENABLE_GxEPD2_GFX 1:
  23. // uncomment it in GxEPD2_GFX.h too, or add #include <GFX.h> before any #include <GxEPD2_GFX.h>
  24. // !!!! ============================================================================================ !!!!
  25. #include <GxEPD2_BW.h>
  26. #include <GxEPD2_3C.h>
  27. #include <GxEPD2_7C.h>
  28. #include <Fonts/FreeMonoBold9pt7b.h>
  29. #include "BitmapDisplay.h"
  30. #include "TextDisplay.h"
  31. // select the display constructor line in one of the following files (old style):
  32. #include "GxEPD2_display_selection.h"
  33. #include "GxEPD2_display_selection_added.h"
  34. //#include "GxEPD2_display_selection_more.h" // private
  35. // or select the display class and display driver class in the following file (new style):
  36. #include "GxEPD2_display_selection_new_style.h"
  37. BitmapDisplay bitmaps(display);
  38. void setup()
  39. {
  40. Serial.begin(115200);
  41. Serial.println();
  42. Serial.println("setup");
  43. delay(100);
  44. display.init(115200);
  45. // first update should be full refresh
  46. helloWorld(display);
  47. delay(1000);
  48. // partial refresh mode can be used to full screen,
  49. // effective if display panel hasFastPartialUpdate
  50. helloFullScreenPartialMode(display);
  51. delay(1000);
  52. helloArduino(display);
  53. delay(1000);
  54. helloEpaper(display);
  55. delay(1000);
  56. //helloValue(display, 123.9, 1);
  57. //delay(1000);
  58. showFont(display, "FreeMonoBold9pt7b", &FreeMonoBold9pt7b);
  59. delay(1000);
  60. //BitmapDisplay(display).drawBitmaps();
  61. bitmaps.drawBitmaps();
  62. //return;
  63. if (display.epd2.hasPartialUpdate)
  64. {
  65. showPartialUpdate();
  66. delay(1000);
  67. } // else // on GDEW0154Z04 only full update available, doesn't look nice
  68. //drawCornerTest();
  69. //showBox(16, 16, 48, 32, false);
  70. //showBox(16, 56, 48, 32, true);
  71. display.powerOff();
  72. deepSleepTest();
  73. Serial.println("setup done");
  74. }
  75. void loop()
  76. {
  77. }
  78. void deepSleepTest()
  79. {
  80. //Serial.println("deepSleepTest");
  81. const char hibernating[] = "hibernating ...";
  82. const char wokeup[] = "woke up";
  83. const char from[] = "from deep sleep";
  84. const char again[] = "again";
  85. display.setRotation(1);
  86. display.setFont(&FreeMonoBold9pt7b);
  87. display.setTextColor(GxEPD_BLACK);
  88. int16_t tbx, tby; uint16_t tbw, tbh;
  89. // center text
  90. display.getTextBounds(hibernating, 0, 0, &tbx, &tby, &tbw, &tbh);
  91. uint16_t x = ((display.width() - tbw) / 2) - tbx;
  92. uint16_t y = ((display.height() - tbh) / 2) - tby;
  93. display.setFullWindow();
  94. display.firstPage();
  95. do
  96. {
  97. display.fillScreen(GxEPD_WHITE);
  98. display.setCursor(x, y);
  99. display.print(hibernating);
  100. }
  101. while (display.nextPage());
  102. display.hibernate();
  103. delay(5000);
  104. display.getTextBounds(wokeup, 0, 0, &tbx, &tby, &tbw, &tbh);
  105. uint16_t wx = (display.width() - tbw) / 2;
  106. uint16_t wy = (display.height() / 3) + tbh / 2; // y is base line!
  107. display.getTextBounds(from, 0, 0, &tbx, &tby, &tbw, &tbh);
  108. uint16_t fx = (display.width() - tbw) / 2;
  109. uint16_t fy = (display.height() * 2 / 3) + tbh / 2; // y is base line!
  110. display.firstPage();
  111. do
  112. {
  113. display.fillScreen(GxEPD_WHITE);
  114. display.setCursor(wx, wy);
  115. display.print(wokeup);
  116. display.setCursor(fx, fy);
  117. display.print(from);
  118. }
  119. while (display.nextPage());
  120. delay(5000);
  121. display.getTextBounds(hibernating, 0, 0, &tbx, &tby, &tbw, &tbh);
  122. uint16_t hx = (display.width() - tbw) / 2;
  123. uint16_t hy = (display.height() / 3) + tbh / 2; // y is base line!
  124. display.getTextBounds(again, 0, 0, &tbx, &tby, &tbw, &tbh);
  125. uint16_t ax = (display.width() - tbw) / 2;
  126. uint16_t ay = (display.height() * 2 / 3) + tbh / 2; // y is base line!
  127. display.firstPage();
  128. do
  129. {
  130. display.fillScreen(GxEPD_WHITE);
  131. display.setCursor(hx, hy);
  132. display.print(hibernating);
  133. display.setCursor(ax, ay);
  134. display.print(again);
  135. }
  136. while (display.nextPage());
  137. display.hibernate();
  138. //Serial.println("deepSleepTest done");
  139. }
  140. void showBox(uint16_t x, uint16_t y, uint16_t w, uint16_t h, bool partial)
  141. {
  142. //Serial.println("showBox");
  143. display.setRotation(1);
  144. if (partial)
  145. {
  146. display.setPartialWindow(x, y, w, h);
  147. }
  148. else
  149. {
  150. display.setFullWindow();
  151. }
  152. display.firstPage();
  153. do
  154. {
  155. display.fillScreen(GxEPD_WHITE);
  156. display.fillRect(x, y, w, h, GxEPD_BLACK);
  157. }
  158. while (display.nextPage());
  159. //Serial.println("showBox done");
  160. }
  161. void drawCornerTest()
  162. {
  163. display.setFullWindow();
  164. display.setFont(&FreeMonoBold9pt7b);
  165. display.setTextColor(GxEPD_BLACK);
  166. for (uint16_t r = 0; r <= 4; r++)
  167. {
  168. display.setRotation(r);
  169. display.firstPage();
  170. do
  171. {
  172. display.fillScreen(GxEPD_WHITE);
  173. display.fillRect(0, 0, 8, 8, GxEPD_BLACK);
  174. display.fillRect(display.width() - 18, 0, 16, 16, GxEPD_BLACK);
  175. display.fillRect(display.width() - 25, display.height() - 25, 24, 24, GxEPD_BLACK);
  176. display.fillRect(0, display.height() - 33, 32, 32, GxEPD_BLACK);
  177. display.setCursor(display.width() / 2, display.height() / 2);
  178. display.print(display.getRotation());
  179. }
  180. while (display.nextPage());
  181. delay(2000);
  182. }
  183. }
  184. // note for partial update window and setPartialWindow() method:
  185. // partial update window size and position is on byte boundary in physical x direction
  186. // the size is increased in setPartialWindow() if x or w are not multiple of 8 for even rotation, y or h for odd rotation
  187. // see also comment in GxEPD2_BW.h, GxEPD2_3C.h or GxEPD2_GFX.h for method setPartialWindow()
  188. // showPartialUpdate() purposely uses values that are not multiples of 8 to test this
  189. void showPartialUpdate()
  190. {
  191. // some useful background
  192. helloWorld(display);
  193. // use asymmetric values for test
  194. uint16_t box_x = 10;
  195. uint16_t box_y = 15;
  196. uint16_t box_w = 70;
  197. uint16_t box_h = 20;
  198. uint16_t cursor_y = box_y + box_h - 6;
  199. float value = 13.95;
  200. uint16_t incr = display.epd2.hasFastPartialUpdate ? 1 : 3;
  201. display.setFont(&FreeMonoBold9pt7b);
  202. display.setTextColor(GxEPD_BLACK);
  203. // show where the update box is
  204. for (uint16_t r = 0; r < 4; r++)
  205. {
  206. display.setRotation(r);
  207. display.setPartialWindow(box_x, box_y, box_w, box_h);
  208. display.firstPage();
  209. do
  210. {
  211. display.fillRect(box_x, box_y, box_w, box_h, GxEPD_BLACK);
  212. //display.fillScreen(GxEPD_BLACK);
  213. }
  214. while (display.nextPage());
  215. delay(2000);
  216. display.firstPage();
  217. do
  218. {
  219. display.fillRect(box_x, box_y, box_w, box_h, GxEPD_WHITE);
  220. }
  221. while (display.nextPage());
  222. delay(1000);
  223. }
  224. //return;
  225. // show updates in the update box
  226. for (uint16_t r = 0; r < 4; r++)
  227. {
  228. display.setRotation(r);
  229. display.setPartialWindow(box_x, box_y, box_w, box_h);
  230. for (uint16_t i = 1; i <= 10; i += incr)
  231. {
  232. display.firstPage();
  233. do
  234. {
  235. display.fillRect(box_x, box_y, box_w, box_h, GxEPD_WHITE);
  236. display.setCursor(box_x, cursor_y);
  237. display.print(value * i, 2);
  238. }
  239. while (display.nextPage());
  240. delay(500);
  241. }
  242. delay(1000);
  243. display.firstPage();
  244. do
  245. {
  246. display.fillRect(box_x, box_y, box_w, box_h, GxEPD_WHITE);
  247. }
  248. while (display.nextPage());
  249. delay(1000);
  250. }
  251. }