GxEPD2_213_Z19c.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. // Controller: UC8151D : https://v4.cecdn.yun300.cn/100001_1909185148/UC8151D.pdf
  6. //
  7. // Author: Jean-Marc Zingg
  8. //
  9. // Version: see library.properties
  10. //
  11. // Library: https://github.com/ZinggJM/GxEPD2
  12. #include "GxEPD2_213_Z19c.h"
  13. GxEPD2_213_Z19c::GxEPD2_213_Z19c(int16_t cs, int16_t dc, int16_t rst, int16_t busy) :
  14. GxEPD2_EPD(cs, dc, rst, busy, LOW, 20000000, WIDTH, HEIGHT, panel, hasColor, hasPartialUpdate, hasFastPartialUpdate)
  15. {
  16. }
  17. void GxEPD2_213_Z19c::clearScreen(uint8_t value)
  18. {
  19. clearScreen(value, 0xFF);
  20. }
  21. void GxEPD2_213_Z19c::clearScreen(uint8_t black_value, uint8_t color_value)
  22. {
  23. _initial_write = false; // initial full screen buffer clean done
  24. _Init_Part();
  25. _writeCommand(0x91); // partial in
  26. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  27. _writeCommand(0x10);
  28. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  29. {
  30. _writeData(black_value);
  31. }
  32. _writeCommand(0x13);
  33. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  34. {
  35. _writeData(color_value);
  36. }
  37. _Update_Part();
  38. _writeCommand(0x92); // partial out
  39. }
  40. void GxEPD2_213_Z19c::writeScreenBuffer(uint8_t value)
  41. {
  42. writeScreenBuffer(value, 0xFF);
  43. }
  44. void GxEPD2_213_Z19c::writeScreenBuffer(uint8_t black_value, uint8_t color_value)
  45. {
  46. _initial_write = false; // initial full screen buffer clean done
  47. _Init_Part();
  48. _writeCommand(0x91); // partial in
  49. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  50. _writeCommand(0x10);
  51. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  52. {
  53. _writeData(black_value);
  54. }
  55. _writeCommand(0x13);
  56. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  57. {
  58. _writeData(color_value);
  59. }
  60. _writeCommand(0x92); // partial out
  61. }
  62. void GxEPD2_213_Z19c::writeImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  63. {
  64. writeImage(bitmap, NULL, x, y, w, h, invert, mirror_y, pgm);
  65. }
  66. void GxEPD2_213_Z19c::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)
  67. {
  68. _writeImage(0x10, black, x, y, w, h, invert, mirror_y, pgm);
  69. _writeImage(0x13, color, x, y, w, h, invert, mirror_y, pgm);
  70. }
  71. void GxEPD2_213_Z19c::_writeImage(uint8_t command, const uint8_t* bitmap, int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  72. {
  73. if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
  74. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  75. int16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
  76. x -= x % 8; // byte boundary
  77. w = wb * 8; // byte boundary
  78. int16_t x1 = x < 0 ? 0 : x; // limit
  79. int16_t y1 = y < 0 ? 0 : y; // limit
  80. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  81. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  82. int16_t dx = x1 - x;
  83. int16_t dy = y1 - y;
  84. w1 -= dx;
  85. h1 -= dy;
  86. if ((w1 <= 0) || (h1 <= 0)) return;
  87. _Init_Part();
  88. _writeCommand(0x91); // partial in
  89. _setPartialRamArea(x1, y1, w1, h1);
  90. _writeCommand(command);
  91. for (int16_t i = 0; i < h1; i++)
  92. {
  93. for (int16_t j = 0; j < w1 / 8; j++)
  94. {
  95. uint8_t data = 0xFF;
  96. if (bitmap)
  97. {
  98. // use wb, h of bitmap for index!
  99. int16_t idx = mirror_y ? j + dx / 8 + ((h - 1 - (i + dy))) * wb : j + dx / 8 + (i + dy) * wb;
  100. if (pgm)
  101. {
  102. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  103. data = pgm_read_byte(&bitmap[idx]);
  104. #else
  105. data = bitmap[idx];
  106. #endif
  107. }
  108. else
  109. {
  110. data = bitmap[idx];
  111. }
  112. if (invert) data = ~data;
  113. }
  114. _writeData(data);
  115. }
  116. }
  117. _writeCommand(0x92); // partial out
  118. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  119. }
  120. void GxEPD2_213_Z19c::writeImagePrevious(const uint8_t* black, int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  121. {
  122. _writeImage(0x10, black, x, y, w, h, invert, mirror_y, pgm);
  123. }
  124. void GxEPD2_213_Z19c::writeImageNew(const uint8_t* black, int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  125. {
  126. _writeImage(0x13, black, x, y, w, h, invert, mirror_y, pgm);
  127. }
  128. void GxEPD2_213_Z19c::writeImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  129. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  130. {
  131. writeImagePart(bitmap, NULL, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  132. }
  133. void GxEPD2_213_Z19c::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,
  134. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  135. {
  136. _writeImagePart(0x10, black, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  137. _writeImagePart(0x13, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  138. }
  139. void GxEPD2_213_Z19c::_writeImagePart(uint8_t command, const uint8_t* bitmap, int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  140. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  141. {
  142. if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
  143. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  144. if ((w_bitmap < 0) || (h_bitmap < 0) || (w < 0) || (h < 0)) return;
  145. if ((x_part < 0) || (x_part >= w_bitmap)) return;
  146. if ((y_part < 0) || (y_part >= h_bitmap)) return;
  147. int16_t wb_bitmap = (w_bitmap + 7) / 8; // width bytes, bitmaps are padded
  148. x_part -= x_part % 8; // byte boundary
  149. w = w_bitmap - x_part < w ? w_bitmap - x_part : w; // limit
  150. h = h_bitmap - y_part < h ? h_bitmap - y_part : h; // limit
  151. x -= x % 8; // byte boundary
  152. w = 8 * ((w + 7) / 8); // byte boundary, bitmaps are padded
  153. int16_t x1 = x < 0 ? 0 : x; // limit
  154. int16_t y1 = y < 0 ? 0 : y; // limit
  155. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  156. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  157. int16_t dx = x1 - x;
  158. int16_t dy = y1 - y;
  159. w1 -= dx;
  160. h1 -= dy;
  161. if ((w1 <= 0) || (h1 <= 0)) return;
  162. if (!_using_partial_mode) _Init_Part();
  163. _writeCommand(0x91); // partial in
  164. _setPartialRamArea(x1, y1, w1, h1);
  165. _writeCommand(command);
  166. for (int16_t i = 0; i < h1; i++)
  167. {
  168. for (int16_t j = 0; j < w1 / 8; j++)
  169. {
  170. uint8_t data;
  171. // use wb_bitmap, h_bitmap of bitmap for index!
  172. int16_t idx = mirror_y ? x_part / 8 + j + dx / 8 + ((h_bitmap - 1 - (y_part + i + dy))) * wb_bitmap : x_part / 8 + j + dx / 8 + (y_part + i + dy) * wb_bitmap;
  173. if (pgm)
  174. {
  175. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  176. data = pgm_read_byte(&bitmap[idx]);
  177. #else
  178. data = bitmap[idx];
  179. #endif
  180. }
  181. else
  182. {
  183. data = bitmap[idx];
  184. }
  185. if (invert) data = ~data;
  186. _writeData(data);
  187. }
  188. }
  189. _writeCommand(0x92); // partial out
  190. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  191. }
  192. void GxEPD2_213_Z19c::writeImagePartPrevious(const uint8_t* black, int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  193. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  194. {
  195. _writeImagePart(0x10, black, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  196. }
  197. void GxEPD2_213_Z19c::writeImagePartNew(const uint8_t* black, int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  198. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  199. {
  200. _writeImagePart(0x13, black, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  201. }
  202. void GxEPD2_213_Z19c::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)
  203. {
  204. if (data1)
  205. {
  206. writeImage(data1, x, y, w, h, invert, mirror_y, pgm);
  207. }
  208. }
  209. void GxEPD2_213_Z19c::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  210. {
  211. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  212. refresh(x, y, w, h);
  213. }
  214. void GxEPD2_213_Z19c::drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  215. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  216. {
  217. writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  218. refresh(x, y, w, h);
  219. }
  220. void GxEPD2_213_Z19c::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)
  221. {
  222. writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  223. refresh(x, y, w, h);
  224. }
  225. void GxEPD2_213_Z19c::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,
  226. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  227. {
  228. writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  229. refresh(x, y, w, h);
  230. }
  231. void GxEPD2_213_Z19c::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)
  232. {
  233. writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  234. refresh(x, y, w, h);
  235. }
  236. void GxEPD2_213_Z19c::refresh(bool partial_update_mode)
  237. {
  238. if (partial_update_mode) refresh(0, 0, WIDTH, HEIGHT);
  239. else _Update_Full();
  240. }
  241. void GxEPD2_213_Z19c::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
  242. {
  243. // intersection with screen
  244. int16_t w1 = x < 0 ? w + x : w; // reduce
  245. int16_t h1 = y < 0 ? h + y : h; // reduce
  246. int16_t x1 = x < 0 ? 0 : x; // limit
  247. int16_t y1 = y < 0 ? 0 : y; // limit
  248. w1 = x1 + w1 < int16_t(WIDTH) ? w1 : int16_t(WIDTH) - x1; // limit
  249. h1 = y1 + h1 < int16_t(HEIGHT) ? h1 : int16_t(HEIGHT) - y1; // limit
  250. if ((w1 <= 0) || (h1 <= 0)) return;
  251. // make x1, w1 multiple of 8
  252. w1 += x1 % 8;
  253. if (w1 % 8 > 0) w1 += 8 - w1 % 8;
  254. x1 -= x1 % 8;
  255. _Init_Part();
  256. if (usePartialUpdateWindow) _writeCommand(0x91); // partial in
  257. _setPartialRamArea(x1, y1, w1, h1);
  258. _Update_Part();
  259. if (usePartialUpdateWindow) _writeCommand(0x92); // partial out
  260. }
  261. void GxEPD2_213_Z19c::powerOff()
  262. {
  263. _PowerOff();
  264. }
  265. void GxEPD2_213_Z19c::hibernate()
  266. {
  267. _PowerOff();
  268. if (_rst >= 0)
  269. {
  270. _writeCommand(0x07); // deep sleep
  271. _writeData(0xA5); // check code
  272. _hibernating = true;
  273. }
  274. }
  275. void GxEPD2_213_Z19c::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  276. {
  277. uint16_t xe = (x + w - 1) | 0x0007; // byte boundary inclusive (last byte)
  278. uint16_t ye = y + h - 1;
  279. x &= 0xFFF8; // byte boundary
  280. _writeCommand(0x90); // partial window
  281. //_writeData(x / 256);
  282. _writeData(x % 256);
  283. //_writeData(xe / 256);
  284. _writeData(xe % 256);
  285. _writeData(y / 256);
  286. _writeData(y % 256);
  287. _writeData(ye / 256);
  288. _writeData(ye % 256);
  289. _writeData(0x01); // don't see any difference
  290. //_writeData(0x00); // don't see any difference
  291. }
  292. void GxEPD2_213_Z19c::_PowerOn()
  293. {
  294. if (!_power_is_on)
  295. {
  296. _writeCommand(0x04);
  297. _waitWhileBusy("_PowerOn", power_on_time);
  298. }
  299. _power_is_on = true;
  300. }
  301. void GxEPD2_213_Z19c::_PowerOff()
  302. {
  303. _writeCommand(0x02); // power off
  304. _waitWhileBusy("_PowerOff", power_off_time);
  305. _power_is_on = false;
  306. }
  307. void GxEPD2_213_Z19c::_InitDisplay()
  308. {
  309. if (_hibernating) _reset();
  310. _writeCommand(0x00); // panel setting
  311. _writeData(0x8f); // LUT from OTP
  312. _writeCommand(0x61); // resolution setting
  313. _writeData (WIDTH);
  314. _writeData (HEIGHT >> 8);
  315. _writeData (HEIGHT & 0xFF);
  316. _writeCommand(0x50); // VCOM AND DATA INTERVAL SETTING
  317. _writeData(0x77); // WBmode:VBDF 17|D7 VBDW 97 VBDB 57 WBRmode:VBDF F7 VBDW 77 VBDB 37 VBDR B7
  318. }
  319. void GxEPD2_213_Z19c::_Init_Full()
  320. {
  321. _InitDisplay();
  322. _PowerOn();
  323. }
  324. void GxEPD2_213_Z19c::_Init_Part()
  325. {
  326. _InitDisplay();
  327. _PowerOn();
  328. }
  329. void GxEPD2_213_Z19c::_Update_Full()
  330. {
  331. _writeCommand(0x12); //display refresh
  332. _waitWhileBusy("_Update_Full", full_refresh_time);
  333. }
  334. void GxEPD2_213_Z19c::_Update_Part()
  335. {
  336. _writeCommand(0x12); //display refresh
  337. _waitWhileBusy("_Update_Part", partial_refresh_time);
  338. }
  339. const unsigned char GxEPD2_213_Z19c::lut_20_vcomDC_partial[] =
  340. {
  341. 0x00, 0x1F, 0x01, 0x00, 0x00, 0x01,
  342. };
  343. const unsigned char GxEPD2_213_Z19c::lut_21_ww_partial[] =
  344. {
  345. 0x00, 0x1F, 0x01, 0x00, 0x00, 0x01,
  346. };
  347. const unsigned char GxEPD2_213_Z19c::lut_22_bw_partial[] PROGMEM =
  348. {
  349. 0x80, 0x1F, 0x01, 0x00, 0x00, 0x01,
  350. };
  351. const unsigned char GxEPD2_213_Z19c::lut_23_wb_partial[] PROGMEM =
  352. {
  353. 0x40, 0x1F, 0x01, 0x00, 0x00, 0x01,
  354. };
  355. const unsigned char GxEPD2_213_Z19c::lut_24_bb_partial[] PROGMEM =
  356. {
  357. 0x00, 0x1F, 0x01, 0x00, 0x00, 0x01,
  358. };
  359. void GxEPD2_213_Z19c::refresh_bw(int16_t x, int16_t y, int16_t w, int16_t h)
  360. {
  361. x -= x % 8; // byte boundary
  362. w -= w % 8; // byte boundary
  363. int16_t x1 = x < 0 ? 0 : x; // limit
  364. int16_t y1 = y < 0 ? 0 : y; // limit
  365. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  366. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  367. w1 -= x1 - x;
  368. h1 -= y1 - y;
  369. _Init_Part();
  370. _writeCommand(0x20);
  371. _writeDataPGM(lut_20_vcomDC_partial, sizeof(lut_20_vcomDC_partial), 44 - sizeof(lut_20_vcomDC_partial));
  372. _writeCommand(0x21);
  373. _writeDataPGM(lut_21_ww_partial, sizeof(lut_21_ww_partial), 42 - sizeof(lut_21_ww_partial));
  374. _writeCommand(0x22);
  375. _writeDataPGM(lut_22_bw_partial, sizeof(lut_22_bw_partial), 42 - sizeof(lut_22_bw_partial));
  376. _writeCommand(0x23);
  377. _writeDataPGM(lut_23_wb_partial, sizeof(lut_23_wb_partial), 42 - sizeof(lut_23_wb_partial));
  378. _writeCommand(0x24);
  379. _writeDataPGM(lut_24_bb_partial, sizeof(lut_24_bb_partial), 42 - sizeof(lut_24_bb_partial));
  380. _writeCommand(0x00); // panel setting
  381. _writeData(0xbf); // LUT from registers, b/w
  382. _writeCommand(0x50); // VCOM AND DATA INTERVAL SETTING
  383. _writeData(0xf7); // WBmode:VBDF 17|D7 VBDW 97 VBDB 57 WBRmode:VBDF F7 VBDW 77 VBDB 37 VBDR B7
  384. _writeCommand(0x91); // partial in
  385. _setPartialRamArea(x1, y1, w1, h1);
  386. _Update_Part();
  387. _writeCommand(0x92); // partial out
  388. _writeCommand(0x00); // panel setting
  389. _writeData(0x8f); // LUT from OTP
  390. }