GxEPD2_260_M01.cpp 13 KB

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