GxEPD2_it103_1872x1404.cpp 21 KB

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