2
0

epd4in2_V2.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /**
  2. * @filename : epd4in2_V2.cpp
  3. * @brief : Implements for Dual-color 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 "epd4in2_V2.h"
  28. Epd::~Epd() {
  29. };
  30. Epd::Epd() {
  31. reset_pin = RST_PIN;
  32. dc_pin = DC_PIN;
  33. cs_pin = CS_PIN;
  34. busy_pin = BUSY_PIN;
  35. width = EPD_WIDTH;
  36. height = EPD_HEIGHT;
  37. };
  38. int Epd::Init(void) {
  39. /* this calls the peripheral hardware interface, see epdif */
  40. if (IfInit() != 0) {
  41. return -1;
  42. }
  43. /* EPD hardware init start */
  44. Reset();
  45. ReadBusy();
  46. SendCommand(0x12);
  47. ReadBusy();
  48. SendCommand(0x21);
  49. SendData(0x40);
  50. SendData(0x00);
  51. SendCommand(0x3C);
  52. SendData(0x05);
  53. SendCommand(0x11);
  54. SendData(0x03);
  55. SendCommand(0x44);
  56. SendData(0x00);
  57. SendData(0x31);
  58. SendCommand(0x45);
  59. SendData(0x00);
  60. SendData(0x00);
  61. SendData(0x2B);
  62. SendData(0x01);
  63. SendCommand(0x4E);
  64. SendData(0x00);
  65. SendCommand(0x4F);
  66. SendData(0x00);
  67. SendData(0x00);
  68. ReadBusy();
  69. /* EPD hardware init end */
  70. return 0;
  71. }
  72. int Epd::Init_Fast(char mode) {
  73. /* this calls the peripheral hardware interface, see epdif */
  74. if (IfInit() != 0) {
  75. return -1;
  76. }
  77. /* EPD hardware init start */
  78. Reset();
  79. ReadBusy();
  80. SendCommand(0x12);
  81. ReadBusy();
  82. SendCommand(0x21);
  83. SendData(0x40);
  84. SendData(0x00);
  85. SendCommand(0x3C);
  86. SendData(0x05);
  87. if(mode == Seconds_1_5S)
  88. {
  89. SendCommand(0x1A);
  90. SendData(0x6E);
  91. }
  92. else
  93. {
  94. SendCommand(0x1A);
  95. SendData(0x5A);
  96. }
  97. SendCommand(0x22);
  98. SendData(0x91);
  99. SendCommand(0x20);
  100. ReadBusy();
  101. SendCommand(0x11);
  102. SendData(0x03);
  103. SendCommand(0x44);
  104. SendData(0x00);
  105. SendData(0x31);
  106. SendCommand(0x45);
  107. SendData(0x00);
  108. SendData(0x00);
  109. SendData(0x2B);
  110. SendData(0x01);
  111. SendCommand(0x4E);
  112. SendData(0x00);
  113. SendCommand(0x4F);
  114. SendData(0x00);
  115. SendData(0x00);
  116. ReadBusy();
  117. /* EPD hardware init end */
  118. return 0;
  119. }
  120. void Epd::Lut(void){
  121. SendCommand(0x32);
  122. for(int i=0; i<227; i++)
  123. SendData(LUT_ALL[i]);
  124. SendCommand(0x3F);
  125. SendData(LUT_ALL[227]);
  126. SendCommand(0x03);
  127. SendData(LUT_ALL[228]);
  128. SendCommand(0x04);
  129. SendData(LUT_ALL[229]);
  130. SendData(LUT_ALL[230]);
  131. SendData(LUT_ALL[231]);
  132. SendCommand(0x2c);
  133. SendData(LUT_ALL[232]);
  134. }
  135. int Epd::Init_4Gray(void) {
  136. /* this calls the peripheral hardware interface, see epdif */
  137. if (IfInit() != 0) {
  138. return -1;
  139. }
  140. /* EPD hardware init start */
  141. Reset();
  142. ReadBusy();
  143. SendCommand(0x12);
  144. ReadBusy();
  145. SendCommand(0x21);
  146. SendData(0x00);
  147. SendData(0x00);
  148. SendCommand(0x3C);
  149. SendData(0x03);
  150. SendCommand(0x0C);
  151. SendData(0x8B);
  152. SendData(0x9C);
  153. SendData(0xA4);
  154. SendData(0x0F);
  155. Lut();
  156. SendCommand(0x11);
  157. SendData(0x03);
  158. SendCommand(0x44);
  159. SendData(0x00);
  160. SendData(0x31);
  161. SendCommand(0x45);
  162. SendData(0x00);
  163. SendData(0x00);
  164. SendData(0x2B);
  165. SendData(0x01);
  166. SendCommand(0x4E);
  167. SendData(0x00);
  168. SendCommand(0x4F);
  169. SendData(0x00);
  170. SendData(0x00);
  171. ReadBusy();
  172. }
  173. /**
  174. * @brief: basic function for sending commands
  175. */
  176. void Epd::SendCommand(unsigned char command) {
  177. DigitalWrite(dc_pin, LOW);
  178. SpiTransfer(command);
  179. }
  180. /**
  181. * @brief: basic function for sending data
  182. */
  183. void Epd::SendData(unsigned char data) {
  184. DigitalWrite(dc_pin, HIGH);
  185. SpiTransfer(data);
  186. }
  187. /**
  188. * @brief: Wait until the busy_pin goes HIGH
  189. */
  190. void Epd::ReadBusy(void) {
  191. while(DigitalRead(busy_pin) == 1) { //1: busy, 0: idle
  192. // Wait
  193. }
  194. }
  195. /**
  196. * @brief: module reset.
  197. * often used to awaken the module in deep sleep,
  198. * see Epd::Sleep();
  199. */
  200. void Epd::Reset(void) {
  201. DigitalWrite(reset_pin, HIGH);
  202. DelayMs(100);
  203. DigitalWrite(reset_pin, LOW);
  204. DelayMs(2);
  205. DigitalWrite(reset_pin, HIGH);
  206. DelayMs(100);
  207. }
  208. /**
  209. * @brief: Turn On Display
  210. */
  211. void Epd::TurnOnDisplay(void)
  212. {
  213. SendCommand(0x22);
  214. SendData(0xF7);
  215. SendCommand(0x20);
  216. ReadBusy();
  217. }
  218. void Epd::TurnOnDisplay_Fast(void)
  219. {
  220. SendCommand(0x22);
  221. SendData(0xC7);
  222. SendCommand(0x20);
  223. ReadBusy();
  224. }
  225. void Epd::TurnOnDisplay_Partial(void)
  226. {
  227. SendCommand(0x22);
  228. SendData(0xFF);
  229. SendCommand(0x20);
  230. ReadBusy();
  231. }
  232. void Epd::TurnOnDisplay_4Gray(void)
  233. {
  234. SendCommand(0x22);
  235. SendData(0xCF);
  236. SendCommand(0x20);
  237. ReadBusy();
  238. }
  239. /******************************************************************************
  240. function : Sends the image buffer in RAM to e-Paper and displays
  241. parameter:
  242. ******************************************************************************/
  243. void Epd::Clear(void)
  244. {
  245. unsigned int Width, Height;
  246. Width = (width % 8 == 0)? (width / 8 ): (width / 8 + 1);
  247. Height = height;
  248. SendCommand(0x24);
  249. for (unsigned int j = 0; j < Height; j++) {
  250. for (unsigned int i = 0; i < Width; i++) {
  251. SendData(0xff);
  252. }
  253. }
  254. SendCommand(0x26);
  255. for (unsigned int j = 0; j < Height; j++) {
  256. for (unsigned int i = 0; i < Width; i++) {
  257. SendData(0xff);
  258. }
  259. }
  260. TurnOnDisplay();
  261. }
  262. void Epd::Display(const unsigned char* Image)
  263. {
  264. unsigned int Width, Height;
  265. Width = (width % 8 == 0)? (width / 8 ): (width / 8 + 1);
  266. Height = height;
  267. SendCommand(0x24);
  268. for (unsigned int j = 0; j < Height; j++) {
  269. for (unsigned int i = 0; i < Width; i++) {
  270. SendData(pgm_read_byte(&Image[i + j * Width]));
  271. }
  272. }
  273. SendCommand(0x26);
  274. for (unsigned int j = 0; j < Height; j++) {
  275. for (unsigned int i = 0; i < Width; i++) {
  276. SendData(pgm_read_byte(&Image[i + j * Width]));
  277. }
  278. }
  279. TurnOnDisplay();
  280. }
  281. void Epd::Display_Fast(const unsigned char* Image)
  282. {
  283. unsigned int Width, Height;
  284. Width = (width % 8 == 0)? (width / 8 ): (width / 8 + 1);
  285. Height = height;
  286. SendCommand(0x24);
  287. for (unsigned int j = 0; j < Height; j++) {
  288. for (unsigned int i = 0; i < Width; i++) {
  289. SendData(pgm_read_byte(&Image[i + j * Width]));
  290. }
  291. }
  292. SendCommand(0x26);
  293. for (unsigned int j = 0; j < Height; j++) {
  294. for (unsigned int i = 0; i < Width; i++) {
  295. SendData(pgm_read_byte(&Image[i + j * Width]));
  296. }
  297. }
  298. TurnOnDisplay_Fast();
  299. }
  300. void Epd::Display_Base(const unsigned char* Image)
  301. {
  302. unsigned int Width, Height;
  303. Width = (width % 8 == 0)? (width / 8 ): (width / 8 + 1);
  304. Height = height;
  305. SendCommand(0x24); //Write Black and White image to RAM
  306. for (unsigned int j = 0; j < Height; j++) {
  307. for (unsigned int i = 0; i < Width; i++) {
  308. SendData(pgm_read_byte(&Image[i + j * Width]));
  309. }
  310. }
  311. SendCommand(0x26); //Write Black and White image to RAM
  312. for (unsigned int j = 0; j < Height; j++) {
  313. for (unsigned int i = 0; i < Width; i++) {
  314. SendData(pgm_read_byte(&Image[i + j * Width]));
  315. }
  316. }
  317. TurnOnDisplay();
  318. }
  319. void Epd::Display_Partial(unsigned char* Image, unsigned int Xstart, unsigned int Ystart, unsigned int Xend, unsigned int Yend)
  320. {
  321. unsigned int i, Width;
  322. unsigned int IMAGE_COUNTER;
  323. if((Xstart % 8 + Xend % 8 == 8 && Xstart % 8 > Xend % 8) || Xstart % 8 + Xend % 8 == 0 || (Xend - Xstart)%8 == 0)
  324. {
  325. Xstart = Xstart / 8 ;
  326. Xend = Xend / 8;
  327. }
  328. else
  329. {
  330. Xstart = Xstart / 8 ;
  331. Xend = Xend % 8 == 0 ? Xend / 8 : Xend / 8 + 1;
  332. }
  333. Width = Xend - Xstart;
  334. IMAGE_COUNTER = Width * (Yend-Ystart);
  335. Serial.println(IMAGE_COUNTER);
  336. Xend -= 1;
  337. Yend -= 1;
  338. //Reset
  339. Reset();
  340. SendCommand(0x3C); //BorderWavefrom
  341. SendData(0x80);
  342. SendCommand(0x21);
  343. SendData(0x00);
  344. SendData(0x00);
  345. SendCommand(0x3C);
  346. SendData(0x80);
  347. //
  348. SendCommand(0x44); // set RAM x address start/end, in page 35
  349. SendData(Xstart & 0xff); // RAM x address start at 00h;
  350. SendData(Xend & 0xff); // RAM x address end at 0fh(15+1)*8->128
  351. SendCommand(0x45); // set RAM y address start/end, in page 35
  352. SendData(Ystart & 0xff); // RAM y address start at 0127h;
  353. SendData((Ystart>>8) & 0x01); // RAM y address start at 0127h;
  354. SendData(Yend & 0xff); // RAM y address end at 00h;
  355. SendData((Yend>>8) & 0x01);
  356. SendCommand(0x4E); // set RAM x address count to 0;
  357. SendData(Xstart & 0xff);
  358. SendCommand(0x4F); // set RAM y address count to 0X127;
  359. SendData(Ystart & 0xff);
  360. SendData((Ystart>>8) & 0x01);
  361. SendCommand(0x24); //Write Black and White image to RAM
  362. for (i = 0; i < IMAGE_COUNTER; i++) {
  363. SendData(Image[i]);
  364. }
  365. TurnOnDisplay_Partial();
  366. }
  367. void Epd::Display_Partial_Not_refresh(unsigned char* Image, unsigned int Xstart, unsigned int Ystart, unsigned int Xend, unsigned int Yend)
  368. {
  369. unsigned int i, Width;
  370. unsigned int IMAGE_COUNTER;
  371. if((Xstart % 8 + Xend % 8 == 8 && Xstart % 8 > Xend % 8) || Xstart % 8 + Xend % 8 == 0 || (Xend - Xstart)%8 == 0)
  372. {
  373. Xstart = Xstart / 8 ;
  374. Xend = Xend / 8;
  375. }
  376. else
  377. {
  378. Xstart = Xstart / 8 ;
  379. Xend = Xend % 8 == 0 ? Xend / 8 : Xend / 8 + 1;
  380. }
  381. Width = Xend - Xstart;
  382. IMAGE_COUNTER = Width * (Yend-Ystart);
  383. Serial.println(IMAGE_COUNTER);
  384. Xend -= 1;
  385. Yend -= 1;
  386. //Reset
  387. Reset();
  388. SendCommand(0x3C); //BorderWavefrom
  389. SendData(0x80);
  390. SendCommand(0x21);
  391. SendData(0x00);
  392. SendData(0x00);
  393. SendCommand(0x3C);
  394. SendData(0x80);
  395. //
  396. SendCommand(0x44); // set RAM x address start/end, in page 35
  397. SendData(Xstart & 0xff); // RAM x address start at 00h;
  398. SendData(Xend & 0xff); // RAM x address end at 0fh(15+1)*8->128
  399. SendCommand(0x45); // set RAM y address start/end, in page 35
  400. SendData(Ystart & 0xff); // RAM y address start at 0127h;
  401. SendData((Ystart>>8) & 0x01); // RAM y address start at 0127h;
  402. SendData(Yend & 0xff); // RAM y address end at 00h;
  403. SendData((Yend>>8) & 0x01);
  404. SendCommand(0x4E); // set RAM x address count to 0;
  405. SendData(Xstart & 0xff);
  406. SendCommand(0x4F); // set RAM y address count to 0X127;
  407. SendData(Ystart & 0xff);
  408. SendData((Ystart>>8) & 0x01);
  409. SendCommand(0x24); //Write Black and White image to RAM
  410. for (i = 0; i < IMAGE_COUNTER; i++) {
  411. SendData(Image[i]);
  412. }
  413. // TurnOnDisplay_Partial();
  414. }
  415. void Epd::Set_4GrayDisplay(const char *Image, int x, int y, int w, int l)
  416. {
  417. int i,j,k,m;
  418. int z=0;
  419. unsigned char temp1,temp2,temp3;
  420. /****Color display description****
  421. white gray1 gray2 black
  422. 0x10| 01 01 00 00
  423. 0x13| 01 00 01 00
  424. *********************************/
  425. SendCommand(0x24);
  426. z=0;
  427. x= x/8*8;
  428. for(m = 0; m<EPD_HEIGHT;m++)
  429. for(i=0;i<EPD_WIDTH/8;i++)
  430. {
  431. if(i >= x/8 && i <(x+w)/8 && m >= y && m < y+l){
  432. temp3=0;
  433. for(j=0;j<2;j++)
  434. {
  435. temp1 = pgm_read_byte(&Image[z*2+j]);
  436. for(k=0;k<2;k++)
  437. {
  438. temp2 = temp1&0xC0 ;
  439. if(temp2 == 0xC0)
  440. temp3 |= 0x01;//white
  441. else if(temp2 == 0x00)
  442. temp3 |= 0x00; //black
  443. else if(temp2 == 0x80)
  444. temp3 |= 0x01; //gray1
  445. else //0x40
  446. temp3 |= 0x00; //gray2
  447. temp3 <<= 1;
  448. temp1 <<= 2;
  449. temp2 = temp1&0xC0 ;
  450. if(temp2 == 0xC0) //white
  451. temp3 |= 0x01;
  452. else if(temp2 == 0x00) //black
  453. temp3 |= 0x00;
  454. else if(temp2 == 0x80)
  455. temp3 |= 0x01; //gray1
  456. else //0x40
  457. temp3 |= 0x00; //gray2
  458. if(j!=1 || k!=1)
  459. temp3 <<= 1;
  460. temp1 <<= 2;
  461. }
  462. }
  463. z++;
  464. SendData(temp3);
  465. }else{
  466. SendData(0xff);
  467. }
  468. }
  469. // new data
  470. SendCommand(0x26);
  471. z=0;
  472. for(m = 0; m<EPD_HEIGHT;m++)
  473. for(i=0;i<EPD_WIDTH/8;i++)
  474. {
  475. if(i >= x/8 && i <(x+w)/8 && m >= y && m < y+l){
  476. temp3=0;
  477. for(j=0;j<2;j++)
  478. {
  479. temp1 = pgm_read_byte(&Image[z*2+j]);
  480. for(k=0;k<2;k++)
  481. {
  482. temp2 = temp1&0xC0 ;
  483. if(temp2 == 0xC0)
  484. temp3 |= 0x01;//white
  485. else if(temp2 == 0x00)
  486. temp3 |= 0x00; //black
  487. else if(temp2 == 0x80)
  488. temp3 |= 0x00; //gray1
  489. else //0x40
  490. temp3 |= 0x01; //gray2
  491. temp3 <<= 1;
  492. temp1 <<= 2;
  493. temp2 = temp1&0xC0 ;
  494. if(temp2 == 0xC0) //white
  495. temp3 |= 0x01;
  496. else if(temp2 == 0x00) //black
  497. temp3 |= 0x00;
  498. else if(temp2 == 0x80)
  499. temp3 |= 0x00; //gray1
  500. else //0x40
  501. temp3 |= 0x01; //gray2
  502. if(j!=1 || k!=1)
  503. temp3 <<= 1;
  504. temp1 <<= 2;
  505. }
  506. }
  507. z++;
  508. SendData(temp3);
  509. }else {
  510. SendData(0xff);
  511. }
  512. }
  513. TurnOnDisplay_4Gray();
  514. }
  515. void Epd::Sleep() {
  516. SendCommand(0x10);
  517. SendData(0x01);
  518. DelayMs(100);
  519. }
  520. const unsigned char LUT_ALL[233]={
  521. 0x01, 0x0A, 0x1B, 0x0F, 0x03, 0x01, 0x01,
  522. 0x05, 0x0A, 0x01, 0x0A, 0x01, 0x01, 0x01,
  523. 0x05, 0x08, 0x03, 0x02, 0x04, 0x01, 0x01,
  524. 0x01, 0x04, 0x04, 0x02, 0x00, 0x01, 0x01,
  525. 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  526. 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  527. 0x01, 0x0A, 0x1B, 0x0F, 0x03, 0x01, 0x01,
  528. 0x05, 0x4A, 0x01, 0x8A, 0x01, 0x01, 0x01,
  529. 0x05, 0x48, 0x03, 0x82, 0x84, 0x01, 0x01,
  530. 0x01, 0x84, 0x84, 0x82, 0x00, 0x01, 0x01,
  531. 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  532. 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  533. 0x01, 0x0A, 0x1B, 0x8F, 0x03, 0x01, 0x01,
  534. 0x05, 0x4A, 0x01, 0x8A, 0x01, 0x01, 0x01,
  535. 0x05, 0x48, 0x83, 0x82, 0x04, 0x01, 0x01,
  536. 0x01, 0x04, 0x04, 0x02, 0x00, 0x01, 0x01,
  537. 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  538. 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  539. 0x01, 0x8A, 0x1B, 0x8F, 0x03, 0x01, 0x01,
  540. 0x05, 0x4A, 0x01, 0x8A, 0x01, 0x01, 0x01,
  541. 0x05, 0x48, 0x83, 0x02, 0x04, 0x01, 0x01,
  542. 0x01, 0x04, 0x04, 0x02, 0x00, 0x01, 0x01,
  543. 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  544. 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  545. 0x01, 0x8A, 0x9B, 0x8F, 0x03, 0x01, 0x01,
  546. 0x05, 0x4A, 0x01, 0x8A, 0x01, 0x01, 0x01,
  547. 0x05, 0x48, 0x03, 0x42, 0x04, 0x01, 0x01,
  548. 0x01, 0x04, 0x04, 0x42, 0x00, 0x01, 0x01,
  549. 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  550. 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  551. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  552. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  553. 0x02, 0x00, 0x00, 0x07, 0x17, 0x41, 0xA8,
  554. 0x32, 0x30,
  555. };
  556. /* END OF FILE */