epd2in13_V2.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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(1) { //LOW: idle, HIGH: busy
  96. if(DigitalRead(busy_pin) == 0)
  97. break;
  98. DelayMs(100);
  99. }
  100. }
  101. int Epd::Init(char Mode)
  102. {
  103. /* this calls the peripheral hardware interface, see epdif */
  104. if (IfInit() != 0) {
  105. return -1;
  106. }
  107. Reset();
  108. int count;
  109. if(Mode == FULL) {
  110. WaitUntilIdle();
  111. SendCommand(0x12); // soft reset
  112. WaitUntilIdle();
  113. SendCommand(0x74); //set analog block control
  114. SendData(0x54);
  115. SendCommand(0x7E); //set digital block control
  116. SendData(0x3B);
  117. SendCommand(0x01); //Driver output control
  118. SendData(0xF9);
  119. SendData(0x00);
  120. SendData(0x00);
  121. SendCommand(0x11); //data entry mode
  122. SendData(0x01);
  123. SendCommand(0x44); //set Ram-X address start/end position
  124. SendData(0x00);
  125. SendData(0x0F); //0x0C-->(15+1)*8=128
  126. SendCommand(0x45); //set Ram-Y address start/end position
  127. SendData(0xF9); //0xF9-->(249+1)=250
  128. SendData(0x00);
  129. SendData(0x00);
  130. SendData(0x00);
  131. SendCommand(0x3C); //BorderWavefrom
  132. SendData(0x03);
  133. SendCommand(0x2C); //VCOM Voltage
  134. SendData(0x55); //
  135. SendCommand(0x03);
  136. SendData(lut_full_update[70]);
  137. SendCommand(0x04); //
  138. SendData(lut_full_update[71]);
  139. SendData(lut_full_update[72]);
  140. SendData(lut_full_update[73]);
  141. SendCommand(0x3A); //Dummy Line
  142. SendData(lut_full_update[74]);
  143. SendCommand(0x3B); //Gate time
  144. SendData(lut_full_update[75]);
  145. SendCommand(0x32);
  146. for(count = 0; count < 70; count++) {
  147. SendData(lut_full_update[count]);
  148. }
  149. SendCommand(0x4E); // set RAM x address count to 0;
  150. SendData(0x00);
  151. SendCommand(0x4F); // set RAM y address count to 0X127;
  152. SendData(0xF9);
  153. SendData(0x00);
  154. WaitUntilIdle();
  155. } else if(Mode == PART) {
  156. SendCommand(0x2C); //VCOM Voltage
  157. SendData(0x26);
  158. WaitUntilIdle();
  159. SendCommand(0x32);
  160. for(count = 0; count < 70; count++) {
  161. SendData(lut_partial_update[count]);
  162. }
  163. SendCommand(0x37);
  164. SendData(0x00);
  165. SendData(0x00);
  166. SendData(0x00);
  167. SendData(0x00);
  168. SendData(0x40);
  169. SendData(0x00);
  170. SendData(0x00);
  171. SendCommand(0x22);
  172. SendData(0xC0);
  173. SendCommand(0x20);
  174. WaitUntilIdle();
  175. SendCommand(0x3C); //BorderWavefrom
  176. SendData(0x01);
  177. } else {
  178. return -1;
  179. }
  180. return 0;
  181. }
  182. /**
  183. * @brief: module reset.
  184. * often used to awaken the module in deep sleep,
  185. * see Epd::Sleep();
  186. */
  187. void Epd::Reset(void)
  188. {
  189. DigitalWrite(reset_pin, HIGH);
  190. DelayMs(200);
  191. DigitalWrite(reset_pin, LOW); //module reset
  192. DelayMs(10);
  193. DigitalWrite(reset_pin, HIGH);
  194. DelayMs(200);
  195. }
  196. void Epd::Clear(void)
  197. {
  198. int w, h;
  199. w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  200. h = EPD_HEIGHT;
  201. SendCommand(0x24);
  202. for (int j = 0; j < h; j++) {
  203. for (int i = 0; i < w; i++) {
  204. SendData(0xff);
  205. }
  206. }
  207. //DISPLAY REFRESH
  208. SendCommand(0x22);
  209. SendData(0xC7);
  210. SendCommand(0x20);
  211. WaitUntilIdle();
  212. }
  213. void Epd::Display(const unsigned char* frame_buffer)
  214. {
  215. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  216. int h = EPD_HEIGHT;
  217. if (frame_buffer != NULL) {
  218. SendCommand(0x24);
  219. for (int j = 0; j < h; j++) {
  220. for (int i = 0; i < w; i++) {
  221. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  222. }
  223. }
  224. }
  225. //DISPLAY REFRESH
  226. SendCommand(0x22);
  227. SendData(0xC7);
  228. SendCommand(0x20);
  229. WaitUntilIdle();
  230. }
  231. void Epd::DisplayPartBaseImage(const unsigned char* frame_buffer)
  232. {
  233. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  234. int h = EPD_HEIGHT;
  235. if (frame_buffer != NULL) {
  236. SendCommand(0x24);
  237. for (int j = 0; j < h; j++) {
  238. for (int i = 0; i < w; i++) {
  239. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  240. }
  241. }
  242. SendCommand(0x26);
  243. for (int j = 0; j < h; j++) {
  244. for (int i = 0; i < w; i++) {
  245. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  246. }
  247. }
  248. }
  249. //DISPLAY REFRESH
  250. SendCommand(0x22);
  251. SendData(0xC7);
  252. SendCommand(0x20);
  253. WaitUntilIdle();
  254. }
  255. void Epd::DisplayPart(const unsigned char* frame_buffer)
  256. {
  257. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  258. int h = EPD_HEIGHT;
  259. if (frame_buffer != NULL) {
  260. SendCommand(0x24);
  261. for (int j = 0; j < h; j++) {
  262. for (int i = 0; i < w; i++) {
  263. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  264. }
  265. }
  266. }
  267. //DISPLAY REFRESH
  268. SendCommand(0x22);
  269. SendData(0x0C);
  270. SendCommand(0x20);
  271. WaitUntilIdle();
  272. }
  273. void Epd::ClearPart(void)
  274. {
  275. int w, h;
  276. w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  277. h = EPD_HEIGHT;
  278. SendCommand(0x24);
  279. for (int j = 0; j < h; j++) {
  280. for (int i = 0; i < w; i++) {
  281. SendData(0xff);
  282. }
  283. }
  284. //DISPLAY REFRESH
  285. SendCommand(0x22);
  286. SendData(0x0C);
  287. SendCommand(0x20);
  288. WaitUntilIdle();
  289. }
  290. /**
  291. * @brief: After this command is transmitted, the chip would enter the
  292. * deep-sleep mode to save power.
  293. * The deep sleep mode would return to standby by hardware reset.
  294. * The only one parameter is a check code, the command would be
  295. * executed if check code = 0xA5.
  296. * You can use Epd::Init() to awaken
  297. */
  298. void Epd::Sleep()
  299. {
  300. SendCommand(0x10); //enter deep sleep
  301. SendData(0x01);
  302. DelayMs(200);
  303. DigitalWrite(reset_pin, LOW);
  304. }
  305. /* END OF FILE */