EPD_5in79g.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*****************************************************************************
  2. * | File : EPD_5in79g.c
  3. * | Author : Waveshare team
  4. * | Function : Electronic paper driver
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2023-07-04
  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_5in79g.h"
  31. #include "Debug.h"
  32. #include <time.h>
  33. /******************************************************************************
  34. function : Software reset
  35. parameter:
  36. ******************************************************************************/
  37. static void EPD_5in79g_Reset(void)
  38. {
  39. DEV_Digital_Write(EPD_RST_PIN, 1);
  40. DEV_Delay_ms(200);
  41. DEV_Digital_Write(EPD_RST_PIN, 0);
  42. DEV_Delay_ms(1);
  43. DEV_Digital_Write(EPD_RST_PIN, 1);
  44. DEV_Delay_ms(200);
  45. }
  46. /******************************************************************************
  47. function : send command
  48. parameter:
  49. Reg : Command register
  50. ******************************************************************************/
  51. static void EPD_5in79g_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_5in79g_SendData(UBYTE Data)
  64. {
  65. DEV_Digital_Write(EPD_CS_PIN, 0);
  66. DEV_Digital_Write(EPD_DC_PIN, 1);
  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_5in79g_ReadBus(void)
  75. {
  76. Debug("e-Paper busy\r\n");
  77. UBYTE busy;
  78. do
  79. {
  80. busy = DEV_Digital_Read(EPD_BUSY_PIN);
  81. DEV_Delay_ms(10);
  82. }
  83. while(!busy);
  84. DEV_Delay_ms(200);
  85. Debug("e-Paper busy release\r\n");
  86. }
  87. /******************************************************************************
  88. function : Turn On Display
  89. parameter:
  90. ******************************************************************************/
  91. static void EPD_5in79g_TurnOnDisplay(void)
  92. {
  93. EPD_5in79g_SendCommand(0xA2); //********************
  94. EPD_5in79g_SendData(0x00);
  95. EPD_5in79g_SendCommand(0x12);
  96. EPD_5in79g_SendData(0x00);
  97. EPD_5in79g_ReadBus();
  98. }
  99. /******************************************************************************
  100. function : Initialize the e-Paper register
  101. parameter:
  102. ******************************************************************************/
  103. UBYTE EPD_5in79g_Init(void)
  104. {
  105. EPD_5in79g_Reset();
  106. // EPD_5in79g_SendCommand(0x4D);
  107. // EPD_5in79g_SendData(0x78);
  108. EPD_5in79g_SendCommand(0xA2); //********************
  109. EPD_5in79g_SendData(0x01);
  110. EPD_5in79g_SendCommand(0x00); //0x00
  111. EPD_5in79g_SendData(0x03);
  112. EPD_5in79g_SendData(0x29);
  113. EPD_5in79g_SendCommand(0xA2); //********************
  114. EPD_5in79g_SendData(0x02);
  115. EPD_5in79g_SendCommand(0x00); //0x00
  116. EPD_5in79g_SendData(0x07);
  117. EPD_5in79g_SendData(0x29);
  118. EPD_5in79g_SendCommand(0xA2); //********************
  119. EPD_5in79g_SendData(0x00);
  120. EPD_5in79g_SendCommand(0x50); //
  121. EPD_5in79g_SendData(0x97);
  122. EPD_5in79g_SendCommand(0x61); //0x61
  123. EPD_5in79g_SendData(0x01);
  124. EPD_5in79g_SendData(0x8c);
  125. EPD_5in79g_SendData(0x01);
  126. EPD_5in79g_SendData(0x10);
  127. EPD_5in79g_SendCommand(0x06); //0x06
  128. EPD_5in79g_SendData(0x38);
  129. EPD_5in79g_SendData(0x38);
  130. EPD_5in79g_SendData(0x38);
  131. EPD_5in79g_SendData(0x00); //////////////////////////////////////////
  132. EPD_5in79g_SendCommand(0xE9); //0xE0
  133. EPD_5in79g_SendData(0x01);
  134. EPD_5in79g_SendCommand(0xE0); //0xE0
  135. EPD_5in79g_SendData(0x01);
  136. EPD_5in79g_SendCommand(0x04);
  137. EPD_5in79g_ReadBus(); //while(1);
  138. return 0;
  139. }
  140. /******************************************************************************
  141. function : Clear screen
  142. parameter:
  143. ******************************************************************************/
  144. void EPD_5in79g_Clear(UBYTE color)
  145. {
  146. UWORD Width, Height;
  147. Width =(EPD_5in79G_WIDTH % 8 == 0)?(EPD_5in79G_WIDTH / 8 ):(EPD_5in79G_WIDTH / 8 + 1);
  148. Height = EPD_5in79G_HEIGHT;
  149. EPD_5in79g_SendCommand(0xA2); //********************
  150. EPD_5in79g_SendData(0x02);
  151. EPD_5in79g_SendCommand(0x10);
  152. for (UWORD j = 0; j < Height; j++) {
  153. for (UWORD i = 0; i < Width; i++) {
  154. EPD_5in79g_SendData((color << 6) | (color << 4) | (color << 2) | color);
  155. }
  156. }
  157. EPD_5in79g_SendCommand(0xA2); //********************
  158. EPD_5in79g_SendData(0x01);
  159. EPD_5in79g_SendCommand(0x10);
  160. for (UWORD j = 0; j < Height; j++) {
  161. for (UWORD i = 0; i < Width; i++) {
  162. EPD_5in79g_SendData((color << 6) | (color << 4) | (color << 2) | color);
  163. }
  164. }
  165. EPD_5in79g_TurnOnDisplay();
  166. }
  167. void EPD_5in79g_Show(void)
  168. {
  169. UWORD Width, Height;
  170. Width =(EPD_5in79G_WIDTH % 8 == 0)?(EPD_5in79G_WIDTH / 8 ):(EPD_5in79G_WIDTH / 8 + 1);
  171. Height = EPD_5in79G_HEIGHT;
  172. unsigned char const Color_seven[4] =
  173. {0x00, 0x55, 0xAA, 0xFF};
  174. UBYTE k,o;
  175. k = 0;
  176. o = 0;
  177. EPD_5in79g_SendCommand(0xA2); //********************
  178. EPD_5in79g_SendData(0x02);
  179. EPD_5in79g_SendCommand(0x10);
  180. for (UWORD j = 0; j < Height; j++) {
  181. for (UWORD i = 0; i < Width; i++) {
  182. EPD_5in79g_SendData(Color_seven[0]);
  183. }
  184. for (UWORD i = 0; i < Width; i++) {
  185. // EPD_5in79g_SendData(0x55);
  186. EPD_5in79g_SendData(Color_seven[1]);
  187. }
  188. }
  189. EPD_5in79g_SendCommand(0xA2); //********************
  190. EPD_5in79g_SendData(0x01);
  191. EPD_5in79g_SendCommand(0x10);
  192. for (UWORD j = 0; j < Height; j++) {
  193. for (UWORD i = 0; i < Width; i++) {
  194. EPD_5in79g_SendData(Color_seven[2]);
  195. }
  196. for (UWORD i = 0; i < Width; i++) {
  197. // EPD_5in79g_SendData(0x55);
  198. EPD_5in79g_SendData(Color_seven[3]);
  199. }
  200. }
  201. EPD_5in79g_TurnOnDisplay();
  202. }
  203. /******************************************************************************
  204. function : Sends the image buffer in RAM to e-Paper and displays
  205. parameter:
  206. ******************************************************************************/
  207. void EPD_5in79g_Display(const UBYTE *Image)
  208. {
  209. UWORD Width, Height, Width1;
  210. Width1 = (EPD_5in79G_WIDTH % 4 == 0)? (EPD_5in79G_WIDTH / 4 ): (EPD_5in79G_WIDTH / 4 + 1);
  211. Width =(EPD_5in79G_WIDTH % 8 == 0)?(EPD_5in79G_WIDTH / 8 ):(EPD_5in79G_WIDTH / 8 + 1);
  212. Height = EPD_5in79G_HEIGHT;
  213. EPD_5in79g_SendCommand(0xA2); //********************
  214. EPD_5in79g_SendData(0x02);
  215. EPD_5in79g_SendCommand(0x10);
  216. for (UWORD j = 0; j < Height/2; j++) {
  217. for (UWORD i = 0; i < Width; i++) {
  218. EPD_5in79g_SendData(Image[i + j * Width1]);
  219. }
  220. for (UWORD i = 0; i < Width; i++) {
  221. EPD_5in79g_SendData(Image[i + (Height - j - 1) * Width1]);
  222. }
  223. }
  224. EPD_5in79g_SendCommand(0xA2); //********************
  225. EPD_5in79g_SendData(0x01);
  226. EPD_5in79g_SendCommand(0x10);
  227. for (UWORD j = 0; j < Height; j++) {
  228. for (UWORD i = 0; i < Width; i++) {
  229. EPD_5in79g_SendData(Image[j * Width1 + i + Width]);
  230. }
  231. for (UWORD i = 0; i < Width; i++) {
  232. EPD_5in79g_SendData(Image[(Height - j - 1) * Width1 + i + Width]);
  233. }
  234. }
  235. EPD_5in79g_TurnOnDisplay();
  236. }
  237. void EPD_5in79g_Display_Partial(const UBYTE *Image)
  238. {
  239. UWORD Width, Height, Width1;
  240. Width1 = (EPD_5in79G_WIDTH % 4 == 0)? (EPD_5in79G_WIDTH / 4 ): (EPD_5in79G_WIDTH / 4 + 1);
  241. Width =(EPD_5in79G_WIDTH % 8 == 0)?(EPD_5in79G_WIDTH / 8 ):(EPD_5in79G_WIDTH / 8 + 1);
  242. Height = EPD_5in79G_HEIGHT;
  243. EPD_5in79g_SendCommand(0xA2); //********************
  244. EPD_5in79g_SendData(0x02);
  245. EPD_5in79g_SendCommand(0x10);
  246. for (UWORD j = 0; j < Height/2; j++) {
  247. for (UWORD i = 0; i < Width; i++) {
  248. EPD_5in79g_SendData(Image[i + j * Width]);
  249. }
  250. for (UWORD i = 0; i < Width; i++) {
  251. EPD_5in79g_SendData(Image[i + (Height - j - 1) * Width]);
  252. }
  253. }
  254. EPD_5in79g_SendCommand(0xA2); //********************
  255. EPD_5in79g_SendData(0x01);
  256. EPD_5in79g_SendCommand(0x10);
  257. for (UWORD j = 0; j < Height; j++) {
  258. for (UWORD i = 0; i < Width; i++) {
  259. EPD_5in79g_SendData(0x55);
  260. }
  261. }
  262. EPD_5in79g_TurnOnDisplay();
  263. }
  264. /******************************************************************************
  265. function : Enter sleep mode
  266. parameter:
  267. ******************************************************************************/
  268. void EPD_5in79g_Sleep(void)
  269. {
  270. // EPD_5in79g_SendCommand(0x02); // POWER_OFF
  271. // EPD_5in79g_SendData(0x00);
  272. // EPD_5in79g_ReadBus();
  273. EPD_5in79g_SendCommand(0x07);
  274. EPD_5in79g_SendData(0xa5);
  275. }