epd2in9d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*****************************************************************************
  2. * | File : epd2in9d.h
  3. * | Author : Waveshare team
  4. * | Function : 2.9inch e-paper
  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 "epd2in9d.h"
  32. /**
  33. * full screen update LUT
  34. **/
  35. const unsigned char EPD_2IN9D_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. const unsigned char EPD_2IN9D_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. const unsigned char EPD_2IN9D_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. const unsigned char EPD_2IN9D_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. const unsigned char EPD_2IN9D_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. const unsigned char EPD_2IN9D_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. const unsigned char EPD_2IN9D_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. const unsigned char EPD_2IN9D_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. const unsigned char EPD_2IN9D_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. const unsigned char EPD_2IN9D_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: Wait until the busy_pin goes HIGH
  160. */
  161. void Epd::WaitUntilIdle(void)
  162. {
  163. char busy;
  164. do {
  165. SendCommand(0x71);
  166. busy = DigitalRead(busy_pin);
  167. busy =!(busy & 0x01);
  168. } while(busy);
  169. DelayMs(200);
  170. }
  171. /******************************************************************************
  172. function : LUT download
  173. parameter:
  174. ******************************************************************************/
  175. void Epd::SetFullReg(void)
  176. {
  177. SendCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  178. SendData(0xb7); //WBmode:VBDF 17|D7 VBDW 97 VBDB 57 WBRmode:VBDF F7 VBDW 77 VBDB 37 VBDR B7
  179. unsigned int count;
  180. SendCommand(0x20);
  181. for(count=0; count<44; count++) {
  182. SendData(EPD_2IN9D_lut_vcomDC[count]);
  183. }
  184. SendCommand(0x21);
  185. for(count=0; count<42; count++) {
  186. SendData(EPD_2IN9D_lut_ww[count]);
  187. }
  188. SendCommand(0x22);
  189. for(count=0; count<42; count++) {
  190. SendData(EPD_2IN9D_lut_bw[count]);
  191. }
  192. SendCommand(0x23);
  193. for(count=0; count<42; count++) {
  194. SendData(EPD_2IN9D_lut_wb[count]);
  195. }
  196. SendCommand(0x24);
  197. for(count=0; count<42; count++) {
  198. SendData(EPD_2IN9D_lut_bb[count]);
  199. }
  200. }
  201. /******************************************************************************
  202. function : LUT download
  203. parameter:
  204. ******************************************************************************/
  205. void Epd::SetPartReg(void)
  206. {
  207. SendCommand(0x82); //vcom_DC setting
  208. SendData(0x00);
  209. SendCommand(0X50);
  210. SendData(0xb7);
  211. unsigned int count;
  212. SendCommand(0x20);
  213. for(count=0; count<44; count++) {
  214. SendData(EPD_2IN9D_lut_vcom1[count]);
  215. }
  216. SendCommand(0x21);
  217. for(count=0; count<42; count++) {
  218. SendData(EPD_2IN9D_lut_ww1[count]);
  219. }
  220. SendCommand(0x22);
  221. for(count=0; count<42; count++) {
  222. SendData(EPD_2IN9D_lut_bw1[count]);
  223. }
  224. SendCommand(0x23);
  225. for(count=0; count<42; count++) {
  226. SendData(EPD_2IN9D_lut_wb1[count]);
  227. }
  228. SendCommand(0x24);
  229. for(count=0; count<42; count++) {
  230. SendData(EPD_2IN9D_lut_bb1[count]);
  231. }
  232. }
  233. void Epd::TurnOnDisplay(void)
  234. {
  235. SendCommand(0x12); //DISPLAY REFRESH
  236. DelayMs(10); //!!!The delay here is necessary, 200uS at least!!!
  237. WaitUntilIdle();
  238. }
  239. int Epd::Init(void)
  240. {
  241. /* this calls the peripheral hardware interface, see epdif */
  242. if (IfInit() != 0) {
  243. return -1;
  244. }
  245. Reset();
  246. SendCommand(0x01); //POWER SETTING
  247. SendData(0x03);
  248. SendData(0x00);
  249. SendData(0x2b);
  250. SendData(0x2b);
  251. SendData(0x03);
  252. SendCommand(0x06); //boost soft start
  253. SendData(0x17); //A
  254. SendData(0x17); //B
  255. SendData(0x17); //C
  256. SendCommand(0x04);
  257. WaitUntilIdle();
  258. SendCommand(0x00); //panel setting
  259. SendData(0xbf); //LUT from OTP,128x296
  260. SendData(0x0e); //VCOM to 0V fast
  261. SendCommand(0x30); //PLL setting
  262. SendData(0x3a); // 3a 100HZ 29 150Hz 39 200HZ 31 171HZ
  263. SendCommand(0x61); //resolution setting
  264. SendData(EPD_WIDTH);
  265. SendData((EPD_HEIGHT >> 8) & 0xff);
  266. SendData(EPD_HEIGHT & 0xff);
  267. SendCommand(0x82); //vcom_DC setting
  268. SendData(0x28);
  269. return 0;
  270. }
  271. /**
  272. * @brief: module reset.
  273. * often used to awaken the module in deep sleep,
  274. * see Epd::Sleep();
  275. */
  276. void Epd::Reset(void)
  277. {
  278. DigitalWrite(reset_pin, HIGH);
  279. DelayMs(200);
  280. DigitalWrite(reset_pin, LOW); //module reset
  281. DelayMs(10);
  282. DigitalWrite(reset_pin, HIGH);
  283. DelayMs(200);
  284. }
  285. void Epd::Clear(void)
  286. {
  287. int w, h;
  288. w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  289. h = EPD_HEIGHT;
  290. SendCommand(0x10);
  291. for (int j = 0; j < h; j++) {
  292. for (int i = 0; i < w; i++) {
  293. SendData(0x00);
  294. }
  295. }
  296. SendCommand(0x13);
  297. for (int j = 0; j < h; j++) {
  298. for (int i = 0; i < w; i++) {
  299. SendData(0xFF);
  300. }
  301. }
  302. SetFullReg();
  303. TurnOnDisplay();
  304. }
  305. void Epd::Display(const unsigned char* frame_buffer)
  306. {
  307. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  308. int h = EPD_HEIGHT;
  309. SendCommand(0x10);
  310. for (int j = 0; j < h; j++) {
  311. for (int i = 0; i < w; i++) {
  312. SendData(0x00);
  313. }
  314. }
  315. // Dev_Delay_ms(10);
  316. SendCommand(0x13);
  317. for (int j = 0; j < h; j++) {
  318. for (int i = 0; i < w; i++) {
  319. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  320. }
  321. }
  322. // Dev_Delay_ms(10);
  323. SetFullReg();
  324. TurnOnDisplay();
  325. }
  326. void Epd::DisplayPart(const unsigned char* frame_buffer)
  327. {
  328. /* Set partial Windows */
  329. SetPartReg();
  330. SendCommand(0x91); //This command makes the display enter partial mode
  331. SendCommand(0x90); //resolution setting
  332. SendData(0); //x-start
  333. SendData(EPD_WIDTH - 1); //x-end
  334. SendData(0);
  335. SendData(0); //y-start
  336. SendData(EPD_HEIGHT / 256);
  337. SendData(EPD_HEIGHT % 256 - 1); //y-end
  338. SendData(0x28);
  339. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  340. int h = EPD_HEIGHT;
  341. /* send data */
  342. SendCommand(0x13);
  343. for (int j = 0; j < h; j++) {
  344. for (int i = 0; i < w; i++) {
  345. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  346. }
  347. }
  348. /* Set partial refresh */
  349. TurnOnDisplay();
  350. }
  351. /**
  352. * @brief: After this command is transmitted, the chip would enter the
  353. * deep-sleep mode to save power.
  354. * The deep sleep mode would return to standby by hardware reset.
  355. * The only one parameter is a check code, the command would be
  356. * executed if check code = 0xA5.
  357. * You can use Epd::Init() to awaken
  358. */
  359. void Epd::Sleep()
  360. {
  361. SendCommand(0X50);
  362. SendData(0xf7);
  363. SendCommand(0X02); //power off
  364. WaitUntilIdle();
  365. SendCommand(0X07); //deep sleep
  366. SendData(0xA5);
  367. DigitalWrite(reset_pin, LOW);
  368. }
  369. /* END OF FILE */