GxEPD2_750.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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.good-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_750.h"
  13. GxEPD2_750::GxEPD2_750(int8_t cs, int8_t dc, int8_t rst, int8_t busy) :
  14. GxEPD2_EPD(cs, dc, rst, busy, LOW, 10000000, WIDTH, HEIGHT, panel, hasColor, hasPartialUpdate, hasFastPartialUpdate)
  15. {
  16. }
  17. void GxEPD2_750::clearScreen(uint8_t value)
  18. {
  19. if (value == 0xFF) value = 0x33; // white value for this controller
  20. if (_initial)
  21. {
  22. _Init_Full();
  23. _writeCommand(0x10);
  24. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 2; i++)
  25. {
  26. _writeData(value);
  27. }
  28. _Update_Full();
  29. _initial = false;
  30. }
  31. if (!_using_partial_mode) _Init_Part();
  32. _writeCommand(0x91); // partial in
  33. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  34. _writeCommand(0x10);
  35. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 2; i++)
  36. {
  37. _writeData(value);
  38. }
  39. _Update_Part();
  40. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  41. _writeCommand(0x10);
  42. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 2; i++)
  43. {
  44. _writeData(value);
  45. }
  46. _Update_Part();
  47. _writeCommand(0x92); // partial out
  48. _initial = false;
  49. }
  50. void GxEPD2_750::writeScreenBuffer(uint8_t value)
  51. {
  52. if (value == 0xFF) value = 0x33; // white value for this controller
  53. if (_initial) clearScreen(value);
  54. else _writeScreenBuffer(value);
  55. }
  56. void GxEPD2_750::_writeScreenBuffer(uint8_t value)
  57. {
  58. if (!_using_partial_mode) _Init_Part();
  59. _writeCommand(0x91); // partial in
  60. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  61. _writeCommand(0x10);
  62. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 2; i++)
  63. {
  64. _writeData(value);
  65. }
  66. _writeCommand(0x92); // partial out
  67. }
  68. void GxEPD2_750::writeImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  69. {
  70. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  71. uint16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
  72. x -= x % 8; // byte boundary
  73. w = wb * 8; // byte boundary
  74. int16_t x1 = x < 0 ? 0 : x; // limit
  75. int16_t y1 = y < 0 ? 0 : y; // limit
  76. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  77. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  78. int16_t dx = x1 - x;
  79. int16_t dy = y1 - y;
  80. w1 -= dx;
  81. h1 -= dy;
  82. if ((w1 <= 0) || (h1 <= 0)) return;
  83. if (!_using_partial_mode) _Init_Part();
  84. _writeCommand(0x91); // partial in
  85. _setPartialRamArea(x1, y1, w1, h1);
  86. _writeCommand(0x10);
  87. for (int16_t i = 0; i < h1; i++)
  88. {
  89. for (int16_t j = 0; j < w1 / 8; j++)
  90. {
  91. uint8_t data;
  92. // use wb, h of bitmap for index!
  93. uint16_t idx = mirror_y ? j + dx / 8 + uint16_t((h - 1 - (i + dy))) * wb : j + dx / 8 + uint16_t(i + dy) * wb;
  94. if (pgm)
  95. {
  96. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  97. data = pgm_read_byte(&bitmap[idx]);
  98. #else
  99. data = bitmap[idx];
  100. #endif
  101. }
  102. else
  103. {
  104. data = bitmap[idx];
  105. }
  106. if (invert) data = ~data;
  107. _send8pixel(~data);
  108. }
  109. }
  110. _writeCommand(0x92); // partial out
  111. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  112. }
  113. void GxEPD2_750::writeImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  114. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  115. {
  116. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  117. if ((w_bitmap < 0) || (h_bitmap < 0) || (w < 0) || (h < 0)) return;
  118. if ((x_part < 0) || (x_part >= w_bitmap)) return;
  119. if ((y_part < 0) || (y_part >= h_bitmap)) return;
  120. uint16_t wb_bitmap = (w_bitmap + 7) / 8; // width bytes, bitmaps are padded
  121. x_part -= x_part % 8; // byte boundary
  122. w = w_bitmap - x_part < w ? w_bitmap - x_part : w; // limit
  123. h = h_bitmap - y_part < h ? h_bitmap - y_part : h; // limit
  124. x -= x % 8; // byte boundary
  125. w = 8 * ((w + 7) / 8); // byte boundary, bitmaps are padded
  126. int16_t x1 = x < 0 ? 0 : x; // limit
  127. int16_t y1 = y < 0 ? 0 : y; // limit
  128. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  129. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  130. int16_t dx = x1 - x;
  131. int16_t dy = y1 - y;
  132. w1 -= dx;
  133. h1 -= dy;
  134. if ((w1 <= 0) || (h1 <= 0)) return;
  135. if (!_using_partial_mode) _Init_Part();
  136. _writeCommand(0x91); // partial in
  137. _setPartialRamArea(x1, y1, w1, h1);
  138. _writeCommand(0x10);
  139. for (int16_t i = 0; i < h1; i++)
  140. {
  141. for (int16_t j = 0; j < w1 / 8; j++)
  142. {
  143. uint8_t data;
  144. // use wb_bitmap, h_bitmap of bitmap for index!
  145. 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;
  146. if (pgm)
  147. {
  148. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  149. data = pgm_read_byte(&bitmap[idx]);
  150. #else
  151. data = bitmap[idx];
  152. #endif
  153. }
  154. else
  155. {
  156. data = bitmap[idx];
  157. }
  158. if (invert) data = ~data;
  159. _send8pixel(~data);
  160. }
  161. }
  162. _writeCommand(0x92); // partial out
  163. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  164. }
  165. void GxEPD2_750::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)
  166. {
  167. if (black)
  168. {
  169. writeImage(black, x, y, w, h, invert, mirror_y, pgm);
  170. }
  171. }
  172. void GxEPD2_750::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,
  173. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  174. {
  175. if (black)
  176. {
  177. writeImagePart(black, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  178. }
  179. }
  180. void GxEPD2_750::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)
  181. {
  182. if (data1)
  183. {
  184. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  185. int16_t wb = (w + 1) / 2; // width bytes, bitmaps are padded
  186. x -= x % 2; // byte boundary
  187. w = wb * 2; // byte boundary
  188. int16_t x1 = x < 0 ? 0 : x; // limit
  189. int16_t y1 = y < 0 ? 0 : y; // limit
  190. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  191. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  192. int16_t dx = x1 - x;
  193. int16_t dy = y1 - y;
  194. w1 -= dx;
  195. h1 -= dy;
  196. if ((w1 <= 0) || (h1 <= 0)) return;
  197. if (!_using_partial_mode) _Init_Part();
  198. _writeCommand(0x91); // partial in
  199. _setPartialRamArea(x1, y1, w1, h1);
  200. _writeCommand(0x10);
  201. for (int16_t i = 0; i < h1; i++)
  202. {
  203. for (int16_t j = 0; j < w1 / 2; j++)
  204. {
  205. uint8_t data;
  206. // use wb, h of bitmap for index!
  207. int16_t idx = mirror_y ? j + dx / 2 + ((h - 1 - (i + dy))) * wb : j + dx / 2 + (i + dy) * wb;
  208. if (pgm)
  209. {
  210. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  211. data = pgm_read_byte(&data1[idx]);
  212. #else
  213. data = data1[idx];
  214. #endif
  215. }
  216. else
  217. {
  218. data = data1[idx];
  219. }
  220. if (invert) data = ~data;
  221. _writeData(data);
  222. }
  223. }
  224. _writeCommand(0x92); // partial out
  225. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  226. }
  227. }
  228. void GxEPD2_750::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  229. {
  230. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  231. refresh(x, y, w, h);
  232. }
  233. void GxEPD2_750::drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  234. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  235. {
  236. writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  237. refresh(x, y, w, h);
  238. }
  239. void GxEPD2_750::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)
  240. {
  241. writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  242. refresh(x, y, w, h);
  243. }
  244. void GxEPD2_750::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,
  245. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  246. {
  247. writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  248. refresh(x, y, w, h);
  249. }
  250. void GxEPD2_750::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)
  251. {
  252. writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  253. refresh(x, y, w, h);
  254. }
  255. void GxEPD2_750::refresh(bool partial_update_mode)
  256. {
  257. if (partial_update_mode) refresh(0, 0, WIDTH, HEIGHT);
  258. else
  259. {
  260. if (_using_partial_mode) _Init_Full();
  261. _Update_Full();
  262. }
  263. }
  264. void GxEPD2_750::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
  265. {
  266. x -= x % 8; // byte boundary
  267. w -= x % 8; // byte boundary
  268. int16_t x1 = x < 0 ? 0 : x; // limit
  269. int16_t y1 = y < 0 ? 0 : y; // limit
  270. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  271. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  272. w1 -= x1 - x;
  273. h1 -= y1 - y;
  274. if (!_using_partial_mode) _Init_Part();
  275. _writeCommand(0x91); // partial in
  276. _setPartialRamArea(x1, y1, w1, h1);
  277. _Update_Part();
  278. _writeCommand(0x92); // partial out
  279. }
  280. void GxEPD2_750::powerOff(void)
  281. {
  282. _PowerOff();
  283. }
  284. void GxEPD2_750::hibernate()
  285. {
  286. _PowerOff();
  287. if (_rst >= 0)
  288. {
  289. // check if it supports this command!
  290. _writeCommand(0x07); // deep sleep
  291. _writeData(0xA5); // check code
  292. _hibernating = true;
  293. }
  294. }
  295. void GxEPD2_750::_send8pixel(uint8_t data)
  296. {
  297. for (uint8_t j = 0; j < 8; j++)
  298. {
  299. uint8_t t = data & 0x80 ? 0x00 : 0x03;
  300. t <<= 4;
  301. data <<= 1;
  302. j++;
  303. t |= data & 0x80 ? 0x00 : 0x03;
  304. data <<= 1;
  305. _writeData(t);
  306. }
  307. }
  308. void GxEPD2_750::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  309. {
  310. uint16_t xe = (x + w - 1) | 0x0007; // byte boundary inclusive (last byte)
  311. uint16_t ye = y + h - 1;
  312. x &= 0xFFF8; // byte boundary
  313. _writeCommand(0x90); // partial window
  314. _writeData(x / 256);
  315. _writeData(x % 256);
  316. _writeData(xe / 256);
  317. _writeData(xe % 256);
  318. _writeData(y / 256);
  319. _writeData(y % 256);
  320. _writeData(ye / 256);
  321. _writeData(ye % 256);
  322. _writeData(0x01); // don't see any difference
  323. //_writeData(0x00); // don't see any difference
  324. }
  325. void GxEPD2_750::_PowerOn()
  326. {
  327. if (!_power_is_on)
  328. {
  329. _writeCommand(0x04);
  330. _waitWhileBusy("_PowerOn", power_on_time);
  331. }
  332. _power_is_on = true;
  333. }
  334. void GxEPD2_750::_PowerOff()
  335. {
  336. _writeCommand(0x02); // power off
  337. _waitWhileBusy("_PowerOff", power_off_time);
  338. _power_is_on = false;
  339. _using_partial_mode = false;
  340. }
  341. void GxEPD2_750::_InitDisplay()
  342. {
  343. if (_hibernating) _reset();
  344. /**********************************release flash sleep**********************************/
  345. _writeCommand(0X65); //FLASH CONTROL
  346. _writeData(0x01);
  347. _writeCommand(0xAB);
  348. _writeCommand(0X65); //FLASH CONTROL
  349. _writeData(0x00);
  350. /**********************************release flash sleep**********************************/
  351. _writeCommand(0x01);
  352. _writeData (0x37); //POWER SETTING
  353. _writeData (0x00);
  354. _writeCommand(0X00); //PANNEL SETTING
  355. _writeData(0xCF);
  356. _writeData(0x08);
  357. _writeCommand(0x06); //boost
  358. _writeData (0xc7);
  359. _writeData (0xcc);
  360. _writeData (0x28);
  361. _writeCommand(0x30); //PLL setting
  362. _writeData (0x3c);
  363. _writeCommand(0X41); //TEMPERATURE SETTING
  364. _writeData(0x00);
  365. _writeCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  366. _writeData(0x77);
  367. _writeCommand(0X60); //TCON SETTING
  368. _writeData(0x22);
  369. _writeCommand(0x61); //tres 640*384
  370. _writeData (0x02); //source 640
  371. _writeData (0x80);
  372. _writeData (0x01); //gate 384
  373. _writeData (0x80);
  374. _writeCommand(0X82); //VDCS SETTING
  375. _writeData(0x1E); //decide by LUT file
  376. _writeCommand(0xe5); //FLASH MODE
  377. _writeData(0x03);
  378. //_writeCommand(0x04); //POWER ON
  379. //_waitWhileBusy();
  380. }
  381. void GxEPD2_750::_Init_Full()
  382. {
  383. _InitDisplay();
  384. _PowerOn();
  385. _using_partial_mode = false;
  386. }
  387. void GxEPD2_750::_Init_Part()
  388. {
  389. _InitDisplay();
  390. _PowerOn();
  391. _using_partial_mode = true;
  392. }
  393. void GxEPD2_750::_Update_Full()
  394. {
  395. _writeCommand(0x12); //display refresh
  396. _waitWhileBusy("_Update_Full", full_refresh_time);
  397. }
  398. void GxEPD2_750::_Update_Part()
  399. {
  400. _writeCommand(0x12); //display refresh
  401. _waitWhileBusy("_Update_Part", partial_refresh_time);
  402. }