EPD_4in37b.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*****************************************************************************
  2. * | File : EPD_4in37b.c
  3. * | Author : Waveshare team
  4. * | Function : 4.37inch e-paper b
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2021-01-07
  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_4in37b.h"
  31. #include "Debug.h"
  32. /******************************************************************************
  33. function : Software reset
  34. parameter:
  35. ******************************************************************************/
  36. static void EPD_4IN37B_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_4IN37B_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_4IN37B_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. static void EPD_4IN37B_ReadBusy(void)
  74. {
  75. Debug("e-Paper busy\r\n");
  76. DEV_Delay_ms(50);
  77. while(DEV_Digital_Read(EPD_BUSY_PIN) == 0) { //LOW: idle, HIGH: busy
  78. DEV_Delay_ms(10);
  79. }
  80. DEV_Delay_ms(50);
  81. Debug("e-Paper busy release\r\n");
  82. }
  83. /******************************************************************************
  84. function : Turn On Display
  85. parameter:
  86. ******************************************************************************/
  87. static void EPD_4IN37B_TurnOnDisplay(void)
  88. {
  89. EPD_4IN37B_SendCommand(0x12);
  90. EPD_4IN37B_ReadBusy();
  91. }
  92. /******************************************************************************
  93. function : Setting the display window
  94. parameter:
  95. ******************************************************************************/
  96. static void EPD_4IN37B_SetWindows(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
  97. {
  98. EPD_4IN37B_SendCommand(0x90);
  99. EPD_4IN37B_SendData(Xstart & 0xf8);
  100. EPD_4IN37B_SendData(Xend | 0x07);
  101. EPD_4IN37B_SendData((Ystart >> 8) & 0x01);
  102. EPD_4IN37B_SendData(Ystart & 0xFF);
  103. EPD_4IN37B_SendData((Yend >> 8) & 0x01);
  104. EPD_4IN37B_SendData(Yend & 0xFF);
  105. EPD_4IN37B_SendData(0x01);
  106. }
  107. /******************************************************************************
  108. function : Initialize the e-Paper register
  109. parameter:
  110. ******************************************************************************/
  111. void EPD_4IN37B_Init(void)
  112. {
  113. EPD_4IN37B_Reset();
  114. EPD_4IN37B_SendCommand(0x04);//soft reset
  115. EPD_4IN37B_ReadBusy();
  116. EPD_4IN37B_SendCommand(0X00); //PANNEL SETTING
  117. EPD_4IN37B_SendData(0x0F); //KW-3f KWR-2F BWROTP 0f BWOTP 1f
  118. }
  119. /******************************************************************************
  120. function : Sends the image buffer in RAM to e-Paper and displays
  121. parameter:
  122. ******************************************************************************/
  123. void EPD_4IN37B_Display(UBYTE *ImageBlack, UBYTE*ImageRed)
  124. {
  125. UWORD Width, Height;
  126. Width = (EPD_4IN37B_WIDTH % 8 == 0)? (EPD_4IN37B_WIDTH / 8 ): (EPD_4IN37B_WIDTH / 8 + 1);
  127. Height = EPD_4IN37B_HEIGHT;
  128. EPD_4IN37B_SendCommand(0x10);
  129. for (UWORD j = 0; j < Height; j++) {
  130. for (UWORD i = 0; i < Width; i++) {
  131. EPD_4IN37B_SendData(ImageBlack[i + j * Width]);
  132. }
  133. }
  134. EPD_4IN37B_SendCommand(0x13);
  135. for (UWORD j = 0; j < Height; j++) {
  136. for (UWORD i = 0; i < Width; i++) {
  137. EPD_4IN37B_SendData(~ImageRed[i + j * Width]);
  138. }
  139. }
  140. EPD_4IN37B_TurnOnDisplay();
  141. }
  142. void EPD_4IN37B_Display_Part(UBYTE *ImageBlack, UBYTE *ImageRed, UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
  143. {
  144. UWORD Width, Height;
  145. Width = Xend - Xstart;
  146. Height = Yend - Ystart;
  147. Width = (Width % 8 == 0)? (Width / 8 ): (Width / 8 + 1);
  148. if(Xstart % 8 != 0) {
  149. Xstart += 8;
  150. if(Xstart > Xend) {
  151. printf("Xstart must be a multiple of 8 and not more than Xend \r\n");
  152. return;
  153. }
  154. }
  155. EPD_4IN37B_SetWindows(Xstart, Ystart, Xend-1, Yend-1);
  156. EPD_4IN37B_SendCommand(0x91);
  157. EPD_4IN37B_SendCommand(0x10);
  158. for (UWORD j = 0; j < Height; j++) {
  159. for (UWORD i = 0; i < Width; i++) {
  160. EPD_4IN37B_SendData(ImageBlack[i + j * Width]);
  161. }
  162. }
  163. EPD_4IN37B_SendCommand(0x13);
  164. for (UWORD j = 0; j < Height; j++) {
  165. for (UWORD i = 0; i < Width; i++) {
  166. EPD_4IN37B_SendData(~ImageRed[i + j * Width]);
  167. }
  168. }
  169. EPD_4IN37B_SendCommand(0x92);
  170. EPD_4IN37B_TurnOnDisplay();
  171. }
  172. /******************************************************************************
  173. function : Clear screen
  174. parameter:
  175. ******************************************************************************/
  176. void EPD_4IN37B_Clear(void)
  177. {
  178. UWORD Width, Height;
  179. Width = (EPD_4IN37B_WIDTH % 8 == 0)? (EPD_4IN37B_WIDTH / 8 ): (EPD_4IN37B_WIDTH / 8 + 1);
  180. Height = EPD_4IN37B_HEIGHT;
  181. EPD_4IN37B_SendCommand(0x10);
  182. for (UWORD j = 0; j < Height; j++) {
  183. for (UWORD i = 0; i < Width; i++) {
  184. EPD_4IN37B_SendData(0xff);
  185. }
  186. }
  187. EPD_4IN37B_SendCommand(0x13);
  188. for (UWORD j = 0; j < Height; j++) {
  189. for (UWORD i = 0; i < Width; i++) {
  190. EPD_4IN37B_SendData(0x00);
  191. }
  192. }
  193. EPD_4IN37B_TurnOnDisplay();
  194. }
  195. /******************************************************************************
  196. function : Enter sleep mode
  197. parameter:
  198. ******************************************************************************/
  199. void EPD_4IN37B_Sleep(void)
  200. {
  201. EPD_4IN37B_SendCommand(0x02);
  202. DEV_Delay_ms(100);
  203. EPD_4IN37B_SendCommand(0x07);
  204. EPD_4IN37B_SendData(0xa5);
  205. }