GxEPD2_150_BN.cpp 14 KB

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