GxEPD2_RefreshTests.ino 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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: these e-papers 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. // see GxEPD2_wiring_examples.h for wiring suggestions and examples
  12. // uncomment next line to use class GFX of library GFX_Root instead of Adafruit_GFX
  13. #include <GFX.h>
  14. // Note: if you use this with ENABLE_GxEPD2_GFX 1:
  15. // uncomment it in GxEPD2_GFX.h too, or add #include <GFX.h> before any #include <GxEPD2_GFX.h>
  16. #include <GxEPD2_BW.h>
  17. #include <GxEPD2_3C.h>
  18. // select the display constructor line in one of the following files (old style):
  19. #include "GxEPD2_display_selection.h"
  20. #include "GxEPD2_display_selection_added.h"
  21. //#include "GxEPD2_display_selection_more.h" // private
  22. // or select the display class and display driver class in the following file (new style):
  23. #include "GxEPD2_display_selection_new_style.h"
  24. #include "Open_Sans_ExtraBold_120.h"
  25. //const uint32_t partial_update_period_s = 2;
  26. const uint32_t partial_update_period_s = 60;
  27. const uint32_t full_update_period_s = 1 * 60 * 60;
  28. const uint32_t speed_up_factor = 20; // for simulation
  29. const uint32_t millis_per_second = 1000 / speed_up_factor;
  30. uint32_t start_time;
  31. uint32_t next_time;
  32. uint32_t previous_time;
  33. uint32_t previous_full_update;
  34. uint32_t total_seconds = 0;
  35. uint32_t seconds, minutes, hours, days;
  36. void setup()
  37. {
  38. Serial.begin(115200);
  39. Serial.println();
  40. Serial.println("setup");
  41. delay(100);
  42. display.init(115200);
  43. // cleanup
  44. for (int i = 0; i < 1; i++)
  45. {
  46. full_black();
  47. delay(500);
  48. full_white();
  49. delay(500);
  50. }
  51. // stripes test
  52. for (int i = 0; i < 1; i++)
  53. {
  54. horizontal_stripes(8);
  55. delay(1000);
  56. vertical_stripes(8);
  57. delay(3000);
  58. }
  59. full_white();
  60. delay(1000);
  61. // clock simulation test, for limited length
  62. start_time = next_time = previous_time = previous_full_update = millis();
  63. for (int i = 0; i < 50; i++)
  64. {
  65. clock_test();
  66. }
  67. delay(3000);
  68. full_white();
  69. delay(1000);
  70. display.powerOff();
  71. delay(1000);
  72. start_time = next_time = previous_time = previous_full_update = millis();
  73. Serial.println("setup done");
  74. }
  75. void loop()
  76. {
  77. // uncomment for continuous clock simulation test
  78. //clock_test();
  79. }
  80. void full_white()
  81. {
  82. display.setFullWindow();
  83. display.firstPage();
  84. do
  85. {
  86. display.fillScreen(GxEPD_WHITE);
  87. }
  88. while (display.nextPage());
  89. }
  90. void full_black()
  91. {
  92. display.setFullWindow();
  93. display.firstPage();
  94. do
  95. {
  96. display.fillScreen(GxEPD_BLACK);
  97. }
  98. while (display.nextPage());
  99. }
  100. void partial_white()
  101. {
  102. display.setPartialWindow(0, 0, display.width(), display.height());
  103. display.firstPage();
  104. do
  105. {
  106. display.fillScreen(GxEPD_WHITE);
  107. }
  108. while (display.nextPage());
  109. }
  110. void partial_black()
  111. {
  112. display.setPartialWindow(0, 0, display.width(), display.height());
  113. display.firstPage();
  114. do
  115. {
  116. display.fillScreen(GxEPD_BLACK);
  117. }
  118. while (display.nextPage());
  119. }
  120. void horizontal_stripes(uint16_t nr)
  121. {
  122. uint16_t h = display.height() / nr;
  123. display.setPartialWindow(0, 0, display.width(), display.height());
  124. display.firstPage();
  125. do
  126. {
  127. uint16_t y = 0;
  128. do
  129. {
  130. display.fillRect(0, y, display.width(), h, GxEPD_BLACK);
  131. y += h;
  132. if (y >= display.height()) break;
  133. display.fillRect(0, y, display.width(), h, GxEPD_WHITE);
  134. y += h;
  135. } while (y < display.height());
  136. }
  137. while (display.nextPage());
  138. }
  139. void vertical_stripes(uint16_t nr)
  140. {
  141. uint16_t w = display.width() / nr;
  142. display.setPartialWindow(0, 0, display.width(), display.height());
  143. display.firstPage();
  144. do
  145. {
  146. uint16_t x = 0;
  147. do
  148. {
  149. display.fillRect(x, 0, w, display.height(), GxEPD_BLACK);
  150. x += w;
  151. if (x >= display.width()) break;
  152. display.fillRect(x, 0, w, display.height(), GxEPD_WHITE);
  153. x += w;
  154. } while (x < display.width());
  155. }
  156. while (display.nextPage());
  157. }
  158. void clock_test()
  159. {
  160. uint32_t actual = millis();
  161. while (actual < next_time)
  162. {
  163. // the "BlinkWithoutDelay" method works also for overflowed millis
  164. if ((actual - previous_time) > (partial_update_period_s * 1000))
  165. {
  166. //Serial.print(actual - previous_time); Serial.print(" > "); Serial.println(partial_update_period_s * 1000);
  167. break;
  168. }
  169. delay(100);
  170. actual = millis();
  171. }
  172. if ((actual - previous_full_update) > full_update_period_s * millis_per_second)
  173. {
  174. //display.clearScreen();
  175. previous_full_update = actual;
  176. }
  177. previous_time = actual;
  178. next_time += uint32_t(partial_update_period_s * millis_per_second);
  179. total_seconds += partial_update_period_s;
  180. seconds = total_seconds % 60;
  181. minutes = (total_seconds / 60) % 60;
  182. hours = (total_seconds / 3600) % 24;
  183. days = (total_seconds / 3600) / 24;
  184. if ((display.width() >= 400) && (display.height() >= 300))
  185. {
  186. showDays();
  187. showTime();
  188. }
  189. else showMinutes();
  190. display.powerOff();
  191. }
  192. String str02(uint32_t v)
  193. {
  194. return (v < 10 ? String(0) + String(v) : String(v));
  195. }
  196. void showMinutes()
  197. {
  198. static int16_t bbx = 400;
  199. static int16_t bby = 300;
  200. static uint16_t bbw = 0;
  201. static uint16_t bbh = 0;
  202. //Serial.println("showMinutes");
  203. String str = str02(minutes);
  204. display.setRotation(display.epd2.WIDTH > display.epd2.HEIGHT ? 0 : 1);
  205. display.setFont(&Open_Sans_ExtraBold_120);
  206. display.setTextColor(GxEPD_BLACK);
  207. int16_t tbx, tby; uint16_t tbw, tbh;
  208. display.getTextBounds(str, 0, 0, &tbx, &tby, &tbw, &tbh);
  209. // place the bounding box, cover any previous ones
  210. int16_t tx = max(0, ((display.width() - tbw) / 2));
  211. int16_t ty = max(0, ((display.height() - tbh) / 2));
  212. bbx = min(bbx, tx);
  213. bby = min(bby, ty);
  214. bbw = max(bbw, tbw);
  215. bbh = max(bbh, tbh);
  216. // calculate the cursor
  217. int16_t x = bbx - tbx;
  218. int16_t y = bby - tby;
  219. display.setPartialWindow(bbx, bby, bbw, bbh);
  220. display.firstPage();
  221. do
  222. {
  223. display.fillScreen(GxEPD_WHITE);
  224. //display.drawRect(bbx, bby, bbw, bbh, GxEPD_BLACK);
  225. display.setCursor(x, y);
  226. display.print(str);
  227. }
  228. while (display.nextPage());
  229. delay(1000);
  230. //Serial.println("showMinutes done");
  231. }
  232. void showDays()
  233. {
  234. static int16_t bbx = 400;
  235. static int16_t bby = 300;
  236. static uint16_t bbw = 0;
  237. static uint16_t bbh = 0;
  238. //Serial.println("showDays");
  239. String str(days); str += " days";
  240. display.setRotation(0);
  241. display.setFont(&Open_Sans_ExtraBold_120);
  242. display.setTextColor(GxEPD_BLACK);
  243. int16_t tbx, tby; uint16_t tbw, tbh;
  244. display.getTextBounds(str, 0, 0, &tbx, &tby, &tbw, &tbh);
  245. // place the bounding box
  246. int16_t tx = max(0, ((display.width() - tbw) / 2));
  247. int16_t ty = max(0, (display.height() / 4 - tbh / 2));
  248. bbx = min(bbx, tx);
  249. bby = min(bby, ty);
  250. bbw = max(bbw, tbw);
  251. bbh = max(bbh, tbh);
  252. // calculate the cursor
  253. uint16_t x = bbx - tbx;
  254. uint16_t y = bby - tby;
  255. display.setPartialWindow(bbx, bby, bbw, bbh);
  256. display.firstPage();
  257. do
  258. {
  259. display.fillScreen(GxEPD_WHITE);
  260. //display.drawRect(bbx, bby, bbw, bbh, GxEPD_BLACK);
  261. display.setCursor(x, y);
  262. display.print(str);
  263. }
  264. while (display.nextPage());
  265. delay(1000);
  266. //Serial.println("showDays done");
  267. }
  268. void showTime()
  269. {
  270. static int16_t bbx = 400;
  271. static int16_t bby = 300;
  272. static uint16_t bbw = 0;
  273. static uint16_t bbh = 0;
  274. //Serial.println("showTime");
  275. //String str = str02(minutes) + ":" + str02(seconds);
  276. String str = str02(hours) + ":" + str02(minutes);
  277. display.setRotation(0);
  278. display.setFont(&Open_Sans_ExtraBold_120);
  279. display.setTextColor(GxEPD_BLACK);
  280. int16_t tbx, tby; uint16_t tbw, tbh;
  281. display.getTextBounds(str, 0, 0, &tbx, &tby, &tbw, &tbh);
  282. // place the bounding box
  283. int16_t tx = max(0, ((display.width() - tbw) / 2));
  284. int16_t ty = max(0, (display.height() * 3 / 4 - tbh / 2));
  285. bbx = min(bbx, tx);
  286. bby = min(bby, ty);
  287. bbw = max(bbw, tbw);
  288. bbh = max(bbh, tbh);
  289. // calculate the cursor
  290. uint16_t x = bbx - tbx;
  291. uint16_t y = bby - tby;
  292. display.setPartialWindow(bbx, bby, bbw, bbh);
  293. display.firstPage();
  294. do
  295. {
  296. display.fillScreen(GxEPD_WHITE);
  297. //display.drawRect(bbx, bby, bbw, bbh, GxEPD_BLACK);
  298. display.setCursor(x, y);
  299. display.print(str);
  300. }
  301. while (display.nextPage());
  302. //Serial.println("showTime done");
  303. }