GxEPD2_BW.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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("gdey/GxEPD2_290_GDEY029T71H.h")
  135. #include "gdey/GxEPD2_290_GDEY029T71H.h"
  136. #endif
  137. #if __has_include("epd/GxEPD2_270.h")
  138. #include "epd/GxEPD2_270.h"
  139. #endif
  140. #if __has_include("gdey/GxEPD2_270_GDEY027T91.h")
  141. #include "gdey/GxEPD2_270_GDEY027T91.h"
  142. #endif
  143. #if __has_include("gdeq/GxEPD2_310_GDEQ031T10.h")
  144. #include "gdeq/GxEPD2_310_GDEQ031T10.h"
  145. #endif
  146. #if __has_include("epd/GxEPD2_371.h")
  147. #include "epd/GxEPD2_371.h"
  148. #endif
  149. #if __has_include("epd/GxEPD2_370_TC1.h")
  150. #include "epd/GxEPD2_370_TC1.h"
  151. #endif
  152. #if __has_include("gdey/GxEPD2_370_GDEY037T03.h")
  153. #include "gdey/GxEPD2_370_GDEY037T03.h"
  154. #endif
  155. #if __has_include("gdem/GxEPD2_397_GDEM0397T81.h")
  156. #include "gdem/GxEPD2_397_GDEM0397T81.h"
  157. #endif
  158. #if __has_include("epd/GxEPD2_420.h")
  159. #include "epd/GxEPD2_420.h"
  160. #endif
  161. #if __has_include("epd/GxEPD2_420_M01.h")
  162. #include "epd/GxEPD2_420_M01.h"
  163. #endif
  164. #if __has_include("gdey/GxEPD2_420_GDEY042T81.h")
  165. #include "gdey/GxEPD2_420_GDEY042T81.h"
  166. #endif
  167. #if __has_include("other/GxEPD2_420_GYE042A87.h")
  168. #include "other/GxEPD2_420_GYE042A87.h"
  169. #endif
  170. #if __has_include("other/GxEPD2_420_SE0420NQ04.h")
  171. #include "other/GxEPD2_420_SE0420NQ04.h"
  172. #endif
  173. #if __has_include("gdeq/GxEPD2_426_GDEQ0426T82.h")
  174. #include "gdeq/GxEPD2_426_GDEQ0426T82.h"
  175. #endif
  176. #if __has_include("gdeh/GxEPD2_576_GDEH0576T81.h")
  177. #include "gdeh/GxEPD2_576_GDEH0576T81.h"
  178. #endif
  179. #if __has_include("gdey/GxEPD2_579_GDEY0579T93.h")
  180. #include "gdey/GxEPD2_579_GDEY0579T93.h"
  181. #endif
  182. #if __has_include("epd/GxEPD2_583.h")
  183. #include "epd/GxEPD2_583.h"
  184. #endif
  185. #if __has_include("epd/GxEPD2_583_T8.h")
  186. #include "epd/GxEPD2_583_T8.h"
  187. #endif
  188. #if __has_include("gdeq/GxEPD2_583_GDEQ0583T31.h")
  189. #include "gdeq/GxEPD2_583_GDEQ0583T31.h"
  190. #endif
  191. #if __has_include("epd/GxEPD2_750.h")
  192. #include "epd/GxEPD2_750.h"
  193. #endif
  194. #if __has_include("epd/GxEPD2_750_T7.h")
  195. #include "epd/GxEPD2_750_T7.h"
  196. #endif
  197. #if __has_include("gdey/GxEPD2_750_GDEY075T7.h")
  198. #include "gdey/GxEPD2_750_GDEY075T7.h"
  199. #endif
  200. #if __has_include("gdem/GxEPD2_1020_GDEM102T91.h")
  201. #include "gdem/GxEPD2_1020_GDEM102T91.h"
  202. #endif
  203. #if __has_include("gdem/GxEPD2_1085_GDEM1085T51.h")
  204. #include "gdem/GxEPD2_1085_GDEM1085T51.h"
  205. #endif
  206. #if __has_include("epd/GxEPD2_1160_T91.h")
  207. #include "epd/GxEPD2_1160_T91.h"
  208. #endif
  209. #if __has_include("gdem/GxEPD2_1330_GDEM133T91.h")
  210. #include "gdem/GxEPD2_1330_GDEM133T91.h"
  211. #endif
  212. #if __has_include("epd/GxEPD2_1248.h")
  213. #include "epd/GxEPD2_1248.h"
  214. #endif
  215. #if __has_include("it8951/GxEPD2_it60.h")
  216. #include "it8951/GxEPD2_it60.h"
  217. #endif
  218. #if __has_include("it8951/GxEPD2_it60_1448x1072.h")
  219. #include "it8951/GxEPD2_it60_1448x1072.h"
  220. #endif
  221. #if __has_include("it8951/GxEPD2_it78_1872x1404.h")
  222. #include "it8951/GxEPD2_it78_1872x1404.h"
  223. #endif
  224. #if __has_include("it8951/GxEPD2_it103_1872x1404.h")
  225. #include "it8951/GxEPD2_it103_1872x1404.h"
  226. #endif
  227. template<typename GxEPD2_Type, const uint16_t page_height>
  228. class GxEPD2_BW : public GxEPD2_GFX_BASE_CLASS
  229. {
  230. public:
  231. GxEPD2_Type epd2;
  232. #if ENABLE_GxEPD2_GFX
  233. GxEPD2_BW(GxEPD2_Type epd2_instance) : GxEPD2_GFX_BASE_CLASS(epd2, GxEPD2_Type::WIDTH_VISIBLE, GxEPD2_Type::HEIGHT), epd2(epd2_instance)
  234. #else
  235. GxEPD2_BW(GxEPD2_Type epd2_instance) : GxEPD2_GFX_BASE_CLASS(GxEPD2_Type::WIDTH_VISIBLE, GxEPD2_Type::HEIGHT), epd2(epd2_instance)
  236. #endif
  237. {
  238. _page_height = page_height;
  239. _pages = (HEIGHT / _page_height) + ((HEIGHT % _page_height) > 0);
  240. _reverse = (epd2_instance.panel == GxEPD2::GDE0213B1);
  241. _mirror = false;
  242. _using_partial_mode = false;
  243. _current_page = 0;
  244. setFullWindow();
  245. }
  246. uint16_t pages()
  247. {
  248. return _pages;
  249. }
  250. uint16_t pageHeight()
  251. {
  252. return _page_height;
  253. }
  254. bool mirror(bool m)
  255. {
  256. _swap_ (_mirror, m);
  257. return m;
  258. }
  259. void drawPixel(int16_t x, int16_t y, uint16_t color)
  260. {
  261. if ((x < 0) || (x >= width()) || (y < 0) || (y >= height())) return;
  262. if (_mirror) x = width() - x - 1;
  263. // check rotation, move pixel around if necessary
  264. switch (getRotation())
  265. {
  266. case 1:
  267. _swap_(x, y);
  268. x = WIDTH - x - 1;
  269. break;
  270. case 2:
  271. x = WIDTH - x - 1;
  272. y = HEIGHT - y - 1;
  273. break;
  274. case 3:
  275. _swap_(x, y);
  276. y = HEIGHT - y - 1;
  277. break;
  278. }
  279. // transpose partial window to 0,0
  280. x -= _pw_x;
  281. if (!_reverse) y -= _pw_y;
  282. else y = HEIGHT - _pw_y - y - 1;
  283. // clip to (partial) window
  284. if ((x < 0) || (x >= int16_t(_pw_w)) || (y < 0) || (y >= int16_t(_pw_h))) return;
  285. // adjust for current page
  286. y -= _current_page * _page_height;
  287. // check if in current page
  288. if ((y < 0) || (y >= int16_t(_page_height))) return;
  289. uint32_t i = x / 8 + uint32_t(y) * uint32_t(_pw_w / 8);
  290. if (color)
  291. _buffer[i] = (_buffer[i] | (1 << (7 - x % 8)));
  292. else
  293. _buffer[i] = (_buffer[i] & (0xFF ^ (1 << (7 - x % 8))));
  294. }
  295. void init(uint32_t serial_diag_bitrate = 0) // = 0 : disabled
  296. {
  297. epd2.init(serial_diag_bitrate);
  298. _using_partial_mode = false;
  299. _current_page = 0;
  300. setFullWindow();
  301. }
  302. // init method with additional parameters:
  303. // initial false for re-init after processor deep sleep wake up, if display power supply was kept
  304. // this can be used to avoid the repeated initial full refresh on displays with fast partial update
  305. // NOTE: garbage will result on fast partial update displays, if initial full update is omitted after power loss
  306. // reset_duration = 10 is default; a value of 2 may help with "clever" reset circuit of newer boards from Waveshare
  307. // pulldown_rst_mode true for alternate RST handling to avoid feeding 5V through RST pin
  308. void init(uint32_t serial_diag_bitrate, bool initial, uint16_t reset_duration = 10, bool pulldown_rst_mode = false)
  309. {
  310. epd2.init(serial_diag_bitrate, initial, reset_duration, pulldown_rst_mode);
  311. _using_partial_mode = false;
  312. _current_page = 0;
  313. setFullWindow();
  314. }
  315. // init method with additional parameters:
  316. // SPIClass& spi: either SPI or alternate HW SPI channel
  317. // SPISettings spi_settings: e.g. for higher SPI speed selection
  318. void init(uint32_t serial_diag_bitrate, bool initial, uint16_t reset_duration, bool pulldown_rst_mode, SPIClass& spi, SPISettings spi_settings)
  319. {
  320. epd2.selectSPI(spi, spi_settings);
  321. epd2.init(serial_diag_bitrate, initial, reset_duration, pulldown_rst_mode);
  322. _using_partial_mode = false;
  323. _current_page = 0;
  324. setFullWindow();
  325. }
  326. // release SPI and control pins
  327. void end()
  328. {
  329. epd2.end();
  330. }
  331. void fillScreen(uint16_t color) // 0x0 black, >0x0 white, to buffer
  332. {
  333. uint8_t data = (color == GxEPD_BLACK) ? 0x00 : 0xFF;
  334. for (uint32_t x = 0; x < sizeof(_buffer); x++)
  335. {
  336. _buffer[x] = data;
  337. }
  338. }
  339. // display buffer content to screen, useful for full screen buffer
  340. void display(bool partial_update_mode = false)
  341. {
  342. if (partial_update_mode) epd2.writeImage(_buffer, 0, 0, GxEPD2_Type::WIDTH, _page_height);
  343. else epd2.writeImageForFullRefresh(_buffer, 0, 0, GxEPD2_Type::WIDTH, _page_height);
  344. epd2.refresh(partial_update_mode);
  345. if (epd2.hasFastPartialUpdate)
  346. {
  347. epd2.writeImageAgain(_buffer, 0, 0, GxEPD2_Type::WIDTH, _page_height);
  348. }
  349. if (!partial_update_mode) epd2.powerOff();
  350. }
  351. // display part of buffer content to screen, useful for full screen buffer
  352. // displayWindow, use parameters according to actual rotation.
  353. // x and w should be multiple of 8, for rotation 0 or 2,
  354. // y and h should be multiple of 8, for rotation 1 or 3,
  355. // else window is increased as needed,
  356. // this is an addressing limitation of the e-paper controllers
  357. void displayWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  358. {
  359. x = gx_uint16_min(x, width());
  360. y = gx_uint16_min(y, height());
  361. w = gx_uint16_min(w, width() - x);
  362. h = gx_uint16_min(h, height() - y);
  363. _rotate(x, y, w, h);
  364. uint16_t y_part = _reverse ? HEIGHT - h - y : y;
  365. epd2.writeImagePart(_buffer, x, y_part, GxEPD2_Type::WIDTH, _page_height, x, y_part, w, h);
  366. epd2.refresh(x, y_part, w, h);
  367. if (epd2.hasFastPartialUpdate)
  368. {
  369. epd2.writeImagePartAgain(_buffer, x, y_part, GxEPD2_Type::WIDTH, _page_height, x, y_part, w, h);
  370. }
  371. }
  372. void setFullWindow()
  373. {
  374. _using_partial_mode = false;
  375. _pw_x = 0;
  376. _pw_y = 0;
  377. _pw_w = GxEPD2_Type::WIDTH;
  378. _pw_h = HEIGHT;
  379. }
  380. // setPartialWindow, use parameters according to actual rotation.
  381. // x and w should be multiple of 8, for rotation 0 or 2,
  382. // y and h should be multiple of 8, for rotation 1 or 3,
  383. // else window is increased as needed,
  384. // this is an addressing limitation of the e-paper controllers
  385. void setPartialWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  386. {
  387. _pw_x = gx_uint16_min(x, width());
  388. _pw_y = gx_uint16_min(y, height());
  389. _pw_w = gx_uint16_min(w, width() - _pw_x);
  390. _pw_h = gx_uint16_min(h, height() - _pw_y);
  391. _rotate(_pw_x, _pw_y, _pw_w, _pw_h);
  392. _using_partial_mode = true;
  393. // make _pw_x, _pw_w multiple of 8
  394. _pw_w += _pw_x % 8;
  395. if (_pw_w % 8 > 0) _pw_w += 8 - _pw_w % 8;
  396. _pw_x -= _pw_x % 8;
  397. if (_reverse) _pw_y = HEIGHT - _pw_h - _pw_y;
  398. }
  399. void firstPage()
  400. {
  401. fillScreen(GxEPD_WHITE);
  402. _current_page = 0;
  403. _second_phase = false;
  404. }
  405. bool nextPage()
  406. {
  407. if (1 == _pages)
  408. {
  409. if (_using_partial_mode)
  410. {
  411. epd2.writeImage(_buffer, _pw_x, _pw_y, _pw_w, _pw_h);
  412. epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h);
  413. if (epd2.hasFastPartialUpdate)
  414. {
  415. epd2.writeImageAgain(_buffer, _pw_x, _pw_y, _pw_w, _pw_h);
  416. //epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h); // not needed
  417. }
  418. }
  419. else // full update
  420. {
  421. epd2.writeImageForFullRefresh(_buffer, 0, 0, GxEPD2_Type::WIDTH, HEIGHT);
  422. epd2.refresh(false);
  423. if (epd2.hasFastPartialUpdate)
  424. {
  425. epd2.writeImageAgain(_buffer, 0, 0, GxEPD2_Type::WIDTH, HEIGHT);
  426. //epd2.refresh(true); // not needed
  427. }
  428. epd2.powerOff();
  429. }
  430. return false;
  431. }
  432. uint16_t page_ys = _current_page * _page_height;
  433. if (_using_partial_mode)
  434. {
  435. //Serial.print(" nextPage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(_pw_y); Serial.print(", ");
  436. //Serial.print(_pw_w); Serial.print(", "); Serial.print(_pw_h); Serial.print(") P"); Serial.println(_current_page);
  437. uint16_t page_ye = _current_page < int16_t(_pages - 1) ? page_ys + _page_height : HEIGHT;
  438. uint16_t dest_ys = _pw_y + page_ys; // transposed
  439. uint16_t dest_ye = gx_uint16_min(_pw_y + _pw_h, _pw_y + page_ye);
  440. if (dest_ye > dest_ys)
  441. {
  442. //Serial.print("writeImage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(dest_ys); Serial.print(", ");
  443. //Serial.print(_pw_w); Serial.print(", "); Serial.print(dest_ye - dest_ys); Serial.println(")");
  444. if (!_second_phase) epd2.writeImage(_buffer, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  445. else epd2.writeImageAgain(_buffer, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  446. }
  447. else
  448. {
  449. //Serial.print("writeImage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(dest_ys); Serial.print(", ");
  450. //Serial.print(_pw_w); Serial.print(", "); Serial.print(dest_ye - dest_ys); Serial.print(") skipped ");
  451. //Serial.print(dest_ys); Serial.print(".."); Serial.println(dest_ye);
  452. }
  453. _current_page++;
  454. if (_current_page == int16_t(_pages))
  455. {
  456. _current_page = 0;
  457. if (!_second_phase)
  458. {
  459. epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h);
  460. if (epd2.hasFastPartialUpdate)
  461. {
  462. _second_phase = true;
  463. fillScreen(GxEPD_WHITE);
  464. return true;
  465. }
  466. }
  467. return false;
  468. }
  469. fillScreen(GxEPD_WHITE);
  470. return true;
  471. }
  472. else // full update
  473. {
  474. if (!_second_phase) epd2.writeImageForFullRefresh(_buffer, 0, page_ys, GxEPD2_Type::WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  475. else epd2.writeImageAgain(_buffer, 0, page_ys, GxEPD2_Type::WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  476. _current_page++;
  477. if (_current_page == int16_t(_pages))
  478. {
  479. _current_page = 0;
  480. if (epd2.hasFastPartialUpdate)
  481. {
  482. if (!_second_phase)
  483. {
  484. epd2.refresh(false); // full update after first phase
  485. _second_phase = true;
  486. fillScreen(GxEPD_WHITE);
  487. return true;
  488. }
  489. //else epd2.refresh(true); // partial update after second phase
  490. } else epd2.refresh(false); // full update after only phase
  491. epd2.powerOff();
  492. return false;
  493. }
  494. fillScreen(GxEPD_WHITE);
  495. return true;
  496. }
  497. }
  498. bool nextPageToPrevious()
  499. {
  500. if (1 == _pages)
  501. {
  502. if (_using_partial_mode)
  503. {
  504. epd2.writeImageToPrevious(_buffer, _pw_x, _pw_y, _pw_w, _pw_h);
  505. }
  506. else // full update
  507. {
  508. epd2.writeImageToPrevious(_buffer, 0, 0, GxEPD2_Type::WIDTH, HEIGHT);
  509. }
  510. return false;
  511. }
  512. uint16_t page_ys = _current_page * _page_height;
  513. if (_using_partial_mode)
  514. {
  515. //Serial.print(" nextPage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(_pw_y); Serial.print(", ");
  516. //Serial.print(_pw_w); Serial.print(", "); Serial.print(_pw_h); Serial.print(") P"); Serial.println(_current_page);
  517. uint16_t page_ye = _current_page < int16_t(_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. //Serial.print("writeImage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(dest_ys); Serial.print(", ");
  523. //Serial.print(_pw_w); Serial.print(", "); Serial.print(dest_ye - dest_ys); Serial.println(")");
  524. epd2.writeImageToPrevious(_buffer, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  525. }
  526. else
  527. {
  528. //Serial.print("writeImage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(dest_ys); Serial.print(", ");
  529. //Serial.print(_pw_w); Serial.print(", "); Serial.print(dest_ye - dest_ys); Serial.print(") skipped ");
  530. //Serial.print(dest_ys); Serial.print(".."); Serial.println(dest_ye);
  531. }
  532. _current_page++;
  533. if (_current_page == int16_t(_pages))
  534. {
  535. return false;
  536. }
  537. fillScreen(GxEPD_WHITE);
  538. return true;
  539. }
  540. else // full update
  541. {
  542. epd2.writeImageToPrevious(_buffer, 0, page_ys, GxEPD2_Type::WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  543. _current_page++;
  544. if (_current_page == int16_t(_pages))
  545. {
  546. return false;
  547. }
  548. fillScreen(GxEPD_WHITE);
  549. return true;
  550. }
  551. }
  552. // GxEPD style paged drawing; drawCallback() is called as many times as needed
  553. void drawPaged(void (*drawCallback)(const void*), const void* pv)
  554. {
  555. if (1 == _pages)
  556. {
  557. fillScreen(GxEPD_WHITE);
  558. drawCallback(pv);
  559. if (_using_partial_mode)
  560. {
  561. epd2.writeImage(_buffer, _pw_x, _pw_y, _pw_w, _pw_h);
  562. epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h);
  563. if (epd2.hasFastPartialUpdate)
  564. {
  565. epd2.writeImageAgain(_buffer, _pw_x, _pw_y, _pw_w, _pw_h);
  566. //epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h); // not needed
  567. }
  568. }
  569. else // full update
  570. {
  571. epd2.writeImageForFullRefresh(_buffer, 0, 0, GxEPD2_Type::WIDTH, HEIGHT);
  572. epd2.refresh(false);
  573. if (epd2.hasFastPartialUpdate)
  574. {
  575. epd2.writeImageAgain(_buffer, 0, 0, GxEPD2_Type::WIDTH, HEIGHT);
  576. //epd2.refresh(true); // not needed
  577. epd2.powerOff();
  578. }
  579. }
  580. return;
  581. }
  582. if (_using_partial_mode)
  583. {
  584. for (uint16_t phase = 1; phase <= 2; phase++)
  585. {
  586. for (_current_page = 0; _current_page < _pages; _current_page++)
  587. {
  588. uint16_t page_ys = _current_page * _page_height;
  589. uint16_t page_ye = _current_page < (_pages - 1) ? page_ys + _page_height : HEIGHT;
  590. uint16_t dest_ys = _pw_y + page_ys; // transposed
  591. uint16_t dest_ye = gx_uint16_min(_pw_y + _pw_h, _pw_y + page_ye);
  592. if (dest_ye > dest_ys)
  593. {
  594. fillScreen(GxEPD_WHITE);
  595. drawCallback(pv);
  596. if (phase == 1) epd2.writeImage(_buffer, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  597. else epd2.writeImageAgain(_buffer, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  598. }
  599. }
  600. epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h);
  601. if (!epd2.hasFastPartialUpdate) break;
  602. // else make both controller buffers have equal content
  603. }
  604. }
  605. else // full update
  606. {
  607. for (_current_page = 0; _current_page < _pages; _current_page++)
  608. {
  609. uint16_t page_ys = _current_page * _page_height;
  610. fillScreen(GxEPD_WHITE);
  611. drawCallback(pv);
  612. epd2.writeImageForFullRefresh(_buffer, 0, page_ys, GxEPD2_Type::WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  613. }
  614. epd2.refresh(false); // full update after first phase
  615. if (epd2.hasFastPartialUpdate)
  616. {
  617. // make both controller buffers have equal content
  618. for (_current_page = 0; _current_page < _pages; _current_page++)
  619. {
  620. uint16_t page_ys = _current_page * _page_height;
  621. fillScreen(GxEPD_WHITE);
  622. drawCallback(pv);
  623. epd2.writeImageAgain(_buffer, 0, page_ys, GxEPD2_Type::WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  624. }
  625. //epd2.refresh(true); // partial update after second phase // not needed
  626. }
  627. epd2.powerOff();
  628. }
  629. _current_page = 0;
  630. }
  631. void drawInvertedBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color)
  632. {
  633. // taken from Adafruit_GFX.cpp, modified
  634. int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
  635. uint8_t byte = 0;
  636. for (int16_t j = 0; j < h; j++)
  637. {
  638. for (int16_t i = 0; i < w; i++ )
  639. {
  640. if (i & 7) byte <<= 1;
  641. else
  642. {
  643. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  644. byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
  645. #else
  646. byte = bitmap[j * byteWidth + i / 8];
  647. #endif
  648. }
  649. if (!(byte & 0x80))
  650. {
  651. drawPixel(x + i, y + j, color);
  652. }
  653. }
  654. }
  655. }
  656. // Support for Bitmaps (Sprites) to Controller Buffer and to Screen
  657. void clearScreen(uint8_t value = 0xFF) // init controller memory and screen (default white)
  658. {
  659. epd2.clearScreen(value);
  660. }
  661. void writeScreenBuffer(uint8_t value = 0xFF) // init controller memory (default white)
  662. {
  663. epd2.writeScreenBuffer(value);
  664. }
  665. // write to controller memory, without screen refresh; x and w should be multiple of 8
  666. 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)
  667. {
  668. epd2.writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  669. }
  670. void writeImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  671. int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false)
  672. {
  673. epd2.writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  674. }
  675. 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)
  676. {
  677. epd2.writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  678. }
  679. void writeImage(const uint8_t* black, const uint8_t* color, int16_t x, int16_t y, int16_t w, int16_t h)
  680. {
  681. epd2.writeImage(black, color, x, y, w, h, false, false, false);
  682. }
  683. 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,
  684. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  685. {
  686. epd2.writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  687. }
  688. 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,
  689. int16_t x, int16_t y, int16_t w, int16_t h)
  690. {
  691. epd2.writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, false, false, false);
  692. }
  693. // write sprite of native data to controller memory, without screen refresh; x and w should be multiple of 8
  694. 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)
  695. {
  696. epd2.writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  697. }
  698. // write to controller memory, with screen refresh; x and w should be multiple of 8
  699. 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)
  700. {
  701. epd2.drawImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  702. }
  703. void drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  704. int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false)
  705. {
  706. epd2.drawImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  707. }
  708. 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)
  709. {
  710. epd2.drawImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  711. }
  712. void drawImage(const uint8_t* black, const uint8_t* color, int16_t x, int16_t y, int16_t w, int16_t h)
  713. {
  714. epd2.drawImage(black, color, x, y, w, h, false, false, false);
  715. }
  716. 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,
  717. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  718. {
  719. epd2.drawImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  720. }
  721. 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,
  722. int16_t x, int16_t y, int16_t w, int16_t h)
  723. {
  724. epd2.drawImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, false, false, false);
  725. }
  726. // write sprite of native data to controller memory, with screen refresh; x and w should be multiple of 8
  727. 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)
  728. {
  729. epd2.drawNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  730. }
  731. void refresh(bool partial_update_mode = false) // screen refresh from controller memory to full screen
  732. {
  733. epd2.refresh(partial_update_mode);
  734. if (!partial_update_mode) epd2.powerOff();
  735. }
  736. void refresh(int16_t x, int16_t y, int16_t w, int16_t h) // screen refresh from controller memory, partial screen
  737. {
  738. epd2.refresh(x, y, w, h);
  739. }
  740. // turns off generation of panel driving voltages, avoids screen fading over time
  741. void powerOff()
  742. {
  743. epd2.powerOff();
  744. }
  745. // turns powerOff() and sets controller to deep sleep for minimum power use, ONLY if wakeable by RST (rst >= 0)
  746. void hibernate()
  747. {
  748. epd2.hibernate();
  749. }
  750. private:
  751. template <typename T> static inline void
  752. _swap_(T & a, T & b)
  753. {
  754. T t = a;
  755. a = b;
  756. b = t;
  757. };
  758. static inline uint16_t gx_uint16_min(uint16_t a, uint16_t b)
  759. {
  760. return (a < b ? a : b);
  761. };
  762. static inline uint16_t gx_uint16_max(uint16_t a, uint16_t b)
  763. {
  764. return (a > b ? a : b);
  765. };
  766. void _rotate(uint16_t& x, uint16_t& y, uint16_t& w, uint16_t& h)
  767. {
  768. switch (getRotation())
  769. {
  770. case 1:
  771. _swap_(x, y);
  772. _swap_(w, h);
  773. x = WIDTH - x - w;
  774. break;
  775. case 2:
  776. x = WIDTH - x - w;
  777. y = HEIGHT - y - h;
  778. break;
  779. case 3:
  780. _swap_(x, y);
  781. _swap_(w, h);
  782. y = HEIGHT - y - h;
  783. break;
  784. }
  785. }
  786. private:
  787. uint8_t _buffer[(GxEPD2_Type::WIDTH / 8) * page_height];
  788. bool _using_partial_mode, _second_phase, _mirror, _reverse;
  789. uint16_t _width_bytes, _pixel_bytes;
  790. int16_t _current_page;
  791. uint16_t _pages, _page_height;
  792. uint16_t _pw_x, _pw_y, _pw_w, _pw_h;
  793. };
  794. #endif