EPD_4in2b_V2_old.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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_old.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_SendData(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_SendData(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_Digital_Write(EPD_DC_PIN, 0);
  165. DEV_SPI_SendData(0x2F);
  166. DEV_Delay_ms(50);
  167. DEV_Digital_Write(EPD_DC_PIN, 1);
  168. i = DEV_SPI_ReadData();
  169. // printf("%02x\n",i);
  170. if(i == 0x01)
  171. {
  172. flag = 0;
  173. EPD_4IN2B_V2_Init_new();
  174. }
  175. else
  176. {
  177. flag = 1;
  178. EPD_4IN2B_V2_Init_old();
  179. }
  180. }
  181. /******************************************************************************
  182. function : Clear screen
  183. parameter:
  184. ******************************************************************************/
  185. void EPD_4IN2B_V2_Clear_old(void)
  186. {
  187. UWORD Width, Height;
  188. Width = (EPD_4IN2B_V2_WIDTH % 8 == 0)? (EPD_4IN2B_V2_WIDTH / 8 ): (EPD_4IN2B_V2_WIDTH / 8 + 1);
  189. Height = EPD_4IN2B_V2_HEIGHT;
  190. EPD_4IN2B_V2_SendCommand(0x10);
  191. for (UWORD j = 0; j < Height; j++) {
  192. for (UWORD i = 0; i < Width; i++) {
  193. EPD_4IN2B_V2_SendData(0xFF);
  194. }
  195. }
  196. EPD_4IN2B_V2_SendCommand(0x13);
  197. for (UWORD j = 0; j < Height; j++) {
  198. for (UWORD i = 0; i < Width; i++) {
  199. EPD_4IN2B_V2_SendData(0xFF);
  200. }
  201. }
  202. EPD_4IN2B_V2_SendCommand(0x12);
  203. DEV_Delay_ms(100);
  204. EPD_4IN2B_V2_ReadBusy();
  205. }
  206. void EPD_4IN2B_V2_Clear_new(void)
  207. {
  208. UWORD Width, Height;
  209. Width = (EPD_4IN2B_V2_WIDTH % 8 == 0)? (EPD_4IN2B_V2_WIDTH / 8 ): (EPD_4IN2B_V2_WIDTH / 8 + 1);
  210. Height = EPD_4IN2B_V2_HEIGHT;
  211. EPD_4IN2B_V2_SendCommand(0x24);
  212. for (UWORD j = 0; j < Height; j++) {
  213. for (UWORD i = 0; i < Width; i++) {
  214. EPD_4IN2B_V2_SendData(0xFF);
  215. }
  216. }
  217. EPD_4IN2B_V2_SendCommand(0x26);
  218. for (UWORD j = 0; j < Height; j++) {
  219. for (UWORD i = 0; i < Width; i++) {
  220. EPD_4IN2B_V2_SendData(0x00);
  221. }
  222. }
  223. EPD_4IN2B_V2_SendCommand(0x22);
  224. EPD_4IN2B_V2_SendData(0xF7);
  225. EPD_4IN2B_V2_SendCommand(0x20);
  226. EPD_4IN2B_V2_ReadBusy();
  227. }
  228. void EPD_4IN2B_V2_Clear(void)
  229. {
  230. if(flag == 0)
  231. EPD_4IN2B_V2_Clear_new();
  232. else
  233. EPD_4IN2B_V2_Clear_old();
  234. }
  235. /******************************************************************************
  236. function : Sends the image buffer in RAM to e-Paper and displays
  237. parameter:
  238. ******************************************************************************/
  239. void EPD_4IN2B_V2_Display_old(const UBYTE *blackimage, const UBYTE *ryimage)
  240. {
  241. UWORD Width, Height;
  242. Width = (EPD_4IN2B_V2_WIDTH % 8 == 0)? (EPD_4IN2B_V2_WIDTH / 8 ): (EPD_4IN2B_V2_WIDTH / 8 + 1);
  243. Height = EPD_4IN2B_V2_HEIGHT;
  244. EPD_4IN2B_V2_SendCommand(0x10);
  245. for (UWORD j = 0; j < Height; j++) {
  246. for (UWORD i = 0; i < Width; i++) {
  247. EPD_4IN2B_V2_SendData(blackimage[i + j * Width]);
  248. }
  249. }
  250. EPD_4IN2B_V2_SendCommand(0x13);
  251. for (UWORD j = 0; j < Height; j++) {
  252. for (UWORD i = 0; i < Width; i++) {
  253. EPD_4IN2B_V2_SendData(ryimage[i + j * Width]);
  254. }
  255. }
  256. EPD_4IN2B_V2_SendCommand(0x12);
  257. DEV_Delay_ms(100);
  258. EPD_4IN2B_V2_ReadBusy();
  259. }
  260. void EPD_4IN2B_V2_Display_new(const UBYTE *blackimage, const UBYTE *ryimage)
  261. {
  262. UWORD Width, Height;
  263. Width = (EPD_4IN2B_V2_WIDTH % 8 == 0)? (EPD_4IN2B_V2_WIDTH / 8 ): (EPD_4IN2B_V2_WIDTH / 8 + 1);
  264. Height = EPD_4IN2B_V2_HEIGHT;
  265. EPD_4IN2B_V2_SendCommand(0x24);
  266. for (UWORD j = 0; j < Height; j++) {
  267. for (UWORD i = 0; i < Width; i++) {
  268. EPD_4IN2B_V2_SendData(blackimage[i + j * Width]);
  269. }
  270. }
  271. EPD_4IN2B_V2_SendCommand(0x26);
  272. for (UWORD j = 0; j < Height; j++) {
  273. for (UWORD i = 0; i < Width; i++) {
  274. EPD_4IN2B_V2_SendData(~ryimage[i + j * Width]);
  275. }
  276. }
  277. EPD_4IN2B_V2_SendCommand(0x22);
  278. EPD_4IN2B_V2_SendData(0xF7);
  279. EPD_4IN2B_V2_SendCommand(0x20);
  280. EPD_4IN2B_V2_ReadBusy();
  281. }
  282. void EPD_4IN2B_V2_Display(const UBYTE *blackimage, const UBYTE *ryimage)
  283. {
  284. if(flag == 0)
  285. EPD_4IN2B_V2_Display_new(blackimage, ryimage);
  286. else
  287. EPD_4IN2B_V2_Display_old(blackimage, ryimage);
  288. }
  289. /******************************************************************************
  290. function : Enter sleep mode
  291. parameter:
  292. ******************************************************************************/
  293. void EPD_4IN2B_V2_Sleep_old(void)
  294. {
  295. EPD_4IN2B_V2_SendCommand(0X50);
  296. EPD_4IN2B_V2_SendData(0xf7);
  297. EPD_4IN2B_V2_SendCommand(0x02);
  298. EPD_4IN2B_V2_ReadBusy();
  299. EPD_4IN2B_V2_SendCommand(0x07);
  300. EPD_4IN2B_V2_SendData(0XA5);
  301. }
  302. void EPD_4IN2B_V2_Sleep_new(void)
  303. {
  304. EPD_4IN2B_V2_SendCommand(0X10); //deep sleep
  305. EPD_4IN2B_V2_SendData(0x03);
  306. }
  307. void EPD_4IN2B_V2_Sleep(void)
  308. {
  309. if(flag == 0)
  310. EPD_4IN2B_V2_Sleep_new();
  311. else
  312. EPD_4IN2B_V2_Sleep_old();
  313. }