EPD_7in5b_HD.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*****************************************************************************
  2. * | File : EPD_7IN5B_HD_7in5.c
  3. * | Author : Waveshare team
  4. * | Function : Electronic paper driver
  5. * | Info :
  6. *----------------
  7. * | This version: HD.0
  8. * | Date : 2018-11-09
  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_7in5b_HD.h"
  31. #include "Debug.h"
  32. /******************************************************************************
  33. function : Software reset
  34. parameter:
  35. ******************************************************************************/
  36. static void EPD_7IN5B_HD_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_7IN5B_HD_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_7IN5B_HD_SendData(UBYTE Data)
  63. {
  64. DEV_Digital_Write(EPD_CS_PIN, 0);
  65. DEV_Digital_Write(EPD_DC_PIN, 1);
  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. void EPD_7IN5B_HD_WaitUntilIdle(void)
  74. {
  75. EPD_Busy_WaitUntil(0,0);
  76. // unsigned char count = 200;
  77. // Debug("e-Paper busy\r\n");
  78. // while(DEV_Digital_Read(EPD_BUSY_PIN)){
  79. // if(!(count--))
  80. // {
  81. // Debug("error: e-Paper busy timeout!!!\r\n");
  82. // break;
  83. // }
  84. // else
  85. // DEV_Delay_ms(100);
  86. // }
  87. // DEV_Delay_ms(200);
  88. // Debug("e-Paper busy release\r\n");
  89. }
  90. /******************************************************************************
  91. function : Turn On Display
  92. parameter:
  93. ******************************************************************************/
  94. static void EPD_7IN5B_HD_TurnOnDisplay(void)
  95. {
  96. EPD_7IN5B_HD_SendCommand(0x22);
  97. EPD_7IN5B_HD_SendData(0xC7); //Load LUT from MCU(0x32)
  98. EPD_7IN5B_HD_SendCommand(0x20);
  99. DEV_Delay_ms(200); //!!!The delay here is necessary, 200uS at least!!!
  100. EPD_7IN5B_HD_WaitUntilIdle(); //waiting for the electronic paper IC to release the idle signal
  101. }
  102. /******************************************************************************
  103. function : Initialize the e-Paper register
  104. parameter:
  105. ******************************************************************************/
  106. UBYTE EPD_7IN5B_HD_Init(UBYTE mode)
  107. {
  108. EPD_7IN5B_HD_Reset();
  109. EPD_7IN5B_HD_SendCommand(0x12); //SWRESET
  110. EPD_7IN5B_HD_WaitUntilIdle(); //waiting for the electronic paper IC to release the idle signal
  111. EPD_7IN5B_HD_SendCommand(0x46); // Auto Write RAM
  112. EPD_7IN5B_HD_SendData(0xF7);
  113. EPD_7IN5B_HD_WaitUntilIdle(); //waiting for the electronic paper IC to release the idle signal
  114. EPD_7IN5B_HD_SendCommand(0x47); // Auto Write RAM
  115. EPD_7IN5B_HD_SendData(0xF7);
  116. EPD_7IN5B_HD_WaitUntilIdle(); //waiting for the electronic paper IC to release the idle signal
  117. EPD_7IN5B_HD_SendCommand(0x0C); // Soft start setting
  118. EPD_7IN5B_HD_SendData(0xAE);
  119. EPD_7IN5B_HD_SendData(0xC7);
  120. EPD_7IN5B_HD_SendData(0xC3);
  121. EPD_7IN5B_HD_SendData(0xC0);
  122. EPD_7IN5B_HD_SendData(0x40);
  123. EPD_7IN5B_HD_SendCommand(0x01); // Set MUX as 527
  124. EPD_7IN5B_HD_SendData(0xAF);
  125. EPD_7IN5B_HD_SendData(0x02);
  126. EPD_7IN5B_HD_SendData(0x01);
  127. EPD_7IN5B_HD_SendCommand(0x11); // Data entry mode
  128. EPD_7IN5B_HD_SendData(0x01);
  129. EPD_7IN5B_HD_SendCommand(0x44);
  130. EPD_7IN5B_HD_SendData(0x00); // RAM x address start at 0
  131. EPD_7IN5B_HD_SendData(0x00);
  132. EPD_7IN5B_HD_SendData(0x6F); // RAM x address end at 36Fh -> 879
  133. EPD_7IN5B_HD_SendData(0x03);
  134. EPD_7IN5B_HD_SendCommand(0x45);
  135. EPD_7IN5B_HD_SendData(0xAF); // RAM y address start at 20Fh;
  136. EPD_7IN5B_HD_SendData(0x02);
  137. EPD_7IN5B_HD_SendData(0x00); // RAM y address end at 00h;
  138. EPD_7IN5B_HD_SendData(0x00);
  139. EPD_7IN5B_HD_SendCommand(0x3C); // VBD
  140. EPD_7IN5B_HD_SendData(0x01); // LUT1, for white
  141. EPD_7IN5B_HD_SendCommand(0x18);
  142. EPD_7IN5B_HD_SendData(0X80);
  143. EPD_7IN5B_HD_SendCommand(0x22);
  144. EPD_7IN5B_HD_SendData(0XB1); //Load Temperature and waveform setting.
  145. EPD_7IN5B_HD_SendCommand(0x20);
  146. EPD_7IN5B_HD_WaitUntilIdle(); //waiting for the electronic paper IC to release the idle signal
  147. EPD_7IN5B_HD_SendCommand(0x4E);
  148. EPD_7IN5B_HD_SendData(0x00);
  149. EPD_7IN5B_HD_SendData(0x00);
  150. EPD_7IN5B_HD_SendCommand(0x4F);
  151. EPD_7IN5B_HD_SendData(0xAF);
  152. EPD_7IN5B_HD_SendData(0x02);
  153. return 0;
  154. }
  155. /******************************************************************************
  156. function : Clear screen
  157. parameter:
  158. ******************************************************************************/
  159. void EPD_7IN5B_HD_Clear(void)
  160. {
  161. UWORD Width, Height;
  162. Width =(EPD_7IN5B_HD_WIDTH % 8 == 0)?(EPD_7IN5B_HD_WIDTH / 8 ):(EPD_7IN5B_HD_WIDTH / 8 + 1);
  163. Height = EPD_7IN5B_HD_HEIGHT;
  164. EPD_7IN5B_HD_SendCommand(0x4F);
  165. EPD_7IN5B_HD_SendData(0xAf);
  166. EPD_7IN5B_HD_SendData(0x02);
  167. UWORD i;
  168. EPD_7IN5B_HD_SendCommand(0x24);
  169. for(i=0; i<Width*Height; i++) {
  170. EPD_7IN5B_HD_SendData(0xff);
  171. }
  172. EPD_7IN5B_HD_SendCommand(0x26);
  173. for(i=0; i<Width*Height; i++) {
  174. EPD_7IN5B_HD_SendData(0x00);
  175. }
  176. EPD_7IN5B_HD_TurnOnDisplay();
  177. }
  178. void EPD_7IN5B_HD_ClearRed(void)
  179. {
  180. UWORD Width, Height;
  181. Width =(EPD_7IN5B_HD_WIDTH % 8 == 0)?(EPD_7IN5B_HD_WIDTH / 8 ):(EPD_7IN5B_HD_WIDTH / 8 + 1);
  182. Height = EPD_7IN5B_HD_HEIGHT;
  183. UWORD i;
  184. EPD_7IN5B_HD_SendCommand(0x4F);
  185. EPD_7IN5B_HD_SendData(0xAF);
  186. EPD_7IN5B_HD_SendData(0x02);
  187. EPD_7IN5B_HD_SendCommand(0x24);
  188. for(i=0; i<Width*Height; i++) {
  189. EPD_7IN5B_HD_SendData(0xff);
  190. }
  191. EPD_7IN5B_HD_SendCommand(0x26);
  192. for(i=0; i<Width*Height; i++) {
  193. EPD_7IN5B_HD_SendData(0xff);
  194. }
  195. EPD_7IN5B_HD_TurnOnDisplay();
  196. }
  197. void EPD_7IN5B_HD_ClearBlack(void)
  198. {
  199. UWORD Width, Height;
  200. Width =(EPD_7IN5B_HD_WIDTH % 8 == 0)?(EPD_7IN5B_HD_WIDTH / 8 ):(EPD_7IN5B_HD_WIDTH / 8 + 1);
  201. Height = EPD_7IN5B_HD_HEIGHT;
  202. UWORD i;
  203. EPD_7IN5B_HD_SendCommand(0x4F);
  204. EPD_7IN5B_HD_SendData(0xAF);
  205. EPD_7IN5B_HD_SendData(0x02);
  206. EPD_7IN5B_HD_SendCommand(0x24);
  207. for(i=0; i<Width*Height; i++) {
  208. EPD_7IN5B_HD_SendData(0x00);
  209. }
  210. EPD_7IN5B_HD_SendCommand(0x26);
  211. for(i=0; i<Width*Height; i++) {
  212. EPD_7IN5B_HD_SendData(0x00);
  213. }
  214. EPD_7IN5B_HD_TurnOnDisplay();
  215. }
  216. /******************************************************************************
  217. function : Sends the image buffer in RAM to e-Paper and displays
  218. parameter:
  219. ******************************************************************************/
  220. void EPD_7IN5B_HD_Display(const UBYTE *blackimage, const UBYTE *ryimage)
  221. {
  222. UDOUBLE Width, Height;
  223. Width =(EPD_7IN5B_HD_WIDTH % 8 == 0)?(EPD_7IN5B_HD_WIDTH / 8 ):(EPD_7IN5B_HD_WIDTH / 8 + 1);
  224. Height = EPD_7IN5B_HD_HEIGHT;
  225. EPD_7IN5B_HD_SendCommand(0x4F);
  226. EPD_7IN5B_HD_SendData(0xAF);
  227. EPD_7IN5B_HD_SendData(0x02);
  228. //send black data
  229. EPD_7IN5B_HD_SendCommand(0x24);
  230. for (UDOUBLE j = 0; j < Height; j++) {
  231. for (UDOUBLE i = 0; i < Width; i++) {
  232. EPD_7IN5B_HD_SendData(blackimage[i + j * Width]);
  233. }
  234. }
  235. //send red data
  236. EPD_7IN5B_HD_SendCommand(0x26);
  237. for (UDOUBLE j = 0; j < Height; j++) {
  238. for (UDOUBLE i = 0; i < Width; i++) {
  239. EPD_7IN5B_HD_SendData(~ryimage[i + j * Width]);
  240. }
  241. }
  242. EPD_7IN5B_HD_TurnOnDisplay();
  243. }
  244. void EPD_7IN5B_HD_DisplayPicture(const UBYTE *blackimage,UBYTE Block)
  245. {
  246. UDOUBLE Width, Height;
  247. Width =(EPD_7IN5B_HD_WIDTH % 8 == 0)?(EPD_7IN5B_HD_WIDTH / 8 ):(EPD_7IN5B_HD_WIDTH / 8 + 1);
  248. Height = EPD_7IN5B_HD_HEIGHT;
  249. if(Block == 0){
  250. EPD_7IN5B_HD_SendCommand(0x4F);
  251. EPD_7IN5B_HD_SendData(0xAF);
  252. EPD_7IN5B_HD_SendData(0x02);
  253. EPD_7IN5B_HD_SendCommand(0x24);
  254. for (UDOUBLE j = 0; j < Height/2; j++) {
  255. for (UDOUBLE i = 0; i < Width; i++) {
  256. EPD_7IN5B_HD_SendData(blackimage[i + j * Width]);
  257. }
  258. }
  259. }else if(Block == 1){
  260. for (UDOUBLE j = 0; j < Height/2; j++) {
  261. for (UDOUBLE i = 0; i < Width; i++) {
  262. EPD_7IN5B_HD_SendData(blackimage[i + j * Width]);
  263. }
  264. }
  265. }else if(Block == 2){
  266. EPD_7IN5B_HD_SendCommand(0x26);
  267. for (UDOUBLE j = 0; j < Height/2; j++) {
  268. for (UDOUBLE i = 0; i < Width; i++) {
  269. EPD_7IN5B_HD_SendData(~blackimage[i + j * Width]);
  270. }
  271. }
  272. }else if(Block == 3){
  273. for (UDOUBLE j = 0; j < Height/2; j++) {
  274. for (UDOUBLE i = 0; i < Width; i++) {
  275. EPD_7IN5B_HD_SendData(~blackimage[i + j * Width]);
  276. }
  277. }
  278. EPD_7IN5B_HD_TurnOnDisplay();
  279. }
  280. }
  281. /******************************************************************************
  282. function : Sends the image buffer in RAM to e-Paper and displays
  283. parameter: image_width and start_X :Must be a multiple of 8
  284. ******************************************************************************/
  285. void EPD_7IN5B_HD_DisplayImage(const UBYTE *blackimage, const UBYTE *ryimage,UDOUBLE start_X, UDOUBLE start_Y, UDOUBLE image_width, UDOUBLE image_high)
  286. {
  287. UDOUBLE Width, Height;
  288. Width =(EPD_7IN5B_HD_WIDTH % 8 == 0)?(EPD_7IN5B_HD_WIDTH / 8 ):(EPD_7IN5B_HD_WIDTH / 8 + 1);
  289. Height = EPD_7IN5B_HD_HEIGHT;
  290. EPD_7IN5B_HD_SendCommand(0x4F);
  291. EPD_7IN5B_HD_SendData(0xAF);
  292. EPD_7IN5B_HD_SendData(0x02);
  293. //send black data
  294. EPD_7IN5B_HD_SendCommand(0x24);
  295. for (UDOUBLE j = 0; j < Height; j++) {
  296. for (UDOUBLE i = 0; i < Width; i++) {
  297. if((i>=start_X/8) && (i<(start_X+image_width)/8) && (j>=start_Y) && (j<(start_Y+image_high))){
  298. EPD_7IN5B_HD_SendData(blackimage[i-start_X/8 + (j - start_Y) * image_width/8]);
  299. }else{
  300. EPD_7IN5B_HD_SendData(0xff);
  301. }
  302. }
  303. }
  304. //send red data
  305. EPD_7IN5B_HD_SendCommand(0x26);
  306. for (UDOUBLE j = 0; j < Height; j++) {
  307. for (UDOUBLE i = 0; i < Width; i++) {
  308. if((i>=start_X/8) && (i<(start_X+image_width)/8) && (j>=start_Y) && (j<(start_Y+image_high))){
  309. EPD_7IN5B_HD_SendData(~ryimage[i-start_X/8 + (j - start_Y) * image_width/8]);
  310. }else{
  311. EPD_7IN5B_HD_SendData(0x00);
  312. }
  313. }
  314. }
  315. EPD_7IN5B_HD_TurnOnDisplay();
  316. }
  317. /******************************************************************************
  318. function : Enter sleep mode
  319. parameter:
  320. ******************************************************************************/
  321. void EPD_7IN5B_HD_Sleep(void)
  322. {
  323. EPD_7IN5B_HD_SendCommand(0x10); //deep sleep
  324. EPD_7IN5B_HD_SendData(0x01);
  325. }