epd1in54_V2.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*****************************************************************************
  2. * | File : epd1in54_V2.cpp
  3. * | Author : Waveshare team
  4. * | Function : 1.54inch e-paper V2
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2019-06-24
  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 <stdlib.h>
  31. #include "epd1in54_V2.h"
  32. // waveform full refresh
  33. unsigned char WF_Full_1IN54[159] =
  34. {
  35. 0x80, 0x48, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  36. 0x40, 0x48, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  37. 0x80, 0x48, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  38. 0x40, 0x48, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  39. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  40. 0xA, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  41. 0x8, 0x1, 0x0, 0x8, 0x1, 0x0, 0x2,
  42. 0xA, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  43. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  44. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  45. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  46. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  47. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  48. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  49. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  50. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  51. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  52. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x0, 0x0, 0x0,
  53. 0x22, 0x17, 0x41, 0x0, 0x32, 0x20
  54. };
  55. // waveform partial refresh(fast)
  56. unsigned char WF_PARTIAL_1IN54_0[159] =
  57. {
  58. 0x0,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  59. 0x80,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  60. 0x40,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  61. 0x0,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  62. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  63. 0xF,0x0,0x0,0x0,0x0,0x0,0x0,
  64. 0x1,0x1,0x0,0x0,0x0,0x0,0x0,
  65. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  66. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  67. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  68. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  69. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  70. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  71. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  72. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  73. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  74. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  75. 0x22,0x22,0x22,0x22,0x22,0x22,0x0,0x0,0x0,
  76. 0x02,0x17,0x41,0xB0,0x32,0x28,
  77. };
  78. Epd::~Epd()
  79. {
  80. };
  81. Epd::Epd()
  82. {
  83. reset_pin = RST_PIN;
  84. dc_pin = DC_PIN;
  85. cs_pin = CS_PIN;
  86. busy_pin = BUSY_PIN;
  87. width = EPD_WIDTH;
  88. height = EPD_HEIGHT;
  89. };
  90. /**
  91. * @brief: basic function for sending commands
  92. */
  93. void Epd::SendCommand(unsigned char command)
  94. {
  95. DigitalWrite(dc_pin, LOW);
  96. SpiTransfer(command);
  97. }
  98. /**
  99. * @brief: basic function for sending data
  100. */
  101. void Epd::SendData(unsigned char data)
  102. {
  103. DigitalWrite(dc_pin, HIGH);
  104. SpiTransfer(data);
  105. }
  106. /**
  107. * @brief: Wait until the busy_pin goes HIGH
  108. */
  109. void Epd::WaitUntilIdle(void)
  110. {
  111. while(DigitalRead(busy_pin) == 1) { //LOW: idle, HIGH: busy
  112. DelayMs(100);
  113. }
  114. DelayMs(200);
  115. }
  116. void Epd::Lut(unsigned char* lut)
  117. {
  118. SendCommand(0x32);
  119. for(unsigned char i=0; i<153; i++)
  120. SendData(lut[i]);
  121. WaitUntilIdle();
  122. }
  123. void Epd::SetLut(unsigned char* lut)
  124. {
  125. Lut(lut);
  126. SendCommand(0x3f);
  127. SendData(lut[153]);
  128. SendCommand(0x03);
  129. SendData(lut[154]);
  130. SendCommand(0x04);
  131. SendData(lut[155]);
  132. SendData(lut[156]);
  133. SendData(lut[157]);
  134. SendCommand(0x2c);
  135. SendData(lut[158]);
  136. }
  137. // High Direction
  138. int Epd::HDirInit(void)
  139. {
  140. /* this calls the peripheral hardware interface, see epdif */
  141. if (IfInit() != 0) {
  142. return -1;
  143. }
  144. /* EPD hardware init start */
  145. Reset();
  146. WaitUntilIdle();
  147. SendCommand(0x12); //SWRESET
  148. WaitUntilIdle();
  149. SendCommand(0x01); //Driver output control
  150. SendData(0xC7);
  151. SendData(0x00);
  152. SendData(0x01);
  153. SendCommand(0x11); //data entry mode
  154. SendData(0x01);
  155. SendCommand(0x44); //set Ram-X address start/end position
  156. SendData(0x00);
  157. SendData(0x18); //0x0C-->(18+1)*8=200
  158. SendCommand(0x45); //set Ram-Y address start/end position
  159. SendData(0xC7); //0xC7-->(199+1)=200
  160. SendData(0x00);
  161. SendData(0x00);
  162. SendData(0x00);
  163. SendCommand(0x3C); //BorderWavefrom
  164. SendData(0x01);
  165. SendCommand(0x18);
  166. SendData(0x80);
  167. SendCommand(0x22); // //Load Temperature and waveform setting.
  168. SendData(0XB1);
  169. SendCommand(0x20);
  170. SendCommand(0x4E); // set RAM x address count to 0;
  171. SendData(0x00);
  172. SendCommand(0x4F); // set RAM y address count to 0X199;
  173. SendData(0xC7);
  174. SendData(0x00);
  175. WaitUntilIdle();
  176. SetLut(WF_Full_1IN54);
  177. /* EPD hardware init end */
  178. return 0;
  179. }
  180. // Low Direction
  181. int Epd::LDirInit(void)
  182. {
  183. /* this calls the peripheral hardware interface, see epdif */
  184. if (IfInit() != 0) {
  185. return -1;
  186. }
  187. /* EPD hardware init start */
  188. Reset();
  189. WaitUntilIdle();
  190. SendCommand(0x12); //SWRESET
  191. WaitUntilIdle();
  192. SendCommand(0x01); //Driver output control
  193. SendData(0xC7);
  194. SendData(0x00);
  195. SendData(0x00);
  196. SendCommand(0x11); //data entry mode
  197. SendData(0x03);
  198. SendCommand(0x44);
  199. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  200. SendData((0 >> 3) & 0xFF);
  201. SendData((199 >> 3) & 0xFF);
  202. SendCommand(0x45);
  203. SendData(0 & 0xFF);
  204. SendData((0 >> 8) & 0xFF);
  205. SendData(199 & 0xFF);
  206. SendData((199 >> 8) & 0xFF);
  207. SendCommand(0x3C); //BorderWavefrom
  208. SendData(0x01);
  209. SendCommand(0x18);
  210. SendData(0x80);
  211. SendCommand(0x22); // //Load Temperature and waveform setting.
  212. SendData(0XB1);
  213. SendCommand(0x20);
  214. SendCommand(0x4E); // set RAM x address count to 0;
  215. SendData(0x00);
  216. SendCommand(0x4F); // set RAM y address count to 0X199;
  217. SendData(0xC7);
  218. SendData(0x00);
  219. WaitUntilIdle();
  220. SetLut(WF_Full_1IN54);
  221. /* EPD hardware init end */
  222. return 0;
  223. }
  224. /**
  225. * @brief: module reset.
  226. * often used to awaken the module in deep sleep,
  227. * see Epd::Sleep();
  228. */
  229. void Epd::Reset(void)
  230. {
  231. DigitalWrite(reset_pin, HIGH);
  232. DelayMs(20);
  233. DigitalWrite(reset_pin, LOW); //module reset
  234. DelayMs(5);
  235. DigitalWrite(reset_pin, HIGH);
  236. DelayMs(20);
  237. }
  238. void Epd::Clear(void)
  239. {
  240. int w, h;
  241. w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  242. h = EPD_HEIGHT;
  243. SendCommand(0x24);
  244. for (int j = 0; j < h; j++) {
  245. for (int i = 0; i < w; i++) {
  246. SendData(0xff);
  247. }
  248. }
  249. SendCommand(0x26);
  250. for (int j = 0; j < h; j++) {
  251. for (int i = 0; i < w; i++) {
  252. SendData(0xff);
  253. }
  254. }
  255. //DISPLAY REFRESH
  256. DisplayFrame();
  257. }
  258. void Epd::Display(const unsigned char* frame_buffer)
  259. {
  260. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  261. int h = EPD_HEIGHT;
  262. if (frame_buffer != NULL) {
  263. SendCommand(0x24);
  264. for (int j = 0; j < h; j++) {
  265. for (int i = 0; i < w; i++) {
  266. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  267. }
  268. }
  269. }
  270. //DISPLAY REFRESH
  271. DisplayFrame();
  272. }
  273. void Epd::DisplayPartBaseImage(const unsigned char* frame_buffer)
  274. {
  275. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  276. int h = EPD_HEIGHT;
  277. if (frame_buffer != NULL) {
  278. SendCommand(0x24);
  279. for (int j = 0; j < h; j++) {
  280. for (int i = 0; i < w; i++) {
  281. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  282. }
  283. }
  284. SendCommand(0x26);
  285. for (int j = 0; j < h; j++) {
  286. for (int i = 0; i < w; i++) {
  287. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  288. }
  289. }
  290. }
  291. //DISPLAY REFRESH
  292. DisplayFrame();
  293. }
  294. void Epd::DisplayPartBaseWhiteImage(void)
  295. {
  296. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  297. int h = EPD_HEIGHT;
  298. SendCommand(0x24);
  299. for (int j = 0; j < h; j++) {
  300. for (int i = 0; i < w; i++) {
  301. SendData(0xff);
  302. }
  303. }
  304. SendCommand(0x26);
  305. for (int j = 0; j < h; j++) {
  306. for (int i = 0; i < w; i++) {
  307. SendData(0xff);
  308. }
  309. }
  310. //DISPLAY REFRESH
  311. DisplayFrame();
  312. }
  313. void Epd::DisplayPart(const unsigned char* frame_buffer)
  314. {
  315. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  316. int h = EPD_HEIGHT;
  317. if (frame_buffer != NULL) {
  318. SendCommand(0x24);
  319. for (int j = 0; j < h; j++) {
  320. for (int i = 0; i < w; i++) {
  321. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  322. }
  323. }
  324. }
  325. //DISPLAY REFRESH
  326. DisplayPartFrame();
  327. }
  328. /**
  329. * @brief: private function to specify the memory area for data R/W
  330. */
  331. void Epd::SetMemoryArea(int x_start, int y_start, int x_end, int y_end)
  332. {
  333. SendCommand(0x44);
  334. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  335. SendData((x_start >> 3) & 0xFF);
  336. SendData((x_end >> 3) & 0xFF);
  337. SendCommand(0x45);
  338. SendData(y_start & 0xFF);
  339. SendData((y_start >> 8) & 0xFF);
  340. SendData(y_end & 0xFF);
  341. SendData((y_end >> 8) & 0xFF);
  342. }
  343. /**
  344. * @brief: private function to specify the start point for data R/W
  345. */
  346. void Epd::SetMemoryPointer(int x, int y)
  347. {
  348. SendCommand(0x4e);
  349. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  350. SendData((x >> 3) & 0xFF);
  351. SendCommand(0x4F);
  352. SendData(y & 0xFF);
  353. SendData((y >> 8) & 0xFF);
  354. WaitUntilIdle();
  355. }
  356. /**
  357. * @brief: update the display
  358. * there are 2 memory areas embedded in the e-paper display
  359. * but once this function is called,
  360. * the the next action of SetFrameMemory or ClearFrame will
  361. * set the other memory area.
  362. */
  363. void Epd::DisplayFrame(void)
  364. {
  365. //DISPLAY REFRESH
  366. SendCommand(0x22);
  367. SendData(0xc7);
  368. SendCommand(0x20);
  369. WaitUntilIdle();
  370. }
  371. void Epd::DisplayPartFrame(void)
  372. {
  373. SendCommand(0x22);
  374. SendData(0xcF);
  375. SendCommand(0x20);
  376. WaitUntilIdle();
  377. }
  378. void Epd::SetFrameMemory(
  379. const unsigned char* image_buffer,
  380. int x,
  381. int y,
  382. int image_width,
  383. int image_height
  384. )
  385. {
  386. int x_end;
  387. int y_end;
  388. DigitalWrite(reset_pin, LOW); //module reset
  389. DelayMs(2);
  390. DigitalWrite(reset_pin, HIGH);
  391. DelayMs(2);
  392. SendCommand(0x3c);
  393. SendData(0x80);
  394. if (
  395. image_buffer == NULL ||
  396. x < 0 || image_width < 0 ||
  397. y < 0 || image_height < 0
  398. ) {
  399. return;
  400. }
  401. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  402. x &= 0xF8;
  403. image_width &= 0xF8;
  404. if (x + image_width >= this->width) {
  405. x_end = this->width - 1;
  406. } else {
  407. x_end = x + image_width - 1;
  408. }
  409. if (y + image_height >= this->height) {
  410. y_end = this->height - 1;
  411. } else {
  412. y_end = y + image_height - 1;
  413. }
  414. SetMemoryArea(x, y, x_end, y_end);
  415. SetMemoryPointer(x, y);
  416. SendCommand(0x24);
  417. /* send the image data */
  418. for (int j = 0; j < y_end - y + 1; j++) {
  419. for (int i = 0; i < (x_end - x + 1) / 8; i++) {
  420. SendData(image_buffer[i + j * (image_width / 8)]);
  421. }
  422. }
  423. }
  424. void Epd::SetFrameMemoryPartial(
  425. const unsigned char* image_buffer,
  426. int x,
  427. int y,
  428. int image_width,
  429. int image_height
  430. )
  431. {
  432. int x_end;
  433. int y_end;
  434. DigitalWrite(reset_pin, LOW); //module reset
  435. DelayMs(2);
  436. DigitalWrite(reset_pin, HIGH);
  437. DelayMs(2);
  438. SetLut(WF_PARTIAL_1IN54_0);
  439. SendCommand(0x37);
  440. SendData(0x00);
  441. SendData(0x00);
  442. SendData(0x00);
  443. SendData(0x00);
  444. SendData(0x00);
  445. SendData(0x40);
  446. SendData(0x00);
  447. SendData(0x00);
  448. SendData(0x00);
  449. SendData(0x00);
  450. SendCommand(0x3c);
  451. SendData(0x80);
  452. SendCommand(0x22);
  453. SendData(0xc0);
  454. SendCommand(0x20);
  455. WaitUntilIdle();
  456. if (
  457. image_buffer == NULL ||
  458. x < 0 || image_width < 0 ||
  459. y < 0 || image_height < 0
  460. ) {
  461. return;
  462. }
  463. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  464. x &= 0xF8;
  465. image_width &= 0xF8;
  466. if (x + image_width >= this->width) {
  467. x_end = this->width - 1;
  468. } else {
  469. x_end = x + image_width - 1;
  470. }
  471. if (y + image_height >= this->height) {
  472. y_end = this->height - 1;
  473. } else {
  474. y_end = y + image_height - 1;
  475. }
  476. SetMemoryArea(x, y, x_end, y_end);
  477. SetMemoryPointer(x, y);
  478. SendCommand(0x24);
  479. /* send the image data */
  480. for (int j = 0; j < y_end - y + 1; j++) {
  481. for (int i = 0; i < (x_end - x + 1) / 8; i++) {
  482. SendData(image_buffer[i + j * (image_width / 8)]);
  483. }
  484. }
  485. }
  486. /**
  487. * @brief: After this command is transmitted, the chip would enter the
  488. * deep-sleep mode to save power.
  489. * The deep sleep mode would return to standby by hardware reset.
  490. * The only one parameter is a check code, the command would be
  491. * executed if check code = 0xA5.
  492. * You can use Epd::Init() to awaken
  493. */
  494. void Epd::Sleep()
  495. {
  496. SendCommand(0x10); //enter deep sleep
  497. SendData(0x01);
  498. DelayMs(200);
  499. DigitalWrite(reset_pin, LOW);
  500. }
  501. /* END OF FILE */