epd2in13_V2.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*****************************************************************************
  2. * | File : epd2in13_V2.cpp
  3. * | Author : Waveshare team
  4. * | Function : 2.13inch 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 "epd2in13_V2.h"
  32. const unsigned char lut_full_update[]= {
  33. 0x80,0x60,0x40,0x00,0x00,0x00,0x00, //LUT0: BB: VS 0 ~7
  34. 0x10,0x60,0x20,0x00,0x00,0x00,0x00, //LUT1: BW: VS 0 ~7
  35. 0x80,0x60,0x40,0x00,0x00,0x00,0x00, //LUT2: WB: VS 0 ~7
  36. 0x10,0x60,0x20,0x00,0x00,0x00,0x00, //LUT3: WW: VS 0 ~7
  37. 0x00,0x00,0x00,0x00,0x00,0x00,0x00, //LUT4: VCOM: VS 0 ~7
  38. 0x03,0x03,0x00,0x00,0x02, // TP0 A~D RP0
  39. 0x09,0x09,0x00,0x00,0x02, // TP1 A~D RP1
  40. 0x03,0x03,0x00,0x00,0x02, // TP2 A~D RP2
  41. 0x00,0x00,0x00,0x00,0x00, // TP3 A~D RP3
  42. 0x00,0x00,0x00,0x00,0x00, // TP4 A~D RP4
  43. 0x00,0x00,0x00,0x00,0x00, // TP5 A~D RP5
  44. 0x00,0x00,0x00,0x00,0x00, // TP6 A~D RP6
  45. 0x15,0x41,0xA8,0x32,0x30,0x0A,
  46. };
  47. const unsigned char lut_partial_update[]= { //20 bytes
  48. 0x00,0x00,0x00,0x00,0x00,0x00,0x00, //LUT0: BB: VS 0 ~7
  49. 0x80,0x00,0x00,0x00,0x00,0x00,0x00, //LUT1: BW: VS 0 ~7
  50. 0x40,0x00,0x00,0x00,0x00,0x00,0x00, //LUT2: WB: VS 0 ~7
  51. 0x00,0x00,0x00,0x00,0x00,0x00,0x00, //LUT3: WW: VS 0 ~7
  52. 0x00,0x00,0x00,0x00,0x00,0x00,0x00, //LUT4: VCOM: VS 0 ~7
  53. 0x0A,0x00,0x00,0x00,0x00, // TP0 A~D RP0
  54. 0x00,0x00,0x00,0x00,0x00, // TP1 A~D RP1
  55. 0x00,0x00,0x00,0x00,0x00, // TP2 A~D RP2
  56. 0x00,0x00,0x00,0x00,0x00, // TP3 A~D RP3
  57. 0x00,0x00,0x00,0x00,0x00, // TP4 A~D RP4
  58. 0x00,0x00,0x00,0x00,0x00, // TP5 A~D RP5
  59. 0x00,0x00,0x00,0x00,0x00, // TP6 A~D RP6
  60. 0x15,0x41,0xA8,0x32,0x30,0x0A,
  61. };
  62. Epd::~Epd()
  63. {
  64. };
  65. Epd::Epd()
  66. {
  67. reset_pin = RST_PIN;
  68. dc_pin = DC_PIN;
  69. cs_pin = CS_PIN;
  70. busy_pin = BUSY_PIN;
  71. width = EPD_WIDTH;
  72. height = EPD_HEIGHT;
  73. };
  74. /**
  75. * @brief: basic function for sending commands
  76. */
  77. void Epd::SendCommand(unsigned char command)
  78. {
  79. DigitalWrite(dc_pin, LOW);
  80. SpiTransfer(command);
  81. }
  82. /**
  83. * @brief: basic function for sending data
  84. */
  85. void Epd::SendData(unsigned char data)
  86. {
  87. DigitalWrite(dc_pin, HIGH);
  88. SpiTransfer(data);
  89. }
  90. /**
  91. * @brief: Wait until the busy_pin goes HIGH
  92. */
  93. void Epd::WaitUntilIdle(void)
  94. {
  95. while(DigitalRead(busy_pin) == 1) { //LOW: idle, HIGH: busy
  96. DelayMs(100);
  97. }
  98. DelayMs(200);
  99. }
  100. int Epd::Init(char Mode)
  101. {
  102. /* this calls the peripheral hardware interface, see epdif */
  103. if (IfInit() != 0) {
  104. return -1;
  105. }
  106. Reset();
  107. int count;
  108. if(Mode == FULL) {
  109. WaitUntilIdle();
  110. SendCommand(0x12); // soft reset
  111. WaitUntilIdle();
  112. SendCommand(0x74); //set analog block control
  113. SendData(0x54);
  114. SendCommand(0x7E); //set digital block control
  115. SendData(0x3B);
  116. SendCommand(0x01); //Driver output control
  117. SendData(0xF9);
  118. SendData(0x00);
  119. SendData(0x00);
  120. SendCommand(0x11); //data entry mode
  121. SendData(0x01);
  122. SendCommand(0x44); //set Ram-X address start/end position
  123. SendData(0x00);
  124. SendData(0x0F); //0x0C-->(15+1)*8=128
  125. SendCommand(0x45); //set Ram-Y address start/end position
  126. SendData(0xF9); //0xF9-->(249+1)=250
  127. SendData(0x00);
  128. SendData(0x00);
  129. SendData(0x00);
  130. SendCommand(0x3C); //BorderWavefrom
  131. SendData(0x03);
  132. SendCommand(0x2C); //VCOM Voltage
  133. SendData(0x55); //
  134. SendCommand(0x03);
  135. SendData(lut_full_update[70]);
  136. SendCommand(0x04); //
  137. SendData(lut_full_update[71]);
  138. SendData(lut_full_update[72]);
  139. SendData(lut_full_update[73]);
  140. SendCommand(0x3A); //Dummy Line
  141. SendData(lut_full_update[74]);
  142. SendCommand(0x3B); //Gate time
  143. SendData(lut_full_update[75]);
  144. SendCommand(0x32);
  145. for(count = 0; count < 70; count++) {
  146. SendData(lut_full_update[count]);
  147. }
  148. SendCommand(0x4E); // set RAM x address count to 0;
  149. SendData(0x00);
  150. SendCommand(0x4F); // set RAM y address count to 0X127;
  151. SendData(0xF9);
  152. SendData(0x00);
  153. WaitUntilIdle();
  154. } else if(Mode == PART) {
  155. SendCommand(0x2C); //VCOM Voltage
  156. SendData(0x26);
  157. WaitUntilIdle();
  158. SendCommand(0x32);
  159. for(count = 0; count < 70; count++) {
  160. SendData(lut_partial_update[count]);
  161. }
  162. SendCommand(0x37);
  163. SendData(0x00);
  164. SendData(0x00);
  165. SendData(0x00);
  166. SendData(0x00);
  167. SendData(0x40);
  168. SendData(0x00);
  169. SendData(0x00);
  170. SendCommand(0x22);
  171. SendData(0xC0);
  172. SendCommand(0x20);
  173. WaitUntilIdle();
  174. SendCommand(0x3C); //BorderWavefrom
  175. SendData(0x01);
  176. } else {
  177. return -1;
  178. }
  179. return 0;
  180. }
  181. /**
  182. * @brief: module reset.
  183. * often used to awaken the module in deep sleep,
  184. * see Epd::Sleep();
  185. */
  186. void Epd::Reset(void)
  187. {
  188. DigitalWrite(reset_pin, HIGH);
  189. DelayMs(200);
  190. DigitalWrite(reset_pin, LOW); //module reset
  191. DelayMs(10);
  192. DigitalWrite(reset_pin, HIGH);
  193. DelayMs(200);
  194. }
  195. void Epd::Clear(void)
  196. {
  197. int w, h;
  198. w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  199. h = EPD_HEIGHT;
  200. SendCommand(0x24);
  201. for (int j = 0; j < h; j++) {
  202. for (int i = 0; i < w; i++) {
  203. SendData(0xff);
  204. }
  205. }
  206. //DISPLAY REFRESH
  207. SendCommand(0x22);
  208. SendData(0xC7);
  209. SendCommand(0x20);
  210. WaitUntilIdle();
  211. }
  212. void Epd::Display(const unsigned char* frame_buffer)
  213. {
  214. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  215. int h = EPD_HEIGHT;
  216. if (frame_buffer != NULL) {
  217. SendCommand(0x24);
  218. for (int j = 0; j < h; j++) {
  219. for (int i = 0; i < w; i++) {
  220. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  221. }
  222. }
  223. }
  224. //DISPLAY REFRESH
  225. SendCommand(0x22);
  226. SendData(0xC7);
  227. SendCommand(0x20);
  228. WaitUntilIdle();
  229. }
  230. void Epd::DisplayPartBaseImage(const unsigned char* frame_buffer)
  231. {
  232. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  233. int h = EPD_HEIGHT;
  234. if (frame_buffer != NULL) {
  235. SendCommand(0x24);
  236. for (int j = 0; j < h; j++) {
  237. for (int i = 0; i < w; i++) {
  238. SendData(frame_buffer[i + j * w]);
  239. }
  240. }
  241. SendCommand(0x26);
  242. for (int j = 0; j < h; j++) {
  243. for (int i = 0; i < w; i++) {
  244. SendData(frame_buffer[i + j * w]);
  245. }
  246. }
  247. }
  248. //DISPLAY REFRESH
  249. SendCommand(0x22);
  250. SendData(0xC7);
  251. SendCommand(0x20);
  252. WaitUntilIdle();
  253. }
  254. void Epd::DisplayPart(const unsigned char* frame_buffer)
  255. {
  256. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  257. int h = EPD_HEIGHT;
  258. if (frame_buffer != NULL) {
  259. SendCommand(0x24);
  260. for (int j = 0; j < h; j++) {
  261. for (int i = 0; i < w; i++) {
  262. SendData(frame_buffer[i + j * w]);
  263. }
  264. }
  265. }
  266. //DISPLAY REFRESH
  267. SendCommand(0x22);
  268. SendData(0x0C);
  269. SendCommand(0x20);
  270. WaitUntilIdle();
  271. }
  272. /**
  273. * @brief: After this command is transmitted, the chip would enter the
  274. * deep-sleep mode to save power.
  275. * The deep sleep mode would return to standby by hardware reset.
  276. * The only one parameter is a check code, the command would be
  277. * executed if check code = 0xA5.
  278. * You can use Epd::Init() to awaken
  279. */
  280. void Epd::Sleep()
  281. {
  282. SendCommand(0x10); //enter deep sleep
  283. SendData(0x01);
  284. DelayMs(200);
  285. DigitalWrite(reset_pin, LOW);
  286. }
  287. /* END OF FILE */