GxEPD2_420_M01.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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: UC8176 (IL0398) : https://www.good-display.com/public/html/pdfjs/viewer/viewernew.html?file=https://v4.cecdn.yun300.cn/100001_1909185148/UC8176-1.pdf
  6. // Controller: IL0398 (UC8176) : http://www.e-paper-display.com/download_detail/downloadsId=537.html
  7. //
  8. // Author: Jean-Marc Zingg
  9. //
  10. // Version: see library.properties
  11. //
  12. // Library: https://github.com/ZinggJM/GxEPD2
  13. #include "GxEPD2_420_M01.h"
  14. GxEPD2_420_M01::GxEPD2_420_M01(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_420_M01::clearScreen(uint8_t value)
  19. {
  20. writeScreenBuffer(value);
  21. refresh(true);
  22. writeScreenBuffer(value);
  23. }
  24. void GxEPD2_420_M01::writeScreenBuffer(uint8_t value)
  25. {
  26. _initial_write = false; // initial full screen buffer clean done
  27. if (!_using_partial_mode) _Init_Part();
  28. if (_initial_refresh)
  29. {
  30. _writeCommand(0x10); // init old data
  31. _startTransfer();
  32. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  33. {
  34. _transfer(value);
  35. }
  36. _endTransfer();
  37. }
  38. _writeCommand(0x13);
  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_420_M01::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. if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
  49. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  50. int16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
  51. x -= x % 8; // byte boundary
  52. w = wb * 8; // byte boundary
  53. int16_t x1 = x < 0 ? 0 : x; // limit
  54. int16_t y1 = y < 0 ? 0 : y; // limit
  55. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  56. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  57. int16_t dx = x1 - x;
  58. int16_t dy = y1 - y;
  59. w1 -= dx;
  60. h1 -= dy;
  61. if ((w1 <= 0) || (h1 <= 0)) return;
  62. if (!_using_partial_mode) _Init_Part();
  63. //uint32_t start = micros();
  64. _writeCommand(0x91); // partial in
  65. _setPartialRamArea(x1, y1, w1, h1);
  66. _writeCommand(0x13);
  67. _startTransfer();
  68. for (int16_t i = 0; i < h1; i++)
  69. {
  70. for (int16_t j = 0; j < w1 / 8; j++)
  71. {
  72. uint8_t data;
  73. // use wb, h of bitmap for index!
  74. int16_t idx = mirror_y ? j + dx / 8 + ((h - 1 - (i + dy))) * wb : j + dx / 8 + (i + dy) * wb;
  75. if (pgm)
  76. {
  77. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  78. data = pgm_read_byte(&bitmap[idx]);
  79. #else
  80. data = bitmap[idx];
  81. #endif
  82. }
  83. else
  84. {
  85. data = bitmap[idx];
  86. }
  87. if (invert) data = ~data;
  88. _transfer(data);
  89. }
  90. }
  91. _endTransfer();
  92. _writeCommand(0x92); // partial out
  93. //Serial.print("GxEPD2_420_M01::writeImage took "); Serial.println(micros() - start);
  94. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  95. }
  96. void GxEPD2_420_M01::writeImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  97. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  98. {
  99. if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
  100. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  101. if ((w_bitmap < 0) || (h_bitmap < 0) || (w < 0) || (h < 0)) return;
  102. if ((x_part < 0) || (x_part >= w_bitmap)) return;
  103. if ((y_part < 0) || (y_part >= h_bitmap)) return;
  104. int16_t wb_bitmap = (w_bitmap + 7) / 8; // width bytes, bitmaps are padded
  105. x_part -= x_part % 8; // byte boundary
  106. w = w_bitmap - x_part < w ? w_bitmap - x_part : w; // limit
  107. h = h_bitmap - y_part < h ? h_bitmap - y_part : h; // limit
  108. x -= x % 8; // byte boundary
  109. w = 8 * ((w + 7) / 8); // byte boundary, bitmaps are padded
  110. int16_t x1 = x < 0 ? 0 : x; // limit
  111. int16_t y1 = y < 0 ? 0 : y; // limit
  112. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  113. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  114. int16_t dx = x1 - x;
  115. int16_t dy = y1 - y;
  116. w1 -= dx;
  117. h1 -= dy;
  118. if ((w1 <= 0) || (h1 <= 0)) return;
  119. if (!_using_partial_mode) _Init_Part();
  120. _writeCommand(0x91); // partial in
  121. _setPartialRamArea(x1, y1, w1, h1);
  122. _writeCommand(0x13);
  123. _startTransfer();
  124. for (int16_t i = 0; i < h1; i++)
  125. {
  126. for (int16_t j = 0; j < w1 / 8; j++)
  127. {
  128. uint8_t data;
  129. // use wb_bitmap, h_bitmap of bitmap for index!
  130. 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;
  131. if (pgm)
  132. {
  133. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  134. data = pgm_read_byte(&bitmap[idx]);
  135. #else
  136. data = bitmap[idx];
  137. #endif
  138. }
  139. else
  140. {
  141. data = bitmap[idx];
  142. }
  143. if (invert) data = ~data;
  144. _transfer(data);
  145. }
  146. }
  147. _endTransfer();
  148. _writeCommand(0x92); // partial out
  149. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  150. }
  151. void GxEPD2_420_M01::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)
  152. {
  153. if (black)
  154. {
  155. writeImage(black, x, y, w, h, invert, mirror_y, pgm);
  156. }
  157. }
  158. void GxEPD2_420_M01::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,
  159. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  160. {
  161. if (black)
  162. {
  163. writeImagePart(black, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  164. }
  165. }
  166. void GxEPD2_420_M01::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)
  167. {
  168. if (data1)
  169. {
  170. writeImage(data1, x, y, w, h, invert, mirror_y, pgm);
  171. }
  172. }
  173. void GxEPD2_420_M01::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  174. {
  175. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  176. refresh(x, y, w, h);
  177. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  178. }
  179. void GxEPD2_420_M01::drawImagePart(const uint8_t bitmap[], 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. writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  183. refresh(x, y, w, h);
  184. writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  185. }
  186. void GxEPD2_420_M01::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)
  187. {
  188. writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  189. refresh(x, y, w, h);
  190. writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  191. }
  192. void GxEPD2_420_M01::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,
  193. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  194. {
  195. writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  196. refresh(x, y, w, h);
  197. writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  198. }
  199. void GxEPD2_420_M01::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)
  200. {
  201. writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  202. refresh(x, y, w, h);
  203. writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  204. }
  205. void GxEPD2_420_M01::refresh(bool partial_update_mode)
  206. {
  207. if (partial_update_mode) refresh(0, 0, WIDTH, HEIGHT);
  208. else
  209. {
  210. if (_using_partial_mode) _Init_Full();
  211. _Update_Full();
  212. _initial_refresh = false; // initial full update done
  213. }
  214. }
  215. void GxEPD2_420_M01::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
  216. {
  217. if (_initial_refresh) return refresh(false); // initial update needs be full update
  218. // intersection with screen
  219. int16_t w1 = x < 0 ? w + x : w; // reduce
  220. int16_t h1 = y < 0 ? h + y : h; // reduce
  221. int16_t x1 = x < 0 ? 0 : x; // limit
  222. int16_t y1 = y < 0 ? 0 : y; // limit
  223. w1 = x1 + w1 < int16_t(WIDTH) ? w1 : int16_t(WIDTH) - x1; // limit
  224. h1 = y1 + h1 < int16_t(HEIGHT) ? h1 : int16_t(HEIGHT) - y1; // limit
  225. if ((w1 <= 0) || (h1 <= 0)) return;
  226. // make x1, w1 multiple of 8
  227. w1 += x1 % 8;
  228. if (w1 % 8 > 0) w1 += 8 - w1 % 8;
  229. x1 -= x1 % 8;
  230. if (!_using_partial_mode) _Init_Part();
  231. if (usePartialUpdateWindow) _writeCommand(0x91); // partial in
  232. _setPartialRamArea(x1, y1, w1, h1);
  233. _Update_Part();
  234. if (usePartialUpdateWindow) _writeCommand(0x92); // partial out
  235. }
  236. void GxEPD2_420_M01::powerOff(void)
  237. {
  238. _PowerOff();
  239. }
  240. void GxEPD2_420_M01::hibernate()
  241. {
  242. _PowerOff();
  243. if (_rst >= 0)
  244. {
  245. _writeCommand(0x07); // deep sleep
  246. _writeData(0xA5); // check code
  247. _hibernating = true;
  248. }
  249. }
  250. void GxEPD2_420_M01::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  251. {
  252. uint16_t xe = (x + w - 1) | 0x0007; // byte boundary inclusive (last byte)
  253. uint16_t ye = y + h - 1;
  254. x &= 0xFFF8; // byte boundary
  255. _writeCommand(0x90); // partial window
  256. _writeData(x / 256);
  257. _writeData(x % 256);
  258. _writeData(xe / 256);
  259. _writeData(xe % 256);
  260. _writeData(y / 256);
  261. _writeData(y % 256);
  262. _writeData(ye / 256);
  263. _writeData(ye % 256);
  264. _writeData(0x01); // don't see any difference
  265. //_writeData(0x00); // don't see any difference
  266. }
  267. void GxEPD2_420_M01::_PowerOn()
  268. {
  269. if (!_power_is_on)
  270. {
  271. _writeCommand(0x04);
  272. _waitWhileBusy("_PowerOn", power_on_time);
  273. }
  274. _power_is_on = true;
  275. }
  276. void GxEPD2_420_M01::_PowerOff()
  277. {
  278. _writeCommand(0x02); // power off
  279. _waitWhileBusy("_PowerOff", power_off_time);
  280. _power_is_on = false;
  281. _using_partial_mode = false;
  282. }
  283. void GxEPD2_420_M01::_InitDisplay()
  284. {
  285. if (_hibernating) _reset();
  286. _writeCommand(0x00); //panel setting
  287. _writeData(0x1f); //LUT from OTP
  288. _writeData(0x0d); //waiting for the electronic paper IC to release the idle signal
  289. _writeCommand(0x50); //VCOM AND DATA INTERVAL SETTING
  290. _writeData(0x97);
  291. }
  292. // experimental partial screen update LUTs with balanced charge
  293. // LUTs are filled with zeroes
  294. #define T1 20 // charge balance pre-phase
  295. #define T2 20 // optional extension
  296. #define T3 40 // color change phase (b/w)
  297. #define T4 40 // optional extension for one color
  298. #define T5 3 // white sustain phase
  299. #define T6 3 // black sustain phase
  300. const unsigned char GxEPD2_420_M01::lut_20_vcom0_partial[] PROGMEM =
  301. {
  302. 0x00, T1, T2, T3, T4, 1, // 00 00 00 00
  303. };
  304. const unsigned char GxEPD2_420_M01::lut_21_ww_partial[] PROGMEM =
  305. { // 10 w
  306. 0x02, T1, T2, T3, T5, 1, // 00 00 00 10
  307. };
  308. const unsigned char GxEPD2_420_M01::lut_22_bw_partial[] PROGMEM =
  309. { // 10 w
  310. //0x48, T1, T2, T3, T4, 1, // 01 00 10 00
  311. 0x5A, T1, T2, T3, T4, 1, // 01 01 10 10 more white
  312. };
  313. const unsigned char GxEPD2_420_M01::lut_23_wb_partial[] PROGMEM =
  314. { // 01 b
  315. 0x84, T1, T2, T3, T4, 1, // 10 00 01 00
  316. //0xA5, T1, T2, T3, T4, 1, // 10 10 01 01 more black
  317. };
  318. const unsigned char GxEPD2_420_M01::lut_24_bb_partial[] PROGMEM =
  319. { // 01 b
  320. 0x01, T1, T2, T3, T6, 1, // 00 00 00 01
  321. };
  322. void GxEPD2_420_M01::_Init_Full()
  323. {
  324. _InitDisplay();
  325. _PowerOn();
  326. _using_partial_mode = false;
  327. }
  328. void GxEPD2_420_M01::_Init_Part()
  329. {
  330. _InitDisplay();
  331. if (hasPartialUpdate)
  332. {
  333. _writeCommand(0x01); // POWER SETTING
  334. _writeData (0x03); // VDS_EN, VDG_EN internal
  335. _writeData (0x00); // VCOM_HV, VGHL_LV=16V
  336. _writeData (0x2b); // VDH=11V
  337. _writeData (0x2b); // VDL=11V
  338. _writeCommand(0x06); // boost soft start
  339. _writeData (0x17); // A
  340. _writeData (0x17); // B
  341. _writeData (0x17); // C
  342. _writeCommand(0x00); // panel setting
  343. _writeData(0x3f); // 300x400 B/W mode, LUT set by register
  344. _writeCommand(0x30); // PLL setting
  345. _writeData (0x3a); // 3a 100HZ 29 150Hz 39 200HZ 31 171HZ
  346. _writeCommand(0x61); // resolution setting
  347. _writeData (WIDTH / 256);
  348. _writeData (WIDTH % 256);
  349. _writeData (HEIGHT / 256);
  350. _writeData (HEIGHT % 256);
  351. _writeCommand(0x82); // vcom_DC setting
  352. _writeData (0x1A); // -0.1 + 26 * -0.05 = -1.4V from OTP
  353. _writeCommand(0x50); // VCOM AND DATA INTERVAL SETTING
  354. //_writeData(0x97); // WBmode:VBDF 17|D7 VBDW 97 VBDB 57 WBRmode:VBDF F7 VBDW 77 VBDB 37 VBDR B7
  355. _writeData(0xd7); // border floating to avoid flashing
  356. _writeCommand(0x20);
  357. _writeDataPGM(lut_20_vcom0_partial, sizeof(lut_20_vcom0_partial), 44 - sizeof(lut_20_vcom0_partial));
  358. _writeCommand(0x21);
  359. _writeDataPGM(lut_21_ww_partial, sizeof(lut_21_ww_partial), 42 - sizeof(lut_21_ww_partial));
  360. _writeCommand(0x22);
  361. _writeDataPGM(lut_22_bw_partial, sizeof(lut_22_bw_partial), 42 - sizeof(lut_22_bw_partial));
  362. _writeCommand(0x23);
  363. _writeDataPGM(lut_23_wb_partial, sizeof(lut_23_wb_partial), 42 - sizeof(lut_23_wb_partial));
  364. _writeCommand(0x24);
  365. _writeDataPGM(lut_24_bb_partial, sizeof(lut_24_bb_partial), 42 - sizeof(lut_24_bb_partial));
  366. }
  367. _PowerOn();
  368. _using_partial_mode = true;
  369. }
  370. void GxEPD2_420_M01::_Update_Full()
  371. {
  372. _writeCommand(0x12); //display refresh
  373. _waitWhileBusy("_Update_Full", full_refresh_time);
  374. }
  375. void GxEPD2_420_M01::_Update_Part()
  376. {
  377. _writeCommand(0x12); //display refresh
  378. _waitWhileBusy("_Update_Part", partial_refresh_time);
  379. }