GxEPD2_154_M10.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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, available here: http://www.e-paper-display.com/download_detail/downloadsId=806.html
  5. // Panel: GDEW0154M10 : http://www.e-paper-display.com/products_detail/productId=544.html
  6. // Controller : UC8151D : http://www.e-paper-display.com/download_detail/downloadsId=1091.html
  7. //
  8. // Author: Jean-Marc Zingg
  9. //
  10. // Version: see library.properties
  11. //
  12. // Library: https://github.com/ZinggJM/GxEPD2
  13. #include "GxEPD2_154_M10.h"
  14. GxEPD2_154_M10::GxEPD2_154_M10(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_154_M10::clearScreen(uint8_t value)
  19. {
  20. writeScreenBuffer(value);
  21. refresh(true);
  22. writeScreenBufferAgain(value);
  23. }
  24. void GxEPD2_154_M10::writeScreenBuffer(uint8_t value)
  25. {
  26. _initial_write = false; // initial full screen buffer clean done
  27. if (!_using_partial_mode) _Init_Part();
  28. _writeScreenBuffer(0x13, value); // set current
  29. if (_initial_refresh) _writeScreenBuffer(0x10, value); // set previous
  30. }
  31. void GxEPD2_154_M10::writeScreenBufferAgain(uint8_t value)
  32. {
  33. if (!_using_partial_mode) _Init_Part();
  34. _writeScreenBuffer(0x10, value); // set previous
  35. }
  36. void GxEPD2_154_M10::_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_154_M10::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_154_M10::_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_154_M10::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_154_M10::_writeImagePart(uint8_t command, 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. if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
  107. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  108. if ((w_bitmap < 0) || (h_bitmap < 0) || (w < 0) || (h < 0)) return;
  109. if ((x_part < 0) || (x_part >= w_bitmap)) return;
  110. if ((y_part < 0) || (y_part >= h_bitmap)) return;
  111. int16_t wb_bitmap = (w_bitmap + 7) / 8; // width bytes, bitmaps are padded
  112. x_part -= x_part % 8; // byte boundary
  113. w = w_bitmap - x_part < w ? w_bitmap - x_part : w; // limit
  114. h = h_bitmap - y_part < h ? h_bitmap - y_part : h; // limit
  115. x -= x % 8; // byte boundary
  116. w = 8 * ((w + 7) / 8); // byte boundary, bitmaps are padded
  117. int16_t x1 = x < 0 ? 0 : x; // limit
  118. int16_t y1 = y < 0 ? 0 : y; // limit
  119. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  120. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  121. int16_t dx = x1 - x;
  122. int16_t dy = y1 - y;
  123. w1 -= dx;
  124. h1 -= dy;
  125. if ((w1 <= 0) || (h1 <= 0)) return;
  126. if (!_using_partial_mode) _Init_Part();
  127. _writeCommand(0x91); // partial in
  128. _setPartialRamArea(x1, y1, w1, h1);
  129. _writeCommand(command);
  130. _startTransfer();
  131. for (int16_t i = 0; i < h1; i++)
  132. {
  133. for (int16_t j = 0; j < w1 / 8; j++)
  134. {
  135. uint8_t data;
  136. // use wb_bitmap, h_bitmap of bitmap for index!
  137. 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;
  138. if (pgm)
  139. {
  140. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  141. data = pgm_read_byte(&bitmap[idx]);
  142. #else
  143. data = bitmap[idx];
  144. #endif
  145. }
  146. else
  147. {
  148. data = bitmap[idx];
  149. }
  150. if (invert) data = ~data;
  151. _transfer(data);
  152. }
  153. }
  154. _endTransfer();
  155. _writeCommand(0x92); // partial out
  156. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  157. }
  158. void GxEPD2_154_M10::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)
  159. {
  160. if (black)
  161. {
  162. writeImage(black, x, y, w, h, invert, mirror_y, pgm);
  163. }
  164. }
  165. void GxEPD2_154_M10::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,
  166. 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. writeImagePart(black, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  171. }
  172. }
  173. void GxEPD2_154_M10::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)
  174. {
  175. if (data1)
  176. {
  177. writeImage(data1, x, y, w, h, invert, mirror_y, pgm);
  178. }
  179. }
  180. void GxEPD2_154_M10::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  181. {
  182. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  183. refresh(x, y, w, h);
  184. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  185. }
  186. void GxEPD2_154_M10::drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  187. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  188. {
  189. writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  190. refresh(x, y, w, h);
  191. writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  192. }
  193. void GxEPD2_154_M10::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)
  194. {
  195. if (black)
  196. {
  197. drawImage(black, x, y, w, h, invert, mirror_y, pgm);
  198. }
  199. }
  200. void GxEPD2_154_M10::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,
  201. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  202. {
  203. if (black)
  204. {
  205. drawImagePart(black, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  206. }
  207. }
  208. void GxEPD2_154_M10::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)
  209. {
  210. if (data1)
  211. {
  212. drawImage(data1, x, y, w, h, invert, mirror_y, pgm);
  213. }
  214. }
  215. void GxEPD2_154_M10::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_154_M10::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. _writeCommand(0x91); // partial in
  242. _setPartialRamArea(x1, y1, w1, h1);
  243. _Update_Part();
  244. _writeCommand(0x92); // partial out
  245. }
  246. void GxEPD2_154_M10::powerOff()
  247. {
  248. _PowerOff();
  249. }
  250. void GxEPD2_154_M10::hibernate()
  251. {
  252. _PowerOff();
  253. if (_rst >= 0)
  254. {
  255. _writeCommand(0x07); // deep sleep mode
  256. _writeData(0xA5); // enter deep sleep
  257. _hibernating = true;
  258. }
  259. }
  260. void GxEPD2_154_M10::_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); // don't see any difference
  275. //_writeData(0x00); // don't see any difference
  276. }
  277. void GxEPD2_154_M10::_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_154_M10::_PowerOff()
  287. {
  288. if (_power_is_on)
  289. {
  290. _writeCommand(0x02);
  291. _waitWhileBusy("_PowerOff", power_off_time);
  292. }
  293. _power_is_on = false;
  294. _using_partial_mode = false;
  295. }
  296. void GxEPD2_154_M10::_InitDisplay()
  297. {
  298. if (_hibernating) _reset();
  299. delay(10); // 10ms according to specs
  300. _writeCommand(0x00); // panel setting
  301. _writeData(0x1f); // LUT from OTP KW-BF KWR-AF BWROTP 0f BWOTP 1f
  302. _writeCommand(0x50); // VCOM AND DATA INTERVAL SETTING
  303. _writeData(0x97); // WBmode:VBDF 17|D7 VBDW 97 VBDB 57 WBRmode:VBDF F7 VBDW 77 VBDB 37 VBDR B7
  304. _writeCommand(0x61); // resolution setting
  305. _writeData (WIDTH);
  306. _writeData (HEIGHT >> 8);
  307. _writeData (HEIGHT & 0xFF);
  308. }
  309. // experimental partial screen update LUTs with partially balanced charge
  310. // LUTs are filled with zeroes
  311. #define T1 10 // charge balance pre-phase
  312. #define T2 0 // optional extension
  313. #define T3 60 // color change phase (b/w)
  314. #define T4 10 // optional extension for one color
  315. const unsigned char GxEPD2_154_M10::lut_20_vcomDC_partial[] PROGMEM =
  316. {
  317. 0x00, T1, T2, T3, T4, 1, // 00 00 00 00
  318. };
  319. const unsigned char GxEPD2_154_M10::lut_21_ww_partial[] PROGMEM =
  320. { // 10 w
  321. 0x00, T1, T2, T3, T4, 1, // 00 00 00 00
  322. };
  323. const unsigned char GxEPD2_154_M10::lut_22_bw_partial[] PROGMEM =
  324. { // 10 w
  325. //0x48, T1, T2, T3, T4, 1, // 01 00 10 00
  326. 0x5A, T1, T2, T3, T4, 1, // 01 01 10 10 more white
  327. };
  328. const unsigned char GxEPD2_154_M10::lut_23_wb_partial[] PROGMEM =
  329. { // 01 b
  330. 0x84, T1, T2, T3, T4, 1, // 10 00 01 00
  331. //0xA5, T1, T2, T3, T4, 1, // 10 10 01 01 more black
  332. };
  333. const unsigned char GxEPD2_154_M10::lut_24_bb_partial[] PROGMEM =
  334. { // 01 b
  335. 0x00, T1, T2, T3, T4, 1, // 00 00 00 00
  336. };
  337. void GxEPD2_154_M10::_Init_Full()
  338. {
  339. _InitDisplay();
  340. _PowerOn();
  341. _using_partial_mode = false;
  342. }
  343. void GxEPD2_154_M10::_Init_Part()
  344. {
  345. _InitDisplay();
  346. if (hasFastPartialUpdate)
  347. {
  348. _writeCommand(0x00); // panel setting
  349. _writeData(0x3f); // LUT from REG
  350. _writeCommand(0x01); // POWER SETTING
  351. _writeData (0x03); // VDS_EN, VDG_EN
  352. _writeData (0x00); // VCOM_HV, VGL_LV default
  353. _writeData (0x21); // VDH as from OTP, TR5
  354. _writeData (0x21); // VDL as from OTP, TR5
  355. _writeData (0x03); // VDHR default
  356. _writeCommand(0x82); // vcom_DC setting
  357. _writeData (0x12); // as from OTP, TR5
  358. _writeCommand(0x50);
  359. _writeData(0x17); //WBmode:VBDF 17|D7 VBDW 97 VBDB 57 WBRmode:VBDF F7 VBDW 77 VBDB 37 VBDR B7
  360. _writeCommand(0x20);
  361. _writeDataPGM(lut_20_vcomDC_partial, sizeof(lut_20_vcomDC_partial), 36 - sizeof(lut_20_vcomDC_partial));
  362. _writeCommand(0x21);
  363. _writeDataPGM(lut_21_ww_partial, sizeof(lut_21_ww_partial), 36 - sizeof(lut_21_ww_partial));
  364. _writeCommand(0x22);
  365. _writeDataPGM(lut_22_bw_partial, sizeof(lut_22_bw_partial), 36 - sizeof(lut_22_bw_partial));
  366. _writeCommand(0x23);
  367. _writeDataPGM(lut_23_wb_partial, sizeof(lut_23_wb_partial), 36 - sizeof(lut_23_wb_partial));
  368. _writeCommand(0x24);
  369. _writeDataPGM(lut_24_bb_partial, sizeof(lut_24_bb_partial), 36 - sizeof(lut_24_bb_partial));
  370. }
  371. _PowerOn();
  372. _using_partial_mode = true;
  373. }
  374. void GxEPD2_154_M10::_Update_Full()
  375. {
  376. _writeCommand(0x12);
  377. _waitWhileBusy("_Update_Full", full_refresh_time);
  378. }
  379. void GxEPD2_154_M10::_Update_Part()
  380. {
  381. _writeCommand(0x12);
  382. _waitWhileBusy("_Update_Part", partial_refresh_time);
  383. }