GxEPD2_583c.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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: IL0371 : http://www.e-paper-display.com/download_detail/downloadsId=536.html
  6. //
  7. // Author: Jean-Marc Zingg
  8. //
  9. // Version: see library.properties
  10. //
  11. // Library: https://github.com/ZinggJM/GxEPD2
  12. #include "GxEPD2_583c.h"
  13. GxEPD2_583c::GxEPD2_583c(int16_t cs, int16_t dc, int16_t rst, int16_t busy) :
  14. GxEPD2_EPD(cs, dc, rst, busy, LOW, 40000000, WIDTH, HEIGHT, panel, hasColor, hasPartialUpdate, hasFastPartialUpdate)
  15. {
  16. }
  17. void GxEPD2_583c::clearScreen(uint8_t value)
  18. {
  19. _initial_write = false; // initial full screen buffer clean done
  20. if (value == 0xFF) value = 0x33; // white value for this controller
  21. _Init_Part();
  22. _writeCommand(0x91); // partial in
  23. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  24. _writeCommand(0x10);
  25. _startTransfer();
  26. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 2; i++)
  27. {
  28. _transfer(value);
  29. }
  30. _endTransfer();
  31. _Update_Part();
  32. _writeCommand(0x92); // partial out
  33. }
  34. void GxEPD2_583c::clearScreen(uint8_t black_value, uint8_t color_value)
  35. {
  36. _initial_write = false; // initial full screen buffer clean done
  37. _Init_Part();
  38. _writeCommand(0x91); // partial in
  39. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  40. _writeCommand(0x10);
  41. _startTransfer();
  42. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  43. {
  44. _send8pixel(~black_value, ~color_value);
  45. }
  46. _endTransfer();
  47. _Update_Part();
  48. _writeCommand(0x92); // partial out
  49. }
  50. void GxEPD2_583c::writeScreenBuffer(uint8_t value)
  51. {
  52. _initial_write = false; // initial full screen buffer clean done
  53. if (value == 0xFF) value = 0x33; // white value for this controller
  54. _Init_Part();
  55. _writeCommand(0x91); // partial in
  56. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  57. _writeCommand(0x10);
  58. _startTransfer();
  59. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 2; i++)
  60. {
  61. _transfer(value);
  62. }
  63. _endTransfer();
  64. _writeCommand(0x92); // partial out
  65. }
  66. void GxEPD2_583c::writeScreenBuffer(uint8_t black_value, uint8_t color_value)
  67. {
  68. _initial_write = false; // initial full screen buffer clean done
  69. _Init_Part();
  70. _writeCommand(0x91); // partial in
  71. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  72. _writeCommand(0x10);
  73. _startTransfer();
  74. for (uint32_t i = 0; uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  75. {
  76. _send8pixel(~black_value, ~color_value);
  77. }
  78. _endTransfer();
  79. _writeCommand(0x92); // partial out
  80. }
  81. void GxEPD2_583c::writeImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  82. {
  83. writeImage(bitmap, NULL, x, y, w, h, invert, mirror_y, pgm);
  84. }
  85. void GxEPD2_583c::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)
  86. {
  87. if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
  88. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  89. uint16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
  90. x -= x % 8; // byte boundary
  91. w = wb * 8; // byte boundary
  92. int16_t x1 = x < 0 ? 0 : x; // limit
  93. int16_t y1 = y < 0 ? 0 : y; // limit
  94. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  95. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  96. int16_t dx = x1 - x;
  97. int16_t dy = y1 - y;
  98. w1 -= dx;
  99. h1 -= dy;
  100. if ((w1 <= 0) || (h1 <= 0)) return;
  101. _Init_Part();
  102. _writeCommand(0x91); // partial in
  103. _setPartialRamArea(x1, y1, w1, h1);
  104. _writeCommand(0x10);
  105. _startTransfer();
  106. for (int16_t i = 0; i < h1; i++)
  107. {
  108. for (int16_t j = 0; j < w1 / 8; j++)
  109. {
  110. uint8_t black_data = 0xFF;
  111. uint8_t color_data = 0xFF;
  112. if (black)
  113. {
  114. // use wb, h of bitmap for index!
  115. uint16_t idx = mirror_y ? j + dx / 8 + uint16_t((h - 1 - (i + dy))) * wb : j + dx / 8 + uint16_t(i + dy) * wb;
  116. if (pgm)
  117. {
  118. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  119. black_data = pgm_read_byte(&black[idx]);
  120. #else
  121. black_data = black[idx];
  122. #endif
  123. }
  124. else
  125. {
  126. black_data = black[idx];
  127. }
  128. if (invert) black_data = ~black_data;
  129. }
  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. color_data = pgm_read_byte(&color[idx]);
  138. #else
  139. color_data = color[idx];
  140. #endif
  141. }
  142. else
  143. {
  144. color_data = color[idx];
  145. }
  146. if (invert) color_data = ~color_data;
  147. }
  148. _send8pixel(~black_data, ~color_data);
  149. }
  150. #if defined(ESP8266)
  151. yield();
  152. #endif
  153. }
  154. _endTransfer();
  155. _writeCommand(0x92); // partial out
  156. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  157. }
  158. void GxEPD2_583c::writeImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  159. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  160. {
  161. writeImagePart(bitmap, NULL, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  162. }
  163. void GxEPD2_583c::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,
  164. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  165. {
  166. if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
  167. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  168. if ((w_bitmap < 0) || (h_bitmap < 0) || (w < 0) || (h < 0)) return;
  169. if ((x_part < 0) || (x_part >= w_bitmap)) return;
  170. if ((y_part < 0) || (y_part >= h_bitmap)) return;
  171. uint16_t wb_bitmap = (w_bitmap + 7) / 8; // width bytes, bitmaps are padded
  172. x_part -= x_part % 8; // byte boundary
  173. w = w_bitmap - x_part < w ? w_bitmap - x_part : w; // limit
  174. h = h_bitmap - y_part < h ? h_bitmap - y_part : h; // limit
  175. x -= x % 8; // byte boundary
  176. w = 8 * ((w + 7) / 8); // byte boundary, bitmaps are padded
  177. int16_t x1 = x < 0 ? 0 : x; // limit
  178. int16_t y1 = y < 0 ? 0 : y; // limit
  179. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  180. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  181. int16_t dx = x1 - x;
  182. int16_t dy = y1 - y;
  183. w1 -= dx;
  184. h1 -= dy;
  185. if ((w1 <= 0) || (h1 <= 0)) return;
  186. if (!_using_partial_mode) _Init_Part();
  187. _writeCommand(0x91); // partial in
  188. _setPartialRamArea(x1, y1, w1, h1);
  189. _writeCommand(0x10);
  190. _startTransfer();
  191. for (int16_t i = 0; i < h1; i++)
  192. {
  193. for (int16_t j = 0; j < w1 / 8; j++)
  194. {
  195. uint8_t black_data = 0xFF;
  196. uint8_t color_data = 0xFF;
  197. // use wb_bitmap, h_bitmap of bitmap for index!
  198. 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;
  199. if (black)
  200. {
  201. if (pgm)
  202. {
  203. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  204. black_data = pgm_read_byte(&black[idx]);
  205. #else
  206. black_data = black[idx];
  207. #endif
  208. }
  209. else
  210. {
  211. black_data = black[idx];
  212. }
  213. if (invert) black_data = ~black_data;
  214. }
  215. if (color)
  216. {
  217. if (pgm)
  218. {
  219. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  220. color_data = pgm_read_byte(&color[idx]);
  221. #else
  222. color_data = color[idx];
  223. #endif
  224. }
  225. else
  226. {
  227. color_data = color[idx];
  228. }
  229. if (invert) color_data = ~color_data;
  230. }
  231. _send8pixel(~black_data, ~color_data);
  232. }
  233. #if defined(ESP8266)
  234. yield();
  235. #endif
  236. }
  237. _endTransfer();
  238. _writeCommand(0x92); // partial out
  239. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  240. }
  241. void GxEPD2_583c::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)
  242. {
  243. if (data1)
  244. {
  245. if (_initial_write) writeScreenBuffer(); // initial full screen buffer clean
  246. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  247. uint16_t wb = (w + 1) / 2; // width bytes, bitmaps are padded
  248. x -= x % 2; // byte boundary
  249. w = wb * 2; // byte boundary
  250. int16_t x1 = x < 0 ? 0 : x; // limit
  251. int16_t y1 = y < 0 ? 0 : y; // limit
  252. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  253. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  254. int16_t dx = x1 - x;
  255. int16_t dy = y1 - y;
  256. w1 -= dx;
  257. h1 -= dy;
  258. if ((w1 <= 0) || (h1 <= 0)) return;
  259. _Init_Part();
  260. _writeCommand(0x91); // partial in
  261. _setPartialRamArea(x1, y1, w1, h1);
  262. _writeCommand(0x10);
  263. _startTransfer();
  264. for (int16_t i = 0; i < h1; i++)
  265. {
  266. for (int16_t j = 0; j < w1 / 2; j++)
  267. {
  268. uint8_t data;
  269. // use wb, h of bitmap for index!
  270. uint16_t idx = mirror_y ? j + dx / 2 + uint16_t((h - 1 - (i + dy))) * wb : j + dx / 2 + uint16_t(i + dy) * wb;
  271. if (pgm)
  272. {
  273. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  274. data = pgm_read_byte(&data1[idx]);
  275. #else
  276. data = data1[idx];
  277. #endif
  278. }
  279. else
  280. {
  281. data = data1[idx];
  282. }
  283. if (invert) data = ~data;
  284. _transfer(data);
  285. }
  286. }
  287. _endTransfer();
  288. _writeCommand(0x92); // partial out
  289. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  290. }
  291. }
  292. void GxEPD2_583c::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  293. {
  294. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  295. refresh(x, y, w, h);
  296. }
  297. void GxEPD2_583c::drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  298. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  299. {
  300. writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  301. refresh(x, y, w, h);
  302. }
  303. void GxEPD2_583c::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)
  304. {
  305. writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  306. refresh(x, y, w, h);
  307. }
  308. void GxEPD2_583c::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,
  309. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  310. {
  311. writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  312. refresh(x, y, w, h);
  313. }
  314. void GxEPD2_583c::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)
  315. {
  316. writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  317. refresh(x, y, w, h);
  318. }
  319. void GxEPD2_583c::refresh(bool partial_update_mode)
  320. {
  321. if (partial_update_mode) refresh(0, 0, WIDTH, HEIGHT);
  322. else _Update_Full();
  323. }
  324. void GxEPD2_583c::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
  325. {
  326. // intersection with screen
  327. int16_t w1 = x < 0 ? w + x : w; // reduce
  328. int16_t h1 = y < 0 ? h + y : h; // reduce
  329. int16_t x1 = x < 0 ? 0 : x; // limit
  330. int16_t y1 = y < 0 ? 0 : y; // limit
  331. w1 = x1 + w1 < int16_t(WIDTH) ? w1 : int16_t(WIDTH) - x1; // limit
  332. h1 = y1 + h1 < int16_t(HEIGHT) ? h1 : int16_t(HEIGHT) - y1; // limit
  333. if ((w1 <= 0) || (h1 <= 0)) return;
  334. // make x1, w1 multiple of 8
  335. w1 += x1 % 8;
  336. if (w1 % 8 > 0) w1 += 8 - w1 % 8;
  337. x1 -= x1 % 8;
  338. _Init_Part();
  339. _setPartialRamArea(x1, y1, w1, h1);
  340. _Update_Part();
  341. }
  342. void GxEPD2_583c::powerOff()
  343. {
  344. _PowerOff();
  345. }
  346. void GxEPD2_583c::hibernate()
  347. {
  348. _PowerOff();
  349. if (_rst >= 0)
  350. {
  351. // check if it supports this command!
  352. _writeCommand(0x07); // deep sleep
  353. _writeData(0xA5); // check code
  354. _hibernating = true;
  355. }
  356. }
  357. void GxEPD2_583c::_send8pixel(uint8_t black_data, uint8_t color_data)
  358. {
  359. for (uint8_t j = 0; j < 8; j++)
  360. {
  361. uint8_t t = 0x00; // black
  362. if (black_data & 0x80); // keep black
  363. else if (color_data & 0x80) t = 0x04; //color
  364. else t = 0x03; // white
  365. t <<= 4;
  366. black_data <<= 1;
  367. color_data <<= 1;
  368. j++;
  369. if (black_data & 0x80); // keep black
  370. else if (color_data & 0x80) t |= 0x04; //color
  371. else t |= 0x03; // white
  372. black_data <<= 1;
  373. color_data <<= 1;
  374. _transfer(t);
  375. }
  376. }
  377. void GxEPD2_583c::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  378. {
  379. uint16_t xe = (x + w - 1) | 0x0007; // byte boundary inclusive (last byte)
  380. uint16_t ye = y + h - 1;
  381. x &= 0xFFF8; // byte boundary
  382. xe |= 0x0007; // byte boundary
  383. _writeCommand(0x90); // partial window
  384. _writeData(x / 256);
  385. _writeData(x % 256);
  386. _writeData(xe / 256);
  387. _writeData(xe % 256);
  388. _writeData(y / 256);
  389. _writeData(y % 256);
  390. _writeData(ye / 256);
  391. _writeData(ye % 256);
  392. //_writeData(0x01); // distortion on full right half
  393. _writeData(0x00); // distortion on right half
  394. }
  395. void GxEPD2_583c::_PowerOn()
  396. {
  397. if (!_power_is_on)
  398. {
  399. _writeCommand(0x04);
  400. _waitWhileBusy("_PowerOn", power_on_time);
  401. }
  402. _power_is_on = true;
  403. }
  404. void GxEPD2_583c::_PowerOff()
  405. {
  406. if (_power_is_on)
  407. {
  408. _writeCommand(0x02); // power off
  409. _waitWhileBusy("_PowerOff", power_off_time);
  410. _power_is_on = false;
  411. }
  412. }
  413. void GxEPD2_583c::_InitDisplay()
  414. {
  415. if (_hibernating) _reset();
  416. _writeCommand(0x01);
  417. _writeData (0x37); //POWER SETTING
  418. _writeData (0x00);
  419. _writeCommand(0X00); //PANNEL SETTING
  420. _writeData(0xCF);
  421. _writeData(0x08);
  422. _writeCommand(0x06); //boost
  423. _writeData (0xc7);
  424. _writeData (0xcc);
  425. _writeData (0x28);
  426. _writeCommand(0x30); //PLL setting
  427. //_writeData (0x3a); //PLL: 0-15��:0x3C, 15+:0x3A
  428. _writeData (0x3c); //PLL: 0-15��:0x3C, 15+:0x3A
  429. _writeCommand(0X41); //TEMPERATURE SETTING
  430. _writeData(0x00);
  431. _writeCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  432. _writeData(0x77);
  433. _writeCommand(0X60); //TCON SETTING
  434. _writeData(0x22);
  435. _writeCommand(0x61); //600*448
  436. _writeData (0x02); //source 600
  437. _writeData (0x58);
  438. _writeData (0x01); //gate 448
  439. _writeData (0xc0);
  440. _writeCommand(0X82); //VCOM VOLTAGE SETTING
  441. _writeData(0x28); //all temperature range
  442. _writeCommand(0xe5); //FLASH MODE
  443. _writeData(0x03);
  444. }
  445. void GxEPD2_583c::_Init_Full()
  446. {
  447. _InitDisplay();
  448. _PowerOn();
  449. }
  450. void GxEPD2_583c::_Init_Part()
  451. {
  452. _InitDisplay();
  453. _PowerOn();
  454. }
  455. void GxEPD2_583c::_Update_Full()
  456. {
  457. _writeCommand(0x12); //display refresh
  458. _waitWhileBusy("_Update_Full", full_refresh_time);
  459. }
  460. void GxEPD2_583c::_Update_Part()
  461. {
  462. _writeCommand(0x12); //display refresh
  463. _waitWhileBusy("_Update_Part", partial_refresh_time);
  464. }