EPD_4in26.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*****************************************************************************
  2. * | File : EPD_4in26.c
  3. * | Author : Waveshare team
  4. * | Function : 4.26inch e-paper test demo
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2023-12-19
  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_4in26.h"
  32. #include "Debug.h"
  33. const unsigned char LUT_DATA_4Gray[112] = //112bytes
  34. {
  35. 0x80, 0x48, 0x4A, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  36. 0x0A, 0x48, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  37. 0x88, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  38. 0xA8, 0x48, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  39. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  40. 0x07, 0x1E, 0x1C, 0x02, 0x00,
  41. 0x05, 0x01, 0x05, 0x01, 0x02,
  42. 0x08, 0x01, 0x01, 0x04, 0x04,
  43. 0x00, 0x02, 0x00, 0x02, 0x01,
  44. 0x00, 0x00, 0x00, 0x00, 0x00,
  45. 0x00, 0x00, 0x00, 0x00, 0x00,
  46. 0x00, 0x00, 0x00, 0x00, 0x00,
  47. 0x00, 0x00, 0x00, 0x00, 0x00,
  48. 0x00, 0x00, 0x00, 0x00, 0x00,
  49. 0x00, 0x00, 0x00, 0x00, 0x01,
  50. 0x22, 0x22, 0x22, 0x22, 0x22,
  51. 0x17, 0x41, 0xA8, 0x32, 0x30,
  52. 0x00, 0x00,
  53. };
  54. /******************************************************************************
  55. function : Software reset
  56. parameter:
  57. ******************************************************************************/
  58. static void EPD_4in26_Reset(void)
  59. {
  60. DEV_Digital_Write(EPD_RST_PIN, 1);
  61. DEV_Delay_ms(100);
  62. DEV_Digital_Write(EPD_RST_PIN, 0);
  63. DEV_Delay_ms(2);
  64. DEV_Digital_Write(EPD_RST_PIN, 1);
  65. DEV_Delay_ms(100);
  66. }
  67. /******************************************************************************
  68. function : send command
  69. parameter:
  70. Reg : Command register
  71. ******************************************************************************/
  72. static void EPD_4in26_SendCommand(UBYTE Reg)
  73. {
  74. DEV_Digital_Write(EPD_DC_PIN, 0);
  75. DEV_Digital_Write(EPD_CS_PIN, 0);
  76. DEV_SPI_WriteByte(Reg);
  77. DEV_Digital_Write(EPD_CS_PIN, 1);
  78. }
  79. /******************************************************************************
  80. function : send data
  81. parameter:
  82. Data : Write data
  83. ******************************************************************************/
  84. static void EPD_4in26_SendData(UBYTE Data)
  85. {
  86. DEV_Digital_Write(EPD_DC_PIN, 1);
  87. DEV_Digital_Write(EPD_CS_PIN, 0);
  88. DEV_SPI_WriteByte(Data);
  89. DEV_Digital_Write(EPD_CS_PIN, 1);
  90. }
  91. static void EPD_4in26_SendData2(UBYTE *pData, UDOUBLE len)
  92. {
  93. DEV_Digital_Write(EPD_DC_PIN, 1);
  94. DEV_Digital_Write(EPD_CS_PIN, 0);
  95. DEV_SPI_Write_nByte(pData, len);
  96. DEV_Digital_Write(EPD_CS_PIN, 1);
  97. }
  98. /******************************************************************************
  99. function : Wait until the busy_pin goes LOW
  100. parameter:
  101. ******************************************************************************/
  102. void EPD_4in26_ReadBusy(void)
  103. {
  104. Debug("e-Paper busy\r\n");
  105. while(1)
  106. { //=1 BUSY
  107. if(DEV_Digital_Read(EPD_BUSY_PIN)==0)
  108. break;
  109. DEV_Delay_ms(20);
  110. }
  111. DEV_Delay_ms(20);
  112. Debug("e-Paper busy release\r\n");
  113. }
  114. /******************************************************************************
  115. function : Turn On Display
  116. parameter:
  117. ******************************************************************************/
  118. static void EPD_4in26_TurnOnDisplay(void)
  119. {
  120. EPD_4in26_SendCommand(0x22); //Display Update Control
  121. EPD_4in26_SendData(0xF7);
  122. EPD_4in26_SendCommand(0x20); //Activate Display Update Sequence
  123. EPD_4in26_ReadBusy();
  124. }
  125. static void EPD_4in26_TurnOnDisplay_Fast(void)
  126. {
  127. EPD_4in26_SendCommand(0x22); //Display Update Control
  128. EPD_4in26_SendData(0xC7);
  129. EPD_4in26_SendCommand(0x20); //Activate Display Update Sequence
  130. EPD_4in26_ReadBusy();
  131. }
  132. static void EPD_4in26_TurnOnDisplay_Part(void)
  133. {
  134. EPD_4in26_SendCommand(0x22); //Display Update Control
  135. EPD_4in26_SendData(0xFF);
  136. EPD_4in26_SendCommand(0x20); //Activate Display Update Sequence
  137. EPD_4in26_ReadBusy();
  138. }
  139. static void EPD_4in26_TurnOnDisplay_4GRAY(void)
  140. {
  141. EPD_4in26_SendCommand(0x22);
  142. EPD_4in26_SendData(0xC7);
  143. EPD_4in26_SendCommand(0x20);
  144. EPD_4in26_ReadBusy();
  145. }
  146. /******************************************************************************
  147. function : set the look-up tables
  148. parameter:
  149. ******************************************************************************/
  150. static void EPD_4in26_Lut(void)
  151. {
  152. unsigned int count;
  153. EPD_4in26_SendCommand(0x32); //vcom
  154. for(count = 0; count < 105 ; count++) {
  155. EPD_4in26_SendData(LUT_DATA_4Gray[count]);
  156. }
  157. EPD_4in26_SendCommand(0x03); //VGH
  158. EPD_4in26_SendData(LUT_DATA_4Gray[105]);
  159. EPD_4in26_SendCommand(0x04); //
  160. EPD_4in26_SendData(LUT_DATA_4Gray[106]); //VSH1
  161. EPD_4in26_SendData(LUT_DATA_4Gray[107]); //VSH2
  162. EPD_4in26_SendData(LUT_DATA_4Gray[108]); //VSL
  163. EPD_4in26_SendCommand(0x2C); //VCOM Voltage
  164. EPD_4in26_SendData(LUT_DATA_4Gray[109]); //0x1C
  165. }
  166. /******************************************************************************
  167. function : Setting the display window
  168. parameter:
  169. ******************************************************************************/
  170. static void EPD_4in26_SetWindows(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
  171. {
  172. EPD_4in26_SendCommand(0x44); // SET_RAM_X_ADDRESS_START_END_POSITION
  173. EPD_4in26_SendData(Xstart & 0xFF);
  174. EPD_4in26_SendData((Xstart>>8) & 0x03);
  175. EPD_4in26_SendData(Xend & 0xFF);
  176. EPD_4in26_SendData((Xend>>8) & 0x03);
  177. EPD_4in26_SendCommand(0x45); // SET_RAM_Y_ADDRESS_START_END_POSITION
  178. EPD_4in26_SendData(Ystart & 0xFF);
  179. EPD_4in26_SendData((Ystart>>8) & 0x03);
  180. EPD_4in26_SendData(Yend & 0xFF);
  181. EPD_4in26_SendData((Yend>>8) & 0x03);
  182. }
  183. /******************************************************************************
  184. function : Set Cursor
  185. parameter:
  186. ******************************************************************************/
  187. static void EPD_4in26_SetCursor(UWORD Xstart, UWORD Ystart)
  188. {
  189. EPD_4in26_SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  190. EPD_4in26_SendData(Xstart & 0xFF);
  191. EPD_4in26_SendData((Xstart>>8) & 0x03);
  192. EPD_4in26_SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  193. EPD_4in26_SendData(Ystart & 0xFF);
  194. EPD_4in26_SendData((Ystart>>8) & 0x03);
  195. }
  196. /******************************************************************************
  197. function : Initialize the e-Paper register
  198. parameter:
  199. ******************************************************************************/
  200. void EPD_4in26_Init(void)
  201. {
  202. EPD_4in26_Reset();
  203. DEV_Delay_ms(100);
  204. EPD_4in26_ReadBusy();
  205. EPD_4in26_SendCommand(0x12); //SWRESET
  206. EPD_4in26_ReadBusy();
  207. EPD_4in26_SendCommand(0x18); // use the internal temperature sensor
  208. EPD_4in26_SendData(0x80);
  209. EPD_4in26_SendCommand(0x0C); //set soft start
  210. EPD_4in26_SendData(0xAE);
  211. EPD_4in26_SendData(0xC7);
  212. EPD_4in26_SendData(0xC3);
  213. EPD_4in26_SendData(0xC0);
  214. EPD_4in26_SendData(0x80);
  215. EPD_4in26_SendCommand(0x01); // drive output control
  216. EPD_4in26_SendData((EPD_4in26_HEIGHT-1)%256); // Y
  217. EPD_4in26_SendData((EPD_4in26_HEIGHT-1)/256); // Y
  218. EPD_4in26_SendData(0x02);
  219. EPD_4in26_SendCommand(0x3C); // Border Border setting
  220. EPD_4in26_SendData(0x01);
  221. EPD_4in26_SendCommand(0x11); // data entry mode
  222. EPD_4in26_SendData(0x01); // X-mode x+ y-
  223. EPD_4in26_SetWindows(0, EPD_4in26_HEIGHT-1, EPD_4in26_WIDTH-1, 0);
  224. EPD_4in26_SetCursor(0, 0);
  225. EPD_4in26_ReadBusy();
  226. }
  227. void EPD_4in26_Init_Fast(void)
  228. {
  229. EPD_4in26_Reset();
  230. DEV_Delay_ms(100);
  231. EPD_4in26_ReadBusy();
  232. EPD_4in26_SendCommand(0x12); //SWRESET
  233. EPD_4in26_ReadBusy();
  234. EPD_4in26_SendCommand(0x18); // use the internal temperature sensor
  235. EPD_4in26_SendData(0x80);
  236. EPD_4in26_SendCommand(0x0C); //set soft start
  237. EPD_4in26_SendData(0xAE);
  238. EPD_4in26_SendData(0xC7);
  239. EPD_4in26_SendData(0xC3);
  240. EPD_4in26_SendData(0xC0);
  241. EPD_4in26_SendData(0x80);
  242. EPD_4in26_SendCommand(0x01); // drive output control
  243. EPD_4in26_SendData((EPD_4in26_HEIGHT-1)%256); // Y
  244. EPD_4in26_SendData((EPD_4in26_HEIGHT-1)/256); // Y
  245. EPD_4in26_SendData(0x02);
  246. EPD_4in26_SendCommand(0x3C); // Border Border setting
  247. EPD_4in26_SendData(0x01);
  248. EPD_4in26_SendCommand(0x11); // data entry mode
  249. EPD_4in26_SendData(0x01); // X-mode x+ y-
  250. EPD_4in26_SetWindows(0, EPD_4in26_HEIGHT-1, EPD_4in26_WIDTH-1, 0);
  251. EPD_4in26_SetCursor(0, 0);
  252. EPD_4in26_ReadBusy();
  253. //TEMP (1.5s)
  254. EPD_4in26_SendCommand(0x1A);
  255. EPD_4in26_SendData(0x5A);
  256. EPD_4in26_SendCommand(0x22);
  257. EPD_4in26_SendData(0x91);
  258. EPD_4in26_SendCommand(0x20);
  259. EPD_4in26_ReadBusy();
  260. }
  261. void EPD_4in26_Init_4GRAY(void)
  262. {
  263. EPD_4in26_Reset();
  264. DEV_Delay_ms(100);
  265. EPD_4in26_ReadBusy();
  266. EPD_4in26_SendCommand(0x12); //SWRESET
  267. EPD_4in26_ReadBusy();
  268. EPD_4in26_SendCommand(0x18); // use the internal temperature sensor
  269. EPD_4in26_SendData(0x80);
  270. EPD_4in26_SendCommand(0x0C); //set soft start
  271. EPD_4in26_SendData(0xAE);
  272. EPD_4in26_SendData(0xC7);
  273. EPD_4in26_SendData(0xC3);
  274. EPD_4in26_SendData(0xC0);
  275. EPD_4in26_SendData(0x80);
  276. EPD_4in26_SendCommand(0x01); // drive output control
  277. EPD_4in26_SendData((EPD_4in26_WIDTH-1)%256); // Y
  278. EPD_4in26_SendData((EPD_4in26_WIDTH-1)/256); // Y
  279. EPD_4in26_SendData(0x02);
  280. EPD_4in26_SendCommand(0x3C); // Border Border setting
  281. EPD_4in26_SendData(0x01);
  282. EPD_4in26_SendCommand(0x11); // data entry mode
  283. EPD_4in26_SendData(0x01); // X-mode x+ y-
  284. EPD_4in26_SetWindows(0, EPD_4in26_HEIGHT-1, EPD_4in26_WIDTH-1, 0);
  285. EPD_4in26_SetCursor(0, 0);
  286. EPD_4in26_ReadBusy();
  287. EPD_4in26_Lut();
  288. }
  289. /******************************************************************************
  290. function : Clear screen
  291. parameter:
  292. ******************************************************************************/
  293. void EPD_4in26_Clear(void)
  294. {
  295. UWORD i;
  296. UWORD height = EPD_4in26_HEIGHT;
  297. UWORD width = EPD_4in26_WIDTH/8;
  298. UBYTE image[EPD_4in26_WIDTH / 8] = {0x00};
  299. for(i=0; i<width; i++) {
  300. image[i] = 0xff;
  301. }
  302. EPD_4in26_SendCommand(0x24); //write RAM for black(0)/white (1)
  303. for(i=0; i<height; i++)
  304. {
  305. EPD_4in26_SendData2(image, width);
  306. }
  307. EPD_4in26_SendCommand(0x26); //write RAM for black(0)/white (1)
  308. for(i=0; i<height; i++)
  309. {
  310. EPD_4in26_SendData2(image, width);
  311. }
  312. EPD_4in26_TurnOnDisplay();
  313. }
  314. /******************************************************************************
  315. function : Sends the image buffer in RAM to e-Paper and displays
  316. parameter:
  317. ******************************************************************************/
  318. void EPD_4in26_Display(UBYTE *Image)
  319. {
  320. UWORD i;
  321. UWORD height = EPD_4in26_HEIGHT;
  322. UWORD width = EPD_4in26_WIDTH/8;
  323. EPD_4in26_SendCommand(0x24); //write RAM for black(0)/white (1)
  324. for(i=0; i<height; i++)
  325. {
  326. EPD_4in26_SendData2((UBYTE *)(Image+i*width), width);
  327. }
  328. EPD_4in26_TurnOnDisplay();
  329. }
  330. void EPD_4in26_Display_Base(UBYTE *Image)
  331. {
  332. UWORD i;
  333. UWORD height = EPD_4in26_HEIGHT;
  334. UWORD width = EPD_4in26_WIDTH/8;
  335. EPD_4in26_SendCommand(0x24); //write RAM for black(0)/white (1)
  336. for(i=0; i<height; i++)
  337. {
  338. EPD_4in26_SendData2((UBYTE *)(Image+i*width), width);
  339. }
  340. EPD_4in26_SendCommand(0x26); //write RAM for black(0)/white (1)
  341. for(i=0; i<height; i++)
  342. {
  343. EPD_4in26_SendData2((UBYTE *)(Image+i*width), width);
  344. }
  345. EPD_4in26_TurnOnDisplay();
  346. }
  347. void EPD_4in26_Display_Fast(UBYTE *Image)
  348. {
  349. UWORD i;
  350. UWORD height = EPD_4in26_HEIGHT;
  351. UWORD width = EPD_4in26_WIDTH/8;
  352. EPD_4in26_SendCommand(0x24); //write RAM for black(0)/white (1)
  353. for(i=0; i<height; i++)
  354. {
  355. EPD_4in26_SendData2((UBYTE *)(Image+i*width), width);
  356. }
  357. EPD_4in26_TurnOnDisplay_Fast();
  358. }
  359. void EPD_4in26_Display_Part(UBYTE *Image, UWORD x, UWORD y, UWORD w, UWORD l)
  360. {
  361. UWORD i;
  362. UWORD height = l;
  363. UWORD width = (w % 8 == 0)? (w / 8 ): (w / 8 + 1);
  364. EPD_4in26_Reset();
  365. EPD_4in26_SendCommand(0x18); // use the internal temperature sensor
  366. EPD_4in26_SendData(0x80);
  367. EPD_4in26_SendCommand(0x3C); // Border Border setting
  368. EPD_4in26_SendData(0x80);
  369. EPD_4in26_SetWindows(x, y, x+w-1, y+l-1);
  370. EPD_4in26_SetCursor(x, y);
  371. EPD_4in26_SendCommand(0x24); //write RAM for black(0)/white (1)
  372. for(i=0; i<height; i++)
  373. {
  374. EPD_4in26_SendData2((UBYTE *)(Image+i*width), width);
  375. }
  376. EPD_4in26_TurnOnDisplay_Part();
  377. }
  378. void EPD_4in26_4GrayDisplay(UBYTE *Image)
  379. {
  380. UDOUBLE i,j,k;
  381. UBYTE temp1,temp2,temp3;
  382. // old data
  383. EPD_4in26_SendCommand(0x24);
  384. for(i=0; i<48000; i++) { //5808*4 46464
  385. temp3=0;
  386. for(j=0; j<2; j++) {
  387. temp1 = Image[i*2+j];
  388. for(k=0; k<2; k++) {
  389. temp2 = temp1&0xC0;
  390. if(temp2 == 0xC0)
  391. temp3 |= 0x00;
  392. else if(temp2 == 0x00)
  393. temp3 |= 0x01;
  394. else if(temp2 == 0x80)
  395. temp3 |= 0x01;
  396. else //0x40
  397. temp3 |= 0x00;
  398. temp3 <<= 1;
  399. temp1 <<= 2;
  400. temp2 = temp1&0xC0 ;
  401. if(temp2 == 0xC0)
  402. temp3 |= 0x00;
  403. else if(temp2 == 0x00)
  404. temp3 |= 0x01;
  405. else if(temp2 == 0x80)
  406. temp3 |= 0x01;
  407. else //0x40
  408. temp3 |= 0x00;
  409. if(j!=1 || k!=1)
  410. temp3 <<= 1;
  411. temp1 <<= 2;
  412. }
  413. }
  414. EPD_4in26_SendData(temp3);
  415. // printf("%x",temp3);
  416. }
  417. EPD_4in26_SendCommand(0x26); //write RAM for black(0)/white (1)
  418. for(i=0; i<48000; i++) { //5808*4 46464
  419. temp3=0;
  420. for(j=0; j<2; j++) {
  421. temp1 = Image[i*2+j];
  422. for(k=0; k<2; k++) {
  423. temp2 = temp1&0xC0 ;
  424. if(temp2 == 0xC0)
  425. temp3 |= 0x00;//white
  426. else if(temp2 == 0x00)
  427. temp3 |= 0x01; //black
  428. else if(temp2 == 0x80)
  429. temp3 |= 0x00; //gray1
  430. else //0x40
  431. temp3 |= 0x01; //gray2
  432. temp3 <<= 1;
  433. temp1 <<= 2;
  434. temp2 = temp1&0xC0 ;
  435. if(temp2 == 0xC0) //white
  436. temp3 |= 0x00;
  437. else if(temp2 == 0x00) //black
  438. temp3 |= 0x01;
  439. else if(temp2 == 0x80)
  440. temp3 |= 0x00; //gray1
  441. else //0x40
  442. temp3 |= 0x01; //gray2
  443. if(j!=1 || k!=1)
  444. temp3 <<= 1;
  445. temp1 <<= 2;
  446. }
  447. }
  448. EPD_4in26_SendData(temp3);
  449. // printf("%x",temp3);
  450. }
  451. EPD_4in26_TurnOnDisplay_4GRAY();
  452. }
  453. /******************************************************************************
  454. function : Enter sleep mode
  455. parameter:
  456. ******************************************************************************/
  457. void EPD_4in26_Sleep(void)
  458. {
  459. EPD_4in26_SendCommand(0x10); //enter deep sleep
  460. EPD_4in26_SendData(0x01);
  461. DEV_Delay_ms(100);
  462. }