EPD_13in3b.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*****************************************************************************
  2. * | File : EPD_13in3b.c
  3. * | Author : Waveshare team
  4. * | Function : 13.3inch e-paper (B)
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2024-04-08
  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_13in3b.h"
  32. #include "Debug.h"
  33. /******************************************************************************
  34. function : Software reset
  35. parameter:
  36. ******************************************************************************/
  37. static void EPD_13IN3B_Reset(void)
  38. {
  39. DEV_Digital_Write(EPD_RST_PIN, 1);
  40. DEV_Delay_ms(100);
  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(100);
  45. }
  46. /******************************************************************************
  47. function : send command
  48. parameter:
  49. Reg : Command register
  50. ******************************************************************************/
  51. static void EPD_13IN3B_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_13IN3B_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. /******************************************************************************
  71. function : Wait until the busy_pin goes LOW
  72. parameter:
  73. ******************************************************************************/
  74. void EPD_13IN3B_ReadBusy(void)
  75. {
  76. Debug("e-Paper busy\r\n");
  77. while(1)
  78. { //=1 BUSY
  79. if(DEV_Digital_Read(EPD_BUSY_PIN)==0)
  80. break;
  81. DEV_Delay_ms(20);
  82. }
  83. DEV_Delay_ms(20);
  84. Debug("e-Paper busy release\r\n");
  85. }
  86. /******************************************************************************
  87. function : Turn On Display
  88. parameter:
  89. ******************************************************************************/
  90. static void EPD_13IN3B_TurnOnDisplay(void)
  91. {
  92. EPD_13IN3B_SendCommand(0x22); //Display Update Control
  93. EPD_13IN3B_SendData(0xF7);
  94. EPD_13IN3B_SendCommand(0x20); //Activate Display Update Sequence
  95. EPD_13IN3B_ReadBusy();
  96. }
  97. static void EPD_13IN3B_TurnOnDisplay_Part(void)
  98. {
  99. EPD_13IN3B_SendCommand(0x22); //Display Update Control
  100. EPD_13IN3B_SendData(0xFF);
  101. EPD_13IN3B_SendCommand(0x20); //Activate Display Update Sequence
  102. EPD_13IN3B_ReadBusy();
  103. }
  104. /******************************************************************************
  105. function : Setting the display window
  106. parameter:
  107. ******************************************************************************/
  108. static void EPD_13IN3B_SetWindows(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
  109. {
  110. EPD_13IN3B_SendCommand(0x44); // SET_RAM_X_ADDRESS_START_END_POSITION
  111. EPD_13IN3B_SendData(Xstart & 0xFF);
  112. EPD_13IN3B_SendData((Xstart>>8) & 0x03);
  113. EPD_13IN3B_SendData(Xend & 0xFF);
  114. EPD_13IN3B_SendData((Xend>>8) & 0x03);
  115. EPD_13IN3B_SendCommand(0x45); // SET_RAM_Y_ADDRESS_START_END_POSITION
  116. EPD_13IN3B_SendData(Ystart & 0xFF);
  117. EPD_13IN3B_SendData((Ystart>>8) & 0x03);
  118. EPD_13IN3B_SendData(Yend & 0xFF);
  119. EPD_13IN3B_SendData((Yend>>8) & 0x03);
  120. }
  121. /******************************************************************************
  122. function : Set Cursor
  123. parameter:
  124. ******************************************************************************/
  125. static void EPD_13IN3B_SetCursor(UWORD Xstart, UWORD Ystart)
  126. {
  127. EPD_13IN3B_SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  128. EPD_13IN3B_SendData(Xstart & 0xFF);
  129. EPD_13IN3B_SendData((Xstart>>8) & 0x03);
  130. EPD_13IN3B_SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  131. EPD_13IN3B_SendData(Ystart & 0xFF);
  132. EPD_13IN3B_SendData((Ystart>>8) & 0x03);
  133. }
  134. /******************************************************************************
  135. function : Initialize the e-Paper register
  136. parameter:
  137. ******************************************************************************/
  138. void EPD_13IN3B_Init(void)
  139. {
  140. EPD_13IN3B_Reset();
  141. DEV_Delay_ms(100);
  142. EPD_13IN3B_ReadBusy();
  143. EPD_13IN3B_SendCommand(0x12);
  144. EPD_13IN3B_ReadBusy();
  145. EPD_13IN3B_SendCommand(0x0C);
  146. EPD_13IN3B_SendData(0xAE);
  147. EPD_13IN3B_SendData(0xC7);
  148. EPD_13IN3B_SendData(0xC3);
  149. EPD_13IN3B_SendData(0xC0);
  150. EPD_13IN3B_SendData(0x80);
  151. EPD_13IN3B_SendCommand(0x01);
  152. EPD_13IN3B_SendData((EPD_13IN3B_HEIGHT-1)%256);
  153. EPD_13IN3B_SendData((EPD_13IN3B_HEIGHT-1)/256);
  154. EPD_13IN3B_SendData(0x00);
  155. EPD_13IN3B_SendCommand(0x11);
  156. EPD_13IN3B_SendData(0x03);
  157. EPD_13IN3B_SetWindows(0, 0, EPD_13IN3B_WIDTH-1, EPD_13IN3B_HEIGHT-1);
  158. EPD_13IN3B_SendCommand(0x3C);
  159. EPD_13IN3B_SendData(0x01);
  160. EPD_13IN3B_SendCommand(0x18);
  161. EPD_13IN3B_SendData(0x80);
  162. // EPD_13IN3B_SendCommand(0x22);
  163. // EPD_13IN3B_SendData(0xB1);
  164. // EPD_13IN3B_SendCommand(0x20);
  165. EPD_13IN3B_SetCursor(0, 0);
  166. EPD_13IN3B_ReadBusy();
  167. }
  168. /******************************************************************************
  169. function : Clear screen
  170. parameter:
  171. ******************************************************************************/
  172. void EPD_13IN3B_Clear(void)
  173. {
  174. UWORD i, j;
  175. UWORD height = EPD_13IN3B_HEIGHT;
  176. UWORD width = EPD_13IN3B_WIDTH/8;
  177. EPD_13IN3B_SendCommand(0x24);
  178. for(i=0; i<height; i++)
  179. {
  180. for(j=0; j<width; j++)
  181. EPD_13IN3B_SendData(0xff);
  182. }
  183. EPD_13IN3B_SendCommand(0x26);
  184. for (UWORD j = 0; j < height; j++) {
  185. for (UWORD i = 0; i < width; i++) {
  186. EPD_13IN3B_SendData(0x00);
  187. }
  188. }
  189. EPD_13IN3B_TurnOnDisplay();
  190. }
  191. void EPD_13IN3B_Clear_Black(void)
  192. {
  193. UWORD Width, Height;
  194. Width = (EPD_13IN3B_WIDTH % 8 == 0)? (EPD_13IN3B_WIDTH / 8 ): (EPD_13IN3B_WIDTH / 8 + 1);
  195. Height = EPD_13IN3B_HEIGHT;
  196. EPD_13IN3B_SendCommand(0x24);
  197. for (UWORD j = 0; j < Height; j++) {
  198. for (UWORD i = 0; i < Width; i++) {
  199. EPD_13IN3B_SendData(0x00);
  200. }
  201. }
  202. EPD_13IN3B_SendCommand(0x26);
  203. for (UWORD j = 0; j < Height; j++) {
  204. for (UWORD i = 0; i < Width; i++) {
  205. EPD_13IN3B_SendData(0x00);
  206. }
  207. }
  208. EPD_13IN3B_TurnOnDisplay();
  209. }
  210. void EPD_13IN3B_Clear_Red(void)
  211. {
  212. UWORD Width, Height;
  213. Width = (EPD_13IN3B_WIDTH % 8 == 0)? (EPD_13IN3B_WIDTH / 8 ): (EPD_13IN3B_WIDTH / 8 + 1);
  214. Height = EPD_13IN3B_HEIGHT;
  215. EPD_13IN3B_SendCommand(0x24);
  216. for (UWORD j = 0; j < Height; j++) {
  217. for (UWORD i = 0; i < Width; i++) {
  218. EPD_13IN3B_SendData(0xFF);
  219. }
  220. }
  221. EPD_13IN3B_SendCommand(0x26);
  222. for (UWORD j = 0; j < Height; j++) {
  223. for (UWORD i = 0; i < Width; i++) {
  224. EPD_13IN3B_SendData(0xFF);
  225. }
  226. }
  227. EPD_13IN3B_TurnOnDisplay();
  228. }
  229. void EPD_13IN3B_Clear_Base(void)
  230. {
  231. UWORD i, j;
  232. UWORD height = EPD_13IN3B_HEIGHT;
  233. UWORD width = EPD_13IN3B_WIDTH/8;
  234. EPD_13IN3B_SendCommand(0x24);
  235. for(i=0; i<height; i++)
  236. {
  237. for(j=0; j<width; j++)
  238. EPD_13IN3B_SendData(0xff);
  239. }
  240. EPD_13IN3B_SendCommand(0x26);
  241. for (UWORD j = 0; j < height; j++) {
  242. for (UWORD i = 0; i < width; i++) {
  243. EPD_13IN3B_SendData(0x00);
  244. }
  245. }
  246. EPD_13IN3B_TurnOnDisplay();
  247. EPD_13IN3B_SendCommand(0x26);
  248. for(i=0; i<height; i++)
  249. {
  250. for(j=0; j<width; j++)
  251. EPD_13IN3B_SendData(0xff);
  252. }
  253. }
  254. /******************************************************************************
  255. function : Sends the image buffer in RAM to e-Paper and displays
  256. parameter:
  257. ******************************************************************************/
  258. void EPD_13IN3B_Display(const UBYTE *blackimage, const UBYTE *ryimage)
  259. {
  260. UWORD Width, Height;
  261. Width = (EPD_13IN3B_WIDTH % 8 == 0)? (EPD_13IN3B_WIDTH / 8 ): (EPD_13IN3B_WIDTH / 8 + 1);
  262. Height = EPD_13IN3B_HEIGHT;
  263. EPD_13IN3B_SendCommand(0x24);
  264. for (UWORD j = 0; j < Height; j++) {
  265. for (UWORD i = 0; i < Width; i++) {
  266. EPD_13IN3B_SendData(blackimage[i + j * Width]);
  267. }
  268. }
  269. EPD_13IN3B_SendCommand(0x26);
  270. for (UWORD j = 0; j < Height; j++) {
  271. for (UWORD i = 0; i < Width; i++) {
  272. EPD_13IN3B_SendData(~ryimage[i + j * Width]);
  273. }
  274. }
  275. EPD_13IN3B_TurnOnDisplay();
  276. }
  277. //Partial refresh of background display, this function is necessary, please do not delete it!!!
  278. void EPD_13IN3B_Display_Base(const UBYTE *blackimage, const UBYTE *ryimage)
  279. {
  280. UWORD Width, Height;
  281. Width = (EPD_13IN3B_WIDTH % 8 == 0)? (EPD_13IN3B_WIDTH / 8 ): (EPD_13IN3B_WIDTH / 8 + 1);
  282. Height = EPD_13IN3B_HEIGHT;
  283. EPD_13IN3B_SendCommand(0x24);
  284. for (UWORD j = 0; j < Height; j++) {
  285. for (UWORD i = 0; i < Width; i++) {
  286. EPD_13IN3B_SendData(blackimage[i + j * Width]);
  287. }
  288. }
  289. EPD_13IN3B_SendCommand(0x26);
  290. for (UWORD j = 0; j < Height; j++) {
  291. for (UWORD i = 0; i < Width; i++) {
  292. EPD_13IN3B_SendData(~ryimage[i + j * Width]);
  293. }
  294. }
  295. EPD_13IN3B_TurnOnDisplay();
  296. EPD_13IN3B_SendCommand(0x26);
  297. for (UWORD j = 0; j < Height; j++) {
  298. for (UWORD i = 0; i < Width; i++) {
  299. EPD_13IN3B_SendData(blackimage[i + j * Width]);
  300. }
  301. }
  302. }
  303. void EPD_13IN3B_Display_WritePicture(const UBYTE *image, UBYTE Block)
  304. {
  305. UWORD Width, Height;
  306. Width = (EPD_13IN3B_WIDTH % 8 == 0)? (EPD_13IN3B_WIDTH / 8 ): (EPD_13IN3B_WIDTH / 8 + 1);
  307. Height = EPD_13IN3B_HEIGHT;
  308. if(Block == 0)
  309. {
  310. EPD_13IN3B_SendCommand(0x24);
  311. for (UWORD j = 0; j < Height/2; j++) {
  312. for (UWORD i = 0; i < Width; i++) {
  313. EPD_13IN3B_SendData(image[i + j * Width]);
  314. }
  315. }
  316. }
  317. else if(Block == 1)
  318. {
  319. for (UWORD j = 0; j < Height/2; j++) {
  320. for (UWORD i = 0; i < Width; i++) {
  321. EPD_13IN3B_SendData(image[i + j * Width]);
  322. }
  323. }
  324. }
  325. if(Block == 2)
  326. {
  327. EPD_13IN3B_SendCommand(0x26);
  328. for (UWORD j = 0; j < Height/2; j++) {
  329. for (UWORD i = 0; i < Width; i++) {
  330. EPD_13IN3B_SendData(~image[i + j * Width]);
  331. }
  332. }
  333. }
  334. else if(Block == 3)
  335. {
  336. for (UWORD j = 0; j < Height/2; j++) {
  337. for (UWORD i = 0; i < Width; i++) {
  338. EPD_13IN3B_SendData(~image[i + j * Width]);
  339. }
  340. }
  341. EPD_13IN3B_TurnOnDisplay();
  342. }
  343. }
  344. //Partial refresh display
  345. void EPD_13IN3B_Display_Partial(const UBYTE *Image, UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
  346. {
  347. if((Xstart % 8 + Xend % 8 == 8 && Xstart % 8 > Xend % 8) || Xstart % 8 + Xend % 8 == 0 || (Xend - Xstart)%8 == 0)
  348. {
  349. Xstart = Xstart / 8 ;
  350. Xend = Xend / 8;
  351. }
  352. else
  353. {
  354. Xstart = Xstart / 8 ;
  355. Xend = Xend % 8 == 0 ? Xend / 8 : Xend / 8 + 1;
  356. }
  357. UDOUBLE i, Width;
  358. Width = Xend - Xstart;
  359. UDOUBLE IMAGE_COUNTER = Width * (Yend-Ystart);
  360. printf("IMAGE_COUNTER = %d\r\n", IMAGE_COUNTER);
  361. Xstart = Xstart * 8;
  362. Xend =Xend * 8 - 1;
  363. Yend -= 1;
  364. EPD_13IN3B_SendCommand(0x3C);
  365. EPD_13IN3B_SendData(0x80);
  366. EPD_13IN3B_SendCommand(0x44);
  367. EPD_13IN3B_SendData(Xstart & 0xFF);
  368. EPD_13IN3B_SendData((Xstart>>8) & 0x03);
  369. EPD_13IN3B_SendData(Xend & 0xFF);
  370. EPD_13IN3B_SendData((Xend>>8) & 0x03);
  371. EPD_13IN3B_SendCommand(0x45);
  372. EPD_13IN3B_SendData(Ystart & 0xff);
  373. EPD_13IN3B_SendData((Ystart>>8) & 0x01);
  374. EPD_13IN3B_SendData(Yend & 0xff);
  375. EPD_13IN3B_SendData((Yend>>8) & 0x03);
  376. EPD_13IN3B_SendCommand(0x4E);
  377. EPD_13IN3B_SendData(Xstart & 0xFF);
  378. EPD_13IN3B_SendData((Xstart>>8) & 0x03);
  379. EPD_13IN3B_SendCommand(0x4F);
  380. EPD_13IN3B_SendData(Ystart & 0xff);
  381. EPD_13IN3B_SendData((Ystart>>8) & 0x03);
  382. EPD_13IN3B_SendCommand(0x24);
  383. for (i = 0; i < IMAGE_COUNTER; i++) {
  384. EPD_13IN3B_SendData(Image[i]);
  385. }
  386. EPD_13IN3B_TurnOnDisplay_Part();
  387. EPD_13IN3B_SendCommand(0x26);
  388. for (i = 0; i < IMAGE_COUNTER; i++) {
  389. EPD_13IN3B_SendData(Image[i]);
  390. }
  391. }
  392. /******************************************************************************
  393. function : Enter sleep mode
  394. parameter:
  395. ******************************************************************************/
  396. void EPD_13IN3B_Sleep(void)
  397. {
  398. EPD_13IN3B_SendCommand(0x10);
  399. EPD_13IN3B_SendData(0x03);
  400. DEV_Delay_ms(100);
  401. }