epd2in9d.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. * partial screen update LUT
  34. **/
  35. const unsigned char EPD_2IN9D_lut_vcom1[] = {
  36. 0x00, 0x19, 0x01, 0x00, 0x00, 0x01,
  37. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  38. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  39. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  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_ww1[] = {
  46. 0x00, 0x19, 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. 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_bw1[] = {
  55. 0x80, 0x19, 0x01, 0x00, 0x00, 0x01,
  56. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  57. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  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_wb1[] = {
  64. 0x40, 0x19, 0x01, 0x00, 0x00, 0x01,
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  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_bb1[] = {
  73. 0x00, 0x19, 0x01, 0x00, 0x00, 0x01,
  74. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  75. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  76. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  77. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  78. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  79. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  80. };
  81. Epd::~Epd()
  82. {
  83. };
  84. Epd::Epd()
  85. {
  86. reset_pin = RST_PIN;
  87. dc_pin = DC_PIN;
  88. cs_pin = CS_PIN;
  89. busy_pin = BUSY_PIN;
  90. width = EPD_WIDTH;
  91. height = EPD_HEIGHT;
  92. };
  93. /**
  94. * @brief: basic function for sending commands
  95. */
  96. void Epd::SendCommand(unsigned char command)
  97. {
  98. DigitalWrite(dc_pin, LOW);
  99. SpiTransfer(command);
  100. }
  101. /**
  102. * @brief: basic function for sending data
  103. */
  104. void Epd::SendData(unsigned char data)
  105. {
  106. DigitalWrite(dc_pin, HIGH);
  107. SpiTransfer(data);
  108. }
  109. /**
  110. * @brief: Wait until the busy_pin goes HIGH
  111. */
  112. void Epd::WaitUntilIdle(void)
  113. {
  114. char busy;
  115. do {
  116. SendCommand(0x71);
  117. busy = DigitalRead(busy_pin);
  118. busy =!(busy & 0x01);
  119. } while(busy);
  120. DelayMs(20);
  121. }
  122. /******************************************************************************
  123. function : LUT download
  124. parameter:
  125. ******************************************************************************/
  126. void Epd::SetPartReg(void)
  127. {
  128. SendCommand(0x01); //POWER SETTING
  129. SendData(0x03);
  130. SendData(0x00);
  131. SendData(0x2b);
  132. SendData(0x2b);
  133. SendData(0x03);
  134. SendCommand(0x06); //boost soft start
  135. SendData(0x17); //A
  136. SendData(0x17); //B
  137. SendData(0x17); //C
  138. SendCommand(0x04);
  139. WaitUntilIdle();
  140. SendCommand(0x00); //panel setting
  141. SendData(0xbf); //LUT from OTP,128x296
  142. SendCommand(0x30); //PLL setting
  143. SendData(0x3a); // 3a 100HZ 29 150Hz 39 200HZ 31 171HZ
  144. SendCommand(0x61); //resolution setting
  145. SendData(EPD_WIDTH);
  146. SendData((EPD_WIDTH >> 8) & 0xff);
  147. SendData(EPD_HEIGHT & 0xff);
  148. SendCommand(0x82); //vcom_DC setting
  149. SendData(0x12);
  150. SendCommand(0X50);
  151. SendData(0x97);
  152. unsigned int count;
  153. SendCommand(0x20);
  154. for(count=0; count<44; count++) {
  155. SendData(EPD_2IN9D_lut_vcom1[count]);
  156. }
  157. SendCommand(0x21);
  158. for(count=0; count<42; count++) {
  159. SendData(EPD_2IN9D_lut_ww1[count]);
  160. }
  161. SendCommand(0x22);
  162. for(count=0; count<42; count++) {
  163. SendData(EPD_2IN9D_lut_bw1[count]);
  164. }
  165. SendCommand(0x23);
  166. for(count=0; count<42; count++) {
  167. SendData(EPD_2IN9D_lut_wb1[count]);
  168. }
  169. SendCommand(0x24);
  170. for(count=0; count<42; count++) {
  171. SendData(EPD_2IN9D_lut_bb1[count]);
  172. }
  173. }
  174. void Epd::TurnOnDisplay(void)
  175. {
  176. SendCommand(0x12); //DISPLAY REFRESH
  177. DelayMs(10); //!!!The delay here is necessary, 200uS at least!!!
  178. WaitUntilIdle();
  179. }
  180. int Epd::Init(void)
  181. {
  182. /* this calls the peripheral hardware interface, see epdif */
  183. if (IfInit() != 0) {
  184. return -1;
  185. }
  186. Reset();
  187. SendCommand(0x04);
  188. WaitUntilIdle();//waiting for the electronic paper IC to release the idle signal
  189. SendCommand(0x00); //panel setting
  190. SendData(0x1f); //LUT from OTP,KW-BF KWR-AF BWROTP 0f BWOTP 1f
  191. SendCommand(0x61); //resolution setting
  192. SendData (0x80);
  193. SendData (0x01);
  194. SendData (0x28);
  195. SendCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  196. return 0;
  197. }
  198. /**
  199. * @brief: module reset.
  200. * often used to awaken the module in deep sleep,
  201. * see Epd::Sleep();
  202. */
  203. void Epd::Reset(void)
  204. {
  205. DigitalWrite(reset_pin, HIGH);
  206. DelayMs(20);
  207. DigitalWrite(reset_pin, LOW); //module reset
  208. DelayMs(2);
  209. DigitalWrite(reset_pin, HIGH);
  210. DelayMs(20);
  211. DigitalWrite(reset_pin, LOW); //module reset
  212. DelayMs(2);
  213. DigitalWrite(reset_pin, HIGH);
  214. DelayMs(20);
  215. DigitalWrite(reset_pin, LOW); //module reset
  216. DelayMs(2);
  217. DigitalWrite(reset_pin, HIGH);
  218. DelayMs(20);
  219. }
  220. void Epd::Clear(void)
  221. {
  222. int w, h;
  223. w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  224. h = EPD_HEIGHT;
  225. SendCommand(0x10);
  226. for (int j = 0; j < h; j++) {
  227. for (int i = 0; i < w; i++) {
  228. SendData(0x00);
  229. }
  230. }
  231. SendCommand(0x13);
  232. for (int j = 0; j < h; j++) {
  233. for (int i = 0; i < w; i++) {
  234. SendData(0xFF);
  235. }
  236. }
  237. TurnOnDisplay();
  238. }
  239. void Epd::Display(const unsigned char* frame_buffer)
  240. {
  241. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  242. int h = EPD_HEIGHT;
  243. SendCommand(0x10);
  244. for (int j = 0; j < h; j++) {
  245. for (int i = 0; i < w; i++) {
  246. SendData(0x00);
  247. }
  248. }
  249. // Dev_Delay_ms(10);
  250. SendCommand(0x13);
  251. for (int j = 0; j < h; j++) {
  252. for (int i = 0; i < w; i++) {
  253. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  254. }
  255. }
  256. // Dev_Delay_ms(10);
  257. TurnOnDisplay();
  258. }
  259. void Epd::DisplayPart(const unsigned char* frame_buffer)
  260. {
  261. /* Set partial Windows */
  262. SetPartReg();
  263. SendCommand(0x91); //This command makes the display enter partial mode
  264. SendCommand(0x90); //resolution setting
  265. SendData(0); //x-start
  266. SendData(EPD_WIDTH - 1); //x-end
  267. SendData(0);
  268. SendData(0); //y-start
  269. SendData(EPD_HEIGHT / 256);
  270. SendData(EPD_HEIGHT % 256 - 1); //y-end
  271. SendData(0x28);
  272. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  273. int h = EPD_HEIGHT;
  274. /* send data */
  275. SendCommand(0x13);
  276. for (int j = 0; j < h; j++) {
  277. for (int i = 0; i < w; i++) {
  278. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  279. }
  280. }
  281. /* Set partial refresh */
  282. TurnOnDisplay();
  283. }
  284. /**
  285. * @brief: After this command is transmitted, the chip would enter the
  286. * deep-sleep mode to save power.
  287. * The deep sleep mode would return to standby by hardware reset.
  288. * The only one parameter is a check code, the command would be
  289. * executed if check code = 0xA5.
  290. * You can use Epd::Init() to awaken
  291. */
  292. void Epd::Sleep()
  293. {
  294. SendCommand(0X50);
  295. SendData(0xf7);
  296. SendCommand(0X02); //power off
  297. WaitUntilIdle();
  298. SendCommand(0X07); //deep sleep
  299. SendData(0xA5);
  300. DigitalWrite(reset_pin, LOW);
  301. }
  302. /* END OF FILE */