GxEPD2_397_GDEM0397T81.cpp 15 KB

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