GxEPD2_BW_SHM.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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_SHM_H_
  12. #define _GxEPD2_BW_SHM_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_SHM : public GxEPD2_GFX_BASE_CLASS
  229. {
  230. public:
  231. GxEPD2_Type epd2;
  232. #if ENABLE_GxEPD2_GFX
  233. GxEPD2_BW_SHM(GxEPD2_Type epd2_instance) : GxEPD2_GFX_BASE_CLASS(epd2, GxEPD2_Type::WIDTH_VISIBLE, GxEPD2_Type::HEIGHT), epd2(epd2_instance)
  234. #else
  235. GxEPD2_BW_SHM(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. _p_full_shadow_buffer = 0;
  245. _uses_interleaved_fast_refresh = (epd2.panel == GxEPD2::GDEH0576T81);
  246. setFullWindow();
  247. }
  248. uint16_t pages()
  249. {
  250. return _pages;
  251. }
  252. uint16_t pageHeight()
  253. {
  254. return _page_height;
  255. }
  256. bool mirror(bool m)
  257. {
  258. _swap_ (_mirror, m);
  259. return m;
  260. }
  261. void drawPixel(int16_t x, int16_t y, uint16_t color)
  262. {
  263. if ((x < 0) || (x >= width()) || (y < 0) || (y >= height())) return;
  264. if (_mirror) x = width() - x - 1;
  265. // check rotation, move pixel around if necessary
  266. switch (getRotation())
  267. {
  268. case 1:
  269. _swap_(x, y);
  270. x = WIDTH - x - 1;
  271. break;
  272. case 2:
  273. x = WIDTH - x - 1;
  274. y = HEIGHT - y - 1;
  275. break;
  276. case 3:
  277. _swap_(x, y);
  278. y = HEIGHT - y - 1;
  279. break;
  280. }
  281. // transpose partial window to 0,0
  282. x -= _pw_x;
  283. if (!_reverse) y -= _pw_y;
  284. else y = HEIGHT - _pw_y - y - 1;
  285. // clip to (partial) window
  286. if ((x < 0) || (x >= int16_t(_pw_w)) || (y < 0) || (y >= int16_t(_pw_h))) return;
  287. // adjust for current page
  288. y -= _current_page * _page_height;
  289. // check if in current page
  290. if ((y < 0) || (y >= int16_t(_page_height))) return;
  291. uint32_t i = x / 8 + uint32_t(y) * uint32_t(_pw_w / 8);
  292. if (color)
  293. _buffer[i] = (_buffer[i] | (1 << (7 - x % 8)));
  294. else
  295. _buffer[i] = (_buffer[i] & (0xFF ^ (1 << (7 - x % 8))));
  296. }
  297. void init(uint32_t serial_diag_bitrate = 0) // = 0 : disabled
  298. {
  299. epd2.init(serial_diag_bitrate);
  300. _using_partial_mode = false;
  301. _current_page = 0;
  302. setFullWindow();
  303. if (!_p_full_shadow_buffer) _p_full_shadow_buffer = (uint8_t*) malloc(uint32_t(GxEPD2_Type::HEIGHT) * uint32_t(GxEPD2_Type::WIDTH) / 8);
  304. //Serial.print("_p_full_shadow_buffer = 0x"); Serial.println(uint32_t(_p_full_shadow_buffer), HEX);
  305. }
  306. // init method with additional parameters:
  307. // initial false for re-init after processor deep sleep wake up, if display power supply was kept
  308. // this can be used to avoid the repeated initial full refresh on displays with fast partial update
  309. // NOTE: garbage will result on fast partial update displays, if initial full update is omitted after power loss
  310. // reset_duration = 10 is default; a value of 2 may help with "clever" reset circuit of newer boards from Waveshare
  311. // pulldown_rst_mode true for alternate RST handling to avoid feeding 5V through RST pin
  312. void init(uint32_t serial_diag_bitrate, bool initial, uint16_t reset_duration = 10, bool pulldown_rst_mode = false)
  313. {
  314. epd2.init(serial_diag_bitrate, initial, reset_duration, pulldown_rst_mode);
  315. _using_partial_mode = false;
  316. _current_page = 0;
  317. setFullWindow();
  318. if (!_p_full_shadow_buffer) _p_full_shadow_buffer = (uint8_t*) malloc(uint32_t(GxEPD2_Type::HEIGHT) * uint32_t(GxEPD2_Type::WIDTH) / 8);
  319. //Serial.print("_p_full_shadow_buffer = 0x"); Serial.println(uint32_t(_p_full_shadow_buffer), HEX);
  320. }
  321. // init method with additional parameters:
  322. // SPIClass& spi: either SPI or alternate HW SPI channel
  323. // SPISettings spi_settings: e.g. for higher SPI speed selection
  324. void init(uint32_t serial_diag_bitrate, bool initial, uint16_t reset_duration, bool pulldown_rst_mode, SPIClass& spi, SPISettings spi_settings)
  325. {
  326. epd2.selectSPI(spi, spi_settings);
  327. epd2.init(serial_diag_bitrate, initial, reset_duration, pulldown_rst_mode);
  328. _using_partial_mode = false;
  329. _current_page = 0;
  330. setFullWindow();
  331. if (!_p_full_shadow_buffer) _p_full_shadow_buffer = (uint8_t*) malloc(uint32_t(GxEPD2_Type::HEIGHT) * uint32_t(GxEPD2_Type::WIDTH) / 8);
  332. //Serial.print("_p_full_shadow_buffer = 0x"); Serial.println(uint32_t(_p_full_shadow_buffer), HEX);
  333. }
  334. // release SPI and control pins
  335. void end()
  336. {
  337. if (_p_full_shadow_buffer) free(_p_full_shadow_buffer);
  338. epd2.end();
  339. }
  340. void fillScreen(uint16_t color) // 0x0 black, >0x0 white, to buffer
  341. {
  342. uint8_t data = (color == GxEPD_BLACK) ? 0x00 : 0xFF;
  343. for (uint32_t x = 0; x < sizeof(_buffer); x++)
  344. {
  345. _buffer[x] = data;
  346. }
  347. }
  348. // display buffer content to screen, useful for full screen buffer
  349. void display(bool partial_update_mode = false)
  350. {
  351. if (_uses_interleaved_fast_refresh && _p_full_shadow_buffer && partial_update_mode)
  352. {
  353. epd2.drawNative(_p_full_shadow_buffer, _buffer, 0, 0, GxEPD2_Type::WIDTH, _page_height);
  354. }
  355. else
  356. {
  357. if (partial_update_mode) epd2.writeImage(_buffer, 0, 0, GxEPD2_Type::WIDTH, _page_height);
  358. else epd2.writeImageForFullRefresh(_buffer, 0, 0, GxEPD2_Type::WIDTH, _page_height);
  359. epd2.refresh(partial_update_mode);
  360. if (epd2.hasFastPartialUpdate)
  361. {
  362. epd2.writeImageAgain(_buffer, 0, 0, GxEPD2_Type::WIDTH, _page_height);
  363. }
  364. }
  365. if (!partial_update_mode) epd2.powerOff();
  366. _writeToShadowBuffer(0, 0, GxEPD2_Type::WIDTH, _page_height);
  367. }
  368. // display part of buffer content to screen, useful for full screen buffer
  369. // displayWindow, use parameters according to actual rotation.
  370. // x and w should be multiple of 8, for rotation 0 or 2,
  371. // y and h should be multiple of 8, for rotation 1 or 3,
  372. // else window is increased as needed,
  373. // this is an addressing limitation of the e-paper controllers
  374. void displayWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  375. {
  376. x = gx_uint16_min(x, width());
  377. y = gx_uint16_min(y, height());
  378. w = gx_uint16_min(w, width() - x);
  379. h = gx_uint16_min(h, height() - y);
  380. _rotate(x, y, w, h);
  381. uint16_t y_part = _reverse ? HEIGHT - h - y : y;
  382. if (_uses_interleaved_fast_refresh && _p_full_shadow_buffer)
  383. {
  384. epd2.writeNativePart(_p_full_shadow_buffer, _buffer, x, y_part, GxEPD2_Type::WIDTH, _page_height, x, y_part, w, h);
  385. epd2.refresh(x, y_part, w, h);
  386. }
  387. else
  388. {
  389. epd2.writeImagePart(_buffer, x, y_part, GxEPD2_Type::WIDTH, _page_height, x, y_part, w, h);
  390. epd2.refresh(x, y_part, w, h);
  391. if (epd2.hasFastPartialUpdate)
  392. {
  393. epd2.writeImagePartAgain(_buffer, x, y_part, GxEPD2_Type::WIDTH, _page_height, x, y_part, w, h);
  394. }
  395. }
  396. _writePartToShadowBuffer(x, y_part, GxEPD2_Type::WIDTH, _page_height, x, y_part, w, h);
  397. }
  398. void setFullWindow()
  399. {
  400. _using_partial_mode = false;
  401. _pw_x = 0;
  402. _pw_y = 0;
  403. _pw_w = GxEPD2_Type::WIDTH;
  404. _pw_h = HEIGHT;
  405. }
  406. // setPartialWindow, use parameters according to actual rotation.
  407. // x and w should be multiple of 8, for rotation 0 or 2,
  408. // y and h should be multiple of 8, for rotation 1 or 3,
  409. // else window is increased as needed,
  410. // this is an addressing limitation of the e-paper controllers
  411. void setPartialWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  412. {
  413. _pw_x = gx_uint16_min(x, width());
  414. _pw_y = gx_uint16_min(y, height());
  415. _pw_w = gx_uint16_min(w, width() - _pw_x);
  416. _pw_h = gx_uint16_min(h, height() - _pw_y);
  417. _rotate(_pw_x, _pw_y, _pw_w, _pw_h);
  418. _using_partial_mode = true;
  419. // make _pw_x, _pw_w multiple of 8
  420. _pw_w += _pw_x % 8;
  421. if (_pw_w % 8 > 0) _pw_w += 8 - _pw_w % 8;
  422. _pw_x -= _pw_x % 8;
  423. if (_reverse) _pw_y = HEIGHT - _pw_h - _pw_y;
  424. }
  425. void firstPage()
  426. {
  427. fillScreen(GxEPD_WHITE);
  428. _current_page = 0;
  429. _second_phase = false;
  430. }
  431. bool nextPage()
  432. {
  433. if (1 == _pages)
  434. {
  435. if (_using_partial_mode)
  436. {
  437. if (_uses_interleaved_fast_refresh && _p_full_shadow_buffer)
  438. {
  439. epd2.writeNativePart(_p_full_shadow_buffer, _buffer, 0, 0, _pw_w, _pw_h, _pw_x, _pw_y, _pw_w, _pw_h);
  440. epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h);
  441. }
  442. else
  443. {
  444. epd2.writeImage(_buffer, _pw_x, _pw_y, _pw_w, _pw_h);
  445. epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h);
  446. if (epd2.hasFastPartialUpdate)
  447. {
  448. epd2.writeImageAgain(_buffer, _pw_x, _pw_y, _pw_w, _pw_h);
  449. }
  450. }
  451. _writeToShadowBuffer(_pw_x, _pw_y, _pw_w, _pw_h);
  452. }
  453. else // full update
  454. {
  455. epd2.writeImageForFullRefresh(_buffer, 0, 0, GxEPD2_Type::WIDTH, HEIGHT);
  456. epd2.refresh(false);
  457. if (epd2.hasFastPartialUpdate)
  458. {
  459. epd2.writeImageAgain(_buffer, 0, 0, GxEPD2_Type::WIDTH, HEIGHT);
  460. //epd2.refresh(true); // not needed
  461. }
  462. epd2.powerOff();
  463. _writeToShadowBuffer(0, 0, GxEPD2_Type::WIDTH, HEIGHT);
  464. }
  465. return false;
  466. }
  467. uint16_t page_ys = _current_page * _page_height;
  468. if (_using_partial_mode)
  469. {
  470. //Serial.print(" nextPage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(_pw_y); Serial.print(", ");
  471. //Serial.print(_pw_w); Serial.print(", "); Serial.print(_pw_h); Serial.print(") P"); Serial.println(_current_page);
  472. uint16_t page_ye = _current_page < int16_t(_pages - 1) ? page_ys + _page_height : HEIGHT;
  473. uint16_t dest_ys = _pw_y + page_ys; // transposed
  474. uint16_t dest_ye = gx_uint16_min(_pw_y + _pw_h, _pw_y + page_ye);
  475. if (dest_ye > dest_ys)
  476. {
  477. //Serial.print("writeImage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(dest_ys); Serial.print(", ");
  478. //Serial.print(_pw_w); Serial.print(", "); Serial.print(dest_ye - dest_ys); Serial.println(")");
  479. if (_uses_interleaved_fast_refresh && _p_full_shadow_buffer)
  480. {
  481. if (!_second_phase) epd2.writeNativePart(_p_full_shadow_buffer, _buffer, 0, 0, _pw_w, dest_ye - dest_ys, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  482. }
  483. else
  484. {
  485. if (!_second_phase) epd2.writeImage(_buffer, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  486. else epd2.writeImageAgain(_buffer, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  487. }
  488. _writeToShadowBuffer(_pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  489. }
  490. else
  491. {
  492. //Serial.print("writeImage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(dest_ys); Serial.print(", ");
  493. //Serial.print(_pw_w); Serial.print(", "); Serial.print(dest_ye - dest_ys); Serial.print(") skipped ");
  494. //Serial.print(dest_ys); Serial.print(".."); Serial.println(dest_ye);
  495. }
  496. _current_page++;
  497. if (_current_page == int16_t(_pages))
  498. {
  499. _current_page = 0;
  500. if (!_second_phase)
  501. {
  502. epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h);
  503. if (epd2.hasFastPartialUpdate)
  504. {
  505. _second_phase = true;
  506. fillScreen(GxEPD_WHITE);
  507. return true;
  508. }
  509. }
  510. return false;
  511. }
  512. fillScreen(GxEPD_WHITE);
  513. return true;
  514. }
  515. else // full update
  516. {
  517. if (!_second_phase) epd2.writeImageForFullRefresh(_buffer, 0, page_ys, GxEPD2_Type::WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  518. else epd2.writeImageAgain(_buffer, 0, page_ys, GxEPD2_Type::WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  519. if (!_second_phase) _writeToShadowBuffer(0, page_ys, GxEPD2_Type::WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  520. _current_page++;
  521. if (_current_page == int16_t(_pages))
  522. {
  523. _current_page = 0;
  524. if (epd2.hasFastPartialUpdate)
  525. {
  526. if (!_second_phase)
  527. {
  528. epd2.refresh(false); // full update after first phase
  529. _second_phase = true;
  530. fillScreen(GxEPD_WHITE);
  531. return true;
  532. }
  533. //else epd2.refresh(true); // partial update after second phase
  534. } else epd2.refresh(false); // full update after only phase
  535. epd2.powerOff();
  536. return false;
  537. }
  538. fillScreen(GxEPD_WHITE);
  539. return true;
  540. }
  541. }
  542. bool nextPageToPrevious()
  543. {
  544. if (1 == _pages)
  545. {
  546. if (_using_partial_mode)
  547. {
  548. epd2.writeImageToPrevious(_buffer, _pw_x, _pw_y, _pw_w, _pw_h);
  549. }
  550. else // full update
  551. {
  552. epd2.writeImageToPrevious(_buffer, 0, 0, GxEPD2_Type::WIDTH, HEIGHT);
  553. }
  554. return false;
  555. }
  556. uint16_t page_ys = _current_page * _page_height;
  557. if (_using_partial_mode)
  558. {
  559. //Serial.print(" nextPage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(_pw_y); Serial.print(", ");
  560. //Serial.print(_pw_w); Serial.print(", "); Serial.print(_pw_h); Serial.print(") P"); Serial.println(_current_page);
  561. uint16_t page_ye = _current_page < int16_t(_pages - 1) ? page_ys + _page_height : HEIGHT;
  562. uint16_t dest_ys = _pw_y + page_ys; // transposed
  563. uint16_t dest_ye = gx_uint16_min(_pw_y + _pw_h, _pw_y + page_ye);
  564. if (dest_ye > dest_ys)
  565. {
  566. //Serial.print("writeImage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(dest_ys); Serial.print(", ");
  567. //Serial.print(_pw_w); Serial.print(", "); Serial.print(dest_ye - dest_ys); Serial.println(")");
  568. epd2.writeImageToPrevious(_buffer, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  569. }
  570. else
  571. {
  572. //Serial.print("writeImage("); Serial.print(_pw_x); Serial.print(", "); Serial.print(dest_ys); Serial.print(", ");
  573. //Serial.print(_pw_w); Serial.print(", "); Serial.print(dest_ye - dest_ys); Serial.print(") skipped ");
  574. //Serial.print(dest_ys); Serial.print(".."); Serial.println(dest_ye);
  575. }
  576. _current_page++;
  577. if (_current_page == int16_t(_pages))
  578. {
  579. return false;
  580. }
  581. fillScreen(GxEPD_WHITE);
  582. return true;
  583. }
  584. else // full update
  585. {
  586. epd2.writeImageToPrevious(_buffer, 0, page_ys, GxEPD2_Type::WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  587. _current_page++;
  588. if (_current_page == int16_t(_pages))
  589. {
  590. return false;
  591. }
  592. fillScreen(GxEPD_WHITE);
  593. return true;
  594. }
  595. }
  596. // GxEPD style paged drawing; drawCallback() is called as many times as needed
  597. void drawPaged(void (*drawCallback)(const void*), const void* pv)
  598. {
  599. if (1 == _pages)
  600. {
  601. fillScreen(GxEPD_WHITE);
  602. drawCallback(pv);
  603. if (_using_partial_mode)
  604. {
  605. if (_uses_interleaved_fast_refresh && _p_full_shadow_buffer)
  606. {
  607. epd2.writeNativePart(_p_full_shadow_buffer, _buffer, 0, 0, _pw_w, _pw_h, _pw_x, _pw_y, _pw_w, _pw_h);
  608. epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h);
  609. }
  610. else
  611. {
  612. epd2.writeImage(_buffer, _pw_x, _pw_y, _pw_w, _pw_h);
  613. epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h);
  614. if (epd2.hasFastPartialUpdate)
  615. {
  616. epd2.writeImageAgain(_buffer, _pw_x, _pw_y, _pw_w, _pw_h);
  617. //epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h); // not needed
  618. }
  619. }
  620. _writeToShadowBuffer(_pw_x, _pw_y, _pw_w, _pw_h);
  621. }
  622. else // full update
  623. {
  624. epd2.writeImageForFullRefresh(_buffer, 0, 0, GxEPD2_Type::WIDTH, HEIGHT);
  625. epd2.refresh(false);
  626. if (epd2.hasFastPartialUpdate)
  627. {
  628. epd2.writeImageAgain(_buffer, 0, 0, GxEPD2_Type::WIDTH, HEIGHT);
  629. //epd2.refresh(true); // not needed
  630. epd2.powerOff();
  631. }
  632. _writeToShadowBuffer(0, 0, GxEPD2_Type::WIDTH, HEIGHT);
  633. }
  634. return;
  635. }
  636. if (_using_partial_mode)
  637. {
  638. for (uint16_t phase = 1; phase <= 2; phase++)
  639. {
  640. for (_current_page = 0; _current_page < _pages; _current_page++)
  641. {
  642. uint16_t page_ys = _current_page * _page_height;
  643. uint16_t page_ye = _current_page < (_pages - 1) ? page_ys + _page_height : HEIGHT;
  644. uint16_t dest_ys = _pw_y + page_ys; // transposed
  645. uint16_t dest_ye = gx_uint16_min(_pw_y + _pw_h, _pw_y + page_ye);
  646. if (dest_ye > dest_ys)
  647. {
  648. fillScreen(GxEPD_WHITE);
  649. drawCallback(pv);
  650. if (_uses_interleaved_fast_refresh && _p_full_shadow_buffer)
  651. {
  652. if (phase == 1) epd2.writeNativePart(_p_full_shadow_buffer, _buffer, 0, 0, _pw_w, dest_ye - dest_ys, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  653. if (phase == 1) _writePartToShadowBuffer(0, 0, _pw_w, dest_ye - dest_ys, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  654. }
  655. else
  656. {
  657. if (phase == 1) epd2.writeImage(_buffer, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  658. else epd2.writeImageAgain(_buffer, _pw_x, dest_ys, _pw_w, dest_ye - dest_ys);
  659. }
  660. }
  661. }
  662. epd2.refresh(_pw_x, _pw_y, _pw_w, _pw_h);
  663. if (!epd2.hasFastPartialUpdate) break;
  664. // else make both controller buffers have equal content
  665. }
  666. }
  667. else // full update
  668. {
  669. for (_current_page = 0; _current_page < _pages; _current_page++)
  670. {
  671. uint16_t page_ys = _current_page * _page_height;
  672. fillScreen(GxEPD_WHITE);
  673. drawCallback(pv);
  674. epd2.writeImageForFullRefresh(_buffer, 0, page_ys, GxEPD2_Type::WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  675. _writeToShadowBuffer(0, page_ys, GxEPD2_Type::WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  676. }
  677. epd2.refresh(false); // full update after first phase
  678. if (epd2.hasFastPartialUpdate)
  679. {
  680. // make both controller buffers have equal content
  681. for (_current_page = 0; _current_page < _pages; _current_page++)
  682. {
  683. uint16_t page_ys = _current_page * _page_height;
  684. fillScreen(GxEPD_WHITE);
  685. drawCallback(pv);
  686. epd2.writeImageAgain(_buffer, 0, page_ys, GxEPD2_Type::WIDTH, gx_uint16_min(_page_height, HEIGHT - page_ys));
  687. }
  688. //epd2.refresh(true); // partial update after second phase // not needed
  689. }
  690. epd2.powerOff();
  691. }
  692. _current_page = 0;
  693. }
  694. void drawInvertedBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color)
  695. {
  696. // taken from Adafruit_GFX.cpp, modified
  697. int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
  698. uint8_t byte = 0;
  699. for (int16_t j = 0; j < h; j++)
  700. {
  701. for (int16_t i = 0; i < w; i++ )
  702. {
  703. if (i & 7) byte <<= 1;
  704. else
  705. {
  706. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  707. byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
  708. #else
  709. byte = bitmap[j * byteWidth + i / 8];
  710. #endif
  711. }
  712. if (!(byte & 0x80))
  713. {
  714. drawPixel(x + i, y + j, color);
  715. }
  716. }
  717. }
  718. }
  719. // Support for Bitmaps (Sprites) to Controller Buffer and to Screen
  720. void clearScreen(uint8_t value = 0xFF) // init controller memory and screen (default white)
  721. {
  722. epd2.clearScreen(value);
  723. _clearShadowBuffer();
  724. }
  725. void writeScreenBuffer(uint8_t value = 0xFF) // init controller memory (default white)
  726. {
  727. epd2.writeScreenBuffer(value);
  728. _clearShadowBuffer();
  729. }
  730. // write to controller memory, without screen refresh; x and w should be multiple of 8
  731. 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)
  732. {
  733. epd2.writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  734. }
  735. void writeImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  736. int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false)
  737. {
  738. epd2.writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  739. }
  740. 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)
  741. {
  742. epd2.writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  743. }
  744. void writeImage(const uint8_t* black, const uint8_t* color, int16_t x, int16_t y, int16_t w, int16_t h)
  745. {
  746. epd2.writeImage(black, color, x, y, w, h, false, false, false);
  747. }
  748. 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,
  749. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  750. {
  751. epd2.writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  752. }
  753. 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,
  754. int16_t x, int16_t y, int16_t w, int16_t h)
  755. {
  756. epd2.writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, false, false, false);
  757. }
  758. // write sprite of native data to controller memory, without screen refresh; x and w should be multiple of 8
  759. 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)
  760. {
  761. epd2.writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  762. }
  763. // write to controller memory, with screen refresh; x and w should be multiple of 8
  764. 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)
  765. {
  766. epd2.drawImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  767. }
  768. void drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  769. int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false)
  770. {
  771. epd2.drawImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  772. }
  773. 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)
  774. {
  775. epd2.drawImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  776. }
  777. void drawImage(const uint8_t* black, const uint8_t* color, int16_t x, int16_t y, int16_t w, int16_t h)
  778. {
  779. epd2.drawImage(black, color, x, y, w, h, false, false, false);
  780. }
  781. 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,
  782. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  783. {
  784. epd2.drawImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  785. }
  786. 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,
  787. int16_t x, int16_t y, int16_t w, int16_t h)
  788. {
  789. epd2.drawImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, false, false, false);
  790. }
  791. // write sprite of native data to controller memory, with screen refresh; x and w should be multiple of 8
  792. 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)
  793. {
  794. epd2.drawNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  795. }
  796. void refresh(bool partial_update_mode = false) // screen refresh from controller memory to full screen
  797. {
  798. epd2.refresh(partial_update_mode);
  799. if (!partial_update_mode) epd2.powerOff();
  800. }
  801. void refresh(int16_t x, int16_t y, int16_t w, int16_t h) // screen refresh from controller memory, partial screen
  802. {
  803. epd2.refresh(x, y, w, h);
  804. }
  805. // turns off generation of panel driving voltages, avoids screen fading over time
  806. void powerOff()
  807. {
  808. epd2.powerOff();
  809. }
  810. // turns powerOff() and sets controller to deep sleep for minimum power use, ONLY if wakeable by RST (rst >= 0)
  811. void hibernate()
  812. {
  813. epd2.hibernate();
  814. }
  815. private:
  816. template <typename T> static inline void
  817. _swap_(T & a, T & b)
  818. {
  819. T t = a;
  820. a = b;
  821. b = t;
  822. };
  823. static inline uint16_t gx_uint16_min(uint16_t a, uint16_t b)
  824. {
  825. return (a < b ? a : b);
  826. };
  827. static inline uint16_t gx_uint16_max(uint16_t a, uint16_t b)
  828. {
  829. return (a > b ? a : b);
  830. };
  831. void _rotate(uint16_t& x, uint16_t& y, uint16_t& w, uint16_t& h)
  832. {
  833. switch (getRotation())
  834. {
  835. case 1:
  836. _swap_(x, y);
  837. _swap_(w, h);
  838. x = WIDTH - x - w;
  839. break;
  840. case 2:
  841. x = WIDTH - x - w;
  842. y = HEIGHT - y - h;
  843. break;
  844. case 3:
  845. _swap_(x, y);
  846. _swap_(w, h);
  847. y = HEIGHT - y - h;
  848. break;
  849. }
  850. }
  851. void _clearShadowBuffer()
  852. {
  853. //Serial.println("_clearShadowBuffer()");
  854. if (_p_full_shadow_buffer)
  855. {
  856. memset(_p_full_shadow_buffer, 0xFF, GxEPD2_Type::WIDTH * GxEPD2_Type::HEIGHT / 8);
  857. }
  858. }
  859. void _writeToShadowBuffer(int16_t x, int16_t y, int16_t w, int16_t h)
  860. {
  861. //Serial.print("_writeToShadowBuffer("); Serial.print(x); Serial.print(", "); Serial.print(y); Serial.print(", ");
  862. //Serial.print(w); Serial.print(", "); Serial.print(h); Serial.println(")");
  863. if (_p_full_shadow_buffer)
  864. {
  865. uint32_t idx = x / 8 + y * (GxEPD2_Type::WIDTH / 8); // destination at x,y
  866. if (w == GxEPD2_Type::WIDTH) memcpy(_p_full_shadow_buffer + idx, _buffer, w * h / 8); // origin at 0,0
  867. else
  868. {
  869. for (int16_t i = 0; i < h; i++)
  870. {
  871. uint32_t ids = i * (w / 8); // origin at 0,0
  872. memcpy(_p_full_shadow_buffer + idx, _buffer + ids, w / 8);
  873. idx += (GxEPD2_Type::WIDTH / 8);
  874. }
  875. }
  876. }
  877. }
  878. void _writePartToShadowBuffer(int16_t x_part, int16_t y_part, int16_t w_part, int16_t h_part,
  879. int16_t x, int16_t y, int16_t w, int16_t h)
  880. {
  881. //Serial.print("_writePartToShadowBuffer("); Serial.print(x); Serial.print(", "); Serial.print(y); Serial.print(", ");
  882. //Serial.print(w); Serial.print(", "); Serial.print(h); Serial.println(")");
  883. if (_p_full_shadow_buffer)
  884. {
  885. for (int16_t i = 0; i < h; i++)
  886. {
  887. uint32_t ids = x_part / 8 + (y_part + i) * (w_part / 8);
  888. uint32_t idx = x / 8 + (y + i) * (GxEPD2_Type::WIDTH / 8);
  889. memcpy(_p_full_shadow_buffer + idx, _buffer + ids, w / 8);
  890. }
  891. }
  892. }
  893. private:
  894. uint8_t _buffer[(GxEPD2_Type::WIDTH / 8) * page_height];
  895. bool _using_partial_mode, _second_phase, _mirror, _reverse;
  896. uint16_t _width_bytes, _pixel_bytes;
  897. int16_t _current_page;
  898. uint16_t _pages, _page_height;
  899. uint16_t _pw_x, _pw_y, _pw_w, _pw_h;
  900. bool _uses_interleaved_fast_refresh;
  901. uint8_t* _p_full_shadow_buffer;
  902. };
  903. #endif