epd2in13_V3.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*****************************************************************************
  2. * | File : epd2in13_V3.cpp
  3. * | Author : Waveshare team
  4. * | Function : 2.13inch e-paper V3
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2021-11-01
  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_V3.h"
  32. const unsigned char lut_full_update[]= {
  33. 0x80, 0x4A, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  34. 0x40, 0x4A, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  35. 0x80, 0x4A, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  36. 0x40, 0x4A, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  37. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  38. 0xF, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  39. 0xF, 0x0, 0x0, 0xF, 0x0, 0x0, 0x2,
  40. 0xF, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  41. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  42. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  43. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  44. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  45. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  46. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  47. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  48. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  49. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  50. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x0, 0x0, 0x0,
  51. 0x22, 0x17, 0x41, 0x0, 0x32, 0x36
  52. };
  53. const unsigned char lut_partial_update[]= {
  54. 0x0,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  55. 0x80,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  56. 0x40,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  57. 0x0,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  58. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  59. 0x14,0x0,0x0,0x0,0x0,0x0,0x0,
  60. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  61. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  62. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  63. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  64. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  65. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  66. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  67. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  68. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  69. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  70. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  71. 0x22,0x22,0x22,0x22,0x22,0x22,0x0,0x0,0x0,
  72. 0x22,0x17,0x41,0x00,0x32,0x36,
  73. };
  74. Epd::~Epd()
  75. {
  76. };
  77. Epd::Epd()
  78. {
  79. reset_pin = RST_PIN;
  80. dc_pin = DC_PIN;
  81. cs_pin = CS_PIN;
  82. busy_pin = BUSY_PIN;
  83. width = EPD_WIDTH;
  84. height = EPD_HEIGHT;
  85. };
  86. /**
  87. * @brief: basic function for sending commands
  88. */
  89. void Epd::SendCommand(unsigned char command)
  90. {
  91. DigitalWrite(dc_pin, LOW);
  92. SpiTransfer(command);
  93. }
  94. /**
  95. * @brief: basic function for sending data
  96. */
  97. void Epd::SendData(unsigned char data)
  98. {
  99. DigitalWrite(dc_pin, HIGH);
  100. SpiTransfer(data);
  101. }
  102. /**
  103. * @brief: Wait until the busy_pin goes HIGH
  104. */
  105. void Epd::WaitUntilIdle(void)
  106. {
  107. while(1) { //LOW: idle, HIGH: busy
  108. if(DigitalRead(busy_pin) == 0)
  109. break;
  110. DelayMs(10);
  111. }
  112. }
  113. void Epd::SetWindows(unsigned char Xstart, unsigned char Ystart, unsigned char Xend, unsigned char Yend)
  114. {
  115. SendCommand(0x44); // SET_RAM_X_ADDRESS_START_END_POSITION
  116. SendData((Xstart>>3) & 0xFF);
  117. SendData((Xend>>3) & 0xFF);
  118. SendCommand(0x45); // SET_RAM_Y_ADDRESS_START_END_POSITION
  119. SendData(Ystart & 0xFF);
  120. SendData((Ystart >> 8) & 0xFF);
  121. SendData(Yend & 0xFF);
  122. SendData((Yend >> 8) & 0xFF);
  123. }
  124. void Epd::SetCursor(unsigned char Xstart, unsigned char Ystart)
  125. {
  126. SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  127. SendData(Xstart & 0xFF);
  128. SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  129. SendData(Ystart & 0xFF);
  130. SendData((Ystart >> 8) & 0xFF);
  131. }
  132. void Epd::Lut(unsigned char *lut)
  133. {
  134. unsigned char count;
  135. SendCommand(0x32);
  136. for(count = 0; count < 153; count++) {
  137. SendData(lut[count]);
  138. }
  139. SendCommand(0x3f);
  140. SendData(*(lut+153));
  141. SendCommand(0x03); // gate voltage
  142. SendData(*(lut+154));
  143. SendCommand(0x04); // source voltage
  144. SendData(*(lut+155)); // VSH
  145. SendData(*(lut+156)); // VSH2
  146. SendData(*(lut+157)); // VSL
  147. SendCommand(0x2c); // VCOM
  148. SendData(*(lut+158));
  149. }
  150. int Epd::Init(char Mode)
  151. {
  152. /* this calls the peripheral hardware interface, see epdif */
  153. if (IfInit() != 0) {
  154. return -1;
  155. }
  156. Reset();
  157. int count;
  158. if(Mode == FULL) {
  159. WaitUntilIdle();
  160. SendCommand(0x12); // soft reset
  161. WaitUntilIdle();
  162. SendCommand(0x01); //Driver output control
  163. SendData(0xF9);
  164. SendData(0x00);
  165. SendData(0x00);
  166. SendCommand(0x11); //data entry mode
  167. SendData(0x03);
  168. SetWindows(0, 0, EPD_WIDTH-1, EPD_HEIGHT-1);
  169. SetCursor(0, 0);
  170. SendCommand(0x3C); //BorderWavefrom
  171. SendData(0x05);
  172. SendCommand(0x21); // Display update control
  173. SendData(0x00);
  174. SendData(0x80);
  175. SendCommand(0x18); //Read built-in temperature sensor
  176. SendData(0x80);
  177. WaitUntilIdle();
  178. Lut(lut_full_update);
  179. } else if(Mode == PART) {
  180. DigitalWrite(reset_pin, LOW); //module reset
  181. DelayMs(1);
  182. DigitalWrite(reset_pin, HIGH);
  183. Lut(lut_partial_update);
  184. SendCommand(0x37);
  185. SendData(0x00);
  186. SendData(0x00);
  187. SendData(0x00);
  188. SendData(0x00);
  189. SendData(0x00);
  190. SendData(0x40);
  191. SendData(0x00);
  192. SendData(0x00);
  193. SendData(0x00);
  194. SendData(0x00);
  195. SendCommand(0x3C); //BorderWavefrom
  196. SendData(0x80);
  197. SendCommand(0x22); //Display Update Sequence Option
  198. SendData(0xC0); // Enable clock and Enable analog
  199. SendCommand(0x20); //Activate Display Update Sequence
  200. WaitUntilIdle();
  201. SetWindows(0, 0, EPD_WIDTH-1, EPD_HEIGHT-1);
  202. SetCursor(0, 0);
  203. } else {
  204. return -1;
  205. }
  206. return 0;
  207. }
  208. /**
  209. * @brief: module reset.
  210. * often used to awaken the module in deep sleep,
  211. * see Epd::Sleep();
  212. */
  213. void Epd::Reset(void)
  214. {
  215. DigitalWrite(reset_pin, HIGH);
  216. DelayMs(20);
  217. DigitalWrite(reset_pin, LOW); //module reset
  218. DelayMs(2);
  219. DigitalWrite(reset_pin, HIGH);
  220. DelayMs(20);
  221. }
  222. void Epd::Clear(void)
  223. {
  224. int w, h;
  225. w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  226. h = EPD_HEIGHT;
  227. SendCommand(0x24);
  228. for (int j = 0; j < h; j++) {
  229. for (int i = 0; i < w; i++) {
  230. SendData(0xff);
  231. }
  232. }
  233. //DISPLAY REFRESH
  234. SendCommand(0x22);
  235. SendData(0xC7);
  236. SendCommand(0x20);
  237. WaitUntilIdle();
  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. if (frame_buffer != NULL) {
  244. SendCommand(0x24);
  245. for (int j = 0; j < h; j++) {
  246. for (int i = 0; i < w; i++) {
  247. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  248. }
  249. }
  250. }
  251. //DISPLAY REFRESH
  252. SendCommand(0x22);
  253. SendData(0xC7);
  254. SendCommand(0x20);
  255. WaitUntilIdle();
  256. }
  257. void Epd::DisplayPartBaseImage(const unsigned char* frame_buffer)
  258. {
  259. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  260. int h = EPD_HEIGHT;
  261. if (frame_buffer != NULL) {
  262. SendCommand(0x24);
  263. for (int j = 0; j < h; j++) {
  264. for (int i = 0; i < w; i++) {
  265. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  266. }
  267. }
  268. SendCommand(0x26);
  269. for (int j = 0; j < h; j++) {
  270. for (int i = 0; i < w; i++) {
  271. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  272. }
  273. }
  274. }
  275. //DISPLAY REFRESH
  276. SendCommand(0x22);
  277. SendData(0xC7);
  278. SendCommand(0x20);
  279. WaitUntilIdle();
  280. }
  281. void Epd::DisplayPart(const unsigned char* frame_buffer)
  282. {
  283. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  284. int h = EPD_HEIGHT;
  285. if (frame_buffer != NULL) {
  286. SendCommand(0x24);
  287. for (int j = 0; j < h; j++) {
  288. for (int i = 0; i < w; i++) {
  289. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  290. }
  291. }
  292. }
  293. //DISPLAY REFRESH
  294. SendCommand(0x22);
  295. SendData(0x0f);
  296. SendCommand(0x20);
  297. WaitUntilIdle();
  298. }
  299. void Epd::ClearPart(void)
  300. {
  301. int w, h;
  302. w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  303. h = EPD_HEIGHT;
  304. SendCommand(0x24);
  305. for (int j = 0; j < h; j++) {
  306. for (int i = 0; i < w; i++) {
  307. SendData(0xff);
  308. }
  309. }
  310. //DISPLAY REFRESH
  311. SendCommand(0x22);
  312. SendData(0x0f);
  313. SendCommand(0x20);
  314. WaitUntilIdle();
  315. }
  316. /**
  317. * @brief: After this command is transmitted, the chip would enter the
  318. * deep-sleep mode to save power.
  319. * The deep sleep mode would return to standby by hardware reset.
  320. * The only one parameter is a check code, the command would be
  321. * executed if check code = 0xA5.
  322. * You can use Epd::Init() to awaken
  323. */
  324. void Epd::Sleep()
  325. {
  326. SendCommand(0x10); //enter deep sleep
  327. SendData(0x01);
  328. DelayMs(200);
  329. DigitalWrite(reset_pin, LOW);
  330. }
  331. /* END OF FILE */