GxEPD2_4C.h 20 KB

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