GxEPD2_290_I6FD.cpp 15 KB

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