epd7in5_V2.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**
  2. * @filename : epd7in5.cpp
  3. * @brief : Implements for e-paper library
  4. * @author : Yehui from Waveshare
  5. *
  6. * Copyright (C) Waveshare August 10 2017
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documnetation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdlib.h>
  27. #include "epd7in5_V2.h"
  28. unsigned char Voltage_Frame_7IN5_V2[]={
  29. 0x6, 0x3F, 0x3F, 0x11, 0x24, 0x7, 0x17,
  30. };
  31. unsigned char LUT_VCOM_7IN5_V2[]={
  32. 0x0, 0xF, 0xF, 0x0, 0x0, 0x1,
  33. 0x0, 0xF, 0x1, 0xF, 0x1, 0x2,
  34. 0x0, 0xF, 0xF, 0x0, 0x0, 0x1,
  35. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  36. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  37. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  38. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  39. };
  40. unsigned char LUT_WW_7IN5_V2[]={
  41. 0x10, 0xF, 0xF, 0x0, 0x0, 0x1,
  42. 0x84, 0xF, 0x1, 0xF, 0x1, 0x2,
  43. 0x20, 0xF, 0xF, 0x0, 0x0, 0x1,
  44. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  45. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  46. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  47. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  48. };
  49. unsigned char LUT_BW_7IN5_V2[]={
  50. 0x10, 0xF, 0xF, 0x0, 0x0, 0x1,
  51. 0x84, 0xF, 0x1, 0xF, 0x1, 0x2,
  52. 0x20, 0xF, 0xF, 0x0, 0x0, 0x1,
  53. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  54. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  55. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  56. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  57. };
  58. unsigned char LUT_WB_7IN5_V2[]={
  59. 0x80, 0xF, 0xF, 0x0, 0x0, 0x1,
  60. 0x84, 0xF, 0x1, 0xF, 0x1, 0x2,
  61. 0x40, 0xF, 0xF, 0x0, 0x0, 0x1,
  62. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  63. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  64. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  65. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  66. };
  67. unsigned char LUT_BB_7IN5_V2[]={
  68. 0x80, 0xF, 0xF, 0x0, 0x0, 0x1,
  69. 0x84, 0xF, 0x1, 0xF, 0x1, 0x2,
  70. 0x40, 0xF, 0xF, 0x0, 0x0, 0x1,
  71. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  72. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  73. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  74. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  75. };
  76. Epd::~Epd() {
  77. };
  78. Epd::Epd() {
  79. reset_pin = RST_PIN;
  80. dc_pin = DC_PIN;
  81. cs_pin = CS_PIN;
  82. busy_pin = BUSY_PIN;
  83. width = EPD_WIDTH;
  84. height = EPD_HEIGHT;
  85. };
  86. int Epd::Init(void) {
  87. if (IfInit() != 0) {
  88. return -1;
  89. }
  90. Reset();
  91. // SendCommand(0x01);
  92. // SendData(0x07);
  93. // SendData(0x07);
  94. // SendData(0x3f);
  95. // SendData(0x3f);
  96. // SendCommand(0x04);
  97. // DelayMs(100);
  98. // WaitUntilIdle();
  99. // SendCommand(0X00); //PANNEL SETTING
  100. // SendData(0x1F); //KW-3f KWR-2F BWROTP 0f BWOTP 1f
  101. // SendCommand(0x61); //tres
  102. // SendData(0x03); //source 800
  103. // SendData(0x20);
  104. // SendData(0x01); //gate 480
  105. // SendData(0xE0);
  106. // SendCommand(0X15);
  107. // SendData(0x00);
  108. // SendCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  109. // SendData(0x10);
  110. // SendData(0x07);
  111. // SendCommand(0X60); //TCON SETTING
  112. // SendData(0x22);
  113. SendCommand(0x01); // power setting
  114. SendData(0x17); // 1-0=11: internal power
  115. SendData(*(Voltage_Frame_7IN5_V2+6)); // VGH&VGL
  116. SendData(*(Voltage_Frame_7IN5_V2+1)); // VSH
  117. SendData(*(Voltage_Frame_7IN5_V2+2)); // VSL
  118. SendData(*(Voltage_Frame_7IN5_V2+3)); // VSHR
  119. SendCommand(0x82); // VCOM DC Setting
  120. SendData(*(Voltage_Frame_7IN5_V2+4)); // VCOM
  121. SendCommand(0x06); // Booster Setting
  122. SendData(0x27);
  123. SendData(0x27);
  124. SendData(0x2F);
  125. SendData(0x17);
  126. SendCommand(0x30); // OSC Setting
  127. SendData(*(Voltage_Frame_7IN5_V2+0)); // 2-0=100: N=4 ; 5-3=111: M=7 ; 3C=50Hz 3A=100HZ
  128. SendCommand(0x04); //POWER ON
  129. DelayMs(100);
  130. WaitUntilIdle();
  131. SendCommand(0X00); //PANNEL SETTING
  132. SendData(0x3F); //KW-3f KWR-2F BWROTP 0f BWOTP 1f
  133. SendCommand(0x61); //tres
  134. SendData(0x03); //source 800
  135. SendData(0x20);
  136. SendData(0x01); //gate 480
  137. SendData(0xE0);
  138. SendCommand(0X15);
  139. SendData(0x00);
  140. SendCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  141. SendData(0x10);
  142. SendData(0x00);
  143. SendCommand(0X60); //TCON SETTING
  144. SendData(0x22);
  145. SendCommand(0x65); // Resolution setting
  146. SendData(0x00);
  147. SendData(0x00);//800*480
  148. SendData(0x00);
  149. SendData(0x00);
  150. SetLut_by_host(LUT_VCOM_7IN5_V2, LUT_WW_7IN5_V2, LUT_BW_7IN5_V2, LUT_WB_7IN5_V2, LUT_BB_7IN5_V2);
  151. return 0;
  152. }
  153. /**
  154. * @brief: basic function for sending commands
  155. */
  156. void Epd::SendCommand(unsigned char command) {
  157. DigitalWrite(dc_pin, LOW);
  158. SpiTransfer(command);
  159. }
  160. /**
  161. * @brief: basic function for sending data
  162. */
  163. void Epd::SendData(unsigned char data) {
  164. DigitalWrite(dc_pin, HIGH);
  165. SpiTransfer(data);
  166. }
  167. /**
  168. * @brief: Wait until the busy_pin goes HIGH
  169. */
  170. void Epd::WaitUntilIdle(void) {
  171. unsigned char busy;
  172. do{
  173. SendCommand(0x71);
  174. busy = DigitalRead(busy_pin);
  175. }while(busy == 0);
  176. DelayMs(20);
  177. }
  178. /**
  179. * @brief: module reset.
  180. * often used to awaken the module in deep sleep,
  181. * see Epd::Sleep();
  182. */
  183. void Epd::Reset(void) {
  184. DigitalWrite(reset_pin, LOW); //module reset
  185. DelayMs(4);
  186. DigitalWrite(reset_pin, HIGH);
  187. DelayMs(20);
  188. }
  189. void Epd::DisplayFrame(const unsigned char* frame_buffer) {
  190. SendCommand(0x13);
  191. for (unsigned long j = 0; j < height; j++) {
  192. for (unsigned long i = 0; i < width; i++) {
  193. SendData(~frame_buffer[i + j * width]);
  194. }
  195. }
  196. SendCommand(0x12);
  197. DelayMs(100);
  198. WaitUntilIdle();
  199. }
  200. void Epd::Displaypart(const unsigned char* pbuffer, unsigned long xStart, unsigned long yStart,unsigned long Picture_Width,unsigned long Picture_Height) {
  201. SendCommand(0x13);
  202. int * padd;
  203. // xStart = xStart/8;
  204. // xStart = xStart*8;
  205. for (unsigned long j = 0; j < height; j++) {
  206. for (unsigned long i = 0; i < width/8; i++) {
  207. if( (j>=yStart) && (j<yStart+Picture_Height) && (i*8>=xStart) && (i*8<xStart+Picture_Width)){
  208. SendData(~(pgm_read_byte(&(pbuffer[i-xStart/8 + (Picture_Width)/8*(j-yStart)]))) );
  209. // SendData(0xff);
  210. }else {
  211. SendData(0x00);
  212. }
  213. }
  214. }
  215. SendCommand(0x12);
  216. DelayMs(100);
  217. WaitUntilIdle();
  218. }
  219. void Epd::SetLut_by_host(unsigned char* lut_vcom, unsigned char* lut_ww, unsigned char* lut_bw, unsigned char* lut_wb, unsigned char* lut_bb)
  220. {
  221. unsigned char count;
  222. SendCommand(0x20); //VCOM
  223. for(count=0; count<60; count++)
  224. SendData(lut_vcom[count]);
  225. SendCommand(0x21); //LUTBW
  226. for(count=0; count<60; count++)
  227. SendData(lut_ww[count]);
  228. SendCommand(0x22); //LUTBW
  229. for(count=0; count<60; count++)
  230. SendData(lut_bw[count]);
  231. SendCommand(0x23); //LUTWB
  232. for(count=0; count<60; count++)
  233. SendData(lut_wb[count]);
  234. SendCommand(0x24); //LUTBB
  235. for(count=0; count<60; count++)
  236. SendData(lut_bb[count]);
  237. }
  238. /**
  239. * @brief: After this command is transmitted, the chip would enter the
  240. * deep-sleep mode to save power.
  241. * The deep sleep mode would return to standby by hardware reset.
  242. * The only one parameter is a check code, the command would be
  243. * executed if check code = 0xA5.
  244. * You can use EPD_Reset() to awaken
  245. */
  246. void Epd::Sleep(void) {
  247. SendCommand(0X02);
  248. WaitUntilIdle();
  249. SendCommand(0X07);
  250. SendData(0xA5);
  251. }
  252. void Epd::Clear(void) {
  253. SendCommand(0x10);
  254. for(unsigned long i=0; i<height*width; i++) {
  255. SendData(0x00);
  256. }
  257. SendCommand(0x13);
  258. for(unsigned long i=0; i<height*width; i++) {
  259. SendData(0x00);
  260. }
  261. SendCommand(0x12);
  262. DelayMs(100);
  263. WaitUntilIdle();
  264. }
  265. /* END OF FILE */