GxEPD2_BW.h 26 KB

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