EPD_2in7_V2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*****************************************************************************
  2. * | File : EPD_2in7_V2.c
  3. * | Author : Waveshare team
  4. * | Function : 2.7inch V2 e-paper
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2021-04-22
  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_2in7_V2.h"
  32. #include "Debug.h"
  33. UBYTE WF_PARTIAL_2in7_V2[159] =
  34. {
  35. 0x0,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  36. 0x80,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  37. 0x40,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  38. 0x0,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  39. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  40. 0xF,0x0,0x0,0x0,0x0,0x0,0x1,
  41. 0x1,0x1,0x0,0x0,0x0,0x0,0x0,
  42. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  43. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  44. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  45. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  46. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  47. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  48. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  49. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  50. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  51. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  52. 0x22,0x22,0x22,0x22,0x22,0x22,0x0,0x0,0x0,
  53. 0x22,0x17,0x41,0xB0,0x32,0x32,
  54. };
  55. /******************************************************************************
  56. function : Software reset
  57. parameter:
  58. ******************************************************************************/
  59. static void EPD_2IN7_V2_Reset(void)
  60. {
  61. DEV_Digital_Write(EPD_RST_PIN, 1);
  62. DEV_Delay_ms(20);
  63. DEV_Digital_Write(EPD_RST_PIN, 0);
  64. DEV_Delay_ms(2);
  65. DEV_Digital_Write(EPD_RST_PIN, 1);
  66. DEV_Delay_ms(20);
  67. }
  68. /******************************************************************************
  69. function : send command
  70. parameter:
  71. Reg : Command register
  72. ******************************************************************************/
  73. static void EPD_2IN7_V2_SendCommand(UBYTE Reg)
  74. {
  75. DEV_Digital_Write(EPD_DC_PIN, 0);
  76. DEV_Digital_Write(EPD_CS_PIN, 0);
  77. DEV_SPI_WriteByte(Reg);
  78. DEV_Digital_Write(EPD_CS_PIN, 1);
  79. }
  80. /******************************************************************************
  81. function : send data
  82. parameter:
  83. Data : Write data
  84. ******************************************************************************/
  85. static void EPD_2IN7_V2_SendData(UBYTE Data)
  86. {
  87. DEV_Digital_Write(EPD_DC_PIN, 1);
  88. DEV_Digital_Write(EPD_CS_PIN, 0);
  89. DEV_SPI_WriteByte(Data);
  90. DEV_Digital_Write(EPD_CS_PIN, 1);
  91. }
  92. /******************************************************************************
  93. function : Wait until the busy_pin goes LOW
  94. parameter:
  95. ******************************************************************************/
  96. static void EPD_2IN7_V2_ReadBusy(void)
  97. {
  98. Debug("e-Paper busy\r\n");
  99. do {
  100. if(DEV_Digital_Read(EPD_BUSY_PIN) == 0)
  101. break;
  102. } while(1);
  103. DEV_Delay_ms(20);
  104. Debug("e-Paper busy release\r\n");
  105. }
  106. /******************************************************************************
  107. function : Turn on display
  108. parameter:
  109. ******************************************************************************/
  110. static void EPD_2IN7_V2_TurnOnDisplay(void)
  111. {
  112. EPD_2IN7_V2_SendCommand(0x20);
  113. EPD_2IN7_V2_ReadBusy();
  114. }
  115. static void EPD_2IN7_V2_TurnOnDisplay_Partial(void)
  116. {
  117. EPD_2IN7_V2_SendCommand(0x22);
  118. EPD_2IN7_V2_SendData(0x0f);
  119. EPD_2IN7_V2_SendCommand(0x20);
  120. EPD_2IN7_V2_ReadBusy();
  121. }
  122. /******************************************************************************
  123. function : set the look-up tables
  124. parameter:
  125. ******************************************************************************/
  126. static void EPD_2IN7_V2_SetLut(void)
  127. {
  128. unsigned int count;
  129. EPD_2IN7_V2_SendCommand(0x32); //vcom
  130. for(count = 0; count < 153; count++) {
  131. EPD_2IN7_V2_SendData(WF_PARTIAL_2in7_V2[count]);
  132. }
  133. }
  134. /******************************************************************************
  135. function : Setting the display window
  136. parameter:
  137. ******************************************************************************/
  138. static void EPD_2IN7_V2_SetWindows(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
  139. {
  140. EPD_2IN7_V2_SendCommand(0x44); // SET_RAM_X_ADDRESS_START_END_POSITION
  141. EPD_2IN7_V2_SendData((Xstart>>3) & 0xFF);
  142. EPD_2IN7_V2_SendData((Xend>>3) & 0xFF);
  143. EPD_2IN7_V2_SendCommand(0x45); // SET_RAM_Y_ADDRESS_START_END_POSITION
  144. EPD_2IN7_V2_SendData(Ystart & 0xFF);
  145. EPD_2IN7_V2_SendData((Ystart >> 8) & 0xFF);
  146. EPD_2IN7_V2_SendData(Yend & 0xFF);
  147. EPD_2IN7_V2_SendData((Yend >> 8) & 0xFF);
  148. }
  149. /******************************************************************************
  150. function : Set Cursor
  151. parameter:
  152. ******************************************************************************/
  153. static void EPD_2IN7_V2_SetCursor(UWORD Xstart, UWORD Ystart)
  154. {
  155. EPD_2IN7_V2_SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  156. EPD_2IN7_V2_SendData(Xstart & 0xFF);
  157. EPD_2IN7_V2_SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  158. EPD_2IN7_V2_SendData(Ystart & 0xFF);
  159. EPD_2IN7_V2_SendData((Ystart >> 8) & 0xFF);
  160. }
  161. /******************************************************************************
  162. function : Initialize the e-Paper register
  163. parameter:
  164. ******************************************************************************/
  165. void EPD_2IN7_V2_Init(void)
  166. {
  167. EPD_2IN7_V2_Reset();
  168. EPD_2IN7_V2_ReadBusy();
  169. EPD_2IN7_V2_SendCommand(0x12);
  170. EPD_2IN7_V2_ReadBusy();
  171. EPD_2IN7_V2_SendCommand(0x01); //Driver output control
  172. EPD_2IN7_V2_SendData(0x27);
  173. EPD_2IN7_V2_SendData(0x01);
  174. EPD_2IN7_V2_SendData(0x00);
  175. EPD_2IN7_V2_SendCommand(0x11); //data entry mode
  176. EPD_2IN7_V2_SendData(0x03);
  177. EPD_2IN7_V2_SetWindows(0, 0, EPD_2IN7_V2_WIDTH-1, EPD_2IN7_V2_HEIGHT-1);
  178. EPD_2IN7_V2_SetCursor(0, 0);
  179. }
  180. /******************************************************************************
  181. function : Clear screen
  182. parameter:
  183. ******************************************************************************/
  184. void EPD_2IN7_V2_Clear(void)
  185. {
  186. UWORD Width, Height;
  187. Width = (EPD_2IN7_V2_WIDTH % 8 == 0)? (EPD_2IN7_V2_WIDTH / 8 ): (EPD_2IN7_V2_WIDTH / 8 + 1);
  188. Height = EPD_2IN7_V2_HEIGHT;
  189. EPD_2IN7_V2_SendCommand(0x24);
  190. for (UWORD j = 0; j < Height; j++) {
  191. for (UWORD i = 0; i < Width; i++) {
  192. EPD_2IN7_V2_SendData(0XFF);
  193. }
  194. }
  195. EPD_2IN7_V2_SendCommand(0x26);
  196. for (UWORD j = 0; j < Height; j++) {
  197. for (UWORD i = 0; i < Width; i++) {
  198. EPD_2IN7_V2_SendData(0X00);
  199. }
  200. }
  201. EPD_2IN7_V2_TurnOnDisplay();
  202. }
  203. /******************************************************************************
  204. function : Sends the image buffer in RAM to e-Paper and displays
  205. parameter:
  206. ******************************************************************************/
  207. void EPD_2IN7_V2_Display(UBYTE *Image)
  208. {
  209. UWORD Width, Height;
  210. Width = (EPD_2IN7_V2_WIDTH % 8 == 0)? (EPD_2IN7_V2_WIDTH / 8 ): (EPD_2IN7_V2_WIDTH / 8 + 1);
  211. Height = EPD_2IN7_V2_HEIGHT;
  212. EPD_2IN7_V2_SendCommand(0x24);
  213. for (UWORD j = 0; j < Height; j++) {
  214. for (UWORD i = 0; i < Width; i++) {
  215. EPD_2IN7_V2_SendData(Image[i + j * Width]);
  216. }
  217. }
  218. EPD_2IN7_V2_TurnOnDisplay();
  219. }
  220. void EPD_2IN7_V2_Display_Base(UBYTE *Image)
  221. {
  222. UWORD Width, Height;
  223. Width = (EPD_2IN7_V2_WIDTH % 8 == 0)? (EPD_2IN7_V2_WIDTH / 8 ): (EPD_2IN7_V2_WIDTH / 8 + 1);
  224. Height = EPD_2IN7_V2_HEIGHT;
  225. EPD_2IN7_V2_SendCommand(0x24); //Write Black and White image to RAM
  226. for (UWORD j = 0; j < Height; j++) {
  227. for (UWORD i = 0; i < Width; i++) {
  228. EPD_2IN7_V2_SendData(Image[i + j * Width]);
  229. }
  230. }
  231. EPD_2IN7_V2_SendCommand(0x26); //Write Black and White image to RAM
  232. for (UWORD j = 0; j < Height; j++) {
  233. for (UWORD i = 0; i < Width; i++) {
  234. EPD_2IN7_V2_SendData(Image[i + j * Width]);
  235. }
  236. }
  237. EPD_2IN7_V2_TurnOnDisplay();
  238. }
  239. void EPD_2IN7_V2_Display_Partial(UBYTE *Image)
  240. {
  241. UWORD Width, Height;
  242. Width = (EPD_2IN7_V2_WIDTH % 8 == 0)? (EPD_2IN7_V2_WIDTH / 8 ): (EPD_2IN7_V2_WIDTH / 8 + 1);
  243. Height = EPD_2IN7_V2_HEIGHT;
  244. DEV_Digital_Write(EPD_RST_PIN, 0);
  245. DEV_Delay_ms(1);
  246. DEV_Digital_Write(EPD_RST_PIN, 1);
  247. EPD_2IN7_V2_ReadBusy();
  248. EPD_2IN7_V2_SetLut();
  249. EPD_2IN7_V2_SendCommand(0x37);
  250. EPD_2IN7_V2_SendData(0x00);
  251. EPD_2IN7_V2_SendData(0x00);
  252. EPD_2IN7_V2_SendData(0x00);
  253. EPD_2IN7_V2_SendData(0x00);
  254. EPD_2IN7_V2_SendData(0x00);
  255. EPD_2IN7_V2_SendData(0x40);
  256. EPD_2IN7_V2_SendData(0x00);
  257. EPD_2IN7_V2_SendData(0x00);
  258. EPD_2IN7_V2_SendData(0x00);
  259. EPD_2IN7_V2_SendData(0x00);
  260. EPD_2IN7_V2_SendCommand(0x3c);
  261. EPD_2IN7_V2_SendData(0x80);
  262. EPD_2IN7_V2_SendCommand(0x22);
  263. EPD_2IN7_V2_SendData(0xc0);
  264. EPD_2IN7_V2_SendCommand(0x20);
  265. EPD_2IN7_V2_ReadBusy();
  266. EPD_2IN7_V2_SendCommand(0x24);
  267. for (UWORD j = 0; j < Height; j++) {
  268. for (UWORD i = 0; i < Width; i++) {
  269. EPD_2IN7_V2_SendData(Image[i + j * Width]);
  270. }
  271. }
  272. EPD_2IN7_V2_TurnOnDisplay_Partial();
  273. }
  274. /******************************************************************************
  275. function : Enter sleep mode
  276. parameter:
  277. ******************************************************************************/
  278. void EPD_2IN7_V2_Sleep(void)
  279. {
  280. EPD_2IN7_V2_SendCommand(0X10);
  281. EPD_2IN7_V2_SendData(0x01);
  282. }