GxEPD2_154_Z90c.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // Display Library for SPI e-paper panels from Dalian Good Display and boards from Waveshare.
  2. // Requires HW SPI and Adafruit_GFX. Caution: these e-papers require 3.3V supply AND data lines!
  3. //
  4. // based on Demo Example from Good Display: http://www.e-paper-display.com/download_list/downloadcategoryid=34&isMode=false.html
  5. // Controller: SSD1682 : https://www.good-display.com/companyfile/203.html
  6. //
  7. // Author: Jean-Marc Zingg
  8. //
  9. // Version: see library.properties
  10. //
  11. // Library: https://github.com/ZinggJM/GxEPD2
  12. #include "GxEPD2_154_Z90c.h"
  13. GxEPD2_154_Z90c::GxEPD2_154_Z90c(int8_t cs, int8_t dc, int8_t rst, int8_t busy) :
  14. GxEPD2_EPD(cs, dc, rst, busy, HIGH, 20000000, WIDTH, HEIGHT, panel, hasColor, hasPartialUpdate, hasFastPartialUpdate)
  15. {
  16. }
  17. void GxEPD2_154_Z90c::clearScreen(uint8_t value)
  18. {
  19. clearScreen(value, 0xFF);
  20. }
  21. void GxEPD2_154_Z90c::clearScreen(uint8_t black_value, uint8_t color_value)
  22. {
  23. _initial_write = false; // initial full screen buffer clean done
  24. _Init_Part();
  25. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  26. _writeCommand(0x24);
  27. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  28. {
  29. _writeData(black_value);
  30. }
  31. _writeCommand(0x26);
  32. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  33. {
  34. _writeData(~color_value);
  35. }
  36. _Update_Part();
  37. }
  38. void GxEPD2_154_Z90c::writeScreenBuffer(uint8_t value)
  39. {
  40. writeScreenBuffer(value, 0xFF);
  41. }
  42. void GxEPD2_154_Z90c::writeScreenBuffer(uint8_t black_value, uint8_t color_value)
  43. {
  44. _initial_write = false; // initial full screen buffer clean done
  45. _Init_Part();
  46. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  47. _writeCommand(0x24);
  48. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  49. {
  50. _writeData(black_value);
  51. }
  52. _writeCommand(0x26);
  53. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  54. {
  55. _writeData(~color_value);
  56. }
  57. }
  58. void GxEPD2_154_Z90c::writeImage(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(bitmap, NULL, x, y, w, h, invert, mirror_y, pgm);
  61. }
  62. void GxEPD2_154_Z90c::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)
  63. {
  64. if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
  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. _Init_Part();
  79. _setPartialRamArea(x1, y1, w1, h1);
  80. _writeCommand(0x24);
  81. for (int16_t i = 0; i < h1; i++)
  82. {
  83. for (int16_t j = 0; j < w1 / 8; j++)
  84. {
  85. uint8_t data = 0xFF;
  86. if (black)
  87. {
  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(&black[idx]);
  94. #else
  95. data = black[idx];
  96. #endif
  97. }
  98. else
  99. {
  100. data = black[idx];
  101. }
  102. if (invert) data = ~data;
  103. }
  104. _writeData(data);
  105. }
  106. }
  107. _writeCommand(0x26);
  108. for (int16_t i = 0; i < h1; i++)
  109. {
  110. for (int16_t j = 0; j < w1 / 8; j++)
  111. {
  112. uint8_t data = 0xFF;
  113. if (color)
  114. {
  115. // use wb, h of bitmap for index!
  116. int16_t idx = mirror_y ? j + dx / 8 + ((h - 1 - (i + dy))) * wb : j + dx / 8 + (i + dy) * wb;
  117. if (pgm)
  118. {
  119. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  120. data = pgm_read_byte(&color[idx]);
  121. #else
  122. data = color[idx];
  123. #endif
  124. }
  125. else
  126. {
  127. data = color[idx];
  128. }
  129. if (invert) data = ~data;
  130. }
  131. _writeData(~data);
  132. }
  133. }
  134. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  135. }
  136. void GxEPD2_154_Z90c::writeImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  137. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  138. {
  139. writeImagePart(bitmap, NULL, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  140. }
  141. void GxEPD2_154_Z90c::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,
  142. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  143. {
  144. if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
  145. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  146. if ((w_bitmap < 0) || (h_bitmap < 0) || (w < 0) || (h < 0)) return;
  147. if ((x_part < 0) || (x_part >= w_bitmap)) return;
  148. if ((y_part < 0) || (y_part >= h_bitmap)) return;
  149. int16_t wb_bitmap = (w_bitmap + 7) / 8; // width bytes, bitmaps are padded
  150. x_part -= x_part % 8; // byte boundary
  151. w = w_bitmap - x_part < w ? w_bitmap - x_part : w; // limit
  152. h = h_bitmap - y_part < h ? h_bitmap - y_part : h; // limit
  153. x -= x % 8; // byte boundary
  154. w = 8 * ((w + 7) / 8); // byte boundary, bitmaps are padded
  155. int16_t x1 = x < 0 ? 0 : x; // limit
  156. int16_t y1 = y < 0 ? 0 : y; // limit
  157. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  158. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  159. int16_t dx = x1 - x;
  160. int16_t dy = y1 - y;
  161. w1 -= dx;
  162. h1 -= dy;
  163. if ((w1 <= 0) || (h1 <= 0)) return;
  164. if (!_using_partial_mode) _Init_Part();
  165. _setPartialRamArea(x1, y1, w1, h1);
  166. _writeCommand(0x24);
  167. for (int16_t i = 0; i < h1; i++)
  168. {
  169. for (int16_t j = 0; j < w1 / 8; j++)
  170. {
  171. uint8_t data;
  172. // use wb_bitmap, h_bitmap of bitmap for index!
  173. 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;
  174. if (pgm)
  175. {
  176. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  177. data = pgm_read_byte(&black[idx]);
  178. #else
  179. data = black[idx];
  180. #endif
  181. }
  182. else
  183. {
  184. data = black[idx];
  185. }
  186. if (invert) data = ~data;
  187. _writeData(data);
  188. }
  189. }
  190. _writeCommand(0x26);
  191. for (int16_t i = 0; i < h1; i++)
  192. {
  193. for (int16_t j = 0; j < w1 / 8; j++)
  194. {
  195. uint8_t data = 0xFF;
  196. if (color)
  197. {
  198. // use wb_bitmap, h_bitmap of bitmap for index!
  199. 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;
  200. if (pgm)
  201. {
  202. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  203. data = pgm_read_byte(&color[idx]);
  204. #else
  205. data = color[idx];
  206. #endif
  207. }
  208. else
  209. {
  210. data = color[idx];
  211. }
  212. if (invert) data = ~data;
  213. }
  214. _writeData(~data);
  215. }
  216. }
  217. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  218. }
  219. void GxEPD2_154_Z90c::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)
  220. {
  221. if (data1)
  222. {
  223. writeImage(data1, x, y, w, h, invert, mirror_y, pgm);
  224. }
  225. }
  226. void GxEPD2_154_Z90c::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  227. {
  228. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  229. refresh(x, y, w, h);
  230. }
  231. void GxEPD2_154_Z90c::drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  232. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  233. {
  234. writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  235. refresh(x, y, w, h);
  236. }
  237. void GxEPD2_154_Z90c::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)
  238. {
  239. writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  240. refresh(x, y, w, h);
  241. }
  242. void GxEPD2_154_Z90c::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,
  243. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  244. {
  245. writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  246. refresh(x, y, w, h);
  247. }
  248. void GxEPD2_154_Z90c::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)
  249. {
  250. writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  251. refresh(x, y, w, h);
  252. }
  253. void GxEPD2_154_Z90c::refresh(bool partial_update_mode)
  254. {
  255. if (partial_update_mode) refresh(0, 0, WIDTH, HEIGHT);
  256. else _Update_Full();
  257. }
  258. void GxEPD2_154_Z90c::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
  259. {
  260. x -= x % 8; // byte boundary
  261. w -= x % 8; // byte boundary
  262. int16_t x1 = x < 0 ? 0 : x; // limit
  263. int16_t y1 = y < 0 ? 0 : y; // limit
  264. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  265. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  266. w1 -= x1 - x;
  267. h1 -= y1 - y;
  268. _Init_Part();
  269. _setPartialRamArea(x1, y1, w1, h1);
  270. _Update_Part();
  271. }
  272. void GxEPD2_154_Z90c::powerOff()
  273. {
  274. _PowerOff();
  275. }
  276. void GxEPD2_154_Z90c::hibernate()
  277. {
  278. _PowerOff();
  279. if (_rst >= 0)
  280. {
  281. _writeCommand(0x10); // deep sleep
  282. _writeData(0x01); // check code
  283. _hibernating = true;
  284. }
  285. }
  286. void GxEPD2_154_Z90c::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  287. {
  288. uint16_t xe = (x + w - 1);
  289. uint16_t ye = y + h - 1;
  290. _writeCommand(0x44);
  291. _writeData(x / 8);
  292. _writeData(xe / 8);
  293. _writeCommand(0x45);
  294. _writeData(y % 256);
  295. _writeData(y / 256);
  296. _writeData(ye % 256);
  297. _writeData(ye / 256);
  298. _writeCommand(0x4E);
  299. _writeData(x / 8);
  300. _writeCommand(0x4F);
  301. _writeData(y % 256);
  302. _writeData(y / 256);
  303. }
  304. void GxEPD2_154_Z90c::_PowerOn()
  305. {
  306. if (!_power_is_on)
  307. {
  308. _writeCommand(0x22);
  309. _writeData(0xc0);
  310. _writeCommand(0x20);
  311. _waitWhileBusy("_PowerOn", power_on_time);
  312. }
  313. _power_is_on = true;
  314. }
  315. void GxEPD2_154_Z90c::_PowerOff()
  316. {
  317. _writeCommand(0x22);
  318. _writeData(0xc3);
  319. _writeCommand(0x20);
  320. _waitWhileBusy("_PowerOff", power_off_time);
  321. _power_is_on = false;
  322. }
  323. void GxEPD2_154_Z90c::_InitDisplay()
  324. {
  325. if (_hibernating) _reset();
  326. _writeCommand(0x12); //SWRESET
  327. _waitWhileBusy(0, power_on_time);
  328. _writeCommand(0x01); //Driver output control
  329. _writeData(0xC7);
  330. _writeData(0x00);
  331. _writeData(0x00);
  332. _writeCommand(0x11); //data entry mode
  333. _writeData(0x03);
  334. _writeCommand(0x3C); //BorderWavefrom
  335. _writeData(0x05);
  336. _writeCommand(0x18); //Read built-in temperature sensor
  337. _writeData(0x80);
  338. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  339. }
  340. void GxEPD2_154_Z90c::_Init_Full()
  341. {
  342. _InitDisplay();
  343. _PowerOn();
  344. }
  345. void GxEPD2_154_Z90c::_Init_Part()
  346. {
  347. _InitDisplay();
  348. _PowerOn();
  349. }
  350. void GxEPD2_154_Z90c::_Update_Full()
  351. {
  352. _writeCommand(0x22); //Display Update Control
  353. _writeData(0xF7);
  354. _writeCommand(0x20); //Activate Display Update Sequence
  355. _waitWhileBusy("_Update_Full", full_refresh_time);
  356. }
  357. void GxEPD2_154_Z90c::_Update_Part()
  358. {
  359. _writeCommand(0x22); //Display Update Control
  360. _writeData(0xF7);
  361. _writeCommand(0x20); //Activate Display Update Sequence
  362. _waitWhileBusy("_Update_Part", partial_refresh_time);
  363. }