GxEPD2_266_BN.cpp 14 KB

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