GxEPD2_3C.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. // Display Library 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. // 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. #ifndef _GxEPD2_3C_H_
  12. #define _GxEPD2_3C_H_
  13. // uncomment next line to use class GFX of library GFX_Root instead of Adafruit_GFX
  14. //#include <GFX.h>
  15. #ifndef ENABLE_GxEPD2_GFX
  16. // default is off
  17. #define ENABLE_GxEPD2_GFX 0
  18. #endif
  19. #if ENABLE_GxEPD2_GFX
  20. #include "GxEPD2_GFX.h"
  21. #define GxEPD2_GFX_BASE_CLASS GxEPD2_GFX
  22. #elif defined(_GFX_H_)
  23. #define GxEPD2_GFX_BASE_CLASS GFX
  24. #else
  25. #include <Adafruit_GFX.h>
  26. #define GxEPD2_GFX_BASE_CLASS Adafruit_GFX
  27. #endif
  28. #include "GxEPD2_EPD.h"
  29. #include "epd3c/GxEPD2_154c.h"
  30. #include "epd3c/GxEPD2_213c.h"
  31. #include "epd3c/GxEPD2_290c.h"
  32. #include "epd3c/GxEPD2_270c.h"
  33. #include "epd3c/GxEPD2_420c.h"
  34. #include "epd3c/GxEPD2_583c.h"
  35. #include "epd3c/GxEPD2_565c.h"
  36. #include "epd3c/GxEPD2_750c.h"
  37. #include "epd3c/GxEPD2_750c_Z08.h"
  38. #include "epd3c/GxEPD2_750c_Z90.h"
  39. template<typename GxEPD2_Type, const uint16_t page_height>
  40. class GxEPD2_3C : public GxEPD2_GFX_BASE_CLASS
  41. {
  42. public:
  43. GxEPD2_Type epd2;
  44. #if ENABLE_GxEPD2_GFX
  45. GxEPD2_3C(GxEPD2_Type epd2_instance) : GxEPD2_GFX_BASE_CLASS(epd2, GxEPD2_Type::WIDTH, GxEPD2_Type::HEIGHT), epd2(epd2_instance)
  46. #else
  47. GxEPD2_3C(GxEPD2_Type epd2_instance) : GxEPD2_GFX_BASE_CLASS(GxEPD2_Type::WIDTH, GxEPD2_Type::HEIGHT), epd2(epd2_instance)
  48. #endif
  49. {
  50. _page_height = page_height;
  51. _pages = (HEIGHT / _page_height) + ((HEIGHT % _page_height) > 0);
  52. _mirror = false;
  53. _using_partial_mode = false;
  54. _current_page = 0;
  55. setFullWindow();
  56. }
  57. uint16_t pages()
  58. {
  59. return _pages;
  60. }
  61. uint16_t pageHeight()
  62. {
  63. return _page_height;
  64. }
  65. bool mirror(bool m)
  66. {
  67. _swap_ (_mirror, m);
  68. return m;
  69. }
  70. void drawPixel(int16_t x, int16_t y, uint16_t color)
  71. {
  72. if ((x < 0) || (x >= width()) || (y < 0) || (y >= height())) return;
  73. if (_mirror) x = width() - x - 1;
  74. // check rotation, move pixel around if necessary
  75. switch (getRotation())
  76. {
  77. case 1:
  78. _swap_(x, y);
  79. x = WIDTH - x - 1;
  80. break;
  81. case 2:
  82. x = WIDTH - x - 1;
  83. y = HEIGHT - y - 1;
  84. break;
  85. case 3:
  86. _swap_(x, y);
  87. y = HEIGHT - y - 1;
  88. break;
  89. }
  90. // transpose partial window to 0,0
  91. x -= _pw_x;
  92. y -= _pw_y;
  93. // clip to (partial) window
  94. if ((x < 0) || (x >= _pw_w) || (y < 0) || (y >= _pw_h)) return;
  95. // adjust for current page
  96. y -= _current_page * _page_height;
  97. // check if in current page
  98. if ((y < 0) || (y >= _page_height)) return;
  99. uint16_t i = x / 8 + y * (_pw_w / 8);
  100. _black_buffer[i] = (_black_buffer[i] | (1 << (7 - x % 8))); // white
  101. _color_buffer[i] = (_color_buffer[i] | (1 << (7 - x % 8)));
  102. if (color == GxEPD_WHITE) return;
  103. else if (color == GxEPD_BLACK) _black_buffer[i] = (_black_buffer[i] & (0xFF ^ (1 << (7 - x % 8))));
  104. else if ((color == GxEPD_RED) || (color == GxEPD_YELLOW)) _color_buffer[i] = (_color_buffer[i] & (0xFF ^ (1 << (7 - x % 8))));
  105. }
  106. void init(uint32_t serial_diag_bitrate = 0) // = 0 : disabled
  107. {
  108. epd2.init(serial_diag_bitrate);
  109. _using_partial_mode = false;
  110. _current_page = 0;
  111. setFullWindow();
  112. }
  113. // init method with additional parameters:
  114. // initial false for re-init after processor deep sleep wake up, if display power supply was kept
  115. // only relevant for b/w displays with fast partial update
  116. // reset_duration = 20 is default; a value of 2 may help with "clever" reset circuit of newer boards from Waveshare
  117. // pulldown_rst_mode true for alternate RST handling to avoid feeding 5V through RST pin
  118. void init(uint32_t serial_diag_bitrate, bool initial, uint16_t reset_duration = 20, bool pulldown_rst_mode = false)
  119. {
  120. epd2.init(serial_diag_bitrate, initial, reset_duration, pulldown_rst_mode);
  121. _using_partial_mode = false;
  122. _current_page = 0;
  123. setFullWindow();
  124. }
  125. void fillScreen(uint16_t color) // 0x0 black, >0x0 white, to buffer
  126. {
  127. uint8_t black = 0xFF;
  128. uint8_t red = 0xFF;
  129. if (color == GxEPD_WHITE);
  130. else if (color == GxEPD_BLACK) black = 0x00;
  131. else if ((color == GxEPD_RED) || (color == GxEPD_YELLOW)) red = 0x00;
  132. for (uint16_t x = 0; x < sizeof(_black_buffer); x++)
  133. {
  134. _black_buffer[x] = black;
  135. _color_buffer[x] = red;
  136. }
  137. }
  138. // display buffer content to screen, useful for full screen buffer
  139. void display(bool partial_update_mode = false)
  140. {
  141. epd2.writeImage(_black_buffer, _color_buffer, 0, 0, WIDTH, _page_height);
  142. epd2.refresh(partial_update_mode);
  143. if (!partial_update_mode) epd2.powerOff();
  144. }
  145. // display part of buffer content to screen, useful for full screen buffer
  146. // displayWindow, use parameters according to actual rotation.
  147. // x and w should be multiple of 8, for rotation 0 or 2,
  148. // y and h should be multiple of 8, for rotation 1 or 3,
  149. // else window is increased as needed,
  150. // this is an addressing limitation of the e-paper controllers
  151. void displayWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  152. {
  153. x = gx_uint16_min(x, width());
  154. y = gx_uint16_min(y, height());
  155. w = gx_uint16_min(w, width() - x);
  156. h = gx_uint16_min(h, height() - y);
  157. _rotate(x, y, w, h);
  158. epd2.writeImagePart(_black_buffer, _color_buffer, x, y, WIDTH, _page_height, x, y, w, h);
  159. epd2.refresh(x, y, w, h);
  160. }
  161. void setFullWindow()
  162. {
  163. _using_partial_mode = false;
  164. _pw_x = 0;
  165. _pw_y = 0;
  166. _pw_w = WIDTH;
  167. _pw_h = HEIGHT;
  168. }
  169. // setPartialWindow, use parameters according to actual rotation.
  170. // x and w should be multiple of 8, for rotation 0 or 2,
  171. // y and h should be multiple of 8, for rotation 1 or 3,
  172. // else window is increased as needed,
  173. // this is an addressing limitation of the e-paper controllers
  174. void setPartialWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  175. {
  176. if (!epd2.hasPartialUpdate) return;
  177. _pw_x = gx_uint16_min(x, width());
  178. _pw_y = gx_uint16_min(y, height());
  179. _pw_w = gx_uint16_min(w, width() - _pw_x);
  180. _pw_h = gx_uint16_min(h, height() - _pw_y);
  181. _rotate(_pw_x, _pw_y, _pw_w, _pw_h);
  182. _using_partial_mode = true;
  183. // make _pw_x, _pw_w multiple of 8
  184. _pw_w += _pw_x % 8;
  185. if (_pw_w % 8 > 0) _pw_w += 8 - _pw_w % 8;
  186. _pw_x -= _pw_x % 8;
  187. }
  188. void firstPage()
  189. {
  190. fillScreen(GxEPD_WHITE);
  191. _current_page = 0;
  192. _second_phase = false;
  193. epd2.setPaged(); // for GxEPD2_154c paged workaround
  194. }
  195. bool nextPage()
  196. {
  197. uint16_t page_ys = _current_page * _page_height;
  198. if (_using_partial_mode)
  199. {
  200. //Serial.print(" nextPage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(_pw_y); Serial.print(", ");
  201. //Serial.print(_pw_w); Serial.print(", "); Serial.print(_pw_h); Serial.print(") P"); Serial.println(_current_page);
  202. uint16_t page_ye = _current_page < (_pages - 1) ? page_ys + _page_height : HEIGHT;
  203. uint16_t dest_ys = _pw_y + page_ys; // transposed
  204. uint16_t dest_ye = gx_uint16_min(_pw_y + _pw_h, _pw_y + page_ye);
  205. if (dest_ye > dest_ys)
  206. {
  207. //Serial.print("writeImage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(dest_ys); Serial.print(", ");
  208. //Serial.print(_pw_w); Serial.print(", "); Serial.print(dest_ye - dest_ys); Serial.println(")");
  209. epd2.writeImage(_black_buffer, _color_buffer, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  210. }
  211. else
  212. {
  213. //Serial.print("writeImage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(dest_ys); Serial.print(", ");
  214. //Serial.print(_pw_w); Serial.print(", "); Serial.print(dest_ye - dest_ys); Serial.print(") skipped ");
  215. //Serial.print(dest_ys); Serial.print(".."); Serial.println(dest_ye);
  216. }
  217. _current_page++;
  218. if (_current_page == _pages)
  219. {
  220. _current_page = 0;
  221. if (!_second_phase)
  222. {
  223. epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h);
  224. if (epd2.hasFastPartialUpdate)
  225. {
  226. _second_phase = true;
  227. return true;
  228. }
  229. }
  230. return false;
  231. }
  232. fillScreen(GxEPD_WHITE);
  233. return true;
  234. }
  235. else // full update
  236. {
  237. epd2.writeImage(_black_buffer, _color_buffer, 0, page_ys, WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  238. _current_page++;
  239. if (_current_page == _pages)
  240. {
  241. _current_page = 0;
  242. if ((epd2.panel == GxEPD2::GDEW0154Z04) && (_pages > 1))
  243. {
  244. if (!_second_phase)
  245. {
  246. epd2.refresh(false); // full update after first phase
  247. _second_phase = true;
  248. fillScreen(GxEPD_WHITE);
  249. return true;
  250. }
  251. else epd2.refresh(true); // partial update after second phase
  252. } else epd2.refresh(false); // full update after only phase
  253. epd2.powerOff();
  254. return false;
  255. }
  256. fillScreen(GxEPD_WHITE);
  257. return true;
  258. }
  259. }
  260. // GxEPD style paged drawing; drawCallback() is called as many times as needed
  261. void drawPaged(void (*drawCallback)(const void*), const void* pv)
  262. {
  263. if (_using_partial_mode)
  264. {
  265. for (_current_page = 0; _current_page < _pages; _current_page++)
  266. {
  267. uint16_t page_ys = _current_page * _page_height;
  268. uint16_t page_ye = _current_page < (_pages - 1) ? page_ys + _page_height : HEIGHT;
  269. uint16_t dest_ys = _pw_y + page_ys; // transposed
  270. uint16_t dest_ye = gx_uint16_min(_pw_y + _pw_h, _pw_y + page_ye);
  271. if (dest_ye > dest_ys)
  272. {
  273. fillScreen(GxEPD_WHITE);
  274. drawCallback(pv);
  275. epd2.writeImage(_black_buffer, _color_buffer, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  276. }
  277. }
  278. epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h);
  279. }
  280. else // full update
  281. {
  282. epd2.setPaged(); // for GxEPD2_154c paged workaround
  283. for (_current_page = 0; _current_page < _pages; _current_page++)
  284. {
  285. uint16_t page_ys = _current_page * _page_height;
  286. fillScreen(GxEPD_WHITE);
  287. drawCallback(pv);
  288. epd2.writeImage(_black_buffer, _color_buffer, 0, page_ys, WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  289. }
  290. if (epd2.panel == GxEPD2::GDEW0154Z04)
  291. { // GxEPD2_154c paged workaround: write color part
  292. for (_current_page = 0; _current_page < _pages; _current_page++)
  293. {
  294. uint16_t page_ys = _current_page * _page_height;
  295. fillScreen(GxEPD_WHITE);
  296. drawCallback(pv);
  297. epd2.writeImage(_black_buffer, _color_buffer, 0, page_ys, WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  298. }
  299. }
  300. epd2.refresh(false); // full update
  301. epd2.powerOff();
  302. }
  303. _current_page = 0;
  304. }
  305. void drawInvertedBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color)
  306. {
  307. // taken from Adafruit_GFX.cpp, modified
  308. int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
  309. uint8_t byte = 0;
  310. for (int16_t j = 0; j < h; j++)
  311. {
  312. for (int16_t i = 0; i < w; i++ )
  313. {
  314. if (i & 7) byte <<= 1;
  315. else
  316. {
  317. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  318. byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
  319. #else
  320. byte = bitmap[j * byteWidth + i / 8];
  321. #endif
  322. }
  323. if (!(byte & 0x80))
  324. {
  325. drawPixel(x + i, y + j, color);
  326. }
  327. }
  328. }
  329. }
  330. // Support for Bitmaps (Sprites) to Controller Buffer and to Screen
  331. void clearScreen(uint8_t value = 0xFF) // init controller memory and screen (default white)
  332. {
  333. epd2.clearScreen(value);
  334. }
  335. void writeScreenBuffer(uint8_t value = 0xFF) // init controller memory (default white)
  336. {
  337. epd2.writeScreenBuffer(value);
  338. }
  339. // write to controller memory, without screen refresh; x and w should be multiple of 8
  340. void writeImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false)
  341. {
  342. epd2.writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  343. }
  344. void writeImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  345. int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false)
  346. {
  347. epd2.writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  348. }
  349. void writeImage(const uint8_t* black, const uint8_t* color, int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  350. {
  351. epd2.writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  352. }
  353. void writeImage(const uint8_t* black, const uint8_t* color, int16_t x, int16_t y, int16_t w, int16_t h)
  354. {
  355. epd2.writeImage(black, color, x, y, w, h, false, false, false);
  356. }
  357. void writeImagePart(const uint8_t* black, const uint8_t* color, int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  358. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  359. {
  360. epd2.writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  361. }
  362. void writeImagePart(const uint8_t* black, const uint8_t* color, int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  363. int16_t x, int16_t y, int16_t w, int16_t h)
  364. {
  365. epd2.writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, false, false, false);
  366. }
  367. // write sprite of native data to controller memory, without screen refresh; x and w should be multiple of 8
  368. void writeNative(const uint8_t* data1, const uint8_t* data2, int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  369. {
  370. epd2.writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  371. }
  372. // write to controller memory, with screen refresh; x and w should be multiple of 8
  373. void drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false)
  374. {
  375. epd2.drawImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  376. }
  377. void drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  378. int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false)
  379. {
  380. epd2.drawImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  381. }
  382. void drawImage(const uint8_t* black, const uint8_t* color, int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  383. {
  384. epd2.drawImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  385. }
  386. void drawImage(const uint8_t* black, const uint8_t* color, int16_t x, int16_t y, int16_t w, int16_t h)
  387. {
  388. epd2.drawImage(black, color, x, y, w, h, false, false, false);
  389. }
  390. void drawImagePart(const uint8_t* black, const uint8_t* color, int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  391. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  392. {
  393. epd2.drawImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  394. }
  395. void drawImagePart(const uint8_t* black, const uint8_t* color, int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  396. int16_t x, int16_t y, int16_t w, int16_t h)
  397. {
  398. epd2.drawImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, false, false, false);
  399. }
  400. // write sprite of native data to controller memory, with screen refresh; x and w should be multiple of 8
  401. void drawNative(const uint8_t* data1, const uint8_t* data2, int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  402. {
  403. epd2.drawNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  404. }
  405. void refresh(bool partial_update_mode = false) // screen refresh from controller memory to full screen
  406. {
  407. epd2.refresh(partial_update_mode);
  408. if (!partial_update_mode) epd2.powerOff();
  409. }
  410. void refresh(int16_t x, int16_t y, int16_t w, int16_t h) // screen refresh from controller memory, partial screen
  411. {
  412. epd2.refresh(x, y, w, h);
  413. }
  414. // turns off generation of panel driving voltages, avoids screen fading over time
  415. void powerOff()
  416. {
  417. epd2.powerOff();
  418. }
  419. // turns powerOff() and sets controller to deep sleep for minimum power use, ONLY if wakeable by RST (rst >= 0)
  420. void hibernate()
  421. {
  422. epd2.hibernate();
  423. }
  424. private:
  425. template <typename T> static inline void
  426. _swap_(T & a, T & b)
  427. {
  428. T t = a;
  429. a = b;
  430. b = t;
  431. };
  432. static inline uint16_t gx_uint16_min(uint16_t a, uint16_t b)
  433. {
  434. return (a < b ? a : b);
  435. };
  436. static inline uint16_t gx_uint16_max(uint16_t a, uint16_t b)
  437. {
  438. return (a > b ? a : b);
  439. };
  440. void _rotate(uint16_t& x, uint16_t& y, uint16_t& w, uint16_t& h)
  441. {
  442. switch (getRotation())
  443. {
  444. case 1:
  445. _swap_(x, y);
  446. _swap_(w, h);
  447. x = WIDTH - x - w;
  448. break;
  449. case 2:
  450. x = WIDTH - x - w;
  451. y = HEIGHT - y - h;
  452. break;
  453. case 3:
  454. _swap_(x, y);
  455. _swap_(w, h);
  456. y = HEIGHT - y - h;
  457. break;
  458. }
  459. }
  460. private:
  461. uint8_t _black_buffer[(GxEPD2_Type::WIDTH / 8) * page_height];
  462. uint8_t _color_buffer[(GxEPD2_Type::WIDTH / 8) * page_height];
  463. bool _using_partial_mode, _second_phase, _mirror;
  464. uint16_t _width_bytes, _pixel_bytes;
  465. int16_t _current_page;
  466. uint16_t _pages, _page_height;
  467. uint16_t _pw_x, _pw_y, _pw_w, _pw_h;
  468. };
  469. #endif