EPD_4in2b_V2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*****************************************************************************
  2. * | File : EPD_4in2b_V2.c
  3. * | Author : Waveshare team
  4. * | Function : 4.2inch e-paper b V2
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2020-11-25
  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_4in2b_V2.h"
  32. #include "Debug.h"
  33. static uint8_t flag=0;
  34. /******************************************************************************
  35. function : Software reset
  36. parameter:
  37. ******************************************************************************/
  38. static void EPD_4IN2B_V2_Reset(void)
  39. {
  40. DEV_Digital_Write(EPD_RST_PIN, 1);
  41. DEV_Delay_ms(200);
  42. DEV_Digital_Write(EPD_RST_PIN, 0);
  43. DEV_Delay_ms(2);
  44. DEV_Digital_Write(EPD_RST_PIN, 1);
  45. DEV_Delay_ms(200);
  46. }
  47. /******************************************************************************
  48. function : send command
  49. parameter:
  50. Reg : Command register
  51. ******************************************************************************/
  52. static void EPD_4IN2B_V2_SendCommand(UBYTE Reg)
  53. {
  54. DEV_Digital_Write(EPD_DC_PIN, 0);
  55. DEV_Digital_Write(EPD_CS_PIN, 0);
  56. DEV_SPI_WriteByte(Reg);
  57. DEV_Digital_Write(EPD_CS_PIN, 1);
  58. }
  59. /******************************************************************************
  60. function : send data
  61. parameter:
  62. Data : Write data
  63. ******************************************************************************/
  64. static void EPD_4IN2B_V2_SendData(UBYTE Data)
  65. {
  66. DEV_Digital_Write(EPD_DC_PIN, 1);
  67. DEV_Digital_Write(EPD_CS_PIN, 0);
  68. DEV_SPI_WriteByte(Data);
  69. DEV_Digital_Write(EPD_CS_PIN, 1);
  70. }
  71. /******************************************************************************
  72. function : Wait until the busy_pin goes LOW
  73. parameter:
  74. ******************************************************************************/
  75. void EPD_4IN2B_V2_ReadBusy_old(void)
  76. {
  77. Debug("e-Paper busy\r\n");
  78. do{
  79. EPD_4IN2B_V2_SendCommand(0x71);
  80. DEV_Delay_ms(20);
  81. }while(!(DEV_Digital_Read(EPD_BUSY_PIN))); //0: busy, 1: idle
  82. DEV_Delay_ms(20);
  83. Debug("e-Paper busy release\r\n");
  84. }
  85. void EPD_4IN2B_V2_ReadBusy_new(void)
  86. {
  87. Debug("e-Paper busy\r\n");
  88. while(DEV_Digital_Read(EPD_BUSY_PIN) == 1) { //LOW: idle, HIGH: busy
  89. DEV_Delay_ms(10);
  90. }
  91. Debug("e-Paper busy release\r\n");
  92. }
  93. void EPD_4IN2B_V2_ReadBusy(void)
  94. {
  95. if(flag == 0)
  96. EPD_4IN2B_V2_ReadBusy_new();
  97. else
  98. EPD_4IN2B_V2_ReadBusy_old();
  99. }
  100. /******************************************************************************
  101. function : Setting the display window
  102. parameter:
  103. ******************************************************************************/
  104. static void EPD_4IN2B_V2_SetWindows(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
  105. {
  106. EPD_4IN2B_V2_SendCommand(0x44); // SET_RAM_X_ADDRESS_START_END_POSITION
  107. EPD_4IN2B_V2_SendData((Xstart>>3) & 0xFF);
  108. EPD_4IN2B_V2_SendData((Xend>>3) & 0xFF);
  109. EPD_4IN2B_V2_SendCommand(0x45); // SET_RAM_Y_ADDRESS_START_END_POSITION
  110. EPD_4IN2B_V2_SendData(Ystart & 0xFF);
  111. EPD_4IN2B_V2_SendData((Ystart >> 8) & 0xFF);
  112. EPD_4IN2B_V2_SendData(Yend & 0xFF);
  113. EPD_4IN2B_V2_SendData((Yend >> 8) & 0xFF);
  114. }
  115. /******************************************************************************
  116. function : Set Cursor
  117. parameter:
  118. ******************************************************************************/
  119. static void EPD_4IN2B_V2_SetCursor(UWORD Xstart, UWORD Ystart)
  120. {
  121. EPD_4IN2B_V2_SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  122. EPD_4IN2B_V2_SendData((Xstart>>3) & 0xFF);
  123. EPD_4IN2B_V2_SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  124. EPD_4IN2B_V2_SendData(Ystart & 0xFF);
  125. EPD_4IN2B_V2_SendData((Ystart >> 8) & 0xFF);
  126. }
  127. /******************************************************************************
  128. function : Initialize the e-Paper register
  129. parameter:
  130. ******************************************************************************/
  131. void EPD_4IN2B_V2_Init_old(void)
  132. {
  133. EPD_4IN2B_V2_Reset();
  134. EPD_4IN2B_V2_ReadBusy();
  135. EPD_4IN2B_V2_SendCommand(0x04); // soft reset
  136. EPD_4IN2B_V2_ReadBusy();
  137. EPD_4IN2B_V2_SendCommand(0x00); //BorderWavefrom
  138. EPD_4IN2B_V2_SendData(0x0F);
  139. }
  140. void EPD_4IN2B_V2_Init_new(void)
  141. {
  142. EPD_4IN2B_V2_Reset();
  143. EPD_4IN2B_V2_ReadBusy();
  144. EPD_4IN2B_V2_SendCommand(0x12); // soft reset
  145. EPD_4IN2B_V2_ReadBusy();
  146. // EPD_4IN2B_V2_SendCommand(0x01); //Driver output control
  147. // EPD_4IN2B_V2_SendData((EPD_4IN2B_V2_HEIGHT-1)%256);
  148. // EPD_4IN2B_V2_SendData((EPD_4IN2B_V2_HEIGHT-1)/256);
  149. // EPD_4IN2B_V2_SendData(0x00);
  150. EPD_4IN2B_V2_SendCommand(0x3C); //BorderWavefrom
  151. EPD_4IN2B_V2_SendData(0x05);
  152. EPD_4IN2B_V2_SendCommand(0x18); //Read built-in temperature sensor
  153. EPD_4IN2B_V2_SendData(0x80);
  154. EPD_4IN2B_V2_SendCommand(0x11); // data entry mode
  155. EPD_4IN2B_V2_SendData(0x03); // X-mode
  156. EPD_4IN2B_V2_SetWindows(0, 0, EPD_4IN2B_V2_WIDTH-1, EPD_4IN2B_V2_HEIGHT-1);
  157. EPD_4IN2B_V2_SetCursor(0, 0);
  158. EPD_4IN2B_V2_ReadBusy();
  159. }
  160. void EPD_4IN2B_V2_Init()
  161. {
  162. uint8_t i=0;
  163. EPD_4IN2B_V2_Reset();
  164. DEV_GPIO_Init();
  165. DEV_Digital_Write(EPD_DC_PIN, 0);
  166. DEV_SPI_SendData(0x2F);
  167. DEV_Delay_ms(50);
  168. DEV_Digital_Write(EPD_DC_PIN, 1);
  169. i = DEV_SPI_ReadData();
  170. printf("%02x\n",i);
  171. DEV_SPI_Init();
  172. if(i == 0x01)
  173. {
  174. flag = 0;
  175. EPD_4IN2B_V2_Init_new();
  176. }
  177. else
  178. {
  179. flag = 1;
  180. EPD_4IN2B_V2_Init_old();
  181. }
  182. }
  183. /******************************************************************************
  184. function : Clear screen
  185. parameter:
  186. ******************************************************************************/
  187. void EPD_4IN2B_V2_Clear_old(void)
  188. {
  189. UWORD Width, Height;
  190. Width = (EPD_4IN2B_V2_WIDTH % 8 == 0)? (EPD_4IN2B_V2_WIDTH / 8 ): (EPD_4IN2B_V2_WIDTH / 8 + 1);
  191. Height = EPD_4IN2B_V2_HEIGHT;
  192. EPD_4IN2B_V2_SendCommand(0x10);
  193. for (UWORD j = 0; j < Height; j++) {
  194. for (UWORD i = 0; i < Width; i++) {
  195. EPD_4IN2B_V2_SendData(0xFF);
  196. }
  197. }
  198. EPD_4IN2B_V2_SendCommand(0x13);
  199. for (UWORD j = 0; j < Height; j++) {
  200. for (UWORD i = 0; i < Width; i++) {
  201. EPD_4IN2B_V2_SendData(0x00);
  202. }
  203. }
  204. EPD_4IN2B_V2_SendCommand(0x12);
  205. DEV_Delay_ms(100);
  206. EPD_4IN2B_V2_ReadBusy();
  207. }
  208. void EPD_4IN2B_V2_Clear_new(void)
  209. {
  210. UWORD Width, Height;
  211. Width = (EPD_4IN2B_V2_WIDTH % 8 == 0)? (EPD_4IN2B_V2_WIDTH / 8 ): (EPD_4IN2B_V2_WIDTH / 8 + 1);
  212. Height = EPD_4IN2B_V2_HEIGHT;
  213. EPD_4IN2B_V2_SendCommand(0x24);
  214. for (UWORD j = 0; j < Height; j++) {
  215. for (UWORD i = 0; i < Width; i++) {
  216. EPD_4IN2B_V2_SendData(0xFF);
  217. }
  218. }
  219. EPD_4IN2B_V2_SendCommand(0x26);
  220. for (UWORD j = 0; j < Height; j++) {
  221. for (UWORD i = 0; i < Width; i++) {
  222. EPD_4IN2B_V2_SendData(0x00);
  223. }
  224. }
  225. EPD_4IN2B_V2_SendCommand(0x22);
  226. EPD_4IN2B_V2_SendData(0xF7);
  227. EPD_4IN2B_V2_SendCommand(0x20);
  228. EPD_4IN2B_V2_ReadBusy();
  229. }
  230. void EPD_4IN2B_V2_Clear(void)
  231. {
  232. if(flag == 0)
  233. EPD_4IN2B_V2_Clear_new();
  234. else
  235. EPD_4IN2B_V2_Clear_old();
  236. }
  237. /******************************************************************************
  238. function : Sends the image buffer in RAM to e-Paper and displays
  239. parameter:
  240. ******************************************************************************/
  241. void EPD_4IN2B_V2_Display_old(const UBYTE *blackimage, const UBYTE *ryimage)
  242. {
  243. UWORD Width, Height;
  244. Width = (EPD_4IN2B_V2_WIDTH % 8 == 0)? (EPD_4IN2B_V2_WIDTH / 8 ): (EPD_4IN2B_V2_WIDTH / 8 + 1);
  245. Height = EPD_4IN2B_V2_HEIGHT;
  246. EPD_4IN2B_V2_SendCommand(0x10);
  247. for (UWORD j = 0; j < Height; j++) {
  248. for (UWORD i = 0; i < Width; i++) {
  249. EPD_4IN2B_V2_SendData(blackimage[i + j * Width]);
  250. }
  251. }
  252. EPD_4IN2B_V2_SendCommand(0x13);
  253. for (UWORD j = 0; j < Height; j++) {
  254. for (UWORD i = 0; i < Width; i++) {
  255. EPD_4IN2B_V2_SendData(~ryimage[i + j * Width]);
  256. }
  257. }
  258. EPD_4IN2B_V2_SendCommand(0x12);
  259. DEV_Delay_ms(100);
  260. EPD_4IN2B_V2_ReadBusy();
  261. }
  262. void EPD_4IN2B_V2_Display_new(const UBYTE *blackimage, const UBYTE *ryimage)
  263. {
  264. UWORD Width, Height;
  265. Width = (EPD_4IN2B_V2_WIDTH % 8 == 0)? (EPD_4IN2B_V2_WIDTH / 8 ): (EPD_4IN2B_V2_WIDTH / 8 + 1);
  266. Height = EPD_4IN2B_V2_HEIGHT;
  267. EPD_4IN2B_V2_SendCommand(0x24);
  268. for (UWORD j = 0; j < Height; j++) {
  269. for (UWORD i = 0; i < Width; i++) {
  270. EPD_4IN2B_V2_SendData(blackimage[i + j * Width]);
  271. }
  272. }
  273. EPD_4IN2B_V2_SendCommand(0x26);
  274. for (UWORD j = 0; j < Height; j++) {
  275. for (UWORD i = 0; i < Width; i++) {
  276. EPD_4IN2B_V2_SendData(~ryimage[i + j * Width]);
  277. }
  278. }
  279. EPD_4IN2B_V2_SendCommand(0x22);
  280. EPD_4IN2B_V2_SendData(0xF7);
  281. EPD_4IN2B_V2_SendCommand(0x20);
  282. EPD_4IN2B_V2_ReadBusy();
  283. }
  284. void EPD_4IN2B_V2_Display(const UBYTE *blackimage, const UBYTE *ryimage)
  285. {
  286. if(flag == 0)
  287. EPD_4IN2B_V2_Display_new(blackimage, ryimage);
  288. else
  289. EPD_4IN2B_V2_Display_old(blackimage, ryimage);
  290. }
  291. /******************************************************************************
  292. function : Enter sleep mode
  293. parameter:
  294. ******************************************************************************/
  295. void EPD_4IN2B_V2_Sleep_old(void)
  296. {
  297. EPD_4IN2B_V2_SendCommand(0X50);
  298. EPD_4IN2B_V2_SendData(0xf7);
  299. EPD_4IN2B_V2_SendCommand(0x02);
  300. EPD_4IN2B_V2_ReadBusy();
  301. EPD_4IN2B_V2_SendCommand(0x07);
  302. EPD_4IN2B_V2_SendData(0XA5);
  303. }
  304. void EPD_4IN2B_V2_Sleep_new(void)
  305. {
  306. EPD_4IN2B_V2_SendCommand(0X10); //deep sleep
  307. EPD_4IN2B_V2_SendData(0x03);
  308. }
  309. void EPD_4IN2B_V2_Sleep(void)
  310. {
  311. if(flag == 0)
  312. EPD_4IN2B_V2_Sleep_new();
  313. else
  314. EPD_4IN2B_V2_Sleep_old();
  315. }