GxEPD2_1160_T91.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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: https://www.good-display.com/comp/xcompanyFile/downloadNew.do?appId=24&fid=553&id=374
  5. // Panel: GDEH116T91 : https://www.good-display.com/product/246.html
  6. // Controller : SSD1677 : https://v4.cecdn.yun300.cn/100001_1909185148/SSD1677.pdf
  7. //
  8. // Author: Jean-Marc Zingg
  9. //
  10. // Version: see library.properties
  11. //
  12. // Library: https://github.com/ZinggJM/GxEPD2
  13. #include "GxEPD2_1160_T91.h"
  14. GxEPD2_1160_T91::GxEPD2_1160_T91(int16_t cs, int16_t dc, int16_t rst, int16_t busy) :
  15. GxEPD2_EPD(cs, dc, rst, busy, HIGH, 10000000, WIDTH, HEIGHT, panel, hasColor, hasPartialUpdate, hasFastPartialUpdate)
  16. {
  17. }
  18. void GxEPD2_1160_T91::clearScreen(uint8_t value)
  19. {
  20. // full refresh needed for all cases (previous != screen)
  21. _writeScreenBuffer(0x26, value); // set previous
  22. _writeScreenBuffer(0x24, value); // set current
  23. refresh(false); // full refresh
  24. _initial_write = false;
  25. }
  26. void GxEPD2_1160_T91::writeScreenBuffer(uint8_t value)
  27. {
  28. if (_initial_write) return clearScreen(value);
  29. _writeScreenBuffer(0x24, value); // set current
  30. }
  31. void GxEPD2_1160_T91::writeScreenBufferAgain(uint8_t value)
  32. {
  33. _writeScreenBuffer(0x24, value); // set current
  34. _writeScreenBuffer(0x26, value); // set previous
  35. }
  36. void GxEPD2_1160_T91::_writeScreenBuffer(uint8_t command, uint8_t value)
  37. {
  38. if (!_init_display_done) _InitDisplay();
  39. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  40. _writeCommand(command);
  41. _startTransfer();
  42. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  43. {
  44. _transfer(value);
  45. }
  46. _endTransfer();
  47. }
  48. void GxEPD2_1160_T91::writeImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  49. {
  50. _writeImage(0x24, bitmap, x, y, w, h, invert, mirror_y, pgm);
  51. }
  52. void GxEPD2_1160_T91::writeImageForFullRefresh(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  53. {
  54. _writeImage(0x26, bitmap, x, y, w, h, invert, mirror_y, pgm); // set previous
  55. _writeImage(0x24, bitmap, x, y, w, h, invert, mirror_y, pgm); // set current
  56. }
  57. void GxEPD2_1160_T91::writeImageAgain(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  58. {
  59. _writeImage(0x26, bitmap, x, y, w, h, invert, mirror_y, pgm); // set previous
  60. _writeImage(0x24, bitmap, x, y, w, h, invert, mirror_y, pgm); // set current
  61. }
  62. void GxEPD2_1160_T91::writeImageToPrevious(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  63. {
  64. _writeImage(0x26, bitmap, x, y, w, h, invert, mirror_y, pgm);
  65. }
  66. void GxEPD2_1160_T91::_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)
  67. {
  68. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  69. int32_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
  70. x -= x % 8; // byte boundary
  71. w = wb * 8; // byte boundary
  72. int16_t x1 = x < 0 ? 0 : x; // limit
  73. int16_t y1 = y < 0 ? 0 : y; // limit
  74. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  75. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  76. int16_t dx = x1 - x;
  77. int16_t dy = y1 - y;
  78. w1 -= dx;
  79. h1 -= dy;
  80. if ((w1 <= 0) || (h1 <= 0)) return;
  81. if (!_init_display_done) _InitDisplay();
  82. if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
  83. _setPartialRamArea(x1, y1, w1, h1);
  84. _writeCommand(command);
  85. _startTransfer();
  86. for (int16_t i = 0; i < h1; i++)
  87. {
  88. for (int16_t j = 0; j < w1 / 8; j++)
  89. {
  90. uint8_t data;
  91. // use wb, h of bitmap for index!
  92. int32_t idx = mirror_y ? j + dx / 8 + ((h - 1 - (i + dy))) * wb : j + dx / 8 + (i + dy) * wb;
  93. if (pgm)
  94. {
  95. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  96. data = pgm_read_byte(&bitmap[idx]);
  97. #else
  98. data = bitmap[idx];
  99. #endif
  100. }
  101. else
  102. {
  103. data = bitmap[idx];
  104. }
  105. if (invert) data = ~data;
  106. _transfer(data);
  107. }
  108. }
  109. _endTransfer();
  110. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  111. }
  112. void GxEPD2_1160_T91::writeImagePart(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(0x24, bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  116. }
  117. void GxEPD2_1160_T91::writeImagePartAgain(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. _writeImagePart(0x26, bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm); // set previous
  121. _writeImagePart(0x24, bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm); // set current
  122. }
  123. void GxEPD2_1160_T91::writeImagePartToPrevious(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  124. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  125. {
  126. _writeImagePart(0x26, bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  127. }
  128. void GxEPD2_1160_T91::_writeImagePart(uint8_t command, const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  129. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  130. {
  131. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  132. if ((w_bitmap < 0) || (h_bitmap < 0) || (w < 0) || (h < 0)) return;
  133. if ((x_part < 0) || (x_part >= w_bitmap)) return;
  134. if ((y_part < 0) || (y_part >= h_bitmap)) return;
  135. int32_t wb_bitmap = (w_bitmap + 7) / 8; // width bytes, bitmaps are padded
  136. x_part -= x_part % 8; // byte boundary
  137. w = w_bitmap - x_part < w ? w_bitmap - x_part : w; // limit
  138. h = h_bitmap - y_part < h ? h_bitmap - y_part : h; // limit
  139. x -= x % 8; // byte boundary
  140. w = 8 * ((w + 7) / 8); // byte boundary, bitmaps are padded
  141. int16_t x1 = x < 0 ? 0 : x; // limit
  142. int16_t y1 = y < 0 ? 0 : y; // limit
  143. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  144. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  145. int16_t dx = x1 - x;
  146. int16_t dy = y1 - y;
  147. w1 -= dx;
  148. h1 -= dy;
  149. if ((w1 <= 0) || (h1 <= 0)) return;
  150. if (!_init_display_done) _InitDisplay();
  151. if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
  152. _setPartialRamArea(x1, y1, w1, h1);
  153. _writeCommand(command);
  154. _startTransfer();
  155. for (int16_t i = 0; i < h1; i++)
  156. {
  157. for (int16_t j = 0; j < w1 / 8; j++)
  158. {
  159. uint8_t data;
  160. // use wb_bitmap, h_bitmap of bitmap for index!
  161. int32_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;
  162. if (pgm)
  163. {
  164. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  165. data = pgm_read_byte(&bitmap[idx]);
  166. #else
  167. data = bitmap[idx];
  168. #endif
  169. }
  170. else
  171. {
  172. data = bitmap[idx];
  173. }
  174. if (invert) data = ~data;
  175. _transfer(data);
  176. }
  177. }
  178. _endTransfer();
  179. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  180. }
  181. void GxEPD2_1160_T91::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)
  182. {
  183. if (black)
  184. {
  185. writeImage(black, x, y, w, h, invert, mirror_y, pgm);
  186. }
  187. }
  188. void GxEPD2_1160_T91::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,
  189. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  190. {
  191. if (black)
  192. {
  193. writeImagePart(black, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  194. }
  195. }
  196. void GxEPD2_1160_T91::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)
  197. {
  198. if (data1)
  199. {
  200. writeImage(data1, x, y, w, h, invert, mirror_y, pgm);
  201. }
  202. }
  203. void GxEPD2_1160_T91::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  204. {
  205. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  206. refresh(x, y, w, h);
  207. writeImageAgain(bitmap, x, y, w, h, invert, mirror_y, pgm);
  208. }
  209. void GxEPD2_1160_T91::drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  210. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  211. {
  212. writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  213. refresh(x, y, w, h);
  214. writeImagePartAgain(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  215. }
  216. void GxEPD2_1160_T91::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)
  217. {
  218. if (black)
  219. {
  220. drawImage(black, x, y, w, h, invert, mirror_y, pgm);
  221. }
  222. }
  223. void GxEPD2_1160_T91::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,
  224. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  225. {
  226. if (black)
  227. {
  228. drawImagePart(black, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  229. }
  230. }
  231. void GxEPD2_1160_T91::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)
  232. {
  233. if (data1)
  234. {
  235. drawImage(data1, x, y, w, h, invert, mirror_y, pgm);
  236. }
  237. }
  238. void GxEPD2_1160_T91::refresh(bool partial_update_mode)
  239. {
  240. if (partial_update_mode) refresh(0, 0, WIDTH, HEIGHT);
  241. else
  242. {
  243. _Update_Full();
  244. _initial_refresh = false; // initial full update done
  245. }
  246. }
  247. void GxEPD2_1160_T91::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
  248. {
  249. if (_initial_refresh) return refresh(false); // initial update needs be full update
  250. // intersection with screen
  251. int16_t w1 = x < 0 ? w + x : w; // reduce
  252. int16_t h1 = y < 0 ? h + y : h; // reduce
  253. int16_t x1 = x < 0 ? 0 : x; // limit
  254. int16_t y1 = y < 0 ? 0 : y; // limit
  255. w1 = x1 + w1 < int16_t(WIDTH) ? w1 : int16_t(WIDTH) - x1; // limit
  256. h1 = y1 + h1 < int16_t(HEIGHT) ? h1 : int16_t(HEIGHT) - y1; // limit
  257. if ((w1 <= 0) || (h1 <= 0)) return;
  258. // make x1, w1 multiple of 8
  259. w1 += x1 % 8;
  260. if (w1 % 8 > 0) w1 += 8 - w1 % 8;
  261. x1 -= x1 % 8;
  262. _setPartialRamArea(x1, y1, w1, h1);
  263. _Update_Part();
  264. }
  265. void GxEPD2_1160_T91::powerOff()
  266. {
  267. _PowerOff();
  268. }
  269. void GxEPD2_1160_T91::hibernate()
  270. {
  271. _PowerOff();
  272. if (_rst >= 0)
  273. {
  274. _writeCommand(0x10); // deep sleep mode
  275. _writeData(0x3); // enter deep sleep
  276. _hibernating = true;
  277. _init_display_done = false;
  278. }
  279. }
  280. void GxEPD2_1160_T91::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  281. {
  282. _writeCommand(0x11); // set ram entry mode
  283. _writeData(0x03); // x increase, y increase : normal mode
  284. _writeCommand(0x44);
  285. _writeData(x % 256);
  286. _writeData(x / 256);
  287. _writeData((x + w - 1) % 256);
  288. _writeData((x + w - 1) / 256);
  289. _writeCommand(0x45);
  290. _writeData(y % 256);
  291. _writeData(y / 256);
  292. _writeData((y + h - 1) % 256);
  293. _writeData((y + h - 1) / 256);
  294. _writeCommand(0x4e);
  295. _writeData(x % 256);
  296. _writeData(x / 256);
  297. _writeCommand(0x4f);
  298. _writeData(y % 256);
  299. _writeData(y / 256);
  300. }
  301. void GxEPD2_1160_T91::_PowerOn()
  302. {
  303. if (!_power_is_on)
  304. {
  305. _writeCommand(0x22);
  306. _writeData(0xc0);
  307. _writeCommand(0x20);
  308. _waitWhileBusy("_PowerOn", power_on_time);
  309. }
  310. _power_is_on = true;
  311. }
  312. void GxEPD2_1160_T91::_PowerOff()
  313. {
  314. if (_power_is_on)
  315. {
  316. _writeCommand(0x22);
  317. _writeData(0x83);
  318. _writeCommand(0x20);
  319. _waitWhileBusy("_PowerOff", power_off_time);
  320. }
  321. _power_is_on = false;
  322. _using_partial_mode = false;
  323. }
  324. void GxEPD2_1160_T91::_InitDisplay()
  325. {
  326. if (_hibernating) _reset();
  327. delay(10); // 10ms according to specs
  328. _writeCommand(0x12); //SWRESET
  329. delay(10); // 10ms according to specs
  330. _writeCommand(0x0C); // Soft start setting
  331. _writeData(0xAE);
  332. _writeData(0xC7);
  333. _writeData(0xC3);
  334. _writeData(0xC0);
  335. _writeData(0x40);
  336. _writeCommand(0x01); // Set MUX as 639
  337. _writeData(0x7F);
  338. _writeData(0x02);
  339. _writeData(0x00);
  340. _writeCommand(0x3C); // VBD
  341. _writeData(0x01); // LUT1, for white
  342. _writeCommand(0x18); //Read built-in temperature sensor
  343. _writeData(0x80);
  344. _writeCommand(0x22);
  345. _writeData(0xB1); //Load Temperature and waveform setting.
  346. _writeCommand(0x20);
  347. _waitWhileBusy("_InitDisplay", power_on_time);
  348. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  349. _using_partial_mode = false;
  350. _init_display_done = true;
  351. }
  352. const uint8_t GxEPD2_1160_T91::lut_partial[] PROGMEM =
  353. {
  354. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //1
  355. 0x01, 0x2A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  356. 0x0A, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //3
  357. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  358. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //5
  359. 0x00, 0x00, 0x05, 0x05, 0x00, 0x05, 0x03, 0x05, 0x05, 0x00,
  360. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //7
  361. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  362. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //9
  363. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  364. 0x22, 0x22, 0x22, 0x22, 0x22
  365. };
  366. void GxEPD2_1160_T91::_Init_Part()
  367. {
  368. if (hasFastPartialUpdate)
  369. {
  370. _writeCommand(0x3C); // Border Waveform Control
  371. _writeData(0xC0); // HiZ, [POR], floating
  372. _writeCommand(0x32);
  373. _writeDataPGM(lut_partial, sizeof(lut_partial));
  374. }
  375. _using_partial_mode = true;
  376. }
  377. void GxEPD2_1160_T91::_Update_Full()
  378. {
  379. _using_partial_mode = false;
  380. _PowerOn();
  381. _writeCommand(0x22);
  382. _writeData(0xf4);
  383. _writeCommand(0x20);
  384. _waitWhileBusy("_Update_Full", full_refresh_time);
  385. }
  386. void GxEPD2_1160_T91::_Update_Part()
  387. {
  388. if (!_using_partial_mode) _Init_Part();
  389. _PowerOn();
  390. _writeCommand(0x22);
  391. _writeData(0xcc);
  392. _writeCommand(0x20);
  393. _waitWhileBusy("_Update_Part", hasFastPartialUpdate ? partial_refresh_time : full_refresh_time);
  394. }