GxEPD2_213_Z19c.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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(int8_t cs, int8_t dc, int8_t rst, int8_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. if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
  69. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  70. int16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
  71. x -= x % 8; // byte boundary
  72. w = wb * 8; // byte boundary
  73. int16_t x1 = x < 0 ? 0 : x; // limit
  74. int16_t y1 = y < 0 ? 0 : y; // limit
  75. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  76. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  77. int16_t dx = x1 - x;
  78. int16_t dy = y1 - y;
  79. w1 -= dx;
  80. h1 -= dy;
  81. if ((w1 <= 0) || (h1 <= 0)) return;
  82. _Init_Part();
  83. _writeCommand(0x91); // partial in
  84. _setPartialRamArea(x1, y1, w1, h1);
  85. _writeCommand(0x10);
  86. for (int16_t i = 0; i < h1; i++)
  87. {
  88. for (int16_t j = 0; j < w1 / 8; j++)
  89. {
  90. uint8_t data = 0xFF;
  91. if (black)
  92. {
  93. // use wb, h of bitmap for index!
  94. int16_t idx = mirror_y ? j + dx / 8 + ((h - 1 - (i + dy))) * wb : j + dx / 8 + (i + dy) * wb;
  95. if (pgm)
  96. {
  97. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  98. data = pgm_read_byte(&black[idx]);
  99. #else
  100. data = black[idx];
  101. #endif
  102. }
  103. else
  104. {
  105. data = black[idx];
  106. }
  107. if (invert) data = ~data;
  108. }
  109. _writeData(data);
  110. }
  111. }
  112. _writeCommand(0x13);
  113. for (int16_t i = 0; i < h1; i++)
  114. {
  115. for (int16_t j = 0; j < w1 / 8; j++)
  116. {
  117. uint8_t data = 0xFF;
  118. if (color)
  119. {
  120. // use wb, h of bitmap for index!
  121. int16_t idx = mirror_y ? j + dx / 8 + ((h - 1 - (i + dy))) * wb : j + dx / 8 + (i + dy) * wb;
  122. if (pgm)
  123. {
  124. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  125. data = pgm_read_byte(&color[idx]);
  126. #else
  127. data = color[idx];
  128. #endif
  129. }
  130. else
  131. {
  132. data = color[idx];
  133. }
  134. if (invert) data = ~data;
  135. }
  136. _writeData(data);
  137. }
  138. }
  139. _writeCommand(0x92); // partial out
  140. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  141. }
  142. 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,
  143. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  144. {
  145. writeImagePart(bitmap, NULL, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  146. }
  147. 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,
  148. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  149. {
  150. if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
  151. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  152. if ((w_bitmap < 0) || (h_bitmap < 0) || (w < 0) || (h < 0)) return;
  153. if ((x_part < 0) || (x_part >= w_bitmap)) return;
  154. if ((y_part < 0) || (y_part >= h_bitmap)) return;
  155. int16_t wb_bitmap = (w_bitmap + 7) / 8; // width bytes, bitmaps are padded
  156. x_part -= x_part % 8; // byte boundary
  157. w = w_bitmap - x_part < w ? w_bitmap - x_part : w; // limit
  158. h = h_bitmap - y_part < h ? h_bitmap - y_part : h; // limit
  159. x -= x % 8; // byte boundary
  160. w = 8 * ((w + 7) / 8); // byte boundary, bitmaps are padded
  161. int16_t x1 = x < 0 ? 0 : x; // limit
  162. int16_t y1 = y < 0 ? 0 : y; // limit
  163. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  164. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  165. int16_t dx = x1 - x;
  166. int16_t dy = y1 - y;
  167. w1 -= dx;
  168. h1 -= dy;
  169. if ((w1 <= 0) || (h1 <= 0)) return;
  170. if (!_using_partial_mode) _Init_Part();
  171. _writeCommand(0x91); // partial in
  172. _setPartialRamArea(x1, y1, w1, h1);
  173. _writeCommand(0x10);
  174. for (int16_t i = 0; i < h1; i++)
  175. {
  176. for (int16_t j = 0; j < w1 / 8; j++)
  177. {
  178. uint8_t data;
  179. // use wb_bitmap, h_bitmap of bitmap for index!
  180. 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;
  181. if (pgm)
  182. {
  183. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  184. data = pgm_read_byte(&black[idx]);
  185. #else
  186. data = black[idx];
  187. #endif
  188. }
  189. else
  190. {
  191. data = black[idx];
  192. }
  193. if (invert) data = ~data;
  194. _writeData(data);
  195. }
  196. }
  197. _writeCommand(0x13);
  198. for (int16_t i = 0; i < h1; i++)
  199. {
  200. for (int16_t j = 0; j < w1 / 8; j++)
  201. {
  202. uint8_t data = 0xFF;
  203. if (color)
  204. {
  205. // use wb_bitmap, h_bitmap of bitmap for index!
  206. 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;
  207. if (pgm)
  208. {
  209. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  210. data = pgm_read_byte(&color[idx]);
  211. #else
  212. data = color[idx];
  213. #endif
  214. }
  215. else
  216. {
  217. data = color[idx];
  218. }
  219. if (invert) data = ~data;
  220. }
  221. _writeData(data);
  222. }
  223. }
  224. _writeCommand(0x92); // partial out
  225. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  226. }
  227. 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)
  228. {
  229. if (data1)
  230. {
  231. writeImage(data1, x, y, w, h, invert, mirror_y, pgm);
  232. }
  233. }
  234. 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)
  235. {
  236. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  237. refresh(x, y, w, h);
  238. }
  239. 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,
  240. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  241. {
  242. writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  243. refresh(x, y, w, h);
  244. }
  245. 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)
  246. {
  247. writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  248. refresh(x, y, w, h);
  249. }
  250. 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,
  251. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  252. {
  253. writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  254. refresh(x, y, w, h);
  255. }
  256. 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)
  257. {
  258. writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  259. refresh(x, y, w, h);
  260. }
  261. void GxEPD2_213_Z19c::refresh(bool partial_update_mode)
  262. {
  263. if (partial_update_mode) refresh(0, 0, WIDTH, HEIGHT);
  264. else _Update_Full();
  265. }
  266. void GxEPD2_213_Z19c::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
  267. {
  268. x -= x % 8; // byte boundary
  269. w -= w % 8; // byte boundary
  270. int16_t x1 = x < 0 ? 0 : x; // limit
  271. int16_t y1 = y < 0 ? 0 : y; // limit
  272. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  273. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  274. w1 -= x1 - x;
  275. h1 -= y1 - y;
  276. _Init_Part();
  277. if (usePartialUpdateWindow) _writeCommand(0x91); // partial in
  278. _setPartialRamArea(x1, y1, w1, h1);
  279. _Update_Part();
  280. if (usePartialUpdateWindow) _writeCommand(0x92); // partial out
  281. }
  282. void GxEPD2_213_Z19c::powerOff()
  283. {
  284. _PowerOff();
  285. }
  286. void GxEPD2_213_Z19c::hibernate()
  287. {
  288. _PowerOff();
  289. if (_rst >= 0)
  290. {
  291. _writeCommand(0x07); // deep sleep
  292. _writeData(0xA5); // check code
  293. _hibernating = true;
  294. }
  295. }
  296. void GxEPD2_213_Z19c::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  297. {
  298. uint16_t xe = (x + w - 1) | 0x0007; // byte boundary inclusive (last byte)
  299. uint16_t ye = y + h - 1;
  300. x &= 0xFFF8; // byte boundary
  301. _writeCommand(0x90); // partial window
  302. //_writeData(x / 256);
  303. _writeData(x % 256);
  304. //_writeData(xe / 256);
  305. _writeData(xe % 256);
  306. _writeData(y / 256);
  307. _writeData(y % 256);
  308. _writeData(ye / 256);
  309. _writeData(ye % 256);
  310. _writeData(0x01); // don't see any difference
  311. //_writeData(0x00); // don't see any difference
  312. }
  313. void GxEPD2_213_Z19c::_PowerOn()
  314. {
  315. if (!_power_is_on)
  316. {
  317. _writeCommand(0x04);
  318. _waitWhileBusy("_PowerOn", power_on_time);
  319. }
  320. _power_is_on = true;
  321. }
  322. void GxEPD2_213_Z19c::_PowerOff()
  323. {
  324. _writeCommand(0x02); // power off
  325. _waitWhileBusy("_PowerOff", power_off_time);
  326. _power_is_on = false;
  327. }
  328. void GxEPD2_213_Z19c::_InitDisplay()
  329. {
  330. if (_hibernating) _reset();
  331. _writeCommand(0x00); // panel setting
  332. _writeData(0x8f); // LUT from OTP
  333. _writeCommand(0x61); // resolution setting
  334. _writeData (WIDTH);
  335. _writeData (HEIGHT >> 8);
  336. _writeData (HEIGHT & 0xFF);
  337. _writeCommand(0x50); // VCOM AND DATA INTERVAL SETTING
  338. _writeData(0x77); // WBmode:VBDF 17|D7 VBDW 97 VBDB 57 WBRmode:VBDF F7 VBDW 77 VBDB 37 VBDR B7
  339. }
  340. void GxEPD2_213_Z19c::_Init_Full()
  341. {
  342. _InitDisplay();
  343. _PowerOn();
  344. }
  345. void GxEPD2_213_Z19c::_Init_Part()
  346. {
  347. _InitDisplay();
  348. _PowerOn();
  349. }
  350. void GxEPD2_213_Z19c::_Update_Full()
  351. {
  352. _writeCommand(0x12); //display refresh
  353. _waitWhileBusy("_Update_Full", full_refresh_time);
  354. }
  355. void GxEPD2_213_Z19c::_Update_Part()
  356. {
  357. _writeCommand(0x12); //display refresh
  358. _waitWhileBusy("_Update_Part", partial_refresh_time);
  359. }