GxEPD2_750_GDEY075T7.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. // based on Demo Example from Good Display: https://www.good-display.com/comp/xcompanyFile/downloadNew.do?appId=24&fid=1373&id=1125
  5. // Panel: GDEY075T7 : https://www.good-display.com/product/396.html
  6. // Controller: UC8179 : https://v4.cecdn.yun300.cn/100001_1909185148/UC8179.pdf
  7. //
  8. // Author: Jean-Marc Zingg
  9. //
  10. // Version: see library.properties
  11. //
  12. // Library: https://github.com/ZinggJM/GxEPD2
  13. #include "GxEPD2_750_GDEY075T7.h"
  14. GxEPD2_750_GDEY075T7::GxEPD2_750_GDEY075T7(int16_t cs, int16_t dc, int16_t rst, int16_t busy) :
  15. GxEPD2_EPD(cs, dc, rst, busy, LOW, 10000000, WIDTH, HEIGHT, panel, hasColor, hasPartialUpdate, hasFastPartialUpdate)
  16. {
  17. }
  18. void GxEPD2_750_GDEY075T7::clearScreen(uint8_t value)
  19. {
  20. writeScreenBuffer(value);
  21. refresh(true);
  22. writeScreenBuffer(value);
  23. }
  24. void GxEPD2_750_GDEY075T7::writeScreenBuffer(uint8_t value)
  25. {
  26. if (!_using_partial_mode) _Init_Part();
  27. _writeScreenBuffer(0x13, value); // set current
  28. if (_initial_refresh) _writeScreenBuffer(0x10, 0x00); // set previous
  29. _initial_write = false; // initial full screen buffer clean done
  30. }
  31. void GxEPD2_750_GDEY075T7::_writeScreenBuffer(uint8_t command, uint8_t value)
  32. {
  33. _writeCommand(command);
  34. _startTransfer();
  35. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  36. {
  37. _transfer(value);
  38. }
  39. _endTransfer();
  40. }
  41. void GxEPD2_750_GDEY075T7::writeImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  42. {
  43. _writeImage(0x13, bitmap, x, y, w, h, invert, mirror_y, pgm);
  44. }
  45. void GxEPD2_750_GDEY075T7::writeImageForFullRefresh(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  46. {
  47. _writeImage(0x13, bitmap, x, y, w, h, invert, mirror_y, pgm);
  48. }
  49. void GxEPD2_750_GDEY075T7::writeImageToPrevious(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  50. {
  51. _writeImage(0x10, bitmap, x, y, w, h, invert, mirror_y, pgm);
  52. }
  53. void GxEPD2_750_GDEY075T7::_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)
  54. {
  55. if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
  56. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  57. uint16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
  58. x -= x % 8; // byte boundary
  59. w = wb * 8; // byte boundary
  60. int16_t x1 = x < 0 ? 0 : x; // limit
  61. int16_t y1 = y < 0 ? 0 : y; // limit
  62. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  63. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  64. int16_t dx = x1 - x;
  65. int16_t dy = y1 - y;
  66. w1 -= dx;
  67. h1 -= dy;
  68. if ((w1 <= 0) || (h1 <= 0)) return;
  69. if (!_using_partial_mode) _Init_Part();
  70. _writeCommand(0x91); // partial in
  71. _setPartialRamArea(x1, y1, w1, h1);
  72. _writeCommand(command);
  73. _startTransfer();
  74. for (int16_t i = 0; i < h1; i++)
  75. {
  76. for (int16_t j = 0; j < w1 / 8; j++)
  77. {
  78. uint8_t data;
  79. // use wb, h of bitmap for index!
  80. uint16_t idx = mirror_y ? j + dx / 8 + uint16_t((h - 1 - (i + dy))) * wb : j + dx / 8 + uint16_t(i + dy) * wb;
  81. if (pgm)
  82. {
  83. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  84. data = pgm_read_byte(&bitmap[idx]);
  85. #else
  86. data = bitmap[idx];
  87. #endif
  88. }
  89. else
  90. {
  91. data = bitmap[idx];
  92. }
  93. if (invert) data = ~data;
  94. _transfer(data);
  95. }
  96. }
  97. _endTransfer();
  98. _writeCommand(0x92); // partial out
  99. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  100. }
  101. void GxEPD2_750_GDEY075T7::writeImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  102. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  103. {
  104. _writeImagePart(0x13, bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  105. }
  106. void GxEPD2_750_GDEY075T7::writeImagePartToPrevious(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  107. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  108. {
  109. _writeImagePart(0x10, bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  110. }
  111. void GxEPD2_750_GDEY075T7::_writeImagePart(uint8_t command, const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  112. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  113. {
  114. if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
  115. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  116. if ((w_bitmap < 0) || (h_bitmap < 0) || (w < 0) || (h < 0)) return;
  117. if ((x_part < 0) || (x_part >= w_bitmap)) return;
  118. if ((y_part < 0) || (y_part >= h_bitmap)) return;
  119. uint16_t wb_bitmap = (w_bitmap + 7) / 8; // width bytes, bitmaps are padded
  120. x_part -= x_part % 8; // byte boundary
  121. w = w_bitmap - x_part < w ? w_bitmap - x_part : w; // limit
  122. h = h_bitmap - y_part < h ? h_bitmap - y_part : h; // limit
  123. x -= x % 8; // byte boundary
  124. w = 8 * ((w + 7) / 8); // byte boundary, bitmaps are padded
  125. int16_t x1 = x < 0 ? 0 : x; // limit
  126. int16_t y1 = y < 0 ? 0 : y; // limit
  127. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  128. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  129. int16_t dx = x1 - x;
  130. int16_t dy = y1 - y;
  131. w1 -= dx;
  132. h1 -= dy;
  133. if ((w1 <= 0) || (h1 <= 0)) return;
  134. if (!_using_partial_mode) _Init_Part();
  135. _writeCommand(0x91); // partial in
  136. _setPartialRamArea(x1, y1, w1, h1);
  137. _writeCommand(command);
  138. _startTransfer();
  139. for (int16_t i = 0; i < h1; i++)
  140. {
  141. for (int16_t j = 0; j < w1 / 8; j++)
  142. {
  143. uint8_t data;
  144. // use wb_bitmap, h_bitmap of bitmap for index!
  145. uint16_t idx = mirror_y ? x_part / 8 + j + dx / 8 + uint16_t((h_bitmap - 1 - (y_part + i + dy))) * wb_bitmap : x_part / 8 + j + dx / 8 + uint16_t(y_part + i + dy) * wb_bitmap;
  146. if (pgm)
  147. {
  148. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  149. data = pgm_read_byte(&bitmap[idx]);
  150. #else
  151. data = bitmap[idx];
  152. #endif
  153. }
  154. else
  155. {
  156. data = bitmap[idx];
  157. }
  158. if (invert) data = ~data;
  159. _transfer(data);
  160. }
  161. }
  162. _endTransfer();
  163. _writeCommand(0x92); // partial out
  164. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  165. }
  166. void GxEPD2_750_GDEY075T7::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)
  167. {
  168. if (black)
  169. {
  170. writeImage(black, x, y, w, h, invert, mirror_y, pgm);
  171. }
  172. }
  173. void GxEPD2_750_GDEY075T7::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,
  174. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  175. {
  176. if (black)
  177. {
  178. writeImagePart(black, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  179. }
  180. }
  181. void GxEPD2_750_GDEY075T7::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)
  182. {
  183. if (data1)
  184. {
  185. writeImage(data1, x, y, w, h, invert, mirror_y, pgm);
  186. }
  187. }
  188. void GxEPD2_750_GDEY075T7::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  189. {
  190. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  191. refresh(x, y, w, h);
  192. }
  193. void GxEPD2_750_GDEY075T7::drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  194. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  195. {
  196. writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  197. refresh(x, y, w, h);
  198. }
  199. void GxEPD2_750_GDEY075T7::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)
  200. {
  201. writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  202. refresh(x, y, w, h);
  203. }
  204. void GxEPD2_750_GDEY075T7::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,
  205. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  206. {
  207. writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  208. refresh(x, y, w, h);
  209. }
  210. void GxEPD2_750_GDEY075T7::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)
  211. {
  212. writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  213. refresh(x, y, w, h);
  214. }
  215. void GxEPD2_750_GDEY075T7::refresh(bool partial_update_mode)
  216. {
  217. if (partial_update_mode) refresh(0, 0, WIDTH, HEIGHT);
  218. else
  219. {
  220. if (_using_partial_mode) _Init_Full();
  221. _Update_Full();
  222. _initial_refresh = false; // initial full update done
  223. }
  224. }
  225. void GxEPD2_750_GDEY075T7::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
  226. {
  227. if (_initial_refresh) return refresh(false); // initial update needs be full update
  228. // intersection with screen
  229. int16_t w1 = x < 0 ? w + x : w; // reduce
  230. int16_t h1 = y < 0 ? h + y : h; // reduce
  231. int16_t x1 = x < 0 ? 0 : x; // limit
  232. int16_t y1 = y < 0 ? 0 : y; // limit
  233. w1 = x1 + w1 < int16_t(WIDTH) ? w1 : int16_t(WIDTH) - x1; // limit
  234. h1 = y1 + h1 < int16_t(HEIGHT) ? h1 : int16_t(HEIGHT) - y1; // limit
  235. if ((w1 <= 0) || (h1 <= 0)) return;
  236. // make x1, w1 multiple of 8
  237. w1 += x1 % 8;
  238. if (w1 % 8 > 0) w1 += 8 - w1 % 8;
  239. x1 -= x1 % 8;
  240. if (!_using_partial_mode) _Init_Part();
  241. if (usePartialUpdateWindow) _writeCommand(0x91); // partial in
  242. _setPartialRamArea(x1, y1, w1, h1);
  243. _Update_Part();
  244. if (usePartialUpdateWindow) _writeCommand(0x92); // partial out
  245. }
  246. void GxEPD2_750_GDEY075T7::powerOff(void)
  247. {
  248. _PowerOff();
  249. }
  250. void GxEPD2_750_GDEY075T7::hibernate()
  251. {
  252. _PowerOff();
  253. if (_rst >= 0)
  254. {
  255. _writeCommand(0x07); // deep sleep
  256. _writeData(0xA5); // check code
  257. _hibernating = true;
  258. }
  259. }
  260. void GxEPD2_750_GDEY075T7::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  261. {
  262. uint16_t xe = (x + w - 1) | 0x0007; // byte boundary inclusive (last byte)
  263. uint16_t ye = y + h - 1;
  264. x &= 0xFFF8; // byte boundary
  265. _writeCommand(0x90); // partial window
  266. _writeData(x / 256);
  267. _writeData(x % 256);
  268. _writeData(xe / 256);
  269. _writeData(xe % 256);
  270. _writeData(y / 256);
  271. _writeData(y % 256);
  272. _writeData(ye / 256);
  273. _writeData(ye % 256);
  274. _writeData(0x01);
  275. }
  276. void GxEPD2_750_GDEY075T7::_PowerOn()
  277. {
  278. if (!_power_is_on)
  279. {
  280. _writeCommand(0x04);
  281. _waitWhileBusy("_PowerOn", power_on_time);
  282. }
  283. _power_is_on = true;
  284. }
  285. void GxEPD2_750_GDEY075T7::_PowerOff()
  286. {
  287. if (_power_is_on)
  288. {
  289. _writeCommand(0x02); // power off
  290. _waitWhileBusy("_PowerOff", power_off_time);
  291. }
  292. _power_is_on = false;
  293. _using_partial_mode = false;
  294. }
  295. void GxEPD2_750_GDEY075T7::_InitDisplay()
  296. {
  297. if (_hibernating) _reset();
  298. _writeCommand(0x00); // PANEL SETTING
  299. _writeData(0x1f); // KW: 3f, KWR: 2F, BWROTP: 0f, BWOTP: 1f
  300. // same POWER SETTING as from OTP
  301. _writeCommand(0x01); // POWER SETTING
  302. _writeData (0x07); // enable internal
  303. _writeData (0x07); // VGH=20V,VGL=-20V
  304. _writeData (0x3f); // VDH=15V
  305. _writeData (0x3f); // VDL=-15V
  306. _writeData (0x09); // VDHR=4.2V
  307. //Enhanced display drive(Add 0x06 command)
  308. _writeCommand(0x06); //Booster Soft Start
  309. _writeData (0x17);
  310. _writeData (0x17);
  311. _writeData (0x28);
  312. _writeData (0x17);
  313. _writeCommand(0x61); //tres
  314. _writeData (WIDTH / 256); //source 800
  315. _writeData (WIDTH % 256);
  316. _writeData (HEIGHT / 256); //gate 480
  317. _writeData (HEIGHT % 256);
  318. _writeCommand(0x15); // DUSPI
  319. _writeData(0x00); // disabled
  320. _writeCommand(0x50); // VCOM AND DATA INTERVAL SETTING
  321. _writeData(0x29); // LUTKW, N2OCP: copy new to old
  322. _writeData(0x07); // CDI 10hsynch (default)
  323. _writeCommand(0x60); // TCON SETTING
  324. _writeData(0x22); // S2G G2S, 12 (default)
  325. _writeCommand(0xE3); // PWS
  326. _writeData(0x22); // VCOM 2 line period, Source 2 * 660ns
  327. }
  328. // experimental partial screen update LUTs with balanced charge
  329. // LUTs are filled with zeroes
  330. #define T1 30 // charge balance pre-phase
  331. #define T2 5 // optional extension
  332. #define T3 30 // color change phase (b/w)
  333. #define T4 5 // optional extension for one color
  334. const unsigned char GxEPD2_750_GDEY075T7::lut_20_LUTC_partial[] PROGMEM =
  335. {
  336. 0x00, T1, T2, T3, T4, 1, // 00 00 00 00
  337. };
  338. const unsigned char GxEPD2_750_GDEY075T7::lut_21_LUTWW_partial[] PROGMEM =
  339. { // 10 w
  340. 0x00, T1, T2, T3, T4, 1, // 00 00 00 00
  341. };
  342. const unsigned char GxEPD2_750_GDEY075T7::lut_22_LUTKW_partial[] PROGMEM =
  343. { // 10 w
  344. //0x48, T1, T2, T3, T4, 1, // 01 00 10 00
  345. 0x5A, T1, T2, T3, T4, 1, // 01 01 10 10 more white
  346. };
  347. const unsigned char GxEPD2_750_GDEY075T7::lut_23_LUTWK_partial[] PROGMEM =
  348. { // 01 b
  349. 0x84, T1, T2, T3, T4, 1, // 10 00 01 00
  350. //0xA5, T1, T2, T3, T4, 1, // 10 10 01 01 more black
  351. };
  352. const unsigned char GxEPD2_750_GDEY075T7::lut_24_LUTKK_partial[] PROGMEM =
  353. { // 01 b
  354. 0x00, T1, T2, T3, T4, 1, // 00 00 00 00
  355. };
  356. const unsigned char GxEPD2_750_GDEY075T7::lut_25_LUTBD_partial[] PROGMEM =
  357. {
  358. 0x00, T1, T2, T3, T4, 1, // 00 00 00 00
  359. };
  360. void GxEPD2_750_GDEY075T7::_Init_Full()
  361. {
  362. _InitDisplay();
  363. _writeCommand(0x00); // panel setting
  364. _writeData(0x1f); // full update LUT from OTP
  365. _PowerOn();
  366. _using_partial_mode = false;
  367. }
  368. void GxEPD2_750_GDEY075T7::_Init_Part()
  369. {
  370. _InitDisplay();
  371. if (hasFastPartialUpdate)
  372. {
  373. if (useFastPartialUpdateFromOTP)
  374. {
  375. _writeCommand(0xE0); // Cascade Setting (CCSET)
  376. _writeData(0x02); // TSFIX
  377. _writeCommand(0xE5); // Force Temperature (TSSET)
  378. _writeData(0x6E); // 110
  379. }
  380. else
  381. {
  382. _writeCommand(0x00); //panel setting
  383. _writeData(0x3f); // partial update LUT from registers
  384. _writeCommand(0x82); // vcom_DC setting
  385. _writeData (0x30); // -2.5V same value as in OTP
  386. _writeCommand(0x50); // VCOM AND DATA INTERVAL SETTING
  387. _writeData(0x39); // LUTBD, N2OCP: copy new to old
  388. _writeData(0x07);
  389. _writeCommand(0x20);
  390. _writeDataPGM(lut_20_LUTC_partial, sizeof(lut_20_LUTC_partial), 42 - sizeof(lut_20_LUTC_partial));
  391. _writeCommand(0x21);
  392. _writeDataPGM(lut_21_LUTWW_partial, sizeof(lut_21_LUTWW_partial), 42 - sizeof(lut_21_LUTWW_partial));
  393. _writeCommand(0x22);
  394. _writeDataPGM(lut_22_LUTKW_partial, sizeof(lut_22_LUTKW_partial), 42 - sizeof(lut_22_LUTKW_partial));
  395. _writeCommand(0x23);
  396. _writeDataPGM(lut_23_LUTWK_partial, sizeof(lut_23_LUTWK_partial), 42 - sizeof(lut_23_LUTWK_partial));
  397. _writeCommand(0x24);
  398. _writeDataPGM(lut_24_LUTKK_partial, sizeof(lut_24_LUTKK_partial), 42 - sizeof(lut_24_LUTKK_partial));
  399. _writeCommand(0x25);
  400. _writeDataPGM(lut_25_LUTBD_partial, sizeof(lut_25_LUTBD_partial), 42 - sizeof(lut_25_LUTBD_partial));
  401. }
  402. }
  403. _PowerOn();
  404. _using_partial_mode = true;
  405. }
  406. void GxEPD2_750_GDEY075T7::_Update_Full()
  407. {
  408. if (useFastFullUpdate)
  409. {
  410. _writeCommand(0xE0); // Cascade Setting (CCSET)
  411. _writeData(0x02); // TSFIX
  412. _writeCommand(0xE5); // Force Temperature (TSSET)
  413. _writeData(0x5A); // 90
  414. }
  415. else
  416. {
  417. _writeCommand(0xE0); // Cascade Setting (CCSET)
  418. _writeData(0x00); // no TSFIX, Temperature value is defined by internal temperature sensor
  419. _writeCommand(0x41); // TSE, Enable Temperature Sensor
  420. _writeData(0x00); // TSE, Internal temperature sensor switch
  421. }
  422. _writeCommand(0x12); //display refresh
  423. _waitWhileBusy("_Update_Full", full_refresh_time);
  424. }
  425. void GxEPD2_750_GDEY075T7::_Update_Part()
  426. {
  427. _writeCommand(0x12); //display refresh
  428. _waitWhileBusy("_Update_Part", partial_refresh_time);
  429. }