EPD_7in5_V2.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*****************************************************************************
  2. * | File : EPD_7in5.c
  3. * | Author : Waveshare team
  4. * | Function : Electronic paper driver
  5. * | Info :
  6. *----------------
  7. * | This version: V3.0
  8. * | Date : 2023-12-18
  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_7in5_V2.h"
  32. #include "Debug.h"
  33. /******************************************************************************
  34. function : Software reset
  35. parameter:
  36. ******************************************************************************/
  37. static void EPD_Reset(void)
  38. {
  39. DEV_Digital_Write(EPD_RST_PIN, 1);
  40. DEV_Delay_ms(20);
  41. DEV_Digital_Write(EPD_RST_PIN, 0);
  42. DEV_Delay_ms(2);
  43. DEV_Digital_Write(EPD_RST_PIN, 1);
  44. DEV_Delay_ms(20);
  45. }
  46. /******************************************************************************
  47. function : send command
  48. parameter:
  49. Reg : Command register
  50. ******************************************************************************/
  51. static void EPD_SendCommand(UBYTE Reg)
  52. {
  53. DEV_Digital_Write(EPD_DC_PIN, 0);
  54. DEV_Digital_Write(EPD_CS_PIN, 0);
  55. DEV_SPI_WriteByte(Reg);
  56. DEV_Digital_Write(EPD_CS_PIN, 1);
  57. }
  58. /******************************************************************************
  59. function : send data
  60. parameter:
  61. Data : Write data
  62. ******************************************************************************/
  63. static void EPD_SendData(UBYTE Data)
  64. {
  65. DEV_Digital_Write(EPD_DC_PIN, 1);
  66. DEV_Digital_Write(EPD_CS_PIN, 0);
  67. DEV_SPI_WriteByte(Data);
  68. DEV_Digital_Write(EPD_CS_PIN, 1);
  69. }
  70. static void EPD_SendData2(UBYTE *pData, UDOUBLE len)
  71. {
  72. DEV_Digital_Write(EPD_DC_PIN, 1);
  73. DEV_Digital_Write(EPD_CS_PIN, 0);
  74. DEV_SPI_Write_nByte(pData, len);
  75. DEV_Digital_Write(EPD_CS_PIN, 1);
  76. }
  77. /******************************************************************************
  78. function : Wait until the busy_pin goes LOW
  79. parameter:
  80. ******************************************************************************/
  81. static void EPD_WaitUntilIdle(void)
  82. {
  83. while (1)
  84. {
  85. if(DEV_Digital_Read(EPD_BUSY_PIN) == 1)
  86. break;
  87. }
  88. }
  89. /******************************************************************************
  90. function : Turn On Display
  91. parameter:
  92. ******************************************************************************/
  93. static void EPD_7IN5_V2_TurnOnDisplay(void)
  94. {
  95. EPD_SendCommand(0x12); //DISPLAY REFRESH
  96. DEV_Delay_ms(100); //!!!The delay here is necessary, 200uS at least!!!
  97. EPD_WaitUntilIdle();
  98. }
  99. /******************************************************************************
  100. function : Initialize the e-Paper register
  101. parameter:
  102. ******************************************************************************/
  103. UBYTE EPD_7IN5_V2_Init(void)
  104. {
  105. EPD_Reset();
  106. EPD_SendCommand(0x01); //POWER SETTING
  107. EPD_SendData(0x07);
  108. EPD_SendData(0x07); //VGH=20V,VGL=-20V
  109. EPD_SendData(0x3f); //VDH=15V
  110. EPD_SendData(0x3f); //VDL=-15V
  111. //Enhanced display drive(Add 0x06 command)
  112. EPD_SendCommand(0x06); //Booster Soft Start
  113. EPD_SendData(0x17);
  114. EPD_SendData(0x17);
  115. EPD_SendData(0x28);
  116. EPD_SendData(0x17);
  117. EPD_SendCommand(0x04); //POWER ON
  118. DEV_Delay_ms(100);
  119. EPD_WaitUntilIdle(); //waiting for the electronic paper IC to release the idle signal
  120. EPD_SendCommand(0X00); //PANNEL SETTING
  121. EPD_SendData(0x1F); //KW-3f KWR-2F BWROTP 0f BWOTP 1f
  122. EPD_SendCommand(0x61); //tres
  123. EPD_SendData(0x03); //source 800
  124. EPD_SendData(0x20);
  125. EPD_SendData(0x01); //gate 480
  126. EPD_SendData(0xE0);
  127. EPD_SendCommand(0X15);
  128. EPD_SendData(0x00);
  129. /*
  130. If the screen appears gray, use the annotated initialization command
  131. */
  132. EPD_SendCommand(0X50);
  133. EPD_SendData(0x10);
  134. EPD_SendData(0x07);
  135. // EPD_SendCommand(0X50);
  136. // EPD_SendData(0x10);
  137. // EPD_SendData(0x17);
  138. // EPD_SendCommand(0X52);
  139. // EPD_SendData(0x03);
  140. EPD_SendCommand(0X60); //TCON SETTING
  141. EPD_SendData(0x22);
  142. return 0;
  143. }
  144. UBYTE EPD_7IN5_V2_Init_Fast(void)
  145. {
  146. EPD_Reset();
  147. EPD_SendCommand(0X00); //PANNEL SETTING
  148. EPD_SendData(0x1F); //KW-3f KWR-2F BWROTP 0f BWOTP 1f
  149. /*
  150. If the screen appears gray, use the annotated initialization command
  151. */
  152. EPD_SendCommand(0X50);
  153. EPD_SendData(0x10);
  154. EPD_SendData(0x07);
  155. // EPD_SendCommand(0X50);
  156. // EPD_SendData(0x10);
  157. // EPD_SendData(0x17);
  158. // EPD_SendCommand(0X52);
  159. // EPD_SendData(0x03);
  160. EPD_SendCommand(0x04); //POWER ON
  161. DEV_Delay_ms(100);
  162. EPD_WaitUntilIdle(); //waiting for the electronic paper IC to release the idle signal
  163. //Enhanced display drive(Add 0x06 command)
  164. EPD_SendCommand(0x06); //Booster Soft Start
  165. EPD_SendData (0x27);
  166. EPD_SendData (0x27);
  167. EPD_SendData (0x18);
  168. EPD_SendData (0x17);
  169. EPD_SendCommand(0xE0);
  170. EPD_SendData(0x02);
  171. EPD_SendCommand(0xE5);
  172. EPD_SendData(0x5A);
  173. return 0;
  174. }
  175. UBYTE EPD_7IN5_V2_Init_Part(void)
  176. {
  177. EPD_Reset();
  178. EPD_SendCommand(0X00); //PANNEL SETTING
  179. EPD_SendData(0x1F); //KW-3f KWR-2F BWROTP 0f BWOTP 1f
  180. EPD_SendCommand(0x04); //POWER ON
  181. DEV_Delay_ms(100);
  182. EPD_WaitUntilIdle(); //waiting for the electronic paper IC to release the idle signal
  183. EPD_SendCommand(0xE0);
  184. EPD_SendData(0x02);
  185. EPD_SendCommand(0xE5);
  186. EPD_SendData(0x6E);
  187. return 0;
  188. }
  189. /*
  190. The feature will only be available on screens sold after 24/10/23
  191. */
  192. UBYTE EPD_7IN5_V2_Init_4Gray(void)
  193. {
  194. EPD_Reset();
  195. EPD_SendCommand(0X00); //PANNEL SETTING
  196. EPD_SendData(0x1F); //KW-3f KWR-2F BWROTP 0f BWOTP 1f
  197. EPD_SendCommand(0X50);
  198. EPD_SendData(0x10);
  199. EPD_SendData(0x07);
  200. EPD_SendCommand(0x04); //POWER ON
  201. DEV_Delay_ms(100);
  202. EPD_WaitUntilIdle(); //waiting for the electronic paper IC to release the idle signal
  203. EPD_SendCommand(0x06); //Booster Soft Start
  204. EPD_SendData (0x27);
  205. EPD_SendData (0x27);
  206. EPD_SendData (0x18);
  207. EPD_SendData (0x17);
  208. EPD_SendCommand(0xE0);
  209. EPD_SendData(0x02);
  210. EPD_SendCommand(0xE5);
  211. EPD_SendData(0x5F);
  212. return 0;
  213. }
  214. /******************************************************************************
  215. function : Clear screen
  216. parameter:
  217. ******************************************************************************/
  218. void EPD_7IN5_V2_Clear(void)
  219. {
  220. UWORD Width, Height;
  221. Width =(EPD_7IN5_V2_WIDTH % 8 == 0)?(EPD_7IN5_V2_WIDTH / 8 ):(EPD_7IN5_V2_WIDTH / 8 + 1);
  222. Height = EPD_7IN5_V2_HEIGHT;
  223. UBYTE image[EPD_7IN5_V2_WIDTH / 8] = {0x00};
  224. UWORD i;
  225. EPD_SendCommand(0x10);
  226. for(i=0; i<Width; i++) {
  227. image[i] = 0xFF;
  228. }
  229. for(i=0; i<Height; i++)
  230. {
  231. EPD_SendData2(image, Width);
  232. }
  233. EPD_SendCommand(0x13);
  234. for(i=0; i<Width; i++) {
  235. image[i] = 0x00;
  236. }
  237. for(i=0; i<Height; i++)
  238. {
  239. EPD_SendData2(image, Width);
  240. }
  241. EPD_7IN5_V2_TurnOnDisplay();
  242. }
  243. void EPD_7IN5_V2_ClearBlack(void)
  244. {
  245. UWORD Width, Height;
  246. Width =(EPD_7IN5_V2_WIDTH % 8 == 0)?(EPD_7IN5_V2_WIDTH / 8 ):(EPD_7IN5_V2_WIDTH / 8 + 1);
  247. Height = EPD_7IN5_V2_HEIGHT;
  248. UBYTE image[EPD_7IN5_V2_WIDTH / 8] = {0x00};
  249. UWORD i;
  250. EPD_SendCommand(0x10);
  251. for(i=0; i<Width; i++) {
  252. image[i] = 0x00;
  253. }
  254. for(i=0; i<Height; i++)
  255. {
  256. EPD_SendData2(image, Width);
  257. }
  258. EPD_SendCommand(0x13);
  259. for(i=0; i<Width; i++) {
  260. image[i] = 0xFF;
  261. }
  262. for(i=0; i<Height; i++)
  263. {
  264. EPD_SendData2(image, Width);
  265. }
  266. EPD_7IN5_V2_TurnOnDisplay();
  267. }
  268. /******************************************************************************
  269. function : Sends the image buffer in RAM to e-Paper and displays
  270. parameter:
  271. ******************************************************************************/
  272. void EPD_7IN5_V2_Display(UBYTE *blackimage)
  273. {
  274. UDOUBLE Width, Height;
  275. Width =(EPD_7IN5_V2_WIDTH % 8 == 0)?(EPD_7IN5_V2_WIDTH / 8 ):(EPD_7IN5_V2_WIDTH / 8 + 1);
  276. Height = EPD_7IN5_V2_HEIGHT;
  277. EPD_SendCommand(0x10);
  278. for (UDOUBLE j = 0; j < Height; j++) {
  279. EPD_SendData2((UBYTE *)(blackimage+j*Width), Width);
  280. }
  281. EPD_SendCommand(0x13);
  282. for (UDOUBLE j = 0; j < Height; j++) {
  283. for (UDOUBLE i = 0; i < Width; i++) {
  284. blackimage[i + j * Width] = ~blackimage[i + j * Width];
  285. }
  286. }
  287. for (UDOUBLE j = 0; j < Height; j++) {
  288. EPD_SendData2((UBYTE *)(blackimage+j*Width), Width);
  289. }
  290. EPD_7IN5_V2_TurnOnDisplay();
  291. }
  292. void EPD_7IN5_V2_Display_Part(UBYTE *blackimage,UDOUBLE x_start, UDOUBLE y_start, UDOUBLE x_end, UDOUBLE y_end)
  293. {
  294. UDOUBLE Width, Height;
  295. Width =((x_end - x_start) % 8 == 0)?((x_end - x_start) / 8 ):((x_end - x_start) / 8 + 1);
  296. Height = y_end - y_start;
  297. EPD_SendCommand(0x50);
  298. EPD_SendData(0xA9);
  299. EPD_SendData(0x07);
  300. EPD_SendCommand(0x91); //This command makes the display enter partial mode
  301. EPD_SendCommand(0x90); //resolution setting
  302. EPD_SendData (x_start/256);
  303. EPD_SendData (x_start%256); //x-start
  304. EPD_SendData (x_end/256);
  305. EPD_SendData (x_end%256-1); //x-end
  306. EPD_SendData (y_start/256); //
  307. EPD_SendData (y_start%256); //y-start
  308. EPD_SendData (y_end/256);
  309. EPD_SendData (y_end%256-1); //y-end
  310. EPD_SendData (0x01);
  311. EPD_SendCommand(0x13);
  312. for (UDOUBLE j = 0; j < Height; j++) {
  313. EPD_SendData2((UBYTE *)(blackimage+j*Width), Width);
  314. }
  315. EPD_7IN5_V2_TurnOnDisplay();
  316. }
  317. void EPD_7IN5_V2_Display_4Gray(const UBYTE *Image)
  318. {
  319. UDOUBLE i,j,k;
  320. UBYTE temp1,temp2,temp3;
  321. // old data
  322. EPD_SendCommand(0x10);
  323. for(i=0; i<48000; i++) {
  324. temp3=0;
  325. for(j=0; j<2; j++) {
  326. temp1 = Image[i*2+j];
  327. for(k=0; k<2; k++) {
  328. temp2 = temp1&0xC0;
  329. if(temp2 == 0xC0)
  330. temp3 |= 0x00;
  331. else if(temp2 == 0x00)
  332. temp3 |= 0x01;
  333. else if(temp2 == 0x80)
  334. temp3 |= 0x01;
  335. else //0x40
  336. temp3 |= 0x00;
  337. temp3 <<= 1;
  338. temp1 <<= 2;
  339. temp2 = temp1&0xC0 ;
  340. if(temp2 == 0xC0)
  341. temp3 |= 0x00;
  342. else if(temp2 == 0x00)
  343. temp3 |= 0x01;
  344. else if(temp2 == 0x80)
  345. temp3 |= 0x01;
  346. else //0x40
  347. temp3 |= 0x00;
  348. if(j!=1 || k!=1)
  349. temp3 <<= 1;
  350. temp1 <<= 2;
  351. }
  352. }
  353. EPD_SendData(temp3);
  354. // printf("%x",temp3);
  355. }
  356. EPD_SendCommand(0x13); //write RAM for black(0)/white (1)
  357. for(i=0; i<48000; i++) { //5808*4 46464
  358. temp3=0;
  359. for(j=0; j<2; j++) {
  360. temp1 = Image[i*2+j];
  361. for(k=0; k<2; k++) {
  362. temp2 = temp1&0xC0 ;
  363. if(temp2 == 0xC0)
  364. temp3 |= 0x00;//white
  365. else if(temp2 == 0x00)
  366. temp3 |= 0x01; //black
  367. else if(temp2 == 0x80)
  368. temp3 |= 0x00; //gray1
  369. else //0x40
  370. temp3 |= 0x01; //gray2
  371. temp3 <<= 1;
  372. temp1 <<= 2;
  373. temp2 = temp1&0xC0 ;
  374. if(temp2 == 0xC0) //white
  375. temp3 |= 0x00;
  376. else if(temp2 == 0x00) //black
  377. temp3 |= 0x01;
  378. else if(temp2 == 0x80)
  379. temp3 |= 0x00; //gray1
  380. else //0x40
  381. temp3 |= 0x01; //gray2
  382. if(j!=1 || k!=1)
  383. temp3 <<= 1;
  384. temp1 <<= 2;
  385. }
  386. }
  387. EPD_SendData(temp3);
  388. // printf("%x",temp3);
  389. }
  390. EPD_7IN5_V2_TurnOnDisplay();
  391. }
  392. void EPD_7IN5_V2_WritePicture_4Gray(const UBYTE *Image)
  393. {
  394. UDOUBLE i,j,k,o;
  395. UBYTE temp1,temp2,temp3;
  396. UWORD Width, Height;
  397. Width = (EPD_7IN5_V2_WIDTH % 8 == 0)? (EPD_7IN5_V2_WIDTH / 8 ): (EPD_7IN5_V2_WIDTH / 8 + 1);
  398. Height = EPD_7IN5_V2_HEIGHT;
  399. EPD_SendCommand(0x10);
  400. for(i=0; i<Height; i++) {
  401. for(o=0; o<Width; o++) {
  402. if((o < Width/2)&&(i < Height))
  403. {
  404. temp3=0;
  405. for(j=0; j<2; j++) {
  406. temp1 = Image[(i*Width/2+o)*2+j];
  407. for(k=0; k<2; k++) {
  408. temp2 = temp1&0xC0;
  409. if(temp2 == 0xC0)
  410. temp3 |= 0x00;
  411. else if(temp2 == 0x00)
  412. temp3 |= 0x01;
  413. else if(temp2 == 0x80)
  414. temp3 |= 0x01;
  415. else //0x40
  416. temp3 |= 0x00;
  417. temp3 <<= 1;
  418. temp1 <<= 2;
  419. temp2 = temp1&0xC0 ;
  420. if(temp2 == 0xC0)
  421. temp3 |= 0x00;
  422. else if(temp2 == 0x00)
  423. temp3 |= 0x01;
  424. else if(temp2 == 0x80)
  425. temp3 |= 0x01;
  426. else //0x40
  427. temp3 |= 0x00;
  428. if(j!=1 || k!=1)
  429. temp3 <<= 1;
  430. temp1 <<= 2;
  431. }
  432. }
  433. EPD_SendData(temp3);
  434. // printf("%x",temp3);
  435. }
  436. else
  437. {
  438. EPD_SendData(0x00);
  439. }
  440. }
  441. }
  442. EPD_SendCommand(0x13);
  443. for(i=0; i<Height; i++) {
  444. for(o=0; o<Width; o++) {
  445. if((o < Width/2)&&(i < Height))
  446. {
  447. for(j=0; j<2; j++) {
  448. temp1 = Image[(i*Width/2+o)*2+j];
  449. for(k=0; k<2; k++) {
  450. temp2 = temp1&0xC0 ;
  451. if(temp2 == 0xC0)
  452. temp3 |= 0x00;//white
  453. else if(temp2 == 0x00)
  454. temp3 |= 0x01; //black
  455. else if(temp2 == 0x80)
  456. temp3 |= 0x00; //gray1
  457. else //0x40
  458. temp3 |= 0x01; //gray2
  459. temp3 <<= 1;
  460. temp1 <<= 2;
  461. temp2 = temp1&0xC0 ;
  462. if(temp2 == 0xC0) //white
  463. temp3 |= 0x00;
  464. else if(temp2 == 0x00) //black
  465. temp3 |= 0x01;
  466. else if(temp2 == 0x80)
  467. temp3 |= 0x00; //gray1
  468. else //0x40
  469. temp3 |= 0x01; //gray2
  470. if(j!=1 || k!=1)
  471. temp3 <<= 1;
  472. temp1 <<= 2;
  473. }
  474. }
  475. EPD_SendData(temp3);
  476. // printf("%x",temp3);
  477. }
  478. else
  479. {
  480. EPD_SendData(0x00);
  481. }
  482. }
  483. }
  484. EPD_7IN5_V2_TurnOnDisplay();
  485. }
  486. /******************************************************************************
  487. function : Enter sleep mode
  488. parameter:
  489. ******************************************************************************/
  490. void EPD_7IN5_V2_Sleep(void)
  491. {
  492. EPD_SendCommand(0x50);
  493. EPD_SendData(0XF7);
  494. EPD_SendCommand(0X02); //power off
  495. EPD_WaitUntilIdle();
  496. EPD_SendCommand(0X07); //deep sleep
  497. EPD_SendData(0xA5);
  498. }