GxEPD2_it60.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. // GxEPD2_it60 class is based on Demo Example from Waveshare for Raspberry PI https://github.com/waveshare/IT8951/archive/master.zip
  5. // Controller: IT8951 : https://www.waveshare.com/w/upload/1/18/IT8951_D_V0.2.4.3_20170728.pdf
  6. //
  7. // The GxEPD2_it60 driver class supports the Waveshare e-Paper IT8951 Driver HAT connected with SPI for the ED060SCT 6" e-paper panel (parallel IF)
  8. // https://www.waveshare.com/product/mini-pc/raspberry-pi/hats/6inch-e-paper-hat.htm
  9. // This Driver HAT requires 5V power supply but works with 3.3V data lines; requires both MOSI and MISO SPI lines.
  10. //
  11. // Author: Jean-Marc Zingg
  12. //
  13. // Version: see library.properties
  14. //
  15. // Library: https://github.com/ZinggJM/GxEPD2
  16. #include "GxEPD2_it60.h"
  17. #define VCOM 1500 //e.g. -1.53 = 1530 = 0x5FA
  18. //Built in I80 Command Code
  19. #define IT8951_TCON_SYS_RUN 0x0001
  20. #define IT8951_TCON_STANDBY 0x0002
  21. #define IT8951_TCON_SLEEP 0x0003
  22. #define IT8951_TCON_REG_RD 0x0010
  23. #define IT8951_TCON_REG_WR 0x0011
  24. #define IT8951_TCON_LD_IMG 0x0020
  25. #define IT8951_TCON_LD_IMG_AREA 0x0021
  26. #define IT8951_TCON_LD_IMG_END 0x0022
  27. //I80 User defined command code
  28. #define USDEF_I80_CMD_DPY_AREA 0x0034
  29. #define USDEF_I80_CMD_GET_DEV_INFO 0x0302
  30. #define USDEF_I80_CMD_DPY_BUF_AREA 0x0037
  31. #define USDEF_I80_CMD_VCOM 0x0039
  32. //Rotate mode
  33. #define IT8951_ROTATE_0 0
  34. #define IT8951_ROTATE_90 1
  35. #define IT8951_ROTATE_180 2
  36. #define IT8951_ROTATE_270 3
  37. //Pixel mode , BPP - Bit per Pixel
  38. #define IT8951_2BPP 0
  39. #define IT8951_3BPP 1
  40. #define IT8951_4BPP 2
  41. #define IT8951_8BPP 3
  42. //Endian Type
  43. #define IT8951_LDIMG_L_ENDIAN 0
  44. #define IT8951_LDIMG_B_ENDIAN 1
  45. #define SYS_REG_BASE 0x0000
  46. #define I80CPCR (SYS_REG_BASE + 0x04)
  47. #define MCSR_BASE_ADDR 0x0200
  48. #define LISAR (MCSR_BASE_ADDR + 0x0008)
  49. GxEPD2_it60::GxEPD2_it60(int8_t cs, int8_t dc, int8_t rst, int8_t busy) :
  50. GxEPD2_EPD(cs, dc, rst, busy, LOW, 10000000, WIDTH, HEIGHT, panel, hasColor, hasPartialUpdate, hasFastPartialUpdate),
  51. _spi_settings(24000000, MSBFIRST, SPI_MODE0),
  52. _spi_settings_for_read(1000000, MSBFIRST, SPI_MODE0)
  53. {
  54. }
  55. void GxEPD2_it60::init(uint32_t serial_diag_bitrate)
  56. {
  57. init(serial_diag_bitrate, true, false);
  58. }
  59. void GxEPD2_it60::init(uint32_t serial_diag_bitrate, bool initial, bool pulldown_rst_mode)
  60. {
  61. GxEPD2_EPD::init(serial_diag_bitrate, initial, pulldown_rst_mode);
  62. // we need a long reset pulse
  63. if (_rst >= 0)
  64. {
  65. digitalWrite(_rst, LOW);
  66. delay(200);
  67. digitalWrite(_rst, HIGH);
  68. delay(200);
  69. _waitWhileBusy("init reset_to_ready", reset_to_ready_time);
  70. }
  71. _writeCommand16(USDEF_I80_CMD_GET_DEV_INFO);
  72. _waitWhileBusy("GetIT8951SystemInfo", power_on_time);
  73. _readData16((uint16_t*)&IT8951DevInfo, sizeof(IT8951DevInfo) / 2);
  74. if (_diag_enabled)
  75. {
  76. //Show Device information of IT8951
  77. printf("Panel(W,H) = (%d,%d)\r\n",
  78. IT8951DevInfo.usPanelW, IT8951DevInfo.usPanelH );
  79. printf("Image Buffer Address = %X\r\n",
  80. IT8951DevInfo.usImgBufAddrL | (IT8951DevInfo.usImgBufAddrH << 16));
  81. //Show Firmware and LUT Version
  82. printf("FW Version = %s\r\n", (uint8_t*)IT8951DevInfo.usFWVersion);
  83. printf("LUT Version = %s\r\n", (uint8_t*)IT8951DevInfo.usLUTVersion);
  84. }
  85. //Set to Enable I80 Packed mode
  86. _IT8951WriteReg(I80CPCR, 0x0001);
  87. if (VCOM != _IT8951GetVCOM())
  88. {
  89. _IT8951SetVCOM(VCOM);
  90. printf("VCOM = -%.02fV\n", (float)_IT8951GetVCOM() / 1000);
  91. }
  92. printf("VCOM = -%.02fV\n", (float)_IT8951GetVCOM() / 1000);
  93. }
  94. void GxEPD2_it60::clearScreen(uint8_t value)
  95. {
  96. if (_initial) _Init_Full();
  97. else _Init_Part();
  98. _initial = false;
  99. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  100. SPI.beginTransaction(_spi_settings);
  101. if (_cs >= 0) digitalWrite(_cs, LOW);
  102. _transfer16(0x0000); // preamble for write data
  103. _waitWhileBusy2("clearScreen preamble", default_wait_time);
  104. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT); i++)
  105. {
  106. SPI.transfer(value);
  107. #if defined(ESP8266) || defined(ESP32)
  108. if (0 == i % 10000) yield();
  109. #endif
  110. }
  111. if (_cs >= 0) digitalWrite(_cs, HIGH);
  112. SPI.endTransaction();
  113. _writeCommand16(IT8951_TCON_LD_IMG_END);
  114. _waitWhileBusy2("clearScreen load end", default_wait_time);
  115. _refresh(0, 0, WIDTH, HEIGHT, false);
  116. }
  117. void GxEPD2_it60::writeScreenBuffer(uint8_t value)
  118. {
  119. if (_initial) clearScreen(value);
  120. else _writeScreenBuffer(value);
  121. }
  122. void GxEPD2_it60::_writeScreenBuffer(uint8_t value)
  123. {
  124. if (!_using_partial_mode) _Init_Part();
  125. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  126. SPI.beginTransaction(_spi_settings);
  127. if (_cs >= 0) digitalWrite(_cs, LOW);
  128. _transfer16(0x0000); // preamble for write data
  129. _waitWhileBusy2("clearScreen preamble", default_wait_time);
  130. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT); i++)
  131. {
  132. SPI.transfer(value);
  133. #if defined(ESP8266) || defined(ESP32)
  134. if (0 == i % 10000) yield();
  135. #endif
  136. }
  137. if (_cs >= 0) digitalWrite(_cs, HIGH);
  138. SPI.endTransaction();
  139. _writeCommand16(IT8951_TCON_LD_IMG_END);
  140. _waitWhileBusy2("_writeScreenBuffer load end", default_wait_time);
  141. }
  142. void GxEPD2_it60::writeImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  143. {
  144. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  145. int16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
  146. x -= x % 8; // byte boundary
  147. w = wb * 8; // byte boundary
  148. int16_t x1 = x < 0 ? 0 : x; // limit
  149. int16_t y1 = y < 0 ? 0 : y; // limit
  150. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  151. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  152. int16_t dx = x1 - x;
  153. int16_t dy = y1 - y;
  154. w1 -= dx;
  155. h1 -= dy;
  156. if ((w1 <= 0) || (h1 <= 0)) return;
  157. if (!_using_partial_mode) _Init_Part();
  158. _setPartialRamArea(x1, y1, w1, h1);
  159. SPI.beginTransaction(_spi_settings);
  160. if (_cs >= 0) digitalWrite(_cs, LOW);
  161. _transfer16(0x0000); // preamble for write data
  162. _waitWhileBusy2("writeImage preamble", default_wait_time);
  163. for (int16_t i = 0; i < h1; i++)
  164. {
  165. for (int16_t j = 0; j < w1 / 8; j++)
  166. {
  167. uint8_t data;
  168. // use wb, h of bitmap for index!
  169. uint32_t idx = mirror_y ? uint32_t(j + dx / 8) + uint32_t((h - 1 - (i + dy))) * uint32_t(wb) : uint32_t(j + dx / 8) + uint32_t(i + dy) * uint32_t(wb);
  170. if (pgm)
  171. {
  172. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  173. data = pgm_read_byte(&bitmap[idx]);
  174. #else
  175. data = bitmap[idx];
  176. #endif
  177. }
  178. else
  179. {
  180. data = bitmap[idx];
  181. }
  182. if (invert) data = ~data;
  183. _send8pixel(~data);
  184. }
  185. #if defined(ESP8266) || defined(ESP32)
  186. yield();
  187. #endif
  188. }
  189. if (_cs >= 0) digitalWrite(_cs, HIGH);
  190. SPI.endTransaction();
  191. _writeCommand16(IT8951_TCON_LD_IMG_END);
  192. _waitWhileBusy2("writeImage load end", default_wait_time);
  193. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  194. }
  195. void GxEPD2_it60::writeImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  196. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  197. {
  198. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  199. if ((w_bitmap < 0) || (h_bitmap < 0) || (w < 0) || (h < 0)) return;
  200. if ((x_part < 0) || (x_part >= w_bitmap)) return;
  201. if ((y_part < 0) || (y_part >= h_bitmap)) return;
  202. int16_t wb_bitmap = (w_bitmap + 7) / 8; // width bytes, bitmaps are padded
  203. x_part -= x_part % 8; // byte boundary
  204. w = w_bitmap - x_part < w ? w_bitmap - x_part : w; // limit
  205. h = h_bitmap - y_part < h ? h_bitmap - y_part : h; // limit
  206. x -= x % 8; // byte boundary
  207. w = 8 * ((w + 7) / 8); // byte boundary, bitmaps are padded
  208. int16_t x1 = x < 0 ? 0 : x; // limit
  209. int16_t y1 = y < 0 ? 0 : y; // limit
  210. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  211. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  212. int16_t dx = x1 - x;
  213. int16_t dy = y1 - y;
  214. w1 -= dx;
  215. h1 -= dy;
  216. if ((w1 <= 0) || (h1 <= 0)) return;
  217. if (!_using_partial_mode) _Init_Part();
  218. _setPartialRamArea(x1, y1, w1, h1);
  219. SPI.beginTransaction(_spi_settings);
  220. if (_cs >= 0) digitalWrite(_cs, LOW);
  221. _transfer16(0x0000); // preamble for write data
  222. _waitWhileBusy2("writeImage preamble", default_wait_time);
  223. for (int16_t i = 0; i < h1; i++)
  224. {
  225. for (int16_t j = 0; j < w1 / 8; j++)
  226. {
  227. uint8_t data;
  228. // use wb_bitmap, h_bitmap of bitmap for index!
  229. 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;
  230. if (pgm)
  231. {
  232. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  233. data = pgm_read_byte(&bitmap[idx]);
  234. #else
  235. data = bitmap[idx];
  236. #endif
  237. }
  238. else
  239. {
  240. data = bitmap[idx];
  241. }
  242. if (invert) data = ~data;
  243. _send8pixel(~data);
  244. }
  245. }
  246. if (_cs >= 0) digitalWrite(_cs, HIGH);
  247. SPI.endTransaction();
  248. _writeCommand16(IT8951_TCON_LD_IMG_END);
  249. _waitWhileBusy2("writeImage load end", default_wait_time);
  250. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  251. }
  252. void GxEPD2_it60::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)
  253. {
  254. if (black)
  255. {
  256. writeImage(black, x, y, w, h, invert, mirror_y, pgm);
  257. }
  258. }
  259. void GxEPD2_it60::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,
  260. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  261. {
  262. if (black)
  263. {
  264. writeImagePart(black, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  265. }
  266. }
  267. void GxEPD2_it60::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)
  268. {
  269. if (data1)
  270. {
  271. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  272. int16_t x1 = x < 0 ? 0 : x; // limit
  273. int16_t y1 = y < 0 ? 0 : y; // limit
  274. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  275. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  276. int16_t dx = x1 - x;
  277. int16_t dy = y1 - y;
  278. w1 -= dx;
  279. h1 -= dy;
  280. if ((w1 <= 0) || (h1 <= 0)) return;
  281. if (!_using_partial_mode) _Init_Part();
  282. _setPartialRamArea(x1, y1, w1, h1);
  283. SPI.beginTransaction(_spi_settings);
  284. if (_cs >= 0) digitalWrite(_cs, LOW);
  285. _transfer16(0x0000); // preamble for write data
  286. _waitWhileBusy2("writeNative preamble", default_wait_time);
  287. for (int16_t i = 0; i < h1; i++)
  288. {
  289. for (int16_t j = 0; j < w1; j++)
  290. {
  291. uint8_t data;
  292. // use w, h of bitmap for index!
  293. uint32_t idx = mirror_y ? uint32_t(j + dx) + uint32_t((h - 1 - (i + dy))) * uint32_t(w) : uint32_t(j + dx) + uint32_t(i + dy) * uint32_t(w);
  294. if (pgm)
  295. {
  296. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  297. data = pgm_read_byte(&data1[idx]);
  298. #else
  299. data = data1[idx];
  300. #endif
  301. }
  302. else
  303. {
  304. data = data1[idx];
  305. }
  306. if (invert) data = ~data;
  307. SPI.transfer(data);
  308. }
  309. #if defined(ESP8266) || defined(ESP32)
  310. yield();
  311. #endif
  312. }
  313. if (_cs >= 0) digitalWrite(_cs, HIGH);
  314. SPI.endTransaction();
  315. _writeCommand16(IT8951_TCON_LD_IMG_END);
  316. _waitWhileBusy2("writeNative load end", default_wait_time);
  317. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  318. }
  319. }
  320. void GxEPD2_it60::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  321. {
  322. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  323. _refresh(x, y, w, h, true);
  324. }
  325. void GxEPD2_it60::drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  326. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  327. {
  328. writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  329. _refresh(x, y, w, h, true);
  330. }
  331. void GxEPD2_it60::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)
  332. {
  333. writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  334. _refresh(x, y, w, h, true);
  335. }
  336. void GxEPD2_it60::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,
  337. int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  338. {
  339. writeImagePart(black, color, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  340. _refresh(x, y, w, h, true);
  341. }
  342. void GxEPD2_it60::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)
  343. {
  344. writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  345. _refresh(x, y, w, h, false);
  346. }
  347. void GxEPD2_it60::refresh(bool partial_update_mode)
  348. {
  349. _refresh(0, 0, WIDTH, HEIGHT, partial_update_mode);
  350. }
  351. void GxEPD2_it60::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
  352. {
  353. _refresh(x, y, w, h, true);
  354. }
  355. void GxEPD2_it60::_refresh(int16_t x, int16_t y, int16_t w, int16_t h, bool partial_update_mode)
  356. {
  357. //x -= x % 8; // byte boundary
  358. //w -= x % 8; // byte boundary
  359. int16_t x1 = x < 0 ? 0 : x; // limit
  360. int16_t y1 = y < 0 ? 0 : y; // limit
  361. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  362. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  363. w1 -= x1 - x;
  364. h1 -= y1 - y;
  365. //Send I80 Display Command (User defined command of IT8951)
  366. _writeCommand16(USDEF_I80_CMD_DPY_AREA); //0x0034
  367. _waitWhileBusy2("refresh cmd", refresh_cmd_time);
  368. //Write arguments
  369. _writeData16(x1);
  370. _waitWhileBusy2("refresh x", refresh_par_time);
  371. _writeData16(y1);
  372. _waitWhileBusy2("refresh y", refresh_par_time);
  373. _writeData16(w1);
  374. _waitWhileBusy2("refresh w", refresh_par_time);
  375. _writeData16(h1);
  376. _waitWhileBusy2("refresh h", refresh_par_time);
  377. _writeData16(partial_update_mode ? 1 : 2); // mode
  378. _waitWhileBusy("refresh", full_refresh_time);
  379. }
  380. void GxEPD2_it60::powerOff(void)
  381. {
  382. _PowerOff();
  383. }
  384. void GxEPD2_it60::hibernate()
  385. {
  386. if (_power_is_on) _PowerOff();
  387. if (_rst >= 0)
  388. {
  389. // this does not work, does not reduce power, uses more than in stand by mode
  390. //delay(1000);
  391. //_IT8951Sleep();
  392. //delay(1000);
  393. //_hibernating = true;
  394. }
  395. }
  396. void GxEPD2_it60::_send8pixel(uint8_t data)
  397. {
  398. for (uint8_t j = 0; j < 8; j++)
  399. {
  400. SPI.transfer(data & 0x80 ? 0x00 : 0xFF);
  401. data <<= 1;
  402. }
  403. }
  404. void GxEPD2_it60::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  405. {
  406. //_IT8951WriteReg(LISAR + 2 , IT8951DevInfo.usImgBufAddrH);
  407. //_IT8951WriteReg(LISAR , IT8951DevInfo.usImgBufAddrL);
  408. uint16_t usArg[5];
  409. //usArg[0] = (IT8951_LDIMG_L_ENDIAN << 8 ) | (IT8951_8BPP << 4) | (IT8951_ROTATE_0);
  410. usArg[0] = (IT8951_LDIMG_B_ENDIAN << 8 ) | (IT8951_8BPP << 4) | (IT8951_ROTATE_0);
  411. usArg[1] = x;
  412. usArg[2] = y;
  413. usArg[3] = w;
  414. usArg[4] = h;
  415. _writeCommandData16(IT8951_TCON_LD_IMG_AREA , usArg , 5);
  416. }
  417. void GxEPD2_it60::_PowerOn()
  418. {
  419. if (!_power_is_on)
  420. {
  421. _IT8951SystemRun();
  422. _waitWhileBusy("_PowerOn", power_on_time);
  423. }
  424. _power_is_on = true;
  425. }
  426. void GxEPD2_it60::_PowerOff()
  427. {
  428. _IT8951StandBy();
  429. _waitWhileBusy("_PowerOff", power_off_time);
  430. _power_is_on = false;
  431. _using_partial_mode = false;
  432. }
  433. void GxEPD2_it60::_InitDisplay()
  434. {
  435. // we need a long reset pulse
  436. if (_hibernating && (_rst >= 0))
  437. {
  438. digitalWrite(_rst, LOW);
  439. delay(200);
  440. digitalWrite(_rst, HIGH);
  441. delay(200);
  442. }
  443. }
  444. void GxEPD2_it60::_Init_Full()
  445. {
  446. _InitDisplay();
  447. _PowerOn();
  448. _using_partial_mode = false;
  449. }
  450. void GxEPD2_it60::_Init_Part()
  451. {
  452. _InitDisplay();
  453. _PowerOn();
  454. _using_partial_mode = true;
  455. }
  456. void GxEPD2_it60::_waitWhileBusy2(const char* comment, uint16_t busy_time)
  457. {
  458. if (_busy >= 0)
  459. {
  460. unsigned long start = micros();
  461. while (1)
  462. {
  463. if (digitalRead(_busy) != _busy_level) break;
  464. delay(1);
  465. if (micros() - start > _busy_timeout)
  466. {
  467. Serial.println("Busy Timeout!");
  468. break;
  469. }
  470. }
  471. if (comment)
  472. {
  473. #if !defined(DISABLE_DIAGNOSTIC_OUTPUT)
  474. if (_diag_enabled)
  475. {
  476. unsigned long elapsed = micros() - start;
  477. if (elapsed > diag_min_time * 1000)
  478. {
  479. Serial.print(comment);
  480. Serial.print(" : ");
  481. Serial.println(elapsed);
  482. }
  483. }
  484. #endif
  485. }
  486. (void) start;
  487. }
  488. else delay(busy_time);
  489. }
  490. uint16_t GxEPD2_it60::_transfer16(uint16_t value)
  491. {
  492. uint16_t rv = SPI.transfer(value >> 8) << 8;
  493. return (rv | SPI.transfer(value));
  494. }
  495. void GxEPD2_it60::_writeCommand16(uint16_t c)
  496. {
  497. String s = String("_writeCommand16(0x") + String(c, HEX) + String(")");
  498. _waitWhileBusy2(s.c_str(), default_wait_time);
  499. SPI.beginTransaction(_spi_settings);
  500. if (_cs >= 0) digitalWrite(_cs, LOW);
  501. _transfer16(0x6000); // preamble for write command
  502. _waitWhileBusy2("_writeCommand16 preamble", default_wait_time);
  503. _transfer16(c);
  504. if (_cs >= 0) digitalWrite(_cs, HIGH);
  505. SPI.endTransaction();
  506. //_waitWhileBusy(s.c_str(), default_wait_time);
  507. }
  508. void GxEPD2_it60::_writeData16(uint16_t d)
  509. {
  510. _waitWhileBusy2("_writeData16", default_wait_time);
  511. SPI.beginTransaction(_spi_settings);
  512. if (_cs >= 0) digitalWrite(_cs, LOW);
  513. _transfer16(0x0000); // preamble for write data
  514. _waitWhileBusy2("_writeData16 preamble", default_wait_time);
  515. _transfer16(d);
  516. if (_cs >= 0) digitalWrite(_cs, HIGH);
  517. SPI.endTransaction();
  518. }
  519. void GxEPD2_it60::_writeData16(const uint16_t* d, uint32_t n)
  520. {
  521. _waitWhileBusy2("_writeData16", default_wait_time);
  522. SPI.beginTransaction(_spi_settings);
  523. if (_cs >= 0) digitalWrite(_cs, LOW);
  524. _transfer16(0x0000); // preamble for write data
  525. _waitWhileBusy2("_writeData16 preamble", default_wait_time);
  526. for (uint32_t i = 0; i < n; i++)
  527. {
  528. _transfer16(*d++);
  529. }
  530. if (_cs >= 0) digitalWrite(_cs, HIGH);
  531. SPI.endTransaction();
  532. }
  533. uint16_t GxEPD2_it60::_readData16()
  534. {
  535. _waitWhileBusy2("_readData16", default_wait_time);
  536. SPI.beginTransaction(_spi_settings);
  537. if (_cs >= 0) digitalWrite(_cs, LOW);
  538. _transfer16(0x1000); // preamble for read data
  539. _waitWhileBusy2("_readData16 preamble", default_wait_time);
  540. _transfer16(0); // dummy
  541. _waitWhileBusy2("_readData16 dummy", default_wait_time);
  542. uint16_t rv = _transfer16(0);
  543. if (_cs >= 0) digitalWrite(_cs, HIGH);
  544. SPI.endTransaction();
  545. return rv;
  546. }
  547. void GxEPD2_it60::_readData16(uint16_t* d, uint32_t n)
  548. {
  549. _waitWhileBusy2("_readData16", default_wait_time);
  550. SPI.beginTransaction(_spi_settings);
  551. if (_cs >= 0) digitalWrite(_cs, LOW);
  552. _transfer16(0x1000); // preamble for read data
  553. _waitWhileBusy2("_readData16 preamble", default_wait_time);
  554. _transfer16(0); // dummy
  555. _waitWhileBusy2("_readData16 dummy", default_wait_time);
  556. for (uint32_t i = 0; i < n; i++)
  557. {
  558. *d++ = _transfer16(0);
  559. //_waitWhileBusy("_readData16 data", default_wait_time);
  560. }
  561. if (_cs >= 0) digitalWrite(_cs, HIGH);
  562. SPI.endTransaction();
  563. }
  564. void GxEPD2_it60::_writeCommandData16(uint16_t c, const uint16_t* d, uint16_t n)
  565. {
  566. _writeCommand16(c);
  567. for (uint16_t i = 0; i < n; i++)
  568. {
  569. _writeData16(d[i]);
  570. }
  571. }
  572. void GxEPD2_it60::_IT8951SystemRun()
  573. {
  574. _writeCommand16(IT8951_TCON_SYS_RUN);
  575. }
  576. void GxEPD2_it60::_IT8951StandBy()
  577. {
  578. _writeCommand16(IT8951_TCON_STANDBY);
  579. }
  580. void GxEPD2_it60::_IT8951Sleep()
  581. {
  582. _writeCommand16(IT8951_TCON_SLEEP);
  583. }
  584. uint16_t GxEPD2_it60::_IT8951ReadReg(uint16_t usRegAddr)
  585. {
  586. uint16_t usData;
  587. //Send Cmd and Register Address
  588. _writeCommand16(IT8951_TCON_REG_RD);
  589. _writeData16(usRegAddr);
  590. //Read data from Host Data bus
  591. usData = _readData16();
  592. return usData;
  593. }
  594. void GxEPD2_it60::_IT8951WriteReg(uint16_t usRegAddr, uint16_t usValue)
  595. {
  596. //Send Cmd , Register Address and Write Value
  597. _writeCommand16(IT8951_TCON_REG_WR);
  598. _writeData16(usRegAddr);
  599. _writeData16(usValue);
  600. }
  601. uint16_t GxEPD2_it60::_IT8951GetVCOM(void)
  602. {
  603. uint16_t vcom;
  604. _writeCommand16(USDEF_I80_CMD_VCOM);
  605. _waitWhileBusy2("_IT8951GetVCOM", default_wait_time);
  606. _writeData16(0);
  607. //Read data from Host Data bus
  608. vcom = _readData16();
  609. return vcom;
  610. }
  611. void GxEPD2_it60::_IT8951SetVCOM(uint16_t vcom)
  612. {
  613. _writeCommand16(USDEF_I80_CMD_VCOM);
  614. _waitWhileBusy2("_IT8951SetVCOM", default_wait_time);
  615. _writeData16(1);
  616. //Read data from Host Data bus
  617. _writeData16(vcom);
  618. _waitWhileBusy2("_IT8951SetVCOM", set_vcom_time);
  619. }