GxEPD2_750c_Z08.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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: http://www.e-paper-display.com/download_detail/downloadsId=808.html
  5. // Panel: GDEW075Z08 : http://www.e-paper-display.com/products_detail/productId=457.html
  6. // Controller: GD7965 : http://www.e-paper-display.com/download_detail/downloadsId=821.html
  7. //
  8. // Author: Jean-Marc Zingg
  9. //
  10. // Version: see library.properties
  11. //
  12. // Library: https://github.com/ZinggJM/GxEPD2
  13. #include "GxEPD2_750c_Z08.h"
  14. GxEPD2_750c_Z08::GxEPD2_750c_Z08(int16_t cs, int16_t dc, int16_t rst, int16_t busy) :
  15. GxEPD2_EPD(cs, dc, rst, busy, LOW, 20000000, WIDTH, HEIGHT, panel, hasColor, hasPartialUpdate, hasFastPartialUpdate)
  16. {
  17. }
  18. void GxEPD2_750c_Z08::clearScreen(uint8_t value)
  19. {
  20. clearScreen(value, 0xFF);
  21. }
  22. void GxEPD2_750c_Z08::clearScreen(uint8_t black_value, uint8_t color_value)
  23. {
  24. _initial_write = false; // initial full screen buffer clean done
  25. _Init_Part();
  26. _writeCommand(0x91); // partial in
  27. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  28. _writeCommand(0x10);
  29. _startTransfer();
  30. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  31. {
  32. _transfer(black_value);
  33. }
  34. _endTransfer();
  35. _writeCommand(0x13);
  36. _startTransfer();
  37. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  38. {
  39. _transfer(~color_value);
  40. }
  41. _endTransfer();
  42. _Update_Part();
  43. _writeCommand(0x92); // partial out
  44. }
  45. void GxEPD2_750c_Z08::writeScreenBuffer(uint8_t value)
  46. {
  47. writeScreenBuffer(value, 0xFF);
  48. }
  49. void GxEPD2_750c_Z08::writeScreenBuffer(uint8_t black_value, uint8_t color_value)
  50. {
  51. _initial_write = false; // initial full screen buffer clean done
  52. _Init_Part();
  53. _writeCommand(0x91); // partial in
  54. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  55. _writeCommand(0x10);
  56. _startTransfer();
  57. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  58. {
  59. _transfer(black_value);
  60. }
  61. _endTransfer();
  62. _writeCommand(0x13);
  63. _startTransfer();
  64. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  65. {
  66. _transfer(~color_value);
  67. }
  68. _endTransfer();
  69. _writeCommand(0x92); // partial out
  70. }
  71. void GxEPD2_750c_Z08::writeImage(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. writeImage(bitmap, NULL, x, y, w, h, invert, mirror_y, pgm);
  74. }
  75. void GxEPD2_750c_Z08::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)
  76. {
  77. if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
  78. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  79. uint16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
  80. x -= x % 8; // byte boundary
  81. w = wb * 8; // byte boundary
  82. int16_t x1 = x < 0 ? 0 : x; // limit
  83. int16_t y1 = y < 0 ? 0 : y; // limit
  84. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  85. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  86. int16_t dx = x1 - x;
  87. int16_t dy = y1 - y;
  88. w1 -= dx;
  89. h1 -= dy;
  90. if ((w1 <= 0) || (h1 <= 0)) return;
  91. _Init_Part();
  92. _writeCommand(0x91); // partial in
  93. _setPartialRamArea(x1, y1, w1, h1);
  94. _writeCommand(0x10);
  95. _startTransfer();
  96. for (int16_t i = 0; i < h1; i++)
  97. {
  98. for (int16_t j = 0; j < w1 / 8; j++)
  99. {
  100. uint8_t data = 0xFF;
  101. if (black)
  102. {
  103. // use wb, h of bitmap for index!
  104. uint16_t idx = mirror_y ? j + dx / 8 + uint16_t((h - 1 - (i + dy))) * wb : j + dx / 8 + uint16_t(i + dy) * wb;
  105. if (pgm)
  106. {
  107. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  108. data = pgm_read_byte(&black[idx]);
  109. #else
  110. data = black[idx];
  111. #endif
  112. }
  113. else
  114. {
  115. data = black[idx];
  116. }
  117. if (invert) data = ~data;
  118. }
  119. _transfer(data);
  120. }
  121. }
  122. _endTransfer();
  123. _writeCommand(0x13);
  124. _startTransfer();
  125. for (int16_t i = 0; i < h1; i++)
  126. {
  127. for (int16_t j = 0; j < w1 / 8; j++)
  128. {
  129. uint8_t data = 0xFF;
  130. if (color)
  131. {
  132. // use wb, h of bitmap for index!
  133. uint16_t idx = mirror_y ? j + dx / 8 + uint16_t((h - 1 - (i + dy))) * wb : j + dx / 8 + uint16_t(i + dy) * wb;
  134. if (pgm)
  135. {
  136. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  137. data = pgm_read_byte(&color[idx]);
  138. #else
  139. data = color[idx];
  140. #endif
  141. }
  142. else
  143. {
  144. data = color[idx];
  145. }
  146. if (invert) data = ~data;
  147. }
  148. _transfer(~data);
  149. }
  150. }
  151. _endTransfer();
  152. _writeCommand(0x92); // partial out
  153. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  154. }
  155. void GxEPD2_750c_Z08::writeImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  156. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  157. {
  158. writeImagePart(bitmap, NULL, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  159. }
  160. void GxEPD2_750c_Z08::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,
  161. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  162. {
  163. if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
  164. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  165. if ((w_bitmap < 0) || (h_bitmap < 0) || (w < 0) || (h < 0)) return;
  166. if ((x_part < 0) || (x_part >= w_bitmap)) return;
  167. if ((y_part < 0) || (y_part >= h_bitmap)) return;
  168. uint16_t wb_bitmap = (w_bitmap + 7) / 8; // width bytes, bitmaps are padded
  169. x_part -= x_part % 8; // byte boundary
  170. w = w_bitmap - x_part < w ? w_bitmap - x_part : w; // limit
  171. h = h_bitmap - y_part < h ? h_bitmap - y_part : h; // limit
  172. x -= x % 8; // byte boundary
  173. w = 8 * ((w + 7) / 8); // byte boundary, bitmaps are padded
  174. int16_t x1 = x < 0 ? 0 : x; // limit
  175. int16_t y1 = y < 0 ? 0 : y; // limit
  176. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  177. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  178. int16_t dx = x1 - x;
  179. int16_t dy = y1 - y;
  180. w1 -= dx;
  181. h1 -= dy;
  182. if ((w1 <= 0) || (h1 <= 0)) return;
  183. if (!_using_partial_mode) _Init_Part();
  184. _writeCommand(0x91); // partial in
  185. _setPartialRamArea(x1, y1, w1, h1);
  186. _writeCommand(0x10);
  187. _startTransfer();
  188. for (int16_t i = 0; i < h1; i++)
  189. {
  190. for (int16_t j = 0; j < w1 / 8; j++)
  191. {
  192. uint8_t data;
  193. // use wb_bitmap, h_bitmap of bitmap for index!
  194. uint16_t idx = mirror_y ? x_part / 8 + j + dx / 8 + uint16_t((h_bitmap - 1 - (y_part + i + dy))) * wb_bitmap : x_part / 8 + j + dx / 8 + uint16_t(y_part + i + dy) * wb_bitmap;
  195. if (pgm)
  196. {
  197. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  198. data = pgm_read_byte(&black[idx]);
  199. #else
  200. data = black[idx];
  201. #endif
  202. }
  203. else
  204. {
  205. data = black[idx];
  206. }
  207. if (invert) data = ~data;
  208. _transfer(data);
  209. }
  210. }
  211. _endTransfer();
  212. _writeCommand(0x13);
  213. _startTransfer();
  214. for (int16_t i = 0; i < h1; i++)
  215. {
  216. for (int16_t j = 0; j < w1 / 8; j++)
  217. {
  218. uint8_t data = 0xFF;
  219. if (color)
  220. {
  221. // use wb_bitmap, h_bitmap of bitmap for index!
  222. uint16_t idx = mirror_y ? x_part / 8 + j + dx / 8 + uint16_t((h_bitmap - 1 - (y_part + i + dy))) * wb_bitmap : x_part / 8 + j + dx / 8 + uint16_t(y_part + i + dy) * wb_bitmap;
  223. if (pgm)
  224. {
  225. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  226. data = pgm_read_byte(&color[idx]);
  227. #else
  228. data = color[idx];
  229. #endif
  230. }
  231. else
  232. {
  233. data = color[idx];
  234. }
  235. if (invert) data = ~data;
  236. }
  237. _transfer(~data);
  238. }
  239. }
  240. _endTransfer();
  241. _writeCommand(0x92); // partial out
  242. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  243. }
  244. void GxEPD2_750c_Z08::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)
  245. {
  246. if (data1)
  247. {
  248. writeImage(data1, x, y, w, h, invert, mirror_y, pgm);
  249. }
  250. }
  251. void GxEPD2_750c_Z08::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  252. {
  253. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  254. refresh(x, y, w, h);
  255. }
  256. void GxEPD2_750c_Z08::drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  257. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  258. {
  259. writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  260. refresh(x, y, w, h);
  261. }
  262. void GxEPD2_750c_Z08::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)
  263. {
  264. writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  265. refresh(x, y, w, h);
  266. }
  267. void GxEPD2_750c_Z08::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,
  268. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  269. {
  270. writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  271. refresh(x, y, w, h);
  272. }
  273. void GxEPD2_750c_Z08::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)
  274. {
  275. writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  276. refresh(x, y, w, h);
  277. }
  278. void GxEPD2_750c_Z08::refresh(bool partial_update_mode)
  279. {
  280. if (partial_update_mode) refresh(0, 0, WIDTH, HEIGHT);
  281. else _Update_Full();
  282. }
  283. void GxEPD2_750c_Z08::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
  284. {
  285. // intersection with screen
  286. int16_t w1 = x < 0 ? w + x : w; // reduce
  287. int16_t h1 = y < 0 ? h + y : h; // reduce
  288. int16_t x1 = x < 0 ? 0 : x; // limit
  289. int16_t y1 = y < 0 ? 0 : y; // limit
  290. w1 = x1 + w1 < int16_t(WIDTH) ? w1 : int16_t(WIDTH) - x1; // limit
  291. h1 = y1 + h1 < int16_t(HEIGHT) ? h1 : int16_t(HEIGHT) - y1; // limit
  292. if ((w1 <= 0) || (h1 <= 0)) return;
  293. // make x1, w1 multiple of 8
  294. w1 += x1 % 8;
  295. if (w1 % 8 > 0) w1 += 8 - w1 % 8;
  296. x1 -= x1 % 8;
  297. _Init_Part();
  298. if (usePartialUpdate) _writeCommand(0x91); // partial in
  299. _setPartialRamArea(x1, y1, w1, h1);
  300. _Update_Part();
  301. if (usePartialUpdate) _writeCommand(0x92); // partial out
  302. }
  303. void GxEPD2_750c_Z08::powerOff()
  304. {
  305. _PowerOff();
  306. }
  307. void GxEPD2_750c_Z08::hibernate()
  308. {
  309. _PowerOff();
  310. if (_rst >= 0)
  311. {
  312. _writeCommand(0x07); // deep sleep
  313. _writeData(0xA5); // check code
  314. _hibernating = true;
  315. }
  316. }
  317. void GxEPD2_750c_Z08::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  318. {
  319. uint16_t xe = (x + w - 1) | 0x0007; // byte boundary inclusive (last byte)
  320. uint16_t ye = y + h - 1;
  321. x &= 0xFFF8; // byte boundary
  322. xe |= 0x0007; // byte boundary
  323. _writeCommand(0x90); // partial window
  324. _writeData(x / 256);
  325. _writeData(x % 256);
  326. _writeData(xe / 256);
  327. _writeData(xe % 256);
  328. _writeData(y / 256);
  329. _writeData(y % 256);
  330. _writeData(ye / 256);
  331. _writeData(ye % 256);
  332. _writeData(0x00);
  333. }
  334. void GxEPD2_750c_Z08::_PowerOn()
  335. {
  336. if (!_power_is_on)
  337. {
  338. _writeCommand(0x04);
  339. _waitWhileBusy("_PowerOn", power_on_time);
  340. }
  341. _power_is_on = true;
  342. }
  343. void GxEPD2_750c_Z08::_PowerOff()
  344. {
  345. _writeCommand(0x02); // power off
  346. _waitWhileBusy("_PowerOff", power_off_time);
  347. _power_is_on = false;
  348. }
  349. void GxEPD2_750c_Z08::_InitDisplay()
  350. {
  351. if (_hibernating) _reset();
  352. _writeCommand(0x01); // POWER SETTING
  353. _writeData (0x07);
  354. _writeData (0x07); // VGH=20V,VGL=-20V
  355. _writeData (0x3f); // VDH=15V
  356. _writeData (0x3f); // VDL=-15V
  357. _writeCommand(0x00); //PANEL SETTING
  358. _writeData(0x0f); //KW: 3f, KWR: 2F, BWROTP: 0f, BWOTP: 1f
  359. _writeCommand(0x61); //tres
  360. _writeData (WIDTH / 256); //source 800
  361. _writeData (WIDTH % 256);
  362. _writeData (HEIGHT / 256); //gate 480
  363. _writeData (HEIGHT % 256);
  364. _writeCommand(0x15);
  365. _writeData(0x00);
  366. _writeCommand(0x50); //VCOM AND DATA INTERVAL SETTING
  367. _writeData(0x11);
  368. _writeData(0x07);
  369. _writeCommand(0x60); //TCON SETTING
  370. _writeData(0x22);
  371. }
  372. void GxEPD2_750c_Z08::_Init_Full()
  373. {
  374. _InitDisplay();
  375. _PowerOn();
  376. }
  377. void GxEPD2_750c_Z08::_Init_Part()
  378. {
  379. _InitDisplay();
  380. _PowerOn();
  381. }
  382. void GxEPD2_750c_Z08::_Update_Full()
  383. {
  384. _writeCommand(0x12); //display refresh
  385. _waitWhileBusy("_Update_Full", full_refresh_time);
  386. }
  387. void GxEPD2_750c_Z08::_Update_Part()
  388. {
  389. _writeCommand(0x12); //display refresh
  390. _waitWhileBusy("_Update_Part", partial_refresh_time);
  391. }