EPD_4in2_V2.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*****************************************************************************
  2. * | File : EPD_4in2_V2.h
  3. * | Author : Waveshare team
  4. * | Function : 4.2inch e-paper V2
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2023-09-11
  9. * | Info :
  10. * -----------------------------------------------------------------------------
  11. #
  12. # Permission is hereby granted, free of charge, to any person obtaining a copy
  13. # of this software and associated documnetation files (the "Software"), to deal
  14. # in the Software without restriction, including without limitation the rights
  15. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. # copies of the Software, and to permit persons to whom the Software is
  17. # furished to do so, subject to the following conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included in
  20. # all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. # THE SOFTWARE.
  29. #
  30. ******************************************************************************/
  31. #include "EPD_4in2_V2.h"
  32. #include "Debug.h"
  33. const unsigned char LUT_ALL[233]={
  34. 0x01, 0x0A, 0x1B, 0x0F, 0x03, 0x01, 0x01,
  35. 0x05, 0x0A, 0x01, 0x0A, 0x01, 0x01, 0x01,
  36. 0x05, 0x08, 0x03, 0x02, 0x04, 0x01, 0x01,
  37. 0x01, 0x04, 0x04, 0x02, 0x00, 0x01, 0x01,
  38. 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  39. 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  40. 0x01, 0x0A, 0x1B, 0x0F, 0x03, 0x01, 0x01,
  41. 0x05, 0x4A, 0x01, 0x8A, 0x01, 0x01, 0x01,
  42. 0x05, 0x48, 0x03, 0x82, 0x84, 0x01, 0x01,
  43. 0x01, 0x84, 0x84, 0x82, 0x00, 0x01, 0x01,
  44. 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  45. 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  46. 0x01, 0x0A, 0x1B, 0x8F, 0x03, 0x01, 0x01,
  47. 0x05, 0x4A, 0x01, 0x8A, 0x01, 0x01, 0x01,
  48. 0x05, 0x48, 0x83, 0x82, 0x04, 0x01, 0x01,
  49. 0x01, 0x04, 0x04, 0x02, 0x00, 0x01, 0x01,
  50. 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  51. 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  52. 0x01, 0x8A, 0x1B, 0x8F, 0x03, 0x01, 0x01,
  53. 0x05, 0x4A, 0x01, 0x8A, 0x01, 0x01, 0x01,
  54. 0x05, 0x48, 0x83, 0x02, 0x04, 0x01, 0x01,
  55. 0x01, 0x04, 0x04, 0x02, 0x00, 0x01, 0x01,
  56. 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  57. 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  58. 0x01, 0x8A, 0x9B, 0x8F, 0x03, 0x01, 0x01,
  59. 0x05, 0x4A, 0x01, 0x8A, 0x01, 0x01, 0x01,
  60. 0x05, 0x48, 0x03, 0x42, 0x04, 0x01, 0x01,
  61. 0x01, 0x04, 0x04, 0x42, 0x00, 0x01, 0x01,
  62. 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  63. 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  64. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x02, 0x00, 0x00, 0x07, 0x17, 0x41, 0xA8,
  67. 0x32, 0x30,
  68. };
  69. /******************************************************************************
  70. function : Software reset
  71. parameter:
  72. ******************************************************************************/
  73. static void EPD_4IN2_V2_Reset(void)
  74. {
  75. DEV_Digital_Write(EPD_RST_PIN, 1);
  76. DEV_Delay_ms(100);
  77. DEV_Digital_Write(EPD_RST_PIN, 0);
  78. DEV_Delay_ms(2);
  79. DEV_Digital_Write(EPD_RST_PIN, 1);
  80. DEV_Delay_ms(100);
  81. }
  82. /******************************************************************************
  83. function : send command
  84. parameter:
  85. Reg : Command register
  86. ******************************************************************************/
  87. static void EPD_4IN2_V2_SendCommand(UBYTE Reg)
  88. {
  89. DEV_Digital_Write(EPD_DC_PIN, 0);
  90. DEV_Digital_Write(EPD_CS_PIN, 0);
  91. DEV_SPI_WriteByte(Reg);
  92. DEV_Digital_Write(EPD_CS_PIN, 1);
  93. }
  94. /******************************************************************************
  95. function : send data
  96. parameter:
  97. Data : Write data
  98. ******************************************************************************/
  99. static void EPD_4IN2_V2_SendData(UBYTE Data)
  100. {
  101. DEV_Digital_Write(EPD_DC_PIN, 1);
  102. DEV_Digital_Write(EPD_CS_PIN, 0);
  103. DEV_SPI_WriteByte(Data);
  104. DEV_Digital_Write(EPD_CS_PIN, 1);
  105. }
  106. /******************************************************************************
  107. function : Wait until the busy_pin goes LOW
  108. parameter:
  109. ******************************************************************************/
  110. void EPD_4IN2_V2_ReadBusy(void)
  111. {
  112. Debug("e-Paper busy\r\n");
  113. while(DEV_Digital_Read(EPD_BUSY_PIN) == 1) { //LOW: idle, HIGH: busy
  114. DEV_Delay_ms(10);
  115. }
  116. Debug("e-Paper busy release\r\n");
  117. }
  118. /******************************************************************************
  119. function : Turn On Display
  120. parameter:
  121. ******************************************************************************/
  122. static void EPD_4IN2_V2_TurnOnDisplay(void)
  123. {
  124. EPD_4IN2_V2_SendCommand(0x22);
  125. EPD_4IN2_V2_SendData(0xF7);
  126. EPD_4IN2_V2_SendCommand(0x20);
  127. EPD_4IN2_V2_ReadBusy();
  128. }
  129. static void EPD_4IN2_V2_TurnOnDisplay_Fast(void)
  130. {
  131. EPD_4IN2_V2_SendCommand(0x22);
  132. EPD_4IN2_V2_SendData(0xC7);
  133. EPD_4IN2_V2_SendCommand(0x20);
  134. EPD_4IN2_V2_ReadBusy();
  135. }
  136. static void EPD_4IN2_V2_TurnOnDisplay_Partial(void)
  137. {
  138. EPD_4IN2_V2_SendCommand(0x22);
  139. EPD_4IN2_V2_SendData(0xFF);
  140. EPD_4IN2_V2_SendCommand(0x20);
  141. EPD_4IN2_V2_ReadBusy();
  142. }
  143. static void EPD_4IN2_V2_TurnOnDisplay_4Gray(void)
  144. {
  145. EPD_4IN2_V2_SendCommand(0x22);
  146. EPD_4IN2_V2_SendData(0xCF);
  147. EPD_4IN2_V2_SendCommand(0x20);
  148. EPD_4IN2_V2_ReadBusy();
  149. }
  150. /******************************************************************************
  151. function : Setting the display window
  152. parameter:
  153. ******************************************************************************/
  154. static void EPD_4IN2_V2_SetWindows(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
  155. {
  156. EPD_4IN2_V2_SendCommand(0x44); // SET_RAM_X_ADDRESS_START_END_POSITION
  157. EPD_4IN2_V2_SendData((Xstart>>3) & 0xFF);
  158. EPD_4IN2_V2_SendData((Xend>>3) & 0xFF);
  159. EPD_4IN2_V2_SendCommand(0x45); // SET_RAM_Y_ADDRESS_START_END_POSITION
  160. EPD_4IN2_V2_SendData(Ystart & 0xFF);
  161. EPD_4IN2_V2_SendData((Ystart >> 8) & 0xFF);
  162. EPD_4IN2_V2_SendData(Yend & 0xFF);
  163. EPD_4IN2_V2_SendData((Yend >> 8) & 0xFF);
  164. }
  165. /******************************************************************************
  166. function : Set Cursor
  167. parameter:
  168. ******************************************************************************/
  169. static void EPD_4IN2_V2_SetCursor(UWORD Xstart, UWORD Ystart)
  170. {
  171. EPD_4IN2_V2_SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  172. EPD_4IN2_V2_SendData((Xstart>>3) & 0xFF);
  173. EPD_4IN2_V2_SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  174. EPD_4IN2_V2_SendData(Ystart & 0xFF);
  175. EPD_4IN2_V2_SendData((Ystart >> 8) & 0xFF);
  176. }
  177. //LUT download
  178. static void EPD_4IN2_V2_4Gray_lut(void)
  179. {
  180. unsigned char i;
  181. //WS byte 0~152, the content of VS[nX-LUTm], TP[nX], RP[n], SR[nXY], FR[n] and XON[nXY]
  182. EPD_4IN2_V2_SendCommand(0x32);
  183. for(i=0;i<227;i++)
  184. {
  185. EPD_4IN2_V2_SendData(LUT_ALL[i]);
  186. }
  187. //WS byte 153, the content of Option for LUT end
  188. EPD_4IN2_V2_SendCommand(0x3F);
  189. EPD_4IN2_V2_SendData(LUT_ALL[i++]);
  190. //WS byte 154, the content of gate leve
  191. EPD_4IN2_V2_SendCommand(0x03);
  192. EPD_4IN2_V2_SendData(LUT_ALL[i++]);//VGH
  193. //WS byte 155~157, the content of source level
  194. EPD_4IN2_V2_SendCommand(0x04);
  195. EPD_4IN2_V2_SendData(LUT_ALL[i++]);//VSH1
  196. EPD_4IN2_V2_SendData(LUT_ALL[i++]);//VSH2
  197. EPD_4IN2_V2_SendData(LUT_ALL[i++]);//VSL
  198. //WS byte 158, the content of VCOM level
  199. EPD_4IN2_V2_SendCommand(0x2c);
  200. EPD_4IN2_V2_SendData(LUT_ALL[i++]);//VCOM
  201. }
  202. /******************************************************************************
  203. function : Initialize the e-Paper register
  204. parameter:
  205. ******************************************************************************/
  206. void EPD_4IN2_V2_Init(void)
  207. {
  208. EPD_4IN2_V2_Reset();
  209. EPD_4IN2_V2_ReadBusy();
  210. EPD_4IN2_V2_SendCommand(0x12); // soft reset
  211. EPD_4IN2_V2_ReadBusy();
  212. // EPD_4IN2_V2_SendCommand(0x01); //Driver output control
  213. // EPD_4IN2_V2_SendData((EPD_4IN2_V2_HEIGHT-1)%256);
  214. // EPD_4IN2_V2_SendData((EPD_4IN2_V2_HEIGHT-1)/256);
  215. // EPD_4IN2_V2_SendData(0x00);
  216. EPD_4IN2_V2_SendCommand(0x21); // Display update control
  217. EPD_4IN2_V2_SendData(0x40);
  218. EPD_4IN2_V2_SendData(0x00);
  219. EPD_4IN2_V2_SendCommand(0x3C); //BorderWavefrom
  220. EPD_4IN2_V2_SendData(0x05);
  221. EPD_4IN2_V2_SendCommand(0x11); // data entry mode
  222. EPD_4IN2_V2_SendData(0x03); // X-mode
  223. EPD_4IN2_V2_SetWindows(0, 0, EPD_4IN2_V2_WIDTH-1, EPD_4IN2_V2_HEIGHT-1);
  224. EPD_4IN2_V2_SetCursor(0, 0);
  225. EPD_4IN2_V2_ReadBusy();
  226. }
  227. /******************************************************************************
  228. function : Initialize Fast the e-Paper register
  229. parameter:
  230. ******************************************************************************/
  231. void EPD_4IN2_V2_Init_Fast(UBYTE Mode)
  232. {
  233. EPD_4IN2_V2_Reset();
  234. EPD_4IN2_V2_ReadBusy();
  235. EPD_4IN2_V2_SendCommand(0x12); // soft reset
  236. EPD_4IN2_V2_ReadBusy();
  237. EPD_4IN2_V2_SendCommand(0x21);
  238. EPD_4IN2_V2_SendData(0x40);
  239. EPD_4IN2_V2_SendData(0x00);
  240. EPD_4IN2_V2_SendCommand(0x3C);
  241. EPD_4IN2_V2_SendData(0x05);
  242. if(Mode == Seconds_1_5S)
  243. {
  244. //1.5s
  245. EPD_4IN2_V2_SendCommand(0x1A); // Write to temperature register
  246. EPD_4IN2_V2_SendData(0x6E);
  247. }
  248. else if(Mode == Seconds_1S)
  249. {
  250. //1s
  251. EPD_4IN2_V2_SendCommand(0x1A); // Write to temperature register
  252. EPD_4IN2_V2_SendData(0x5A);
  253. }
  254. EPD_4IN2_V2_SendCommand(0x22); // Load temperature value
  255. EPD_4IN2_V2_SendData(0x91);
  256. EPD_4IN2_V2_SendCommand(0x20);
  257. EPD_4IN2_V2_ReadBusy();
  258. EPD_4IN2_V2_SendCommand(0x11); // data entry mode
  259. EPD_4IN2_V2_SendData(0x03); // X-mode
  260. EPD_4IN2_V2_SetWindows(0, 0, EPD_4IN2_V2_WIDTH-1, EPD_4IN2_V2_HEIGHT-1);
  261. EPD_4IN2_V2_SetCursor(0, 0);
  262. EPD_4IN2_V2_ReadBusy();
  263. }
  264. void EPD_4IN2_V2_Init_4Gray(void)
  265. {
  266. EPD_4IN2_V2_Reset();
  267. EPD_4IN2_V2_SendCommand(0x12); //SWRESET
  268. EPD_4IN2_V2_ReadBusy();
  269. EPD_4IN2_V2_SendCommand(0x21);
  270. EPD_4IN2_V2_SendData(0x00);
  271. EPD_4IN2_V2_SendData(0x00);
  272. EPD_4IN2_V2_SendCommand(0x3C);
  273. EPD_4IN2_V2_SendData(0x03);
  274. EPD_4IN2_V2_SendCommand(0x0C); //BTST
  275. EPD_4IN2_V2_SendData(0x8B);//8B
  276. EPD_4IN2_V2_SendData(0x9C);//9C
  277. EPD_4IN2_V2_SendData(0xA4);//96 A4
  278. EPD_4IN2_V2_SendData(0x0F);//0F
  279. // EPD_4IN2_V2_SendCommand(0x01); // 驱动输出控制 drive output control
  280. // EPD_4IN2_V2_SendData(0x2B); // Y 的低字节
  281. // EPD_4IN2_V2_SendData(0x01); // Y 的高字节
  282. // EPD_4IN2_V2_SendData(0x00);
  283. EPD_4IN2_V2_4Gray_lut(); //LUT
  284. EPD_4IN2_V2_SendCommand(0x11); // data entry mode
  285. EPD_4IN2_V2_SendData(0x03); // X-mode
  286. EPD_4IN2_V2_SetWindows(0, 0, EPD_4IN2_V2_WIDTH-1, EPD_4IN2_V2_HEIGHT-1);
  287. EPD_4IN2_V2_SetCursor(0, 0);
  288. }
  289. /******************************************************************************
  290. function : Clear screen
  291. parameter:
  292. ******************************************************************************/
  293. void EPD_4IN2_V2_Clear(void)
  294. {
  295. UWORD Width, Height;
  296. Width = (EPD_4IN2_V2_WIDTH % 8 == 0)? (EPD_4IN2_V2_WIDTH / 8 ): (EPD_4IN2_V2_WIDTH / 8 + 1);
  297. Height = EPD_4IN2_V2_HEIGHT;
  298. EPD_4IN2_V2_SendCommand(0x24);
  299. for (UWORD j = 0; j < Height; j++) {
  300. for (UWORD i = 0; i < Width; i++) {
  301. EPD_4IN2_V2_SendData(0xFF);
  302. }
  303. }
  304. EPD_4IN2_V2_SendCommand(0x26);
  305. for (UWORD j = 0; j < Height; j++) {
  306. for (UWORD i = 0; i < Width; i++) {
  307. EPD_4IN2_V2_SendData(0xFF);
  308. }
  309. }
  310. EPD_4IN2_V2_TurnOnDisplay();
  311. }
  312. /******************************************************************************
  313. function : Sends the image buffer in RAM to e-Paper and displays
  314. parameter:
  315. ******************************************************************************/
  316. void EPD_4IN2_V2_Display(UBYTE *Image)
  317. {
  318. UWORD Width, Height;
  319. Width = (EPD_4IN2_V2_WIDTH % 8 == 0)? (EPD_4IN2_V2_WIDTH / 8 ): (EPD_4IN2_V2_WIDTH / 8 + 1);
  320. Height = EPD_4IN2_V2_HEIGHT;
  321. EPD_4IN2_V2_SendCommand(0x24);
  322. for (UWORD j = 0; j < Height; j++) {
  323. for (UWORD i = 0; i < Width; i++) {
  324. EPD_4IN2_V2_SendData(Image[i + j * Width]);
  325. }
  326. }
  327. EPD_4IN2_V2_SendCommand(0x26);
  328. for (UWORD j = 0; j < Height; j++) {
  329. for (UWORD i = 0; i < Width; i++) {
  330. EPD_4IN2_V2_SendData(Image[i + j * Width]);
  331. }
  332. }
  333. EPD_4IN2_V2_TurnOnDisplay();
  334. }
  335. /******************************************************************************
  336. function : Sends the image buffer in RAM to e-Paper and fast displays
  337. parameter:
  338. ******************************************************************************/
  339. void EPD_4IN2_V2_Display_Fast(UBYTE *Image)
  340. {
  341. UWORD Width, Height;
  342. Width = (EPD_4IN2_V2_WIDTH % 8 == 0)? (EPD_4IN2_V2_WIDTH / 8 ): (EPD_4IN2_V2_WIDTH / 8 + 1);
  343. Height = EPD_4IN2_V2_HEIGHT;
  344. EPD_4IN2_V2_SendCommand(0x24);
  345. for (UWORD j = 0; j < Height; j++) {
  346. for (UWORD i = 0; i < Width; i++) {
  347. EPD_4IN2_V2_SendData(Image[i + j * Width]);
  348. }
  349. }
  350. EPD_4IN2_V2_SendCommand(0x26);
  351. for (UWORD j = 0; j < Height; j++) {
  352. for (UWORD i = 0; i < Width; i++) {
  353. EPD_4IN2_V2_SendData(Image[i + j * Width]);
  354. }
  355. }
  356. EPD_4IN2_V2_TurnOnDisplay_Fast();
  357. }
  358. void EPD_4IN2_V2_Display_4Gray(UBYTE *Image)
  359. {
  360. UDOUBLE i,j,k,m;
  361. UBYTE temp1,temp2,temp3;
  362. /****Color display description****
  363. white gray2 gray1 black
  364. 0x10| 01 01 00 00
  365. 0x13| 01 00 01 00
  366. *********************************/
  367. EPD_4IN2_V2_SendCommand(0x24);
  368. // EPD_4IN2_HEIGHT
  369. // EPD_4IN2_WIDTH
  370. for(m = 0; m<EPD_4IN2_V2_HEIGHT;m++)
  371. for(i=0;i<EPD_4IN2_V2_WIDTH/8;i++)
  372. {
  373. temp3=0;
  374. for(j=0;j<2;j++)
  375. {
  376. temp1 = Image[(m*(EPD_4IN2_V2_WIDTH/8)+i)*2+j];
  377. for(k=0;k<2;k++)
  378. {
  379. temp2 = temp1&0xC0 ;
  380. if(temp2 == 0xC0)
  381. temp3 |= 0x01;//white
  382. else if(temp2 == 0x00)
  383. temp3 |= 0x00; //black
  384. else if(temp2 == 0x80)
  385. temp3 |= 0x00; //gray1
  386. else //0x40
  387. temp3 |= 0x01; //gray2
  388. temp3 <<= 1;
  389. temp1 <<= 2;
  390. temp2 = temp1&0xC0 ;
  391. if(temp2 == 0xC0) //white
  392. temp3 |= 0x01;
  393. else if(temp2 == 0x00) //black
  394. temp3 |= 0x00;
  395. else if(temp2 == 0x80)
  396. temp3 |= 0x00; //gray1
  397. else //0x40
  398. temp3 |= 0x01; //gray2
  399. if(j!=1 || k!=1)
  400. temp3 <<= 1;
  401. temp1 <<= 2;
  402. }
  403. }
  404. EPD_4IN2_V2_SendData(temp3);
  405. }
  406. // new data
  407. EPD_4IN2_V2_SendCommand(0x26);
  408. for(m = 0; m<EPD_4IN2_V2_HEIGHT;m++)
  409. for(i=0;i<EPD_4IN2_V2_WIDTH/8;i++)
  410. {
  411. temp3=0;
  412. for(j=0;j<2;j++)
  413. {
  414. temp1 = Image[(m*(EPD_4IN2_V2_WIDTH/8)+i)*2+j];
  415. for(k=0;k<2;k++)
  416. {
  417. temp2 = temp1&0xC0 ;
  418. if(temp2 == 0xC0)
  419. temp3 |= 0x01;//white
  420. else if(temp2 == 0x00)
  421. temp3 |= 0x00; //black
  422. else if(temp2 == 0x80)
  423. temp3 |= 0x01; //gray1
  424. else //0x40
  425. temp3 |= 0x00; //gray2
  426. temp3 <<= 1;
  427. temp1 <<= 2;
  428. temp2 = temp1&0xC0 ;
  429. if(temp2 == 0xC0) //white
  430. temp3 |= 0x01;
  431. else if(temp2 == 0x00) //black
  432. temp3 |= 0x00;
  433. else if(temp2 == 0x80)
  434. temp3 |= 0x01; //gray1
  435. else //0x40
  436. temp3 |= 0x00; //gray2
  437. if(j!=1 || k!=1)
  438. temp3 <<= 1;
  439. temp1 <<= 2;
  440. }
  441. }
  442. EPD_4IN2_V2_SendData(temp3);
  443. }
  444. EPD_4IN2_V2_TurnOnDisplay_4Gray();
  445. }
  446. // Send partial data for partial refresh
  447. void EPD_4IN2_V2_PartialDisplay(UBYTE *Image, UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
  448. {
  449. if((Xstart % 8 + Xend % 8 == 8 && Xstart % 8 > Xend % 8) || Xstart % 8 + Xend % 8 == 0 || (Xend - Xstart)%8 == 0)
  450. {
  451. Xstart = Xstart / 8 ;
  452. Xend = Xend / 8;
  453. }
  454. else
  455. {
  456. Xstart = Xstart / 8 ;
  457. Xend = Xend % 8 == 0 ? Xend / 8 : Xend / 8 + 1;
  458. }
  459. UWORD i, Width;
  460. Width = Xend - Xstart;
  461. UWORD IMAGE_COUNTER = Width * (Yend-Ystart);
  462. Xend -= 1;
  463. Yend -= 1;
  464. EPD_4IN2_V2_SendCommand(0x21);
  465. EPD_4IN2_V2_SendData(0x00);
  466. EPD_4IN2_V2_SendData(0x00);
  467. EPD_4IN2_V2_SendCommand(0x3C);
  468. EPD_4IN2_V2_SendData(0x80);
  469. EPD_4IN2_V2_SendCommand(0x11); // data entry mode
  470. EPD_4IN2_V2_SendData(0x03); // X-mode
  471. EPD_4IN2_V2_SendCommand(0x44); // set RAM x address start/end, in page 35
  472. EPD_4IN2_V2_SendData(Xstart & 0xff); // RAM x address start at 00h;
  473. EPD_4IN2_V2_SendData(Xend & 0xff); // RAM x address end at 0fh(15+1)*8->128
  474. EPD_4IN2_V2_SendCommand(0x45); // set RAM y address start/end, in page 35
  475. EPD_4IN2_V2_SendData(Ystart & 0xff); // RAM y address start at 0127h;
  476. EPD_4IN2_V2_SendData((Ystart>>8) & 0x01); // RAM y address start at 0127h;
  477. EPD_4IN2_V2_SendData(Yend & 0xff); // RAM y address end at 00h;
  478. EPD_4IN2_V2_SendData((Yend>>8) & 0x01);
  479. EPD_4IN2_V2_SendCommand(0x4E); // set RAM x address count to 0;
  480. EPD_4IN2_V2_SendData(Xstart & 0xff);
  481. EPD_4IN2_V2_SendCommand(0x4F); // set RAM y address count to 0X127;
  482. EPD_4IN2_V2_SendData(Ystart & 0xff);
  483. EPD_4IN2_V2_SendData((Ystart>>8) & 0x01);
  484. EPD_4IN2_V2_ReadBusy();
  485. EPD_4IN2_V2_SendCommand(0x24);
  486. for (UWORD j = 0; j < IMAGE_COUNTER; j++) {
  487. EPD_4IN2_V2_SendData(Image[j]);
  488. }
  489. EPD_4IN2_V2_TurnOnDisplay_Partial();
  490. }
  491. /******************************************************************************
  492. function : Enter sleep mode
  493. parameter:
  494. ******************************************************************************/
  495. void EPD_4IN2_V2_Sleep(void)
  496. {
  497. EPD_4IN2_V2_SendCommand(0x10); // DEEP_SLEEP
  498. EPD_4IN2_V2_SendData(0x01);
  499. DEV_Delay_ms(200);
  500. }