EPD_13in3e.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*****************************************************************************
  2. * | File : EPD_12in48.c
  3. * | Author : Waveshare team
  4. * | Function : Electronic paper driver
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2018-11-29
  9. * | Info :
  10. #
  11. # Permission is hereby granted, free of charge, to any person obtaining a copy
  12. # of this software and associated documnetation files(the "Software"), to deal
  13. # in the Software without restriction, including without limitation the rights
  14. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. # copies of the Software, and to permit persons to whom the Software is
  16. # furished to do so, subject to the following conditions:
  17. #
  18. # The above copyright notice and this permission notice shall be included in
  19. # all copies or substantial portions of the Software.
  20. #
  21. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. # THE SOFTWARE.
  28. #
  29. ******************************************************************************/
  30. #include "EPD_13in3e.h"
  31. #include "Debug.h"
  32. // const UBYTE spiCsPin[2] = {
  33. // SPI_CS0, SPI_CS1
  34. // };
  35. const UBYTE PSR_V[2] = {
  36. 0xDF, 0x69
  37. };
  38. const UBYTE PWR_V[6] = {
  39. 0x0F, 0x00, 0x28, 0x2C, 0x28, 0x38
  40. };
  41. const UBYTE POF_V[1] = {
  42. 0x00
  43. };
  44. const UBYTE DRF_V[1] = {
  45. 0x00
  46. };
  47. const UBYTE CDI_V[1] = {
  48. 0xF7
  49. };
  50. const UBYTE TCON_V[2] = {
  51. 0x03, 0x03
  52. };
  53. const UBYTE TRES_V[4] = {
  54. 0x04, 0xB0, 0x03, 0x20
  55. };
  56. const UBYTE CMD66_V[6] = {
  57. 0x49, 0x55, 0x13, 0x5D, 0x05, 0x10
  58. };
  59. const UBYTE EN_BUF_V[1] = {
  60. 0x07
  61. };
  62. const UBYTE CCSET_V[1] = {
  63. 0x01
  64. };
  65. const UBYTE PWS_V[1] = {
  66. 0x22
  67. };
  68. const UBYTE AN_TM_V[9] = {
  69. 0xC0, 0x1C, 0x1C, 0xCC, 0xCC, 0xCC, 0x15, 0x15, 0x55
  70. };
  71. const UBYTE AGID_V[1] = {
  72. 0x10
  73. };
  74. const UBYTE BTST_P_V[2] = {
  75. 0xE8, 0x28
  76. };
  77. const UBYTE BOOST_VDDP_EN_V[1] = {
  78. 0x01
  79. };
  80. const UBYTE BTST_N_V[2] = {
  81. 0xE8, 0x28
  82. };
  83. const UBYTE BUCK_BOOST_VDDN_V[1] = {
  84. 0x01
  85. };
  86. const UBYTE TFT_VCOM_POWER_V[1] = {
  87. 0x02
  88. };
  89. static void EPD_13IN3E_CS_ALL(UBYTE Value)
  90. {
  91. DEV_Digital_Write(EPD_CS_M_PIN, Value);
  92. DEV_Digital_Write(EPD_CS_S_PIN, Value);
  93. }
  94. static void EPD_13IN3E_SPI_Sand(UBYTE Cmd, const UBYTE *buf, UDOUBLE Len)
  95. {
  96. DEV_SPI_SendByte(Cmd);
  97. DEV_SPI_SendnByte((UBYTE *)buf,Len);
  98. }
  99. /******************************************************************************
  100. function : Software reset
  101. parameter:
  102. ******************************************************************************/
  103. static void EPD_13IN3E_Reset(void)
  104. {
  105. DEV_Digital_Write(EPD_RST_PIN, 1);
  106. DEV_Delay_ms(30);
  107. DEV_Digital_Write(EPD_RST_PIN, 0);
  108. DEV_Delay_ms(30);
  109. DEV_Digital_Write(EPD_RST_PIN, 1);
  110. DEV_Delay_ms(30);
  111. DEV_Digital_Write(EPD_RST_PIN, 0);
  112. DEV_Delay_ms(30);
  113. DEV_Digital_Write(EPD_RST_PIN, 1);
  114. DEV_Delay_ms(30);
  115. }
  116. /******************************************************************************
  117. function : send command
  118. parameter:
  119. Reg : Command register
  120. ******************************************************************************/
  121. static void EPD_13IN3E_SendCommand(UBYTE Reg)
  122. {
  123. DEV_SPI_SendByte(Reg);
  124. }
  125. /******************************************************************************
  126. function : send data
  127. parameter:
  128. Data : Write data
  129. ******************************************************************************/
  130. static void EPD_13IN3E_SendData(UBYTE Reg)
  131. {
  132. DEV_SPI_SendByte(Reg);
  133. }
  134. static void EPD_13IN3E_SendData2(const UBYTE *buf, uint32_t Len)
  135. {
  136. DEV_SPI_SendnByte((UBYTE *)buf,Len);
  137. }
  138. /******************************************************************************
  139. function : Wait until the busy_pin goes LOW
  140. parameter:
  141. ******************************************************************************/
  142. static void EPD_13IN3E_ReadBusyH(void)
  143. {
  144. Debug("e-Paper busy\r\n");
  145. while(!DEV_Digital_Read(EPD_BUSY_PIN)) { //LOW: busy, HIGH: idle
  146. DEV_Delay_ms(10);
  147. // Debug("e-Paper busy release\r\n");
  148. }
  149. DEV_Delay_ms(20);
  150. Debug("e-Paper busy release\r\n");
  151. }
  152. /******************************************************************************
  153. function : Turn On Display
  154. parameter:
  155. ******************************************************************************/
  156. static void EPD_13IN3E_TurnOnDisplay(void)
  157. {
  158. printf("Write PON \r\n");
  159. EPD_13IN3E_CS_ALL(0);
  160. EPD_13IN3E_SendCommand(0x04); // POWER_ON
  161. EPD_13IN3E_CS_ALL(1);
  162. EPD_13IN3E_ReadBusyH();
  163. printf("Write DRF \r\n");
  164. DEV_Delay_ms(50);
  165. EPD_13IN3E_CS_ALL(0);
  166. EPD_13IN3E_SPI_Sand(DRF, DRF_V, sizeof(DRF_V));
  167. EPD_13IN3E_CS_ALL(1);
  168. EPD_13IN3E_ReadBusyH();
  169. printf("Write POF \r\n");
  170. EPD_13IN3E_CS_ALL(0);
  171. EPD_13IN3E_SPI_Sand(POF, POF_V, sizeof(POF_V));
  172. EPD_13IN3E_CS_ALL(1);
  173. // EPD_13IN3E_ReadBusyH();
  174. printf("Display Done!! \r\n");
  175. }
  176. /******************************************************************************
  177. function : Initialize the e-Paper register
  178. parameter:
  179. ******************************************************************************/
  180. void EPD_13IN3E_Init(void)
  181. {
  182. EPD_13IN3E_Reset();
  183. // EPD_13IN3E_ReadBusyH();
  184. DEV_Digital_Write(EPD_CS_M_PIN, 0);
  185. EPD_13IN3E_SPI_Sand(AN_TM, AN_TM_V, sizeof(AN_TM_V));
  186. EPD_13IN3E_CS_ALL(1);
  187. EPD_13IN3E_CS_ALL(0);
  188. EPD_13IN3E_SPI_Sand(CMD66, CMD66_V, sizeof(CMD66_V));
  189. EPD_13IN3E_CS_ALL(1);
  190. EPD_13IN3E_CS_ALL(0);
  191. EPD_13IN3E_SPI_Sand(PSR, PSR_V, sizeof(PSR_V));
  192. EPD_13IN3E_CS_ALL(1);
  193. EPD_13IN3E_CS_ALL(0);
  194. EPD_13IN3E_SPI_Sand(CDI, CDI_V, sizeof(CDI_V));
  195. EPD_13IN3E_CS_ALL(1);
  196. EPD_13IN3E_CS_ALL(0);
  197. EPD_13IN3E_SPI_Sand(TCON, TCON_V, sizeof(TCON_V));
  198. EPD_13IN3E_CS_ALL(1);
  199. EPD_13IN3E_CS_ALL(0);
  200. EPD_13IN3E_SPI_Sand(AGID, AGID_V, sizeof(AGID_V));
  201. EPD_13IN3E_CS_ALL(1);
  202. EPD_13IN3E_CS_ALL(0);
  203. EPD_13IN3E_SPI_Sand(PWS, PWS_V, sizeof(PWS_V));
  204. EPD_13IN3E_CS_ALL(1);
  205. EPD_13IN3E_CS_ALL(0);
  206. EPD_13IN3E_SPI_Sand(CCSET, CCSET_V, sizeof(CCSET_V));
  207. EPD_13IN3E_CS_ALL(1);
  208. EPD_13IN3E_CS_ALL(0);
  209. EPD_13IN3E_SPI_Sand(TRES, TRES_V, sizeof(TRES_V));
  210. EPD_13IN3E_CS_ALL(1);
  211. DEV_Digital_Write(EPD_CS_M_PIN, 0);
  212. EPD_13IN3E_SPI_Sand(PWR_epd, PWR_V, sizeof(PWR_V));
  213. EPD_13IN3E_CS_ALL(1);
  214. DEV_Digital_Write(EPD_CS_M_PIN, 0);
  215. EPD_13IN3E_SPI_Sand(EN_BUF, EN_BUF_V, sizeof(EN_BUF_V));
  216. EPD_13IN3E_CS_ALL(1);
  217. DEV_Digital_Write(EPD_CS_M_PIN, 0);
  218. EPD_13IN3E_SPI_Sand(BTST_P, BTST_P_V, sizeof(BTST_P_V));
  219. EPD_13IN3E_CS_ALL(1);
  220. DEV_Digital_Write(EPD_CS_M_PIN, 0);
  221. EPD_13IN3E_SPI_Sand(BOOST_VDDP_EN, BOOST_VDDP_EN_V, sizeof(BOOST_VDDP_EN_V));
  222. EPD_13IN3E_CS_ALL(1);
  223. DEV_Digital_Write(EPD_CS_M_PIN, 0);
  224. EPD_13IN3E_SPI_Sand(BTST_N, BTST_N_V, sizeof(BTST_N_V));
  225. EPD_13IN3E_CS_ALL(1);
  226. DEV_Digital_Write(EPD_CS_M_PIN, 0);
  227. EPD_13IN3E_SPI_Sand(BUCK_BOOST_VDDN, BUCK_BOOST_VDDN_V, sizeof(BUCK_BOOST_VDDN_V));
  228. EPD_13IN3E_CS_ALL(1);
  229. DEV_Digital_Write(EPD_CS_M_PIN, 0);
  230. EPD_13IN3E_SPI_Sand(TFT_VCOM_POWER, TFT_VCOM_POWER_V, sizeof(TFT_VCOM_POWER_V));
  231. EPD_13IN3E_CS_ALL(1);
  232. }
  233. /******************************************************************************
  234. function : Clear screen
  235. parameter:
  236. ******************************************************************************/
  237. void EPD_13IN3E_Clear(UBYTE color)
  238. {
  239. UDOUBLE Width, Height;
  240. UBYTE Color;
  241. Width = (EPD_13IN3E_WIDTH % 2 == 0)? (EPD_13IN3E_WIDTH / 2 ): (EPD_13IN3E_WIDTH / 2 + 1);
  242. Height = EPD_13IN3E_HEIGHT;
  243. Color = (color<<4)|color;
  244. UBYTE buf[Width/2];
  245. for (UDOUBLE j = 0; j < Width/2; j++) {
  246. buf[j] = Color;
  247. }
  248. DEV_Digital_Write(EPD_CS_M_PIN, 0);
  249. EPD_13IN3E_SendCommand(0x10);
  250. for (UDOUBLE j = 0; j < EPD_13IN3E_HEIGHT; j++) {
  251. EPD_13IN3E_SendData2(buf, Width/2);
  252. }
  253. EPD_13IN3E_CS_ALL(1);
  254. DEV_Digital_Write(EPD_CS_S_PIN, 0);
  255. EPD_13IN3E_SendCommand(0x10);
  256. for (UDOUBLE j = 0; j < EPD_13IN3E_HEIGHT; j++) {
  257. EPD_13IN3E_SendData2(buf, Width/2);
  258. }
  259. EPD_13IN3E_CS_ALL(1);
  260. EPD_13IN3E_TurnOnDisplay();
  261. }
  262. void EPD_13IN3E_Display(const UBYTE *Image)
  263. {
  264. UDOUBLE Width, Width1, Height;
  265. Width = (EPD_13IN3E_WIDTH % 2 == 0)? (EPD_13IN3E_WIDTH / 2 ): (EPD_13IN3E_WIDTH / 2 + 1);
  266. Width1 = (Width % 2 == 0)? (Width / 2 ): (Width / 2 + 1);
  267. Height = EPD_13IN3E_HEIGHT;
  268. DEV_Digital_Write(EPD_CS_M_PIN, 0);
  269. EPD_13IN3E_SendCommand(0x10);
  270. for(UDOUBLE i=0; i<Height; i++ )
  271. EPD_13IN3E_SendData2(Image + i*Width,Width1);
  272. EPD_13IN3E_CS_ALL(1);
  273. DEV_Digital_Write(EPD_CS_S_PIN, 0);
  274. EPD_13IN3E_SendCommand(0x10);
  275. for(UDOUBLE i=0; i<Height; i++ )
  276. EPD_13IN3E_SendData2(Image + i*Width + Width1,Width1);
  277. EPD_13IN3E_CS_ALL(1);
  278. EPD_13IN3E_TurnOnDisplay();
  279. }
  280. void EPD_13IN3E_DisplayPart(const UBYTE *Image, UWORD xstart, UWORD ystart, UWORD image_width, UWORD image_heigh)
  281. {
  282. UDOUBLE Width, Width1, Height;
  283. Width = (EPD_13IN3E_WIDTH % 2 == 0)? (EPD_13IN3E_WIDTH / 2 ): (EPD_13IN3E_WIDTH / 2 + 1);
  284. Width1 = (Width % 2 == 0)? (Width / 2 ): (Width / 2 + 1);
  285. Height = EPD_13IN3E_HEIGHT;
  286. UWORD Xend = ((xstart + image_width)%2 == 0)?((xstart + image_width) / 2 - 1): ((xstart + image_width) / 2 );
  287. UWORD Yend = ystart + image_heigh-1;
  288. xstart = xstart / 2;
  289. if(xstart > 300 )
  290. {
  291. Xend = Xend - 300;
  292. xstart = xstart - 300;
  293. DEV_Digital_Write(EPD_CS_M_PIN, 0);
  294. EPD_13IN3E_SendCommand(0x10);
  295. for (UDOUBLE i = 0; i < Height; i++) {
  296. for (UDOUBLE j = 0; j < Width1; j++) {
  297. EPD_13IN3E_SendData(0x11);
  298. }
  299. }
  300. EPD_13IN3E_CS_ALL(1);
  301. DEV_Digital_Write(EPD_CS_S_PIN, 0);
  302. EPD_13IN3E_SendCommand(0x10);
  303. for (UDOUBLE i = 0; i < Height; i++) {
  304. for (UDOUBLE j = 0; j < Width1; j++) {
  305. if((i<Yend) && (i>=ystart) && (j<Xend) && (j>=xstart)) {
  306. EPD_13IN3E_SendData(Image[(j-xstart) + (image_width/2*(i-ystart))]);
  307. }
  308. else
  309. EPD_13IN3E_SendData(0x11);
  310. }
  311. }
  312. EPD_13IN3E_CS_ALL(1);
  313. }
  314. else if(Xend < 300 )
  315. {
  316. DEV_Digital_Write(EPD_CS_M_PIN, 0);
  317. EPD_13IN3E_SendCommand(0x10);
  318. for (UDOUBLE i = 0; i < Height; i++) {
  319. for (UDOUBLE j = 0; j < Width1; j++) {
  320. if((i<Yend) && (i>=ystart) && (j<Xend) && (j>=xstart)) {
  321. EPD_13IN3E_SendData(Image[(j-xstart) + (image_width/2*(i-ystart))]);
  322. }
  323. else
  324. EPD_13IN3E_SendData(0x11);
  325. }
  326. }
  327. EPD_13IN3E_CS_ALL(1);
  328. DEV_Digital_Write(EPD_CS_S_PIN, 0);
  329. EPD_13IN3E_SendCommand(0x10);
  330. for (UDOUBLE i = 0; i < Height; i++) {
  331. for (UDOUBLE j = 0; j < Width1; j++) {
  332. EPD_13IN3E_SendData(0x11);
  333. }
  334. }
  335. EPD_13IN3E_CS_ALL(1);
  336. }
  337. else
  338. {
  339. DEV_Digital_Write(EPD_CS_M_PIN, 0);
  340. EPD_13IN3E_SendCommand(0x10);
  341. for (UDOUBLE i = 0; i < Height; i++) {
  342. for (UDOUBLE j = 0; j < Width1; j++) {
  343. if((i<Yend) && (i>=ystart) && (j>=xstart)) {
  344. EPD_13IN3E_SendData(Image[(j-xstart) + (image_width/2*(i-ystart))]);
  345. }
  346. else
  347. EPD_13IN3E_SendData(0x11);
  348. }
  349. }
  350. EPD_13IN3E_CS_ALL(1);
  351. DEV_Digital_Write(EPD_CS_S_PIN, 0);
  352. EPD_13IN3E_SendCommand(0x10);
  353. for (UDOUBLE i = 0; i < Height; i++) {
  354. for (UDOUBLE j = 0; j < Width1; j++) {
  355. if((i<Yend) && (i>=ystart) && (j<Xend-300)) {
  356. EPD_13IN3E_SendData(Image[(j+300-xstart) + (image_width/2*(i-ystart))]);
  357. }
  358. else
  359. EPD_13IN3E_SendData(0x11);
  360. }
  361. }
  362. EPD_13IN3E_CS_ALL(1);
  363. }
  364. EPD_13IN3E_TurnOnDisplay();
  365. }
  366. void EPD_13IN3E_Show6Block(void)
  367. {
  368. unsigned long i, j, k;
  369. UWORD Width, Height;
  370. Width = (EPD_13IN3E_WIDTH % 2 == 0)? (EPD_13IN3E_WIDTH / 2 ): (EPD_13IN3E_WIDTH / 2 + 1);
  371. Height = EPD_13IN3E_HEIGHT;
  372. unsigned char const Color_seven[6] =
  373. {EPD_13IN3E_BLACK, EPD_13IN3E_BLUE, EPD_13IN3E_GREEN,
  374. EPD_13IN3E_RED, EPD_13IN3E_YELLOW, EPD_13IN3E_WHITE};
  375. DEV_Digital_Write(EPD_CS_M_PIN, 0);
  376. EPD_13IN3E_SendCommand(0x10);
  377. for (k = 0; k < 6; k++) {
  378. for (j = 0; j < Height/6; j++) {
  379. for (i = 0; i < Width/2; i++) {
  380. EPD_13IN3E_SendData(Color_seven[k]|(Color_seven[k]<<4));
  381. }
  382. }
  383. }
  384. EPD_13IN3E_CS_ALL(1);
  385. DEV_Digital_Write(EPD_CS_S_PIN, 0);
  386. EPD_13IN3E_SendCommand(0x10);
  387. for (k = 0; k < 6; k++) {
  388. for (j = 0; j < Height/6; j++) {
  389. for (i = 0; i < Width/2; i++) {
  390. EPD_13IN3E_SendData(Color_seven[k]|(Color_seven[k]<<4));
  391. }
  392. }
  393. }
  394. EPD_13IN3E_CS_ALL(1);
  395. EPD_13IN3E_TurnOnDisplay();
  396. }
  397. /******************************************************************************
  398. function : Enter sleep mode
  399. parameter:
  400. ******************************************************************************/
  401. void EPD_13IN3E_Sleep(void)
  402. {
  403. EPD_13IN3E_CS_ALL(0);
  404. EPD_13IN3E_SendCommand(0x07); // DEEP_SLEEP
  405. EPD_13IN3E_SendData(0XA5);
  406. EPD_13IN3E_CS_ALL(1);
  407. }