epd2in7.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. while(DigitalRead(busy_pin) == 0) { //0: busy, 1: idle
  222. DelayMs(100);
  223. }
  224. }
  225. /**
  226. * @brief: module reset.
  227. * often used to awaken the module in deep sleep,
  228. * see Epd::Sleep();
  229. */
  230. void Epd::Reset(void) {
  231. DigitalWrite(reset_pin, LOW);
  232. DelayMs(20);
  233. DigitalWrite(reset_pin, HIGH);
  234. DelayMs(200);
  235. }
  236. /**
  237. * @brief: set the look-up tables
  238. */
  239. void Epd::SetLut(void) {
  240. unsigned int count;
  241. SendCommand(LUT_FOR_VCOM); //vcom
  242. for(count = 0; count < 44; count++) {
  243. SendData(lut_vcom_dc[count]);
  244. }
  245. SendCommand(LUT_WHITE_TO_WHITE); //ww --
  246. for(count = 0; count < 42; count++) {
  247. SendData(lut_ww[count]);
  248. }
  249. SendCommand(LUT_BLACK_TO_WHITE); //bw r
  250. for(count = 0; count < 42; count++) {
  251. SendData(lut_bw[count]);
  252. }
  253. SendCommand(LUT_WHITE_TO_BLACK); //wb w
  254. for(count = 0; count < 42; count++) {
  255. SendData(lut_bb[count]);
  256. }
  257. SendCommand(LUT_BLACK_TO_BLACK); //bb b
  258. for(count = 0; count < 42; count++) {
  259. SendData(lut_wb[count]);
  260. }
  261. }
  262. void Epd::gray_SetLut(void)
  263. {
  264. unsigned int count;
  265. SendCommand(0x20); //vcom
  266. for(count=0;count<44;count++)
  267. {SendData(EPD_2in7_gray_lut_vcom[count]);}
  268. SendCommand(0x21); //red not use
  269. for(count=0;count<42;count++)
  270. {SendData(EPD_2in7_gray_lut_ww[count]);}
  271. SendCommand(0x22); //bw r
  272. for(count=0;count<42;count++)
  273. {SendData(EPD_2in7_gray_lut_bw[count]);}
  274. SendCommand(0x23); //wb w
  275. for(count=0;count<42;count++)
  276. {SendData(EPD_2in7_gray_lut_wb[count]);}
  277. SendCommand(0x24); //bb b
  278. for(count=0;count<42;count++)
  279. {SendData(EPD_2in7_gray_lut_bb[count]);}
  280. SendCommand(0x25); //vcom
  281. for(count=0;count<42;count++)
  282. {SendData(EPD_2in7_gray_lut_ww[count]);}
  283. }
  284. /**
  285. * @brief: transmit partial data to the SRAM
  286. */
  287. void Epd::TransmitPartialData(const unsigned char* buffer, int x, int y, int w, int l) {
  288. if (buffer != NULL) {
  289. SendCommand(PARTIAL_DATA_START_TRANSMISSION_2);
  290. SendData(x >> 8);
  291. SendData(x & 0xf8); // x should be the multiple of 8, the last 3 bit will always be ignored
  292. SendData(y >> 8);
  293. SendData(y & 0xff);
  294. SendData(w >> 8);
  295. SendData(w & 0xf8); // w (width) should be the multiple of 8, the last 3 bit will always be ignored
  296. SendData(l >> 8);
  297. SendData(l & 0xff);
  298. DelayMs(2);
  299. for(int i = 0; i < w / 8 * l; i++) {
  300. SendData(buffer[i]);
  301. }
  302. DelayMs(2);
  303. }
  304. }
  305. /**
  306. * @brief: refreshes a specific part of the display
  307. */
  308. void Epd::RefreshPartial(int x, int y, int w, int l) {
  309. SendCommand(PARTIAL_DISPLAY_REFRESH);
  310. SendData(x >> 8);
  311. SendData(x & 0xf8); // x should be the multiple of 8, the last 3 bit will always be ignored
  312. SendData(y >> 8);
  313. SendData(y & 0xff);
  314. SendData(w >> 8);
  315. SendData(w & 0xf8); // w (width) should be the multiple of 8, the last 3 bit will always be ignored
  316. SendData(l >> 8);
  317. SendData(l & 0xff);
  318. WaitUntilIdle();
  319. }
  320. /**
  321. * @brief: refresh and displays the frame
  322. */
  323. void Epd::DisplayFrame(const unsigned char* frame_buffer) {
  324. if (frame_buffer != NULL) {
  325. SendCommand(DATA_START_TRANSMISSION_1);
  326. DelayMs(2);
  327. for(int i = 0; i < this->width / 8 * this->height; i++) {
  328. SendData(0xFF);
  329. }
  330. DelayMs(2);
  331. SendCommand(DATA_START_TRANSMISSION_2);
  332. DelayMs(2);
  333. for(int i = 0; i < this->width / 8 * this->height; i++) {
  334. SendData(pgm_read_byte(&frame_buffer[i]));
  335. }
  336. DelayMs(2);
  337. SendCommand(DISPLAY_REFRESH);
  338. WaitUntilIdle();
  339. }
  340. }
  341. void Epd::Display4Gray(const unsigned char *Image)
  342. {
  343. int i,j,k;
  344. unsigned char temp1,temp2,temp3;
  345. SendCommand(0x10);
  346. for(i=0;i<5808;i++) //5808*4 46464
  347. {
  348. temp3=0;
  349. for(j=0;j<2;j++)
  350. {
  351. temp1 = pgm_read_byte(&Image[i*2+j]);
  352. for(k=0;k<2;k++)
  353. {
  354. temp2 = temp1&0xC0 ;
  355. if(temp2 == 0xC0)
  356. temp3 |= 0x01;//white
  357. else if(temp2 == 0x00)
  358. temp3 |= 0x00; //black
  359. else if(temp2 == 0x80)
  360. temp3 |= 0x01; //gray1
  361. else //0x40
  362. temp3 |= 0x00; //gray2
  363. temp3 <<= 1;
  364. temp1 <<= 2;
  365. temp2 = temp1&0xC0 ;
  366. if(temp2 == 0xC0) //white
  367. temp3 |= 0x01;
  368. else if(temp2 == 0x00) //black
  369. temp3 |= 0x00;
  370. else if(temp2 == 0x80)
  371. temp3 |= 0x01; //gray1
  372. else //0x40
  373. temp3 |= 0x00; //gray2
  374. if(j!=1 || k!=1)
  375. temp3 <<= 1;
  376. temp1 <<= 2;
  377. }
  378. }
  379. SendData(temp3);
  380. }
  381. // new data
  382. SendCommand(0x13);
  383. for(i=0;i<5808;i++) //5808*4 46464
  384. {
  385. temp3=0;
  386. for(j=0;j<2;j++)
  387. {
  388. temp1 = pgm_read_byte(&Image[i*2+j]);
  389. for(k=0;k<2;k++)
  390. {
  391. temp2 = temp1&0xC0 ;
  392. if(temp2 == 0xC0)
  393. temp3 |= 0x01;//white
  394. else if(temp2 == 0x00)
  395. temp3 |= 0x00; //black
  396. else if(temp2 == 0x80)
  397. temp3 |= 0x00; //gray1
  398. else //0x40
  399. temp3 |= 0x01; //gray2
  400. temp3 <<= 1;
  401. temp1 <<= 2;
  402. temp2 = temp1&0xC0 ;
  403. if(temp2 == 0xC0) //white
  404. temp3 |= 0x01;
  405. else if(temp2 == 0x00) //black
  406. temp3 |= 0x00;
  407. else if(temp2 == 0x80)
  408. temp3 |= 0x00; //gray1
  409. else //0x40
  410. temp3 |= 0x01; //gray2
  411. if(j!=1 || k!=1)
  412. temp3 <<= 1;
  413. temp1 <<= 2;
  414. }
  415. }
  416. SendData(temp3);
  417. }
  418. gray_SetLut();
  419. SendCommand(0x12);
  420. DelayMs(200);
  421. WaitUntilIdle();
  422. }
  423. /**
  424. * @brief: clear the frame data from the SRAM, this won't refresh the display
  425. */
  426. void Epd::ClearFrame(void) {
  427. SendCommand(DATA_START_TRANSMISSION_1);
  428. DelayMs(2);
  429. for(int i = 0; i < width * height / 8; i++) {
  430. SendData(0xFF);
  431. }
  432. DelayMs(2);
  433. SendCommand(DATA_START_TRANSMISSION_2);
  434. DelayMs(2);
  435. for(int i = 0; i < width * height / 8; i++) {
  436. SendData(0xFF);
  437. }
  438. DelayMs(2);
  439. }
  440. /**
  441. * @brief: This displays the frame data from SRAM
  442. */
  443. void Epd::DisplayFrame(void) {
  444. SendCommand(DISPLAY_REFRESH);
  445. WaitUntilIdle();
  446. }
  447. /**
  448. * @brief: After this command is transmitted, the chip would enter the deep-sleep mode to save power.
  449. * The deep sleep mode would return to standby by hardware reset. The only one parameter is a
  450. * check code, the command would be executed if check code = 0xA5.
  451. * You can use Epd::Reset() to awaken and use Epd::Init() to initialize.
  452. */
  453. void Epd::Sleep() {
  454. SendCommand(DEEP_SLEEP);
  455. SendData(0xa5);
  456. }
  457. const unsigned char lut_vcom_dc[] = {
  458. 0x00, 0x00,
  459. 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x05,
  460. 0x00, 0x32, 0x32, 0x00, 0x00, 0x02,
  461. 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x05,
  462. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  463. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  464. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  465. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  466. };
  467. //R21H
  468. const unsigned char lut_ww[] = {
  469. 0x50, 0x0F, 0x0F, 0x00, 0x00, 0x05,
  470. 0x60, 0x32, 0x32, 0x00, 0x00, 0x02,
  471. 0xA0, 0x0F, 0x0F, 0x00, 0x00, 0x05,
  472. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  473. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  474. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  475. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  476. };
  477. //R22H r
  478. const unsigned char lut_bw[] =
  479. {
  480. 0x50, 0x0F, 0x0F, 0x00, 0x00, 0x05,
  481. 0x60, 0x32, 0x32, 0x00, 0x00, 0x02,
  482. 0xA0, 0x0F, 0x0F, 0x00, 0x00, 0x05,
  483. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  484. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  485. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  486. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  487. };
  488. //R24H b
  489. const unsigned char lut_bb[] =
  490. {
  491. 0xA0, 0x0F, 0x0F, 0x00, 0x00, 0x05,
  492. 0x60, 0x32, 0x32, 0x00, 0x00, 0x02,
  493. 0x50, 0x0F, 0x0F, 0x00, 0x00, 0x05,
  494. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  495. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  496. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  497. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  498. };
  499. //R23H w
  500. const unsigned char lut_wb[] =
  501. {
  502. 0xA0, 0x0F, 0x0F, 0x00, 0x00, 0x05,
  503. 0x60, 0x32, 0x32, 0x00, 0x00, 0x02,
  504. 0x50, 0x0F, 0x0F, 0x00, 0x00, 0x05,
  505. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  506. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  507. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  508. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  509. };