GxEPD2_GFX_Example.ino 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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); // default 10ms reset pulse, e.g. for bare panels with DESPI-C02
  45. display.init(115200, true, 2, false); // USE THIS for Waveshare boards with "clever" reset circuit, 2ms reset pulse
  46. // first update should be full refresh
  47. helloWorld(display);
  48. delay(1000);
  49. // partial refresh mode can be used to full screen,
  50. // effective if display panel hasFastPartialUpdate
  51. helloFullScreenPartialMode(display);
  52. delay(1000);
  53. helloArduino(display);
  54. delay(1000);
  55. helloEpaper(display);
  56. delay(1000);
  57. //helloValue(display, 123.9, 1);
  58. //delay(1000);
  59. showFont(display, "FreeMonoBold9pt7b", &FreeMonoBold9pt7b);
  60. delay(1000);
  61. //BitmapDisplay(display).drawBitmaps();
  62. bitmaps.drawBitmaps();
  63. //return;
  64. if (display.epd2.hasPartialUpdate)
  65. {
  66. showPartialUpdate();
  67. delay(1000);
  68. } // else // on GDEW0154Z04 only full update available, doesn't look nice
  69. //drawCornerTest();
  70. //showBox(16, 16, 48, 32, false);
  71. //showBox(16, 56, 48, 32, true);
  72. display.powerOff();
  73. deepSleepTest();
  74. Serial.println("setup done");
  75. }
  76. void loop()
  77. {
  78. }
  79. void deepSleepTest()
  80. {
  81. //Serial.println("deepSleepTest");
  82. const char hibernating[] = "hibernating ...";
  83. const char wokeup[] = "woke up";
  84. const char from[] = "from deep sleep";
  85. const char again[] = "again";
  86. display.setRotation(1);
  87. display.setFont(&FreeMonoBold9pt7b);
  88. display.setTextColor(GxEPD_BLACK);
  89. int16_t tbx, tby; uint16_t tbw, tbh;
  90. // center text
  91. display.getTextBounds(hibernating, 0, 0, &tbx, &tby, &tbw, &tbh);
  92. uint16_t x = ((display.width() - tbw) / 2) - tbx;
  93. uint16_t y = ((display.height() - tbh) / 2) - tby;
  94. display.setFullWindow();
  95. display.firstPage();
  96. do
  97. {
  98. display.fillScreen(GxEPD_WHITE);
  99. display.setCursor(x, y);
  100. display.print(hibernating);
  101. }
  102. while (display.nextPage());
  103. display.hibernate();
  104. delay(5000);
  105. display.getTextBounds(wokeup, 0, 0, &tbx, &tby, &tbw, &tbh);
  106. uint16_t wx = (display.width() - tbw) / 2;
  107. uint16_t wy = (display.height() / 3) + tbh / 2; // y is base line!
  108. display.getTextBounds(from, 0, 0, &tbx, &tby, &tbw, &tbh);
  109. uint16_t fx = (display.width() - tbw) / 2;
  110. uint16_t fy = (display.height() * 2 / 3) + tbh / 2; // y is base line!
  111. display.firstPage();
  112. do
  113. {
  114. display.fillScreen(GxEPD_WHITE);
  115. display.setCursor(wx, wy);
  116. display.print(wokeup);
  117. display.setCursor(fx, fy);
  118. display.print(from);
  119. }
  120. while (display.nextPage());
  121. delay(5000);
  122. display.getTextBounds(hibernating, 0, 0, &tbx, &tby, &tbw, &tbh);
  123. uint16_t hx = (display.width() - tbw) / 2;
  124. uint16_t hy = (display.height() / 3) + tbh / 2; // y is base line!
  125. display.getTextBounds(again, 0, 0, &tbx, &tby, &tbw, &tbh);
  126. uint16_t ax = (display.width() - tbw) / 2;
  127. uint16_t ay = (display.height() * 2 / 3) + tbh / 2; // y is base line!
  128. display.firstPage();
  129. do
  130. {
  131. display.fillScreen(GxEPD_WHITE);
  132. display.setCursor(hx, hy);
  133. display.print(hibernating);
  134. display.setCursor(ax, ay);
  135. display.print(again);
  136. }
  137. while (display.nextPage());
  138. display.hibernate();
  139. //Serial.println("deepSleepTest done");
  140. }
  141. void showBox(uint16_t x, uint16_t y, uint16_t w, uint16_t h, bool partial)
  142. {
  143. //Serial.println("showBox");
  144. display.setRotation(1);
  145. if (partial)
  146. {
  147. display.setPartialWindow(x, y, w, h);
  148. }
  149. else
  150. {
  151. display.setFullWindow();
  152. }
  153. display.firstPage();
  154. do
  155. {
  156. display.fillScreen(GxEPD_WHITE);
  157. display.fillRect(x, y, w, h, GxEPD_BLACK);
  158. }
  159. while (display.nextPage());
  160. //Serial.println("showBox done");
  161. }
  162. void drawCornerTest()
  163. {
  164. display.setFullWindow();
  165. display.setFont(&FreeMonoBold9pt7b);
  166. display.setTextColor(GxEPD_BLACK);
  167. for (uint16_t r = 0; r <= 4; r++)
  168. {
  169. display.setRotation(r);
  170. display.firstPage();
  171. do
  172. {
  173. display.fillScreen(GxEPD_WHITE);
  174. display.fillRect(0, 0, 8, 8, GxEPD_BLACK);
  175. display.fillRect(display.width() - 18, 0, 16, 16, GxEPD_BLACK);
  176. display.fillRect(display.width() - 25, display.height() - 25, 24, 24, GxEPD_BLACK);
  177. display.fillRect(0, display.height() - 33, 32, 32, GxEPD_BLACK);
  178. display.setCursor(display.width() / 2, display.height() / 2);
  179. display.print(display.getRotation());
  180. }
  181. while (display.nextPage());
  182. delay(2000);
  183. }
  184. }
  185. // note for partial update window and setPartialWindow() method:
  186. // partial update window size and position is on byte boundary in physical x direction
  187. // the size is increased in setPartialWindow() if x or w are not multiple of 8 for even rotation, y or h for odd rotation
  188. // see also comment in GxEPD2_BW.h, GxEPD2_3C.h or GxEPD2_GFX.h for method setPartialWindow()
  189. // showPartialUpdate() purposely uses values that are not multiples of 8 to test this
  190. void showPartialUpdate()
  191. {
  192. // some useful background
  193. helloWorld(display);
  194. // use asymmetric values for test
  195. uint16_t box_x = 10;
  196. uint16_t box_y = 15;
  197. uint16_t box_w = 70;
  198. uint16_t box_h = 20;
  199. uint16_t cursor_y = box_y + box_h - 6;
  200. float value = 13.95;
  201. uint16_t incr = display.epd2.hasFastPartialUpdate ? 1 : 3;
  202. display.setFont(&FreeMonoBold9pt7b);
  203. display.setTextColor(GxEPD_BLACK);
  204. // show where the update box is
  205. for (uint16_t r = 0; r < 4; r++)
  206. {
  207. display.setRotation(r);
  208. display.setPartialWindow(box_x, box_y, box_w, box_h);
  209. display.firstPage();
  210. do
  211. {
  212. display.fillRect(box_x, box_y, box_w, box_h, GxEPD_BLACK);
  213. //display.fillScreen(GxEPD_BLACK);
  214. }
  215. while (display.nextPage());
  216. delay(2000);
  217. display.firstPage();
  218. do
  219. {
  220. display.fillRect(box_x, box_y, box_w, box_h, GxEPD_WHITE);
  221. }
  222. while (display.nextPage());
  223. delay(1000);
  224. }
  225. //return;
  226. // show updates in the update box
  227. for (uint16_t r = 0; r < 4; r++)
  228. {
  229. display.setRotation(r);
  230. display.setPartialWindow(box_x, box_y, box_w, box_h);
  231. for (uint16_t i = 1; i <= 10; i += incr)
  232. {
  233. display.firstPage();
  234. do
  235. {
  236. display.fillRect(box_x, box_y, box_w, box_h, GxEPD_WHITE);
  237. display.setCursor(box_x, cursor_y);
  238. display.print(value * i, 2);
  239. }
  240. while (display.nextPage());
  241. delay(500);
  242. }
  243. delay(1000);
  244. display.firstPage();
  245. do
  246. {
  247. display.fillRect(box_x, box_y, box_w, box_h, GxEPD_WHITE);
  248. }
  249. while (display.nextPage());
  250. delay(1000);
  251. }
  252. }