GxEPD2_BW.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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_BW_H_
  12. #define _GxEPD2_BW_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 "epd/GxEPD2_154.h"
  30. #include "epd/GxEPD2_154_D67.h"
  31. #include "epd/GxEPD2_154_T8.h"
  32. #include "epd/GxEPD2_154_M09.h"
  33. #include "epd/GxEPD2_154_M10.h"
  34. #include "epd/GxEPD2_213.h"
  35. #include "epd/GxEPD2_213_B72.h"
  36. #include "epd/GxEPD2_213_B73.h"
  37. #include "epd/GxEPD2_213_B74.h"
  38. #include "epd/GxEPD2_213_flex.h"
  39. #include "epd/GxEPD2_213_M21.h"
  40. #include "epd/GxEPD2_213_T5D.h"
  41. #include "epd/GxEPD2_260.h"
  42. #include "epd/GxEPD2_260_M01.h"
  43. #include "epd/GxEPD2_290.h"
  44. #include "epd/GxEPD2_290_T5.h"
  45. #include "epd/GxEPD2_290_T5D.h"
  46. #include "epd/GxEPD2_290_M06.h"
  47. #include "epd/GxEPD2_290_T94.h"
  48. #include "epd/GxEPD2_270.h"
  49. #include "epd/GxEPD2_371.h"
  50. #include "epd/GxEPD2_420.h"
  51. #include "epd/GxEPD2_420_M01.h"
  52. #include "epd/GxEPD2_583.h"
  53. #include "epd/GxEPD2_583_T8.h"
  54. #include "epd/GxEPD2_750.h"
  55. #include "epd/GxEPD2_750_T7.h"
  56. #include "epd/GxEPD2_1160_T91.h"
  57. #include "epd/GxEPD2_1248.h"
  58. #include "it8951/GxEPD2_it60.h"
  59. #include "it8951/GxEPD2_it60_1448x1072.h"
  60. #include "it8951/GxEPD2_it78_1872x1404.h"
  61. template<typename GxEPD2_Type, const uint16_t page_height>
  62. class GxEPD2_BW : public GxEPD2_GFX_BASE_CLASS
  63. {
  64. public:
  65. GxEPD2_Type epd2;
  66. #if ENABLE_GxEPD2_GFX
  67. GxEPD2_BW(GxEPD2_Type epd2_instance) : GxEPD2_GFX_BASE_CLASS(epd2, GxEPD2_Type::WIDTH, GxEPD2_Type::HEIGHT), epd2(epd2_instance)
  68. #else
  69. GxEPD2_BW(GxEPD2_Type epd2_instance) : GxEPD2_GFX_BASE_CLASS(GxEPD2_Type::WIDTH, GxEPD2_Type::HEIGHT), epd2(epd2_instance)
  70. #endif
  71. {
  72. _page_height = page_height;
  73. _pages = (HEIGHT / _page_height) + ((HEIGHT % _page_height) > 0);
  74. _reverse = (epd2_instance.panel == GxEPD2::GDE0213B1);
  75. _mirror = false;
  76. _using_partial_mode = false;
  77. _current_page = 0;
  78. setFullWindow();
  79. }
  80. uint16_t pages()
  81. {
  82. return _pages;
  83. }
  84. uint16_t pageHeight()
  85. {
  86. return _page_height;
  87. }
  88. bool mirror(bool m)
  89. {
  90. _swap_ (_mirror, m);
  91. return m;
  92. }
  93. void drawPixel(int16_t x, int16_t y, uint16_t color)
  94. {
  95. if ((x < 0) || (x >= width()) || (y < 0) || (y >= height())) return;
  96. if (_mirror) x = width() - x - 1;
  97. // check rotation, move pixel around if necessary
  98. switch (getRotation())
  99. {
  100. case 1:
  101. _swap_(x, y);
  102. x = WIDTH - x - 1;
  103. break;
  104. case 2:
  105. x = WIDTH - x - 1;
  106. y = HEIGHT - y - 1;
  107. break;
  108. case 3:
  109. _swap_(x, y);
  110. y = HEIGHT - y - 1;
  111. break;
  112. }
  113. // transpose partial window to 0,0
  114. x -= _pw_x;
  115. y -= _pw_y;
  116. // clip to (partial) window
  117. if ((x < 0) || (x >= _pw_w) || (y < 0) || (y >= _pw_h)) return;
  118. // adjust for current page
  119. y -= _current_page * _page_height;
  120. if (_reverse) y = _page_height - y - 1;
  121. // check if in current page
  122. if ((y < 0) || (y >= _page_height)) return;
  123. uint16_t i = x / 8 + y * (_pw_w / 8);
  124. if (color)
  125. _buffer[i] = (_buffer[i] | (1 << (7 - x % 8)));
  126. else
  127. _buffer[i] = (_buffer[i] & (0xFF ^ (1 << (7 - x % 8))));
  128. }
  129. void init(uint32_t serial_diag_bitrate = 0) // = 0 : disabled
  130. {
  131. epd2.init(serial_diag_bitrate);
  132. _using_partial_mode = false;
  133. _current_page = 0;
  134. setFullWindow();
  135. }
  136. // init method with additional parameters:
  137. // initial false for re-init after processor deep sleep wake up, if display power supply was kept
  138. // this can be used to avoid the repeated initial full refresh on displays with fast partial update
  139. // NOTE: garbage will result on fast partial update displays, if initial full update is omitted after power loss
  140. // reset_duration = 20 is default; a value of 2 may help with "clever" reset circuit of newer boards from Waveshare
  141. // pulldown_rst_mode true for alternate RST handling to avoid feeding 5V through RST pin
  142. void init(uint32_t serial_diag_bitrate, bool initial, uint16_t reset_duration = 20, bool pulldown_rst_mode = false)
  143. {
  144. epd2.init(serial_diag_bitrate, initial, reset_duration, pulldown_rst_mode);
  145. _using_partial_mode = false;
  146. _current_page = 0;
  147. setFullWindow();
  148. }
  149. void fillScreen(uint16_t color) // 0x0 black, >0x0 white, to buffer
  150. {
  151. uint8_t data = (color == GxEPD_BLACK) ? 0x00 : 0xFF;
  152. for (uint16_t x = 0; x < sizeof(_buffer); x++)
  153. {
  154. _buffer[x] = data;
  155. }
  156. }
  157. // display buffer content to screen, useful for full screen buffer
  158. void display(bool partial_update_mode = false)
  159. {
  160. if (partial_update_mode) epd2.writeImage(_buffer, 0, 0, WIDTH, _page_height);
  161. else epd2.writeImageForFullRefresh(_buffer, 0, 0, WIDTH, _page_height);
  162. epd2.refresh(partial_update_mode);
  163. if (epd2.hasFastPartialUpdate)
  164. {
  165. epd2.writeImageAgain(_buffer, 0, 0, WIDTH, _page_height);
  166. }
  167. if (!partial_update_mode) epd2.powerOff();
  168. }
  169. // display part of buffer content to screen, useful for full screen buffer
  170. // displayWindow, use parameters according to actual rotation.
  171. // x and w should be multiple of 8, for rotation 0 or 2,
  172. // y and h should be multiple of 8, for rotation 1 or 3,
  173. // else window is increased as needed,
  174. // this is an addressing limitation of the e-paper controllers
  175. void displayWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  176. {
  177. x = gx_uint16_min(x, width());
  178. y = gx_uint16_min(y, height());
  179. w = gx_uint16_min(w, width() - x);
  180. h = gx_uint16_min(h, height() - y);
  181. _rotate(x, y, w, h);
  182. uint16_t y_part = _reverse ? HEIGHT - h - y : y;
  183. epd2.writeImagePart(_buffer, x, y_part, WIDTH, _page_height, x, y, w, h);
  184. epd2.refresh(x, y, w, h);
  185. if (epd2.hasFastPartialUpdate)
  186. {
  187. epd2.writeImagePartAgain(_buffer, x, y_part, WIDTH, _page_height, x, y, w, h);
  188. }
  189. }
  190. void setFullWindow()
  191. {
  192. _using_partial_mode = false;
  193. _pw_x = 0;
  194. _pw_y = 0;
  195. _pw_w = WIDTH;
  196. _pw_h = HEIGHT;
  197. }
  198. // setPartialWindow, use parameters according to actual rotation.
  199. // x and w should be multiple of 8, for rotation 0 or 2,
  200. // y and h should be multiple of 8, for rotation 1 or 3,
  201. // else window is increased as needed,
  202. // this is an addressing limitation of the e-paper controllers
  203. void setPartialWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  204. {
  205. _pw_x = gx_uint16_min(x, width());
  206. _pw_y = gx_uint16_min(y, height());
  207. _pw_w = gx_uint16_min(w, width() - _pw_x);
  208. _pw_h = gx_uint16_min(h, height() - _pw_y);
  209. _rotate(_pw_x, _pw_y, _pw_w, _pw_h);
  210. _using_partial_mode = true;
  211. // make _pw_x, _pw_w multiple of 8
  212. _pw_w += _pw_x % 8;
  213. if (_pw_w % 8 > 0) _pw_w += 8 - _pw_w % 8;
  214. _pw_x -= _pw_x % 8;
  215. }
  216. void firstPage()
  217. {
  218. fillScreen(GxEPD_WHITE);
  219. _current_page = 0;
  220. _second_phase = false;
  221. }
  222. bool nextPage()
  223. {
  224. if (1 == _pages)
  225. {
  226. if (_using_partial_mode)
  227. {
  228. uint32_t offset = _reverse ? (HEIGHT - _pw_h) * _pw_w / 8 : 0;
  229. epd2.writeImage(_buffer + offset, _pw_x, _pw_y, _pw_w, _pw_h);
  230. epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h);
  231. if (epd2.hasFastPartialUpdate)
  232. {
  233. epd2.writeImageAgain(_buffer + offset, _pw_x, _pw_y, _pw_w, _pw_h);
  234. //epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h); // not needed
  235. }
  236. }
  237. else // full update
  238. {
  239. epd2.writeImageForFullRefresh(_buffer, 0, 0, WIDTH, HEIGHT);
  240. epd2.refresh(false);
  241. if (epd2.hasFastPartialUpdate)
  242. {
  243. epd2.writeImageAgain(_buffer, 0, 0, WIDTH, HEIGHT);
  244. //epd2.refresh(true); // not needed
  245. }
  246. epd2.powerOff();
  247. }
  248. return false;
  249. }
  250. uint16_t page_ys = _current_page * _page_height;
  251. if (_using_partial_mode)
  252. {
  253. //Serial.print(" nextPage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(_pw_y); Serial.print(", ");
  254. //Serial.print(_pw_w); Serial.print(", "); Serial.print(_pw_h); Serial.print(") P"); Serial.println(_current_page);
  255. uint16_t page_ye = _current_page < (_pages - 1) ? page_ys + _page_height : HEIGHT;
  256. uint16_t dest_ys = _pw_y + page_ys; // transposed
  257. uint16_t dest_ye = gx_uint16_min(_pw_y + _pw_h, _pw_y + page_ye);
  258. if (dest_ye > dest_ys)
  259. {
  260. //Serial.print("writeImage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(dest_ys); Serial.print(", ");
  261. //Serial.print(_pw_w); Serial.print(", "); Serial.print(dest_ye - dest_ys); Serial.println(")");
  262. uint32_t offset = _reverse ? (_page_height - (dest_ye - dest_ys)) * _pw_w / 8 : 0;
  263. if (!_second_phase) epd2.writeImage(_buffer + offset, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  264. else epd2.writeImageAgain(_buffer + offset, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  265. }
  266. else
  267. {
  268. //Serial.print("writeImage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(dest_ys); Serial.print(", ");
  269. //Serial.print(_pw_w); Serial.print(", "); Serial.print(dest_ye - dest_ys); Serial.print(") skipped ");
  270. //Serial.print(dest_ys); Serial.print(".."); Serial.println(dest_ye);
  271. }
  272. _current_page++;
  273. if (_current_page == _pages)
  274. {
  275. _current_page = 0;
  276. if (!_second_phase)
  277. {
  278. epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h);
  279. if (epd2.hasFastPartialUpdate)
  280. {
  281. _second_phase = true;
  282. fillScreen(GxEPD_WHITE);
  283. return true;
  284. }
  285. }
  286. return false;
  287. }
  288. fillScreen(GxEPD_WHITE);
  289. return true;
  290. }
  291. else // full update
  292. {
  293. if (!_second_phase) epd2.writeImageForFullRefresh(_buffer, 0, page_ys, WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  294. else epd2.writeImageAgain(_buffer, 0, page_ys, WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  295. _current_page++;
  296. if (_current_page == _pages)
  297. {
  298. _current_page = 0;
  299. if (epd2.hasFastPartialUpdate)
  300. {
  301. if (!_second_phase)
  302. {
  303. epd2.refresh(false); // full update after first phase
  304. _second_phase = true;
  305. fillScreen(GxEPD_WHITE);
  306. return true;
  307. }
  308. //else epd2.refresh(true); // partial update after second phase
  309. } else epd2.refresh(false); // full update after only phase
  310. epd2.powerOff();
  311. return false;
  312. }
  313. fillScreen(GxEPD_WHITE);
  314. return true;
  315. }
  316. }
  317. // GxEPD style paged drawing; drawCallback() is called as many times as needed
  318. void drawPaged(void (*drawCallback)(const void*), const void* pv)
  319. {
  320. if (1 == _pages)
  321. {
  322. fillScreen(GxEPD_WHITE);
  323. drawCallback(pv);
  324. if (_using_partial_mode)
  325. {
  326. uint32_t offset = _reverse ? (HEIGHT - _pw_h) * _pw_w / 8 : 0;
  327. epd2.writeImage(_buffer + offset, _pw_x, _pw_y, _pw_w, _pw_h);
  328. epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h);
  329. if (epd2.hasFastPartialUpdate)
  330. {
  331. epd2.writeImageAgain(_buffer + offset, _pw_x, _pw_y, _pw_w, _pw_h);
  332. //epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h); // not needed
  333. }
  334. }
  335. else // full update
  336. {
  337. epd2.writeImageForFullRefresh(_buffer, 0, 0, WIDTH, HEIGHT);
  338. epd2.refresh(false);
  339. if (epd2.hasFastPartialUpdate)
  340. {
  341. epd2.writeImageAgain(_buffer, 0, 0, WIDTH, HEIGHT);
  342. //epd2.refresh(true); // not needed
  343. epd2.powerOff();
  344. }
  345. }
  346. return;
  347. }
  348. if (_using_partial_mode)
  349. {
  350. for (uint16_t phase = 1; phase <= 2; phase++)
  351. {
  352. for (_current_page = 0; _current_page < _pages; _current_page++)
  353. {
  354. uint16_t page_ys = _current_page * _page_height;
  355. uint16_t page_ye = _current_page < (_pages - 1) ? page_ys + _page_height : HEIGHT;
  356. uint16_t dest_ys = _pw_y + page_ys; // transposed
  357. uint16_t dest_ye = gx_uint16_min(_pw_y + _pw_h, _pw_y + page_ye);
  358. if (dest_ye > dest_ys)
  359. {
  360. fillScreen(GxEPD_WHITE);
  361. drawCallback(pv);
  362. uint32_t offset = _reverse ? (_page_height - (dest_ye - dest_ys)) * _pw_w / 8 : 0;
  363. if (phase == 1) epd2.writeImage(_buffer + offset, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  364. else epd2.writeImageAgain(_buffer + offset, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  365. }
  366. }
  367. epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h);
  368. if (!epd2.hasFastPartialUpdate) break;
  369. // else make both controller buffers have equal content
  370. }
  371. }
  372. else // full update
  373. {
  374. for (_current_page = 0; _current_page < _pages; _current_page++)
  375. {
  376. uint16_t page_ys = _current_page * _page_height;
  377. fillScreen(GxEPD_WHITE);
  378. drawCallback(pv);
  379. epd2.writeImageForFullRefresh(_buffer, 0, page_ys, WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  380. }
  381. epd2.refresh(false); // full update after first phase
  382. if (epd2.hasFastPartialUpdate)
  383. {
  384. // make both controller buffers have equal content
  385. for (_current_page = 0; _current_page < _pages; _current_page++)
  386. {
  387. uint16_t page_ys = _current_page * _page_height;
  388. fillScreen(GxEPD_WHITE);
  389. drawCallback(pv);
  390. epd2.writeImageAgain(_buffer, 0, page_ys, WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  391. }
  392. //epd2.refresh(true); // partial update after second phase // not needed
  393. }
  394. epd2.powerOff();
  395. }
  396. _current_page = 0;
  397. }
  398. void drawInvertedBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color)
  399. {
  400. // taken from Adafruit_GFX.cpp, modified
  401. int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
  402. uint8_t byte = 0;
  403. for (int16_t j = 0; j < h; j++)
  404. {
  405. for (int16_t i = 0; i < w; i++ )
  406. {
  407. if (i & 7) byte <<= 1;
  408. else
  409. {
  410. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  411. byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
  412. #else
  413. byte = bitmap[j * byteWidth + i / 8];
  414. #endif
  415. }
  416. if (!(byte & 0x80))
  417. {
  418. drawPixel(x + i, y + j, color);
  419. }
  420. }
  421. }
  422. }
  423. // Support for Bitmaps (Sprites) to Controller Buffer and to Screen
  424. void clearScreen(uint8_t value = 0xFF) // init controller memory and screen (default white)
  425. {
  426. epd2.clearScreen(value);
  427. }
  428. void writeScreenBuffer(uint8_t value = 0xFF) // init controller memory (default white)
  429. {
  430. epd2.writeScreenBuffer(value);
  431. }
  432. // write to controller memory, without screen refresh; x and w should be multiple of 8
  433. 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)
  434. {
  435. epd2.writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  436. }
  437. void writeImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  438. int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false)
  439. {
  440. epd2.writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  441. }
  442. 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)
  443. {
  444. epd2.writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  445. }
  446. void writeImage(const uint8_t* black, const uint8_t* color, int16_t x, int16_t y, int16_t w, int16_t h)
  447. {
  448. epd2.writeImage(black, color, x, y, w, h, false, false, false);
  449. }
  450. 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,
  451. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  452. {
  453. epd2.writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  454. }
  455. 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,
  456. int16_t x, int16_t y, int16_t w, int16_t h)
  457. {
  458. epd2.writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, false, false, false);
  459. }
  460. // write sprite of native data to controller memory, without screen refresh; x and w should be multiple of 8
  461. 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)
  462. {
  463. epd2.writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  464. }
  465. // write to controller memory, with screen refresh; x and w should be multiple of 8
  466. 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)
  467. {
  468. epd2.drawImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  469. }
  470. void drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  471. int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false)
  472. {
  473. epd2.drawImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  474. }
  475. 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)
  476. {
  477. epd2.drawImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  478. }
  479. void drawImage(const uint8_t* black, const uint8_t* color, int16_t x, int16_t y, int16_t w, int16_t h)
  480. {
  481. epd2.drawImage(black, color, x, y, w, h, false, false, false);
  482. }
  483. 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,
  484. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  485. {
  486. epd2.drawImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  487. }
  488. 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,
  489. int16_t x, int16_t y, int16_t w, int16_t h)
  490. {
  491. epd2.drawImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, false, false, false);
  492. }
  493. // write sprite of native data to controller memory, with screen refresh; x and w should be multiple of 8
  494. 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)
  495. {
  496. epd2.drawNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  497. }
  498. void refresh(bool partial_update_mode = false) // screen refresh from controller memory to full screen
  499. {
  500. epd2.refresh(partial_update_mode);
  501. if (!partial_update_mode) epd2.powerOff();
  502. }
  503. void refresh(int16_t x, int16_t y, int16_t w, int16_t h) // screen refresh from controller memory, partial screen
  504. {
  505. epd2.refresh(x, y, w, h);
  506. }
  507. // turns off generation of panel driving voltages, avoids screen fading over time
  508. void powerOff()
  509. {
  510. epd2.powerOff();
  511. }
  512. // turns powerOff() and sets controller to deep sleep for minimum power use, ONLY if wakeable by RST (rst >= 0)
  513. void hibernate()
  514. {
  515. epd2.hibernate();
  516. }
  517. private:
  518. template <typename T> static inline void
  519. _swap_(T & a, T & b)
  520. {
  521. T t = a;
  522. a = b;
  523. b = t;
  524. };
  525. static inline uint16_t gx_uint16_min(uint16_t a, uint16_t b)
  526. {
  527. return (a < b ? a : b);
  528. };
  529. static inline uint16_t gx_uint16_max(uint16_t a, uint16_t b)
  530. {
  531. return (a > b ? a : b);
  532. };
  533. void _rotate(uint16_t& x, uint16_t& y, uint16_t& w, uint16_t& h)
  534. {
  535. switch (getRotation())
  536. {
  537. case 1:
  538. _swap_(x, y);
  539. _swap_(w, h);
  540. x = WIDTH - x - w;
  541. break;
  542. case 2:
  543. x = WIDTH - x - w;
  544. y = HEIGHT - y - h;
  545. break;
  546. case 3:
  547. _swap_(x, y);
  548. _swap_(w, h);
  549. y = HEIGHT - y - h;
  550. break;
  551. }
  552. }
  553. private:
  554. uint8_t _buffer[(GxEPD2_Type::WIDTH / 8) * page_height];
  555. bool _using_partial_mode, _second_phase, _mirror, _reverse;
  556. uint16_t _width_bytes, _pixel_bytes;
  557. int16_t _current_page;
  558. uint16_t _pages, _page_height;
  559. uint16_t _pw_x, _pw_y, _pw_w, _pw_h;
  560. };
  561. #endif