GxEPD2_213_BN.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. // Panel: DEPG0213BN : https://www.dke.top/products/213-eiink-display
  5. // Controller : SSD1680 : https://www.good-display.com/companyfile/101.html
  6. // Display: LILYGO® T5 V2.3.1 2.13 inch : https://www.aliexpress.com/item/32869729970.html
  7. //
  8. // Author: Jean-Marc Zingg
  9. //
  10. // Version: see library.properties
  11. //
  12. // Library: https://github.com/ZinggJM/GxEPD2
  13. #include "GxEPD2_213_BN.h"
  14. GxEPD2_213_BN::GxEPD2_213_BN(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_213_BN::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_213_BN::writeScreenBuffer(uint8_t value)
  27. {
  28. if (_initial_write) return clearScreen(value);
  29. _writeScreenBuffer(0x24, value); // set current
  30. }
  31. void GxEPD2_213_BN::writeScreenBufferAgain(uint8_t value)
  32. {
  33. _writeScreenBuffer(0x24, value); // set current
  34. _writeScreenBuffer(0x26, value); // set previous
  35. }
  36. void GxEPD2_213_BN::_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_213_BN::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_213_BN::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_213_BN::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_213_BN::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_213_BN::_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. int16_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. int16_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_213_BN::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_213_BN::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_213_BN::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_213_BN::_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. int16_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. 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;
  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_213_BN::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_213_BN::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_213_BN::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_213_BN::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_213_BN::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_213_BN::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_213_BN::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_213_BN::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_213_BN::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_213_BN::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_213_BN::powerOff()
  266. {
  267. _PowerOff();
  268. }
  269. void GxEPD2_213_BN::hibernate()
  270. {
  271. _PowerOff();
  272. if (_rst >= 0)
  273. {
  274. _writeCommand(0x10); // deep sleep mode
  275. _writeData(0x1); // enter deep sleep
  276. _hibernating = true;
  277. _init_display_done = false;
  278. }
  279. }
  280. void GxEPD2_213_BN::_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 / 8);
  286. _writeData((x + w - 1) / 8);
  287. _writeCommand(0x45);
  288. _writeData(y % 256);
  289. _writeData(y / 256);
  290. _writeData((y + h - 1) % 256);
  291. _writeData((y + h - 1) / 256);
  292. _writeCommand(0x4e);
  293. _writeData(x / 8);
  294. _writeCommand(0x4f);
  295. _writeData(y % 256);
  296. _writeData(y / 256);
  297. }
  298. void GxEPD2_213_BN::_PowerOn()
  299. {
  300. if (!_power_is_on)
  301. {
  302. _writeCommand(0x22);
  303. _writeData(0xf8);
  304. _writeCommand(0x20);
  305. _waitWhileBusy("_PowerOn", power_on_time);
  306. }
  307. _power_is_on = true;
  308. }
  309. void GxEPD2_213_BN::_PowerOff()
  310. {
  311. if (_power_is_on)
  312. {
  313. _writeCommand(0x22);
  314. _writeData(0x83);
  315. _writeCommand(0x20);
  316. _waitWhileBusy("_PowerOff", power_off_time);
  317. }
  318. _power_is_on = false;
  319. _using_partial_mode = false;
  320. }
  321. void GxEPD2_213_BN::_InitDisplay()
  322. {
  323. if (_hibernating) _reset();
  324. delay(10); // 10ms according to specs
  325. _writeCommand(0x12); //SWRESET
  326. delay(10); // 10ms according to specs
  327. _writeCommand(0x01); //Driver output control
  328. _writeData(0x27);
  329. _writeData(0x01);
  330. _writeData(0x00);
  331. _writeCommand(0x11); //data entry mode
  332. _writeData(0x03);
  333. _writeCommand(0x3C); //BorderWavefrom
  334. _writeData(0x05);
  335. _writeCommand(0x21); // Display update control
  336. _writeData(0x00);
  337. _writeData(0x80);
  338. _writeCommand(0x18); //Read built-in temperature sensor
  339. _writeData(0x80);
  340. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  341. _using_partial_mode = false;
  342. _init_display_done = true;
  343. }
  344. const unsigned char GxEPD2_213_BN::lut_partial[] PROGMEM =
  345. {
  346. 0x0, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  347. 0x80, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  348. 0x40, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  349. 0x0, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  350. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  351. 0x0A, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2,
  352. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  353. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  354. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  355. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  356. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  357. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  358. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  359. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  360. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  361. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  362. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  363. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x0, 0x0, 0x0,
  364. };
  365. void GxEPD2_213_BN::_Init_Part()
  366. {
  367. _writeCommand(0x32);
  368. _writeDataPGM(lut_partial, sizeof(lut_partial));
  369. _using_partial_mode = true;
  370. }
  371. void GxEPD2_213_BN::_Update_Full()
  372. {
  373. _using_partial_mode = false;
  374. _PowerOn();
  375. _writeCommand(0x22);
  376. _writeData(0xf4);
  377. _writeCommand(0x20);
  378. _waitWhileBusy("_Update_Full", full_refresh_time);
  379. }
  380. void GxEPD2_213_BN::_Update_Part()
  381. {
  382. if (!_using_partial_mode) _Init_Part();
  383. _PowerOn();
  384. _writeCommand(0x22);
  385. _writeData(0xcc);
  386. _writeCommand(0x20);
  387. _waitWhileBusy("_Update_Part", partial_refresh_time);
  388. }