GxEPD2_154_D67.cpp 12 KB

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