GxEPD2_290c.cpp 12 KB

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