GxEPD2_BW.h 20 KB

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