GxEPD2_260_M01.cpp 14 KB

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