epd2in13d.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*****************************************************************************
  2. * | File : EPD_2in13d
  3. * | Author : Waveshare team
  4. * | Function : 2.13inch e-paper d
  5. * | Info :
  6. *----------------
  7. * | This version: V4.0
  8. * | Date : 2019-12-31
  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 "epd2in13d.h"
  31. #include <Arduino.h>
  32. /**
  33. * full screen update LUT
  34. **/
  35. static const unsigned char EPD_2IN13D_lut_vcomDC[] = {
  36. 0x00, 0x08, 0x00, 0x00, 0x00, 0x02,
  37. 0x60, 0x28, 0x28, 0x00, 0x00, 0x01,
  38. 0x00, 0x14, 0x00, 0x00, 0x00, 0x01,
  39. 0x00, 0x12, 0x12, 0x00, 0x00, 0x01,
  40. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  41. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  42. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43. 0x00, 0x00,
  44. };
  45. static const unsigned char EPD_2IN13D_lut_ww[] = {
  46. 0x40, 0x08, 0x00, 0x00, 0x00, 0x02,
  47. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  48. 0x40, 0x14, 0x00, 0x00, 0x00, 0x01,
  49. 0xA0, 0x12, 0x12, 0x00, 0x00, 0x01,
  50. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  53. };
  54. static const unsigned char EPD_2IN13D_lut_bw[] = {
  55. 0x40, 0x17, 0x00, 0x00, 0x00, 0x02,
  56. 0x90, 0x0F, 0x0F, 0x00, 0x00, 0x03,
  57. 0x40, 0x0A, 0x01, 0x00, 0x00, 0x01,
  58. 0xA0, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62. };
  63. static const unsigned char EPD_2IN13D_lut_wb[] = {
  64. 0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
  65. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  66. 0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
  67. 0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  70. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71. };
  72. static const unsigned char EPD_2IN13D_lut_bb[] = {
  73. 0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
  74. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  75. 0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
  76. 0x50, 0x12, 0x12, 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. /**
  82. * partial screen update LUT
  83. **/
  84. static const unsigned char EPD_2IN13D_lut_vcom1[] = {
  85. 0x00, 0x19, 0x01, 0x00, 0x00, 0x01,
  86. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  87. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  88. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  89. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  90. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  91. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  92. ,0x00, 0x00,
  93. };
  94. static const unsigned char EPD_2IN13D_lut_ww1[] = {
  95. 0x00, 0x19, 0x01, 0x00, 0x00, 0x01,
  96. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  97. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  98. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  99. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  100. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  101. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  102. };
  103. static const unsigned char EPD_2IN13D_lut_bw1[] = {
  104. 0x80, 0x19, 0x01, 0x00, 0x00, 0x01,
  105. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  106. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  107. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  108. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  109. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  110. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  111. };
  112. static const unsigned char EPD_2IN13D_lut_wb1[] = {
  113. 0x40, 0x19, 0x01, 0x00, 0x00, 0x01,
  114. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  115. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  116. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  117. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  118. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  119. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  120. };
  121. static const unsigned char EPD_2IN13D_lut_bb1[] = {
  122. 0x00, 0x19, 0x01, 0x00, 0x00, 0x01,
  123. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  124. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  125. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  126. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  127. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  128. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  129. };
  130. Epd::~Epd()
  131. {
  132. };
  133. Epd::Epd()
  134. {
  135. reset_pin = RST_PIN;
  136. dc_pin = DC_PIN;
  137. cs_pin = CS_PIN;
  138. busy_pin = BUSY_PIN;
  139. width = EPD_WIDTH;
  140. height = EPD_HEIGHT;
  141. };
  142. /**
  143. * @brief: basic function for sending commands
  144. */
  145. void Epd::SendCommand(unsigned char command)
  146. {
  147. DigitalWrite(dc_pin, LOW);
  148. SpiTransfer(command);
  149. }
  150. /**
  151. * @brief: basic function for sending data
  152. */
  153. void Epd::SendData(unsigned char data)
  154. {
  155. DigitalWrite(dc_pin, HIGH);
  156. SpiTransfer(data);
  157. }
  158. /**
  159. * @brief: module reset.
  160. * often used to awaken the module in deep sleep,
  161. * see Epd::Sleep();
  162. */
  163. void Epd::Reset(void)
  164. {
  165. DigitalWrite(reset_pin, HIGH);
  166. DelayMs(200);
  167. DigitalWrite(reset_pin, LOW); //module reset
  168. DelayMs(10);
  169. DigitalWrite(reset_pin, HIGH);
  170. DelayMs(200);
  171. }
  172. /******************************************************************************
  173. function : Wait until the busy_pin goes LOW
  174. parameter:
  175. ******************************************************************************/
  176. void Epd::ReadBusy(void)
  177. {
  178. Serial.print("ReadBusy\n");
  179. char busy;
  180. do {
  181. SendCommand(0x71);
  182. busy = DigitalRead(busy_pin);
  183. busy =!(busy & 0x01);
  184. } while(busy);
  185. Serial.print("ReadBusy over\n");
  186. DelayMs(200);
  187. }
  188. /******************************************************************************
  189. function : LUT download
  190. parameter:
  191. ******************************************************************************/
  192. void Epd::SetFullReg(void)
  193. {
  194. SendCommand(0x82);
  195. SendData(0x00);
  196. SendCommand(0X50);
  197. SendData(0x97);
  198. unsigned int count;
  199. SendCommand(0x20);
  200. for(count=0; count<44; count++) {
  201. SendData(EPD_2IN13D_lut_vcomDC[count]);
  202. }
  203. SendCommand(0x21);
  204. for(count=0; count<42; count++) {
  205. SendData(EPD_2IN13D_lut_ww[count]);
  206. }
  207. SendCommand(0x22);
  208. for(count=0; count<42; count++) {
  209. SendData(EPD_2IN13D_lut_bw[count]);
  210. }
  211. SendCommand(0x23);
  212. for(count=0; count<42; count++) {
  213. SendData(EPD_2IN13D_lut_wb[count]);
  214. }
  215. SendCommand(0x24);
  216. for(count=0; count<42; count++) {
  217. SendData(EPD_2IN13D_lut_bb[count]);
  218. }
  219. }
  220. /******************************************************************************
  221. function : LUT download
  222. parameter:
  223. ******************************************************************************/
  224. void Epd::SetPartReg(void)
  225. {
  226. SendCommand(0x82); //vcom_DC setting
  227. SendData(0x03);
  228. SendCommand(0X50);
  229. SendData(0x47);
  230. unsigned int count;
  231. SendCommand(0x20);
  232. for(count=0; count<44; count++) {
  233. SendData(EPD_2IN13D_lut_vcom1[count]);
  234. }
  235. SendCommand(0x21);
  236. for(count=0; count<42; count++) {
  237. SendData(EPD_2IN13D_lut_ww1[count]);
  238. }
  239. SendCommand(0x22);
  240. for(count=0; count<42; count++) {
  241. SendData(EPD_2IN13D_lut_bw1[count]);
  242. }
  243. SendCommand(0x23);
  244. for(count=0; count<42; count++) {
  245. SendData(EPD_2IN13D_lut_wb1[count]);
  246. }
  247. SendCommand(0x24);
  248. for(count=0; count<42; count++) {
  249. SendData(EPD_2IN13D_lut_bb1[count]);
  250. }
  251. }
  252. /******************************************************************************
  253. function : Turn On Display
  254. parameter:
  255. ******************************************************************************/
  256. void Epd::TurnOnDisplay(void)
  257. {
  258. SendCommand(0x12); //DISPLAY REFRESH
  259. DelayMs(10); //!!!The delay here is necessary, 200uS at least!!!
  260. ReadBusy();
  261. }
  262. /******************************************************************************
  263. function : Initialize the e-Paper register
  264. parameter:
  265. ******************************************************************************/
  266. int Epd::Init()
  267. {
  268. /* this calls the peripheral hardware interface, see epdif */
  269. if (IfInit() != 0) {
  270. return -1;
  271. }
  272. Reset();
  273. SendCommand(0x01); //POWER SETTING
  274. SendData(0x03);
  275. SendData(0x00);
  276. SendData(0x2b);
  277. SendData(0x2b);
  278. SendData(0x03);
  279. SendCommand(0x06); //boost soft start
  280. SendData(0x17); //A
  281. SendData(0x17); //B
  282. SendData(0x17); //C
  283. SendCommand(0x04);
  284. ReadBusy();
  285. SendCommand(0x00); //panel setting
  286. SendData(0xbf); //LUT from OTP,128x296
  287. SendData(0x0e); //VCOM to 0V fast
  288. SendCommand(0x30); //PLL setting
  289. SendData(0x3a); // 3a 100HZ 29 150Hz 39 200HZ 31 171HZ
  290. SendCommand(0x61); //resolution setting
  291. SendData(EPD_WIDTH);
  292. SendData((EPD_HEIGHT >> 8) & 0xff);
  293. SendData(EPD_HEIGHT& 0xff);
  294. SendCommand(0x82); //vcom_DC setting
  295. SendData(0x28);
  296. return 0;
  297. }
  298. void Epd::Clear(void)
  299. {
  300. int w, h;
  301. w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  302. h = EPD_HEIGHT;
  303. SendCommand(0x10);
  304. for (int j = 0; j < h; j++) {
  305. for (int i = 0; i < w; i++) {
  306. SendData(0x00);
  307. }
  308. }
  309. SendCommand(0x13);
  310. for (int j = 0; j < h; j++) {
  311. for (int i = 0; i < w; i++) {
  312. SendData(0xFF);
  313. }
  314. }
  315. SetFullReg();
  316. TurnOnDisplay();
  317. }
  318. /******************************************************************************
  319. function : Sends the image buffer in RAM to e-Paper and displays
  320. parameter:
  321. ******************************************************************************/
  322. void Epd::Display(const unsigned char *Image)
  323. {
  324. int Width, Height;
  325. Width = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  326. Height = EPD_HEIGHT;
  327. SendCommand(0x10);
  328. for (int j = 0; j < Height; j++) {
  329. for (int i = 0; i < Width; i++) {
  330. SendData(0x00);
  331. }
  332. }
  333. SendCommand(0x13);
  334. for (int j = 0; j < Height; j++) {
  335. for (int i = 0; i < Width; i++) {
  336. SendData(Image[i + j * Width]);
  337. }
  338. }
  339. SetFullReg();
  340. TurnOnDisplay();
  341. }
  342. /******************************************************************************
  343. function : Sends the image buffer in RAM to e-Paper and displays
  344. parameter:
  345. ******************************************************************************/
  346. void Epd::Displayimage(const unsigned char *Image)
  347. {
  348. int Width, Height;
  349. Width = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  350. Height = EPD_HEIGHT;
  351. SendCommand(0x10);
  352. for (int j = 0; j < Height; j++) {
  353. for (int i = 0; i < Width; i++) {
  354. SendData(0x00);
  355. }
  356. }
  357. SendCommand(0x13);
  358. for (int j = 0; j < Height; j++) {
  359. for (int i = 0; i < Width; i++) {
  360. SendData(pgm_read_byte(&Image[i + j * Width]));
  361. }
  362. }
  363. SetFullReg();
  364. TurnOnDisplay();
  365. }
  366. /******************************************************************************
  367. function : Sends the image buffer in RAM to e-Paper and displays
  368. parameter:
  369. ******************************************************************************/
  370. void Epd::DisplayPart(const unsigned char *Image)
  371. {
  372. /* Set partial Windows */
  373. SetPartReg();
  374. SendCommand(0x91); //This command makes the display enter partial mode
  375. SendCommand(0x90); //resolution setting
  376. SendData(0); //x-start
  377. SendData(EPD_WIDTH - 1); //x-end
  378. SendData(0);
  379. SendData(0); //y-start
  380. SendData(EPD_HEIGHT / 256);
  381. SendData(EPD_HEIGHT % 256 - 1); //y-end
  382. SendData(0x28);
  383. int Width = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  384. /* send data */
  385. SendCommand(0x10);
  386. for (int j = 0; j < EPD_HEIGHT; j++) {
  387. for (int i = 0; i < Width; i++) {
  388. SendData(~Image[i + j * Width]);
  389. }
  390. }
  391. SendCommand(0x13);
  392. for (int j = 0; j < EPD_HEIGHT; j++) {
  393. for (int i = 0; i < Width; i++) {
  394. SendData(Image[i + j * Width]);
  395. }
  396. }
  397. /* Set partial refresh */
  398. TurnOnDisplay();
  399. }
  400. /******************************************************************************
  401. function : Enter sleep mode
  402. parameter:
  403. ******************************************************************************/
  404. void Epd::Sleep(void)
  405. {
  406. SendCommand(0X50);
  407. SendData(0xf7);
  408. SendCommand(0X02); //power off
  409. SendCommand(0X07); //deep sleep
  410. SendData(0xA5);
  411. DigitalWrite(reset_pin, LOW);
  412. }