GxEPD2_7C.h 20 KB

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