2
0

EPD_1in02d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*****************************************************************************
  2. * | File : EPD_1in02.c
  3. * | Author : Waveshare team
  4. * | Function : Electronic paper driver
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2019-03-12
  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_1in02d.h"
  31. /**
  32. * full screen update LUT
  33. **/
  34. const unsigned char lut_w1[] =
  35. {
  36. 0x60 ,0x5A ,0x5A ,0x00 ,0x00 ,0x01 ,
  37. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  38. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  39. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  40. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  41. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  42. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  43. };
  44. const unsigned char lut_b1[] =
  45. {
  46. 0x90 ,0x5A ,0x5A ,0x00 ,0x00 ,0x01 ,
  47. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  48. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  49. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  50. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  51. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  52. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  53. };
  54. /**
  55. * partial screen update LUT
  56. **/
  57. const unsigned char lut_w[] =
  58. {
  59. 0x60 ,0x01 ,0x01 ,0x00 ,0x00 ,0x01 ,
  60. 0x80 ,0x0f ,0x00 ,0x00 ,0x00 ,0x01 ,
  61. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  62. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  63. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  64. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  65. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  66. };
  67. const unsigned char lut_b[] =
  68. {
  69. 0x90 ,0x01 ,0x01 ,0x00 ,0x00 ,0x01 ,
  70. 0x40 ,0x0f ,0x00 ,0x00 ,0x00 ,0x01 ,
  71. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  72. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  73. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  74. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  75. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
  76. };
  77. /******************************************************************************
  78. function : Software reset
  79. parameter:
  80. ******************************************************************************/
  81. static void EPD_Reset(void)
  82. {
  83. DEV_Digital_Write(EPD_RST_PIN, 0);// Module reset
  84. DEV_Delay_ms(20);
  85. DEV_Digital_Write(EPD_RST_PIN, 1);
  86. DEV_Delay_ms(20);
  87. }
  88. /******************************************************************************
  89. function : send command
  90. parameter:
  91. command : Command register
  92. ******************************************************************************/
  93. static void EPD_SendCommand(UBYTE command)
  94. {
  95. DEV_Digital_Write(EPD_CS_PIN, 0);
  96. DEV_Digital_Write(EPD_DC_PIN, 0);// command write
  97. DEV_SPI_WriteByte(command);
  98. DEV_Digital_Write(EPD_CS_PIN, 1);
  99. }
  100. /******************************************************************************
  101. function : send data
  102. parameter:
  103. Data : Write data
  104. ******************************************************************************/
  105. static void EPD_SendData(UBYTE Data)
  106. {
  107. DEV_Digital_Write(EPD_CS_PIN, 0);
  108. DEV_Digital_Write(EPD_DC_PIN, 1); // command write
  109. DEV_SPI_WriteByte(Data);
  110. DEV_Digital_Write(EPD_CS_PIN, 1);
  111. }
  112. /******************************************************************************
  113. function : LUT download
  114. ******************************************************************************/
  115. void EPD_SetFulltReg(void)
  116. {
  117. unsigned int count;
  118. EPD_SendCommand(0x23);
  119. for(count=0;count<42;count++)
  120. {EPD_SendData(lut_w1[count]);}
  121. EPD_SendCommand(0x24);
  122. for(count=0;count<42;count++)
  123. {EPD_SendData(lut_b1[count]);}
  124. }
  125. /******************************************************************************
  126. function : LUT download
  127. ******************************************************************************/
  128. void EPD_SetPartReg(void)
  129. {
  130. unsigned int count;
  131. EPD_SendCommand(0x23);
  132. for(count=0;count<42;count++){
  133. EPD_SendData(lut_w[count]);
  134. }
  135. EPD_SendCommand(0x24);
  136. for(count=0;count<42;count++){
  137. EPD_SendData(lut_b[count]);
  138. }
  139. }
  140. /******************************************************************************
  141. function : Wait until the busy_pin goes LOW
  142. ******************************************************************************/
  143. void EPD_WaitUntilIdle(void)
  144. {
  145. unsigned char busy;
  146. do
  147. {
  148. EPD_SendCommand(0x71);
  149. busy = DEV_Digital_Read(EPD_BUSY_PIN);
  150. busy =!(busy & 0x01);
  151. }
  152. while(busy);
  153. DEV_Delay_ms(200);
  154. }
  155. /******************************************************************************
  156. function : Turn On Display
  157. ******************************************************************************/
  158. void EPD_TurnOnDisplay(void)
  159. {
  160. EPD_SendCommand(0x04); //power on
  161. EPD_WaitUntilIdle();
  162. EPD_SendCommand(0x12); //Start refreshing the screen
  163. EPD_WaitUntilIdle();
  164. EPD_SendCommand(0x02);
  165. EPD_WaitUntilIdle(); //power off
  166. }
  167. /******************************************************************************
  168. function :Initialize the e-Paper register
  169. ******************************************************************************/
  170. UBYTE EPD_Init(void)
  171. {
  172. EPD_Reset();
  173. EPD_SendCommand(0xD2);
  174. EPD_SendData(0x3F);
  175. EPD_SendCommand(0x00);
  176. EPD_SendData (0x6F); //from outside
  177. EPD_SendCommand(0x01); //power setting
  178. EPD_SendData (0x03);
  179. EPD_SendData (0x00);
  180. EPD_SendData (0x2b);
  181. EPD_SendData (0x2b);
  182. EPD_SendCommand(0x06); //Configuring the charge pump
  183. EPD_SendData(0x3f);
  184. EPD_SendCommand(0x2A); //Setting XON and the options of LUT
  185. EPD_SendData(0x00);
  186. EPD_SendData(0x00);
  187. EPD_SendCommand(0x30); //Set the clock frequency
  188. EPD_SendData(0x13); //50Hz
  189. EPD_SendCommand(0x50); //Set VCOM and data output interval
  190. EPD_SendData(0x57);
  191. EPD_SendCommand(0x60); //Set The non-overlapping period of Gate and Source.
  192. EPD_SendData(0x22);
  193. EPD_SendCommand(0x61); //resolution setting
  194. EPD_SendData (0x50); //source 128
  195. EPD_SendData (0x80);
  196. EPD_SendCommand(0x82); //sets VCOM_DC value
  197. EPD_SendData(0x12); //-1v
  198. EPD_SendCommand(0xe3);//Set POWER SAVING
  199. EPD_SendData(0x33);
  200. EPD_SetFulltReg();
  201. EPD_SendCommand(0x04);//Set POWER SAVING
  202. EPD_WaitUntilIdle();
  203. return 0;
  204. }
  205. /******************************************************************************
  206. function :Partial refresh initialization e-paper
  207. ******************************************************************************/
  208. void EPD_Part_Init(void)
  209. {
  210. EPD_Reset();
  211. EPD_SendCommand(0xD2);
  212. EPD_SendData(0x3F);
  213. EPD_SendCommand(0x00);
  214. EPD_SendData (0x6F); //from outside
  215. EPD_SendCommand(0x01); //power setting
  216. EPD_SendData (0x03);
  217. EPD_SendData (0x00);
  218. EPD_SendData (0x2b);
  219. EPD_SendData (0x2b);
  220. EPD_SendCommand(0x06); //Configuring the charge pump
  221. EPD_SendData(0x3f);
  222. EPD_SendCommand(0x2A); //Setting XON and the options of LUT
  223. EPD_SendData(0x00);
  224. EPD_SendData(0x00);
  225. EPD_SendCommand(0x30); //Set the clock frequency
  226. EPD_SendData(0x17);
  227. EPD_SendCommand(0x50); //Set VCOM and data output interval
  228. EPD_SendData(0xf2);
  229. EPD_SendCommand(0x60); //Set The non-overlapping period of Gate and Source.
  230. EPD_SendData(0x22);
  231. EPD_SendCommand(0x82); //Set VCOM_DC value
  232. EPD_SendData(0x12);//-1v
  233. EPD_SendCommand(0xe3);//Set POWER SAVING
  234. EPD_SendData(0x33);
  235. EPD_SetPartReg();
  236. EPD_SendCommand(0x04);//Set POWER SAVING
  237. EPD_WaitUntilIdle();
  238. }
  239. /******************************************************************************
  240. function : Clear screen
  241. ******************************************************************************/
  242. void EPD_Clear(void)
  243. {
  244. unsigned int i;
  245. EPD_SendCommand(0x10);
  246. for(i=0;i<1280;i++){
  247. EPD_SendData(0X00);
  248. }
  249. EPD_SendCommand(0x13); //Transfer new data
  250. for(i=0;i<1280;i++){
  251. EPD_SendData(0xff);
  252. }
  253. EPD_TurnOnDisplay();
  254. }
  255. /******************************************************************************
  256. function : Sends the image buffer in RAM to e-Paper and displays
  257. parameter:
  258. Image :Displayed data
  259. ******************************************************************************/
  260. void EPD_Display_Image(UBYTE *Image)
  261. {
  262. UWORD Width;
  263. Width = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  264. //EPD_Init();
  265. EPD_SendCommand(0x10);
  266. for (UWORD j = 0; j < EPD_HEIGHT; j++) {
  267. for (UWORD i = 0; i < Width; i++) {
  268. EPD_SendData(0xff);
  269. }
  270. }
  271. EPD_SendCommand(0x13);
  272. for (UWORD j = 0; j < EPD_HEIGHT; j++) {
  273. for (UWORD i = 0; i < Width; i++) {
  274. EPD_SendData(~pgm_read_byte(&Image[i + j * Width]));
  275. }
  276. }
  277. EPD_TurnOnDisplay();
  278. }
  279. void EPD_Display(UBYTE *Image)
  280. {
  281. UWORD Width;
  282. Width = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  283. //EPD_Init();
  284. EPD_SendCommand(0x10);
  285. for (UWORD j = 0; j < EPD_HEIGHT; j++) {
  286. for (UWORD i = 0; i < Width; i++) {
  287. EPD_SendData(0xff);
  288. }
  289. }
  290. EPD_SendCommand(0x13);
  291. for (UWORD j = 0; j < EPD_HEIGHT; j++) {
  292. for (UWORD i = 0; i < Width; i++) {
  293. EPD_SendData(~(Image[i + j * Width]));
  294. }
  295. }
  296. EPD_TurnOnDisplay();
  297. }
  298. /******************************************************************************
  299. function : Sends the image buffer in RAM to e-Paper and displays
  300. parameter:
  301. old_Image: Last displayed data
  302. Image2 : New data
  303. ******************************************************************************/
  304. void EPD_DisplayPartial(UBYTE *old_Image, UBYTE *Image)
  305. {
  306. /* Set partial Windows */
  307. EPD_SendCommand(0x91); //This command makes the display enter partial mode
  308. EPD_SendCommand(0x90); //resolution setting
  309. EPD_SendData(0); //x-start
  310. EPD_SendData(79); //x-end
  311. EPD_SendData(0);
  312. EPD_SendData(127); //y-end
  313. EPD_SendData(0x00);
  314. UWORD Width;
  315. Width = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  316. /* send data */
  317. EPD_SendCommand(0x10);
  318. for (UWORD j = 0; j < EPD_HEIGHT; j++) {
  319. for (UWORD i = 0; i < Width; i++) {
  320. EPD_SendData(old_Image[i + j * Width]);
  321. }
  322. }
  323. EPD_SendCommand(0x13);
  324. for (UWORD j = 0; j < EPD_HEIGHT; j++) {
  325. for (UWORD i = 0; i < Width; i++) {
  326. EPD_SendData(Image[i + j * Width]);
  327. }
  328. }
  329. /* Set partial refresh */
  330. EPD_TurnOnDisplay();
  331. }
  332. /******************************************************************************
  333. function : Enter sleep mode
  334. ******************************************************************************/
  335. void EPD_Sleep(void)
  336. {
  337. EPD_SendCommand(0X02); //power off
  338. EPD_WaitUntilIdle();
  339. EPD_SendCommand(0X07); //deep sleep
  340. EPD_SendData(0xA5);
  341. DEV_Digital_Write(EPD_RST_PIN, 0);// Module reset
  342. }