2
0

epd2in7.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /**
  2. * @filename : epd2in7.cpp
  3. * @brief : Implements for e-paper library
  4. * @author : Yehui from Waveshare
  5. *
  6. * Copyright (C) Waveshare August 18 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 "epd2in7.h"
  28. //////////////////////////////////////full screen update LUT////////////////////////////////////////////
  29. //0~3 gray
  30. static const unsigned char EPD_2in7_gray_lut_vcom[] =
  31. {
  32. 0x00 ,0x00,
  33. 0x00 ,0x0A ,0x00 ,0x00 ,0x00 ,0x01,
  34. 0x60 ,0x14 ,0x14 ,0x00 ,0x00 ,0x01,
  35. 0x00 ,0x14 ,0x00 ,0x00 ,0x00 ,0x01,
  36. 0x00 ,0x13 ,0x0A ,0x01 ,0x00 ,0x01,
  37. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  38. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  39. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  40. };
  41. //R21
  42. static const unsigned char EPD_2in7_gray_lut_ww[] ={
  43. 0x40 ,0x0A ,0x00 ,0x00 ,0x00 ,0x01,
  44. 0x90 ,0x14 ,0x14 ,0x00 ,0x00 ,0x01,
  45. 0x10 ,0x14 ,0x0A ,0x00 ,0x00 ,0x01,
  46. 0xA0 ,0x13 ,0x01 ,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. };
  51. //R22H r
  52. static const unsigned char EPD_2in7_gray_lut_bw[] ={
  53. 0x40 ,0x0A ,0x00 ,0x00 ,0x00 ,0x01,
  54. 0x90 ,0x14 ,0x14 ,0x00 ,0x00 ,0x01,
  55. 0x00 ,0x14 ,0x0A ,0x00 ,0x00 ,0x01,
  56. 0x99 ,0x0C ,0x01 ,0x03 ,0x04 ,0x01,
  57. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  58. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  59. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  60. };
  61. //R23H w
  62. static const unsigned char EPD_2in7_gray_lut_wb[] ={
  63. 0x40 ,0x0A ,0x00 ,0x00 ,0x00 ,0x01,
  64. 0x90 ,0x14 ,0x14 ,0x00 ,0x00 ,0x01,
  65. 0x00 ,0x14 ,0x0A ,0x00 ,0x00 ,0x01,
  66. 0x99 ,0x0B ,0x04 ,0x04 ,0x01 ,0x01,
  67. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  68. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  69. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  70. };
  71. //R24H b
  72. static const unsigned char EPD_2in7_gray_lut_bb[] ={
  73. 0x80 ,0x0A ,0x00 ,0x00 ,0x00 ,0x01,
  74. 0x90 ,0x14 ,0x14 ,0x00 ,0x00 ,0x01,
  75. 0x20 ,0x14 ,0x0A ,0x00 ,0x00 ,0x01,
  76. 0x50 ,0x13 ,0x01 ,0x00 ,0x00 ,0x01,
  77. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  78. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  79. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  80. };
  81. /* END OF FILE */
  82. Epd::~Epd() {
  83. };
  84. Epd::Epd() {
  85. reset_pin = RST_PIN;
  86. dc_pin = DC_PIN;
  87. cs_pin = CS_PIN;
  88. busy_pin = BUSY_PIN;
  89. width = EPD_WIDTH;
  90. height = EPD_HEIGHT;
  91. };
  92. int Epd::Init(void) {
  93. /* this calls the peripheral hardware interface, see epdif */
  94. if (IfInit() != 0) {
  95. return -1;
  96. }
  97. /* EPD hardware init start */
  98. Reset();
  99. SendCommand(POWER_SETTING);
  100. SendData(0x03); // VDS_EN, VDG_EN
  101. SendData(0x00); // VCOM_HV, VGHL_LV[1], VGHL_LV[0]
  102. SendData(0x2b); // VDH
  103. SendData(0x2b); // VDL
  104. SendData(0x09); // VDHR
  105. SendCommand(BOOSTER_SOFT_START);
  106. SendData(0x07);
  107. SendData(0x07);
  108. SendData(0x17);
  109. // Power optimization
  110. SendCommand(0xF8);
  111. SendData(0x60);
  112. SendData(0xA5);
  113. // Power optimization
  114. SendCommand(0xF8);
  115. SendData(0x89);
  116. SendData(0xA5);
  117. // Power optimization
  118. SendCommand(0xF8);
  119. SendData(0x90);
  120. SendData(0x00);
  121. // Power optimization
  122. SendCommand(0xF8);
  123. SendData(0x93);
  124. SendData(0x2A);
  125. // Power optimization
  126. SendCommand(0xF8);
  127. SendData(0xA0);
  128. SendData(0xA5);
  129. // Power optimization
  130. SendCommand(0xF8);
  131. SendData(0xA1);
  132. SendData(0x00);
  133. // Power optimization
  134. SendCommand(0xF8);
  135. SendData(0x73);
  136. SendData(0x41);
  137. SendCommand(PARTIAL_DISPLAY_REFRESH);
  138. SendData(0x00);
  139. SendCommand(POWER_ON);
  140. WaitUntilIdle();
  141. SendCommand(PANEL_SETTING);
  142. SendData(0xAF); //KW-BF KWR-AF BWROTP 0f
  143. SendCommand(PLL_CONTROL);
  144. SendData(0x3A); //3A 100HZ 29 150Hz 39 200HZ 31 171HZ
  145. SendCommand(VCM_DC_SETTING_REGISTER);
  146. SendData(0x12);
  147. DelayMs(2);
  148. SetLut();
  149. /* EPD hardware init end */
  150. return 0;
  151. }
  152. void Epd::Init_4Gray(void)
  153. {
  154. Reset();
  155. SendCommand(0x01); //POWER SETTING
  156. SendData (0x03);
  157. SendData (0x00);
  158. SendData (0x2b);
  159. SendData (0x2b);
  160. SendCommand(0x06); //booster soft start
  161. SendData (0x07); //A
  162. SendData (0x07); //B
  163. SendData (0x17); //C
  164. SendCommand(0xF8); //boost??
  165. SendData (0x60);
  166. SendData (0xA5);
  167. SendCommand(0xF8); //boost??
  168. SendData (0x89);
  169. SendData (0xA5);
  170. SendCommand(0xF8); //boost??
  171. SendData (0x90);
  172. SendData (0x00);
  173. SendCommand(0xF8); //boost??
  174. SendData (0x93);
  175. SendData (0x2A);
  176. SendCommand(0xF8); //boost??
  177. SendData (0xa0);
  178. SendData (0xa5);
  179. SendCommand(0xF8); //boost??
  180. SendData (0xa1);
  181. SendData (0x00);
  182. SendCommand(0xF8); //boost??
  183. SendData (0x73);
  184. SendData (0x41);
  185. SendCommand(0x16);
  186. SendData(0x00);
  187. SendCommand(0x04);
  188. WaitUntilIdle();
  189. SendCommand(0x00); //panel setting
  190. SendData(0xbf); //KW-BF KWR-AF BWROTP 0f
  191. SendCommand(0x30); //PLL setting
  192. SendData (0x90); //100hz
  193. SendCommand(0x61); //resolution setting
  194. SendData (0x00); //176
  195. SendData (0xb0);
  196. SendData (0x01); //264
  197. SendData (0x08);
  198. SendCommand(0x82); //vcom_DC setting
  199. SendData (0x12);
  200. SendCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  201. SendData(0x97);
  202. }
  203. /**
  204. * @brief: basic function for sending commands
  205. */
  206. void Epd::SendCommand(unsigned char command) {
  207. DigitalWrite(dc_pin, LOW);
  208. SpiTransfer(command);
  209. }
  210. /**
  211. * @brief: basic function for sending data
  212. */
  213. void Epd::SendData(unsigned char data) {
  214. DigitalWrite(dc_pin, HIGH);
  215. SpiTransfer(data);
  216. }
  217. /**
  218. * @brief: Wait until the busy_pin goes HIGH
  219. */
  220. void Epd::WaitUntilIdle(void) {
  221. Serial.print("e-Paper busy\r\n");
  222. while(DigitalRead(busy_pin) == 0) { //0: busy, 1: idle
  223. DelayMs(100);
  224. }
  225. Serial.print("e-Paper busy release\r\n");
  226. }
  227. /**
  228. * @brief: module reset.
  229. * often used to awaken the module in deep sleep,
  230. * see Epd::Sleep();
  231. */
  232. void Epd::Reset(void) {
  233. DigitalWrite(reset_pin, HIGH);
  234. DelayMs(200);
  235. DigitalWrite(reset_pin, LOW);
  236. DelayMs(10);
  237. DigitalWrite(reset_pin, HIGH);
  238. DelayMs(200);
  239. }
  240. /**
  241. * @brief: set the look-up tables
  242. */
  243. void Epd::SetLut(void) {
  244. unsigned int count;
  245. SendCommand(LUT_FOR_VCOM); //vcom
  246. for(count = 0; count < 44; count++) {
  247. SendData(lut_vcom_dc[count]);
  248. }
  249. SendCommand(LUT_WHITE_TO_WHITE); //ww --
  250. for(count = 0; count < 42; count++) {
  251. SendData(lut_ww[count]);
  252. }
  253. SendCommand(LUT_BLACK_TO_WHITE); //bw r
  254. for(count = 0; count < 42; count++) {
  255. SendData(lut_bw[count]);
  256. }
  257. SendCommand(LUT_WHITE_TO_BLACK); //wb w
  258. for(count = 0; count < 42; count++) {
  259. SendData(lut_bb[count]);
  260. }
  261. SendCommand(LUT_BLACK_TO_BLACK); //bb b
  262. for(count = 0; count < 42; count++) {
  263. SendData(lut_wb[count]);
  264. }
  265. }
  266. void Epd::gray_SetLut(void)
  267. {
  268. unsigned int count;
  269. SendCommand(0x20); //vcom
  270. for(count=0;count<44;count++)
  271. {SendData(EPD_2in7_gray_lut_vcom[count]);}
  272. SendCommand(0x21); //red not use
  273. for(count=0;count<42;count++)
  274. {SendData(EPD_2in7_gray_lut_ww[count]);}
  275. SendCommand(0x22); //bw r
  276. for(count=0;count<42;count++)
  277. {SendData(EPD_2in7_gray_lut_bw[count]);}
  278. SendCommand(0x23); //wb w
  279. for(count=0;count<42;count++)
  280. {SendData(EPD_2in7_gray_lut_wb[count]);}
  281. SendCommand(0x24); //bb b
  282. for(count=0;count<42;count++)
  283. {SendData(EPD_2in7_gray_lut_bb[count]);}
  284. SendCommand(0x25); //vcom
  285. for(count=0;count<42;count++)
  286. {SendData(EPD_2in7_gray_lut_ww[count]);}
  287. }
  288. /**
  289. * @brief: transmit partial data to the SRAM
  290. */
  291. void Epd::TransmitPartialData(const unsigned char* buffer, int x, int y, int w, int l) {
  292. if (buffer != NULL) {
  293. SendCommand(PARTIAL_DATA_START_TRANSMISSION_2);
  294. SendData(x >> 8);
  295. SendData(x & 0xf8); // x should be the multiple of 8, the last 3 bit will always be ignored
  296. SendData(y >> 8);
  297. SendData(y & 0xff);
  298. SendData(w >> 8);
  299. SendData(w & 0xf8); // w (width) should be the multiple of 8, the last 3 bit will always be ignored
  300. SendData(l >> 8);
  301. SendData(l & 0xff);
  302. DelayMs(2);
  303. for(int i = 0; i < w / 8 * l; i++) {
  304. SendData(buffer[i]);
  305. }
  306. DelayMs(2);
  307. }
  308. }
  309. /**
  310. * @brief: refreshes a specific part of the display
  311. */
  312. void Epd::RefreshPartial(int x, int y, int w, int l) {
  313. SendCommand(PARTIAL_DISPLAY_REFRESH);
  314. SendData(x >> 8);
  315. SendData(x & 0xf8); // x should be the multiple of 8, the last 3 bit will always be ignored
  316. SendData(y >> 8);
  317. SendData(y & 0xff);
  318. SendData(w >> 8);
  319. SendData(w & 0xf8); // w (width) should be the multiple of 8, the last 3 bit will always be ignored
  320. SendData(l >> 8);
  321. SendData(l & 0xff);
  322. WaitUntilIdle();
  323. }
  324. /**
  325. * @brief: refresh and displays the frame
  326. */
  327. void Epd::DisplayFrame(const unsigned char* frame_buffer) {
  328. if (frame_buffer != NULL) {
  329. SendCommand(DATA_START_TRANSMISSION_1);
  330. DelayMs(2);
  331. for(int i = 0; i < 5808; i++) {
  332. SendData(0xff);
  333. }
  334. DelayMs(2);
  335. SendCommand(DATA_START_TRANSMISSION_2);
  336. DelayMs(2);
  337. for(int i = 0; i < 5808; i++) {
  338. SendData(pgm_read_byte(&frame_buffer[i]));
  339. }
  340. DelayMs(2);
  341. SendCommand(0x12);
  342. DelayMs(200);
  343. WaitUntilIdle();
  344. }
  345. }
  346. void Epd::Display4Gray(const unsigned char *Image)
  347. {
  348. int i,j,k;
  349. unsigned char temp1,temp2,temp3;
  350. SendCommand(0x10);
  351. for(i=0;i<5808;i++) //5808*4 46464
  352. {
  353. temp3=0;
  354. for(j=0;j<2;j++)
  355. {
  356. temp1 = pgm_read_byte(&Image[i*2+j]);
  357. for(k=0;k<2;k++)
  358. {
  359. temp2 = temp1&0xC0 ;
  360. if(temp2 == 0xC0)
  361. temp3 |= 0x01;//white
  362. else if(temp2 == 0x00)
  363. temp3 |= 0x00; //black
  364. else if(temp2 == 0x80)
  365. temp3 |= 0x01; //gray1
  366. else //0x40
  367. temp3 |= 0x00; //gray2
  368. temp3 <<= 1;
  369. temp1 <<= 2;
  370. temp2 = temp1&0xC0 ;
  371. if(temp2 == 0xC0) //white
  372. temp3 |= 0x01;
  373. else if(temp2 == 0x00) //black
  374. temp3 |= 0x00;
  375. else if(temp2 == 0x80)
  376. temp3 |= 0x01; //gray1
  377. else //0x40
  378. temp3 |= 0x00; //gray2
  379. if(j!=1 || k!=1)
  380. temp3 <<= 1;
  381. temp1 <<= 2;
  382. }
  383. }
  384. SendData(temp3);
  385. }
  386. // new data
  387. SendCommand(0x13);
  388. for(i=0;i<5808;i++) //5808*4 46464
  389. {
  390. temp3=0;
  391. for(j=0;j<2;j++)
  392. {
  393. temp1 = pgm_read_byte(&Image[i*2+j]);
  394. for(k=0;k<2;k++)
  395. {
  396. temp2 = temp1&0xC0 ;
  397. if(temp2 == 0xC0)
  398. temp3 |= 0x01;//white
  399. else if(temp2 == 0x00)
  400. temp3 |= 0x00; //black
  401. else if(temp2 == 0x80)
  402. temp3 |= 0x00; //gray1
  403. else //0x40
  404. temp3 |= 0x01; //gray2
  405. temp3 <<= 1;
  406. temp1 <<= 2;
  407. temp2 = temp1&0xC0 ;
  408. if(temp2 == 0xC0) //white
  409. temp3 |= 0x01;
  410. else if(temp2 == 0x00) //black
  411. temp3 |= 0x00;
  412. else if(temp2 == 0x80)
  413. temp3 |= 0x00; //gray1
  414. else //0x40
  415. temp3 |= 0x01; //gray2
  416. if(j!=1 || k!=1)
  417. temp3 <<= 1;
  418. temp1 <<= 2;
  419. }
  420. }
  421. SendData(temp3);
  422. }
  423. gray_SetLut();
  424. SendCommand(0x12);
  425. DelayMs(200);
  426. WaitUntilIdle();
  427. }
  428. /**
  429. * @brief: clear the frame data from the SRAM, this won't refresh the display
  430. */
  431. void Epd::ClearFrame(void) {
  432. SendCommand(DATA_START_TRANSMISSION_1);
  433. DelayMs(2);
  434. for(int i = 0; i < width * height / 8; i++) {
  435. SendData(0xFF);
  436. }
  437. DelayMs(2);
  438. SendCommand(DATA_START_TRANSMISSION_2);
  439. DelayMs(2);
  440. for(int i = 0; i < width * height / 8; i++) {
  441. SendData(0xFF);
  442. }
  443. DelayMs(2);
  444. }
  445. /**
  446. * @brief: This displays the frame data from SRAM
  447. */
  448. void Epd::DisplayFrame(void) {
  449. SendCommand(DISPLAY_REFRESH);
  450. DelayMs(200);
  451. WaitUntilIdle();
  452. }
  453. /**
  454. * @brief: After this command is transmitted, the chip would enter the deep-sleep mode to save power.
  455. * The deep sleep mode would return to standby by hardware reset. The only one parameter is a
  456. * check code, the command would be executed if check code = 0xA5.
  457. * You can use Epd::Reset() to awaken and use Epd::Init() to initialize.
  458. */
  459. void Epd::Sleep() {
  460. SendCommand(DEEP_SLEEP);
  461. SendData(0xa5);
  462. }
  463. const unsigned char lut_vcom_dc[] = {
  464. 0x00, 0x00,
  465. 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x05,
  466. 0x00, 0x32, 0x32, 0x00, 0x00, 0x02,
  467. 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x05,
  468. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  469. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  470. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  471. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  472. };
  473. //R21H
  474. const unsigned char lut_ww[] = {
  475. 0x50, 0x0F, 0x0F, 0x00, 0x00, 0x05,
  476. 0x60, 0x32, 0x32, 0x00, 0x00, 0x02,
  477. 0xA0, 0x0F, 0x0F, 0x00, 0x00, 0x05,
  478. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  479. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  480. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  481. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  482. };
  483. //R22H r
  484. const unsigned char lut_bw[] =
  485. {
  486. 0x50, 0x0F, 0x0F, 0x00, 0x00, 0x05,
  487. 0x60, 0x32, 0x32, 0x00, 0x00, 0x02,
  488. 0xA0, 0x0F, 0x0F, 0x00, 0x00, 0x05,
  489. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  490. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  491. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  492. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  493. };
  494. //R24H b
  495. const unsigned char lut_bb[] =
  496. {
  497. 0xA0, 0x0F, 0x0F, 0x00, 0x00, 0x05,
  498. 0x60, 0x32, 0x32, 0x00, 0x00, 0x02,
  499. 0x50, 0x0F, 0x0F, 0x00, 0x00, 0x05,
  500. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  501. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  502. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  503. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  504. };
  505. //R23H w
  506. const unsigned char lut_wb[] =
  507. {
  508. 0xA0, 0x0F, 0x0F, 0x00, 0x00, 0x05,
  509. 0x60, 0x32, 0x32, 0x00, 0x00, 0x02,
  510. 0x50, 0x0F, 0x0F, 0x00, 0x00, 0x05,
  511. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  512. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  513. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  514. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  515. };