GxEPD2_BW.h 22 KB

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