EPD_2in15g.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*****************************************************************************
  2. * | File : EPD_2in15g.c
  3. * | Author : Waveshare team
  4. * | Function : 2inch15 e-paper (G)
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2024-08-07
  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_2in15g.h"
  32. /******************************************************************************
  33. function : Software reset
  34. parameter:
  35. ******************************************************************************/
  36. static void EPD_2IN15G_Reset(void)
  37. {
  38. DEV_Digital_Write(EPD_RST_PIN, 1);
  39. DEV_Delay_ms(200);
  40. DEV_Digital_Write(EPD_RST_PIN, 0);
  41. DEV_Delay_ms(2);
  42. DEV_Digital_Write(EPD_RST_PIN, 1);
  43. DEV_Delay_ms(200);
  44. }
  45. /******************************************************************************
  46. function : send command
  47. parameter:
  48. Reg : Command register
  49. ******************************************************************************/
  50. static void EPD_2IN15G_SendCommand(UBYTE Reg)
  51. {
  52. DEV_Digital_Write(EPD_DC_PIN, 0);
  53. DEV_Digital_Write(EPD_CS_PIN, 0);
  54. DEV_SPI_WriteByte(Reg);
  55. DEV_Digital_Write(EPD_CS_PIN, 1);
  56. }
  57. /******************************************************************************
  58. function : send data
  59. parameter:
  60. Data : Write data
  61. ******************************************************************************/
  62. static void EPD_2IN15G_SendData(UBYTE Data)
  63. {
  64. DEV_Digital_Write(EPD_DC_PIN, 1);
  65. DEV_Digital_Write(EPD_CS_PIN, 0);
  66. DEV_SPI_WriteByte(Data);
  67. DEV_Digital_Write(EPD_CS_PIN, 1);
  68. }
  69. /******************************************************************************
  70. function : Wait until the busy_pin goes LOW
  71. parameter:
  72. ******************************************************************************/
  73. void EPD_2IN15G_ReadBusy(void)
  74. {
  75. Debug("e-Paper busy\r\n");
  76. DEV_Delay_ms(100);
  77. while(DEV_Digital_Read(EPD_BUSY_PIN) == 1) { //LOW: idle, HIGH: busy
  78. DEV_Delay_ms(100);
  79. }
  80. Debug("e-Paper busy release\r\n");
  81. }
  82. /******************************************************************************
  83. function : Wait until the busy_pin goes LOW
  84. parameter:
  85. ******************************************************************************/
  86. static void EPD_2IN15G_ReadBusyH(void)
  87. {
  88. Debug("e-Paper busy H\r\n");
  89. while(!DEV_Digital_Read(EPD_BUSY_PIN)) { //LOW: idle, HIGH: busy
  90. DEV_Delay_ms(5);
  91. }
  92. Debug("e-Paper busy H release\r\n");
  93. }
  94. /******************************************************************************
  95. function : Turn On Display
  96. parameter:
  97. ******************************************************************************/
  98. static void EPD_2IN15G_TurnOnDisplay(void)
  99. {
  100. // EPD_2IN15G_SendCommand(0x04);
  101. // EPD_2IN15G_ReadBusyH();
  102. EPD_2IN15G_SendCommand(0x12); // DISPLAY_REFRESH
  103. EPD_2IN15G_SendData(0x00);
  104. EPD_2IN15G_ReadBusyH();
  105. // EPD_2IN15G_SendCommand(0x20);
  106. // EPD_2IN15G_SendData(0x00);
  107. // EPD_2IN15G_ReadBusyH();
  108. }
  109. /******************************************************************************
  110. function : Initialize the e-Paper register
  111. parameter:
  112. ******************************************************************************/
  113. void EPD_2IN15G_Init(void)
  114. {
  115. EPD_2IN15G_Reset();
  116. EPD_2IN15G_SendCommand(0x4D);
  117. EPD_2IN15G_SendData(0x78);
  118. EPD_2IN15G_SendCommand(0x00); //0x00
  119. EPD_2IN15G_SendData(0x0F);
  120. EPD_2IN15G_SendData(0x29); // D5=0
  121. EPD_2IN15G_SendCommand(0x01); //0x01
  122. EPD_2IN15G_SendData(0x07);
  123. EPD_2IN15G_SendData(0x00);
  124. EPD_2IN15G_SendCommand(0x03); //0x03
  125. EPD_2IN15G_SendData(0x10);
  126. EPD_2IN15G_SendData(0x54);
  127. EPD_2IN15G_SendData(0x44);
  128. EPD_2IN15G_SendCommand(0x06); //0x06
  129. EPD_2IN15G_SendData(0x0F);
  130. EPD_2IN15G_SendData(0x0A);
  131. EPD_2IN15G_SendData(0x2F);
  132. EPD_2IN15G_SendData(0x25);
  133. EPD_2IN15G_SendData(0x22);
  134. EPD_2IN15G_SendData(0x2E);
  135. EPD_2IN15G_SendData(0x21);
  136. EPD_2IN15G_SendCommand(0x30);
  137. EPD_2IN15G_SendData(0x02);
  138. EPD_2IN15G_SendCommand(0x41); //0x41
  139. EPD_2IN15G_SendData(0x00);
  140. EPD_2IN15G_SendCommand(0x50); //0x50
  141. EPD_2IN15G_SendData(0x37);
  142. EPD_2IN15G_SendCommand(0x60); //0x60
  143. EPD_2IN15G_SendData(0x02);
  144. EPD_2IN15G_SendData(0x02);
  145. EPD_2IN15G_SendCommand(0x61); //0x61
  146. EPD_2IN15G_SendData(EPD_2IN15G_WIDTH/256);
  147. EPD_2IN15G_SendData(EPD_2IN15G_WIDTH%256);
  148. EPD_2IN15G_SendData(EPD_2IN15G_HEIGHT/256);
  149. EPD_2IN15G_SendData(EPD_2IN15G_HEIGHT%256);
  150. EPD_2IN15G_SendCommand(0x65); //0x65
  151. EPD_2IN15G_SendData(0x00);
  152. EPD_2IN15G_SendData(0x00);
  153. EPD_2IN15G_SendData(0x00);
  154. EPD_2IN15G_SendData(0x00);
  155. EPD_2IN15G_SendCommand(0XE7); // 0XE7
  156. EPD_2IN15G_SendData(0x1C);
  157. EPD_2IN15G_SendCommand(0xE3); //0xE3
  158. EPD_2IN15G_SendData(0x22);
  159. EPD_2IN15G_SendCommand(0xE0); //0xE0
  160. EPD_2IN15G_SendData(0x00);
  161. EPD_2IN15G_SendCommand(0xB4);
  162. EPD_2IN15G_SendData(0xD0);
  163. EPD_2IN15G_SendCommand(0xB5);
  164. EPD_2IN15G_SendData(0x03);
  165. EPD_2IN15G_SendCommand(0xE9);
  166. EPD_2IN15G_SendData(0x01);
  167. EPD_2IN15G_SendCommand(0x04);
  168. EPD_2IN15G_ReadBusyH();
  169. }
  170. /******************************************************************************
  171. function : Clear screen
  172. parameter:
  173. ******************************************************************************/
  174. void EPD_2IN15G_Clear(UBYTE color)
  175. {
  176. UWORD Width, Height;
  177. Width = (EPD_2IN15G_WIDTH % 4 == 0)? (EPD_2IN15G_WIDTH / 4 ): (EPD_2IN15G_WIDTH / 4 + 1);
  178. Height = EPD_2IN15G_HEIGHT;
  179. EPD_2IN15G_SendCommand(0x10);
  180. for (UWORD j = 0; j < Height; j++) {
  181. for (UWORD i = 0; i < Width; i++) {
  182. EPD_2IN15G_SendData((color << 6) | (color << 4) | (color << 2) | color);
  183. }
  184. }
  185. EPD_2IN15G_TurnOnDisplay();
  186. }
  187. /******************************************************************************
  188. function : Sends the image buffer in RAM to e-Paper and displays
  189. parameter:
  190. ******************************************************************************/
  191. void EPD_2IN15G_Display(const UBYTE *Image)
  192. {
  193. UWORD Width, Height;
  194. Width = (EPD_2IN15G_WIDTH % 4 == 0)? (EPD_2IN15G_WIDTH / 4 ): (EPD_2IN15G_WIDTH / 4 + 1);
  195. Height = EPD_2IN15G_HEIGHT;
  196. EPD_2IN15G_SendCommand(0x10);
  197. for (UWORD j = 0; j < Height; j++) {
  198. for (UWORD i = 0; i < Width; i++) {
  199. EPD_2IN15G_SendData(Image[i + j * Width]);
  200. }
  201. }
  202. EPD_2IN15G_TurnOnDisplay();
  203. }
  204. /******************************************************************************
  205. function : Enter sleep mode
  206. parameter:
  207. ******************************************************************************/
  208. void EPD_2IN15G_Sleep(void)
  209. {
  210. EPD_2IN15G_SendCommand(0x02); // POWER_OFF
  211. EPD_2IN15G_SendData(0X00);
  212. EPD_2IN15G_SendCommand(0x07); // DEEP_SLEEP
  213. EPD_2IN15G_SendData(0XA5);
  214. }