GxEPD2_4C.h 21 KB

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