EPD_4in2b_V2_old.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. static uint8_t flag=0;
  33. /******************************************************************************
  34. function : Software reset
  35. parameter:
  36. ******************************************************************************/
  37. static void EPD_4IN2B_V2_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(2);
  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_4IN2B_V2_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_4IN2B_V2_SendData(UBYTE Data)
  64. {
  65. DEV_Digital_Write(EPD_DC_PIN, 1);
  66. DEV_Digital_Write(EPD_CS_PIN, 0);
  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_4IN2B_V2_ReadBusy_old_1(void)
  75. {
  76. Debug("e-Paper busy\r\n");
  77. do{
  78. EPD_4IN2B_V2_SendCommand(0x71);
  79. DEV_Delay_ms(20);
  80. }while(!(DEV_Digital_Read(EPD_BUSY_PIN))); //0: busy, 1: idle
  81. DEV_Delay_ms(20);
  82. Debug("e-Paper busy release\r\n");
  83. }
  84. void EPD_4IN2B_V2_ReadBusy_new_1(void)
  85. {
  86. Debug("e-Paper busy\r\n");
  87. while(DEV_Digital_Read(EPD_BUSY_PIN) == 1) { //LOW: idle, HIGH: busy
  88. DEV_Delay_ms(10);
  89. }
  90. Debug("e-Paper busy release\r\n");
  91. }
  92. void EPD_4IN2B_V2_ReadBusy_1(void)
  93. {
  94. if(flag == 0)
  95. EPD_4IN2B_V2_ReadBusy_new_1();
  96. else
  97. EPD_4IN2B_V2_ReadBusy_old_1();
  98. }
  99. /******************************************************************************
  100. function : Setting the display window
  101. parameter:
  102. ******************************************************************************/
  103. static void EPD_4IN2B_V2_SetWindows(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
  104. {
  105. EPD_4IN2B_V2_SendCommand(0x44); // SET_RAM_X_ADDRESS_START_END_POSITION
  106. EPD_4IN2B_V2_SendData((Xstart>>3) & 0xFF);
  107. EPD_4IN2B_V2_SendData((Xend>>3) & 0xFF);
  108. EPD_4IN2B_V2_SendCommand(0x45); // SET_RAM_Y_ADDRESS_START_END_POSITION
  109. EPD_4IN2B_V2_SendData(Ystart & 0xFF);
  110. EPD_4IN2B_V2_SendData((Ystart >> 8) & 0xFF);
  111. EPD_4IN2B_V2_SendData(Yend & 0xFF);
  112. EPD_4IN2B_V2_SendData((Yend >> 8) & 0xFF);
  113. }
  114. /******************************************************************************
  115. function : Set Cursor
  116. parameter:
  117. ******************************************************************************/
  118. static void EPD_4IN2B_V2_SetCursor(UWORD Xstart, UWORD Ystart)
  119. {
  120. EPD_4IN2B_V2_SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  121. EPD_4IN2B_V2_SendData((Xstart>>3) & 0xFF);
  122. EPD_4IN2B_V2_SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  123. EPD_4IN2B_V2_SendData(Ystart & 0xFF);
  124. EPD_4IN2B_V2_SendData((Ystart >> 8) & 0xFF);
  125. }
  126. /******************************************************************************
  127. function : Initialize the e-Paper register
  128. parameter:
  129. ******************************************************************************/
  130. void EPD_4IN2B_V2_Init_old_1(void)
  131. {
  132. EPD_4IN2B_V2_Reset();
  133. EPD_4IN2B_V2_ReadBusy_1();
  134. EPD_4IN2B_V2_SendCommand(0x04); // soft reset
  135. EPD_4IN2B_V2_ReadBusy_1();
  136. EPD_4IN2B_V2_SendCommand(0x00); //BorderWavefrom
  137. EPD_4IN2B_V2_SendData(0x0F);
  138. }
  139. void EPD_4IN2B_V2_Init_new_1(void)
  140. {
  141. EPD_4IN2B_V2_Reset();
  142. EPD_4IN2B_V2_ReadBusy_1();
  143. EPD_4IN2B_V2_SendCommand(0x12); // soft reset
  144. EPD_4IN2B_V2_ReadBusy_1();
  145. // EPD_4IN2B_V2_SendCommand(0x01); //Driver output control
  146. // EPD_4IN2B_V2_SendData((EPD_4IN2B_V2_HEIGHT-1)%256);
  147. // EPD_4IN2B_V2_SendData((EPD_4IN2B_V2_HEIGHT-1)/256);
  148. // EPD_4IN2B_V2_SendData(0x00);
  149. EPD_4IN2B_V2_SendCommand(0x3C); //BorderWavefrom
  150. EPD_4IN2B_V2_SendData(0x05);
  151. EPD_4IN2B_V2_SendCommand(0x18); //Read built-in temperature sensor
  152. EPD_4IN2B_V2_SendData(0x80);
  153. EPD_4IN2B_V2_SendCommand(0x11); // data entry mode
  154. EPD_4IN2B_V2_SendData(0x03); // X-mode
  155. EPD_4IN2B_V2_SetWindows(0, 0, EPD_4IN2B_V2_WIDTH-1, EPD_4IN2B_V2_HEIGHT-1);
  156. EPD_4IN2B_V2_SetCursor(0, 0);
  157. EPD_4IN2B_V2_ReadBusy_1();
  158. }
  159. void EPD_4IN2B_V2_Init_1()
  160. {
  161. uint8_t i=0;
  162. EPD_4IN2B_V2_Reset();
  163. DEV_GPIO_Init();
  164. DEV_Digital_Write(EPD_DC_PIN, 0);
  165. DEV_SPI_SendByte(0x2F);
  166. DEV_Delay_ms(50);
  167. DEV_Digital_Write(EPD_DC_PIN, 1);
  168. i = DEV_SPI_ReadByte();
  169. printf("%02x\n",i);
  170. DEV_SPI_Init();
  171. if(i == 0x01)
  172. {
  173. flag = 0;
  174. EPD_4IN2B_V2_Init_new_1();
  175. }
  176. else
  177. {
  178. flag = 1;
  179. EPD_4IN2B_V2_Init_old_1();
  180. }
  181. }
  182. /******************************************************************************
  183. function : Clear screen
  184. parameter:
  185. ******************************************************************************/
  186. void EPD_4IN2B_V2_Clear_old_1(void)
  187. {
  188. UWORD Width, Height;
  189. Width = (EPD_4IN2B_V2_WIDTH % 8 == 0)? (EPD_4IN2B_V2_WIDTH / 8 ): (EPD_4IN2B_V2_WIDTH / 8 + 1);
  190. Height = EPD_4IN2B_V2_HEIGHT;
  191. EPD_4IN2B_V2_SendCommand(0x10);
  192. for (UWORD j = 0; j < Height; j++) {
  193. for (UWORD i = 0; i < Width; i++) {
  194. EPD_4IN2B_V2_SendData(0xFF);
  195. }
  196. }
  197. EPD_4IN2B_V2_SendCommand(0x13);
  198. for (UWORD j = 0; j < Height; j++) {
  199. for (UWORD i = 0; i < Width; i++) {
  200. EPD_4IN2B_V2_SendData(0xFF);
  201. }
  202. }
  203. EPD_4IN2B_V2_SendCommand(0x12);
  204. DEV_Delay_ms(100);
  205. EPD_4IN2B_V2_ReadBusy_1();
  206. }
  207. void EPD_4IN2B_V2_Clear_new_1(void)
  208. {
  209. UWORD Width, Height;
  210. Width = (EPD_4IN2B_V2_WIDTH % 8 == 0)? (EPD_4IN2B_V2_WIDTH / 8 ): (EPD_4IN2B_V2_WIDTH / 8 + 1);
  211. Height = EPD_4IN2B_V2_HEIGHT;
  212. EPD_4IN2B_V2_SendCommand(0x24);
  213. for (UWORD j = 0; j < Height; j++) {
  214. for (UWORD i = 0; i < Width; i++) {
  215. EPD_4IN2B_V2_SendData(0xFF);
  216. }
  217. }
  218. EPD_4IN2B_V2_SendCommand(0x26);
  219. for (UWORD j = 0; j < Height; j++) {
  220. for (UWORD i = 0; i < Width; i++) {
  221. EPD_4IN2B_V2_SendData(0x00);
  222. }
  223. }
  224. EPD_4IN2B_V2_SendCommand(0x22);
  225. EPD_4IN2B_V2_SendData(0xF7);
  226. EPD_4IN2B_V2_SendCommand(0x20);
  227. EPD_4IN2B_V2_ReadBusy_1();
  228. }
  229. void EPD_4IN2B_V2_Clear_1(void)
  230. {
  231. if(flag == 0)
  232. EPD_4IN2B_V2_Clear_new_1();
  233. else
  234. EPD_4IN2B_V2_Clear_old_1();
  235. }
  236. /******************************************************************************
  237. function : Sends the image buffer in RAM to e-Paper and displays
  238. parameter:
  239. ******************************************************************************/
  240. void EPD_4IN2B_V2_Display_old_1(const UBYTE *image, char flag_1)
  241. {
  242. UWORD Width, Height;
  243. Width = (EPD_4IN2B_V2_WIDTH % 8 == 0)? (EPD_4IN2B_V2_WIDTH / 8 ): (EPD_4IN2B_V2_WIDTH / 8 + 1);
  244. Height = EPD_4IN2B_V2_HEIGHT;
  245. if(flag_1 == 0)
  246. {
  247. EPD_4IN2B_V2_SendCommand(0x10);
  248. for (UWORD j = 0; j < Height; j++) {
  249. for (UWORD i = 0; i < Width; i++) {
  250. EPD_4IN2B_V2_SendData(image[i + j * Width]);
  251. }
  252. }
  253. }
  254. else
  255. {
  256. EPD_4IN2B_V2_SendCommand(0x13);
  257. for (UWORD j = 0; j < Height; j++) {
  258. for (UWORD i = 0; i < Width; i++) {
  259. EPD_4IN2B_V2_SendData(image[i + j * Width]);
  260. }
  261. }
  262. EPD_4IN2B_V2_SendCommand(0x12);
  263. DEV_Delay_ms(100);
  264. EPD_4IN2B_V2_ReadBusy_1();
  265. }
  266. }
  267. void EPD_4IN2B_V2_Display_new_1(const UBYTE *image, char flag_1)
  268. {
  269. UWORD Width, Height;
  270. Width = (EPD_4IN2B_V2_WIDTH % 8 == 0)? (EPD_4IN2B_V2_WIDTH / 8 ): (EPD_4IN2B_V2_WIDTH / 8 + 1);
  271. Height = EPD_4IN2B_V2_HEIGHT;
  272. if(flag_1 == 0)
  273. {
  274. EPD_4IN2B_V2_SendCommand(0x24);
  275. for (UWORD j = 0; j < Height; j++) {
  276. for (UWORD i = 0; i < Width; i++) {
  277. EPD_4IN2B_V2_SendData(image[i + j * Width]);
  278. }
  279. }
  280. }
  281. else
  282. {
  283. EPD_4IN2B_V2_SendCommand(0x26);
  284. for (UWORD j = 0; j < Height; j++) {
  285. for (UWORD i = 0; i < Width; i++) {
  286. EPD_4IN2B_V2_SendData(~image[i + j * Width]);
  287. }
  288. }
  289. EPD_4IN2B_V2_SendCommand(0x22);
  290. EPD_4IN2B_V2_SendData(0xF7);
  291. EPD_4IN2B_V2_SendCommand(0x20);
  292. EPD_4IN2B_V2_ReadBusy_1();
  293. }
  294. }
  295. void EPD_4IN2B_V2_Display_1(const UBYTE *image, char flag_1)
  296. {
  297. if(flag == 0)
  298. EPD_4IN2B_V2_Display_new_1(image, flag_1);
  299. else
  300. EPD_4IN2B_V2_Display_old_1(image, flag_1);
  301. }
  302. /******************************************************************************
  303. function : Enter sleep mode
  304. parameter:
  305. ******************************************************************************/
  306. void EPD_4IN2B_V2_Sleep_old_1(void)
  307. {
  308. EPD_4IN2B_V2_SendCommand(0X50);
  309. EPD_4IN2B_V2_SendData(0xf7);
  310. EPD_4IN2B_V2_SendCommand(0x02);
  311. EPD_4IN2B_V2_ReadBusy_1();
  312. EPD_4IN2B_V2_SendCommand(0x07);
  313. EPD_4IN2B_V2_SendData(0XA5);
  314. }
  315. void EPD_4IN2B_V2_Sleep_new_1(void)
  316. {
  317. EPD_4IN2B_V2_SendCommand(0X10); //deep sleep
  318. EPD_4IN2B_V2_SendData(0x03);
  319. }
  320. void EPD_4IN2B_V2_Sleep_1(void)
  321. {
  322. if(flag == 0)
  323. EPD_4IN2B_V2_Sleep_new_1();
  324. else
  325. EPD_4IN2B_V2_Sleep_old_1();
  326. }