epd2in13_V3.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. /******************************************************************************
  78. function : Pin definition
  79. parameter:
  80. ******************************************************************************/
  81. Epd::Epd()
  82. {
  83. reset_pin = RST_PIN;
  84. dc_pin = DC_PIN;
  85. cs_pin = CS_PIN;
  86. busy_pin = BUSY_PIN;
  87. width = EPD_WIDTH;
  88. height = EPD_HEIGHT;
  89. };
  90. /******************************************************************************
  91. function : send command
  92. parameter:
  93. command : Command register
  94. ******************************************************************************/
  95. void Epd::SendCommand(unsigned char command)
  96. {
  97. DigitalWrite(dc_pin, LOW);
  98. SpiTransfer(command);
  99. }
  100. /******************************************************************************
  101. function : send data
  102. parameter:
  103. Data : Write data
  104. ******************************************************************************/
  105. void Epd::SendData(unsigned char data)
  106. {
  107. DigitalWrite(dc_pin, HIGH);
  108. SpiTransfer(data);
  109. }
  110. /******************************************************************************
  111. function : Wait until the busy_pin goes LOW
  112. parameter:
  113. ******************************************************************************/
  114. void Epd::WaitUntilIdle(void)
  115. {
  116. while(1) { //LOW: idle, HIGH: busy
  117. if(DigitalRead(busy_pin) == 0)
  118. break;
  119. DelayMs(10);
  120. }
  121. }
  122. /******************************************************************************
  123. function : Setting the display window
  124. parameter:
  125. Xstart : X-axis starting position
  126. Ystart : Y-axis starting position
  127. Xend : End position of X-axis
  128. Yend : End position of Y-axis
  129. ******************************************************************************/
  130. void Epd::SetWindows(unsigned char Xstart, unsigned char Ystart, unsigned char Xend, unsigned char Yend)
  131. {
  132. SendCommand(0x44); // SET_RAM_X_ADDRESS_START_END_POSITION
  133. SendData((Xstart>>3) & 0xFF);
  134. SendData((Xend>>3) & 0xFF);
  135. SendCommand(0x45); // SET_RAM_Y_ADDRESS_START_END_POSITION
  136. SendData(Ystart & 0xFF);
  137. SendData((Ystart >> 8) & 0xFF);
  138. SendData(Yend & 0xFF);
  139. SendData((Yend >> 8) & 0xFF);
  140. }
  141. /******************************************************************************
  142. function : Set Cursor
  143. parameter:
  144. Xstart : X-axis starting position
  145. Ystart : Y-axis starting position
  146. ******************************************************************************/
  147. void Epd::SetCursor(unsigned char Xstart, unsigned char Ystart)
  148. {
  149. SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  150. SendData(Xstart & 0xFF);
  151. SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  152. SendData(Ystart & 0xFF);
  153. SendData((Ystart >> 8) & 0xFF);
  154. }
  155. /******************************************************************************
  156. function : Send lut data and configuration
  157. parameter:
  158. lut : lut data
  159. ******************************************************************************/
  160. void Epd::Lut(unsigned char *lut)
  161. {
  162. unsigned char count;
  163. SendCommand(0x32);
  164. for(count = 0; count < 153; count++) {
  165. SendData(lut[count]);
  166. }
  167. SendCommand(0x3f);
  168. SendData(*(lut+153));
  169. SendCommand(0x03); // gate voltage
  170. SendData(*(lut+154));
  171. SendCommand(0x04); // source voltage
  172. SendData(*(lut+155)); // VSH
  173. SendData(*(lut+156)); // VSH2
  174. SendData(*(lut+157)); // VSL
  175. SendCommand(0x2c); // VCOM
  176. SendData(*(lut+158));
  177. }
  178. /******************************************************************************
  179. function : Initialize the e-Paper register
  180. parameter:
  181. Mode : Mode selection
  182. ******************************************************************************/
  183. int Epd::Init(char Mode)
  184. {
  185. /* this calls the peripheral hardware interface, see epdif */
  186. if (IfInit() != 0) {
  187. return -1;
  188. }
  189. Reset();
  190. int count;
  191. if(Mode == FULL) {
  192. WaitUntilIdle();
  193. SendCommand(0x12); // soft reset
  194. WaitUntilIdle();
  195. SendCommand(0x01); //Driver output control
  196. SendData(0xF9);
  197. SendData(0x00);
  198. SendData(0x00);
  199. SendCommand(0x11); //data entry mode
  200. SendData(0x03);
  201. SetWindows(0, 0, EPD_WIDTH-1, EPD_HEIGHT-1);
  202. SetCursor(0, 0);
  203. SendCommand(0x3C); //BorderWavefrom
  204. SendData(0x05);
  205. SendCommand(0x21); // Display update control
  206. SendData(0x00);
  207. SendData(0x80);
  208. SendCommand(0x18); //Read built-in temperature sensor
  209. SendData(0x80);
  210. WaitUntilIdle();
  211. Lut(lut_full_update);
  212. } else if(Mode == PART) {
  213. DigitalWrite(reset_pin, LOW); //module reset
  214. DelayMs(1);
  215. DigitalWrite(reset_pin, HIGH);
  216. Lut(lut_partial_update);
  217. SendCommand(0x37);
  218. SendData(0x00);
  219. SendData(0x00);
  220. SendData(0x00);
  221. SendData(0x00);
  222. SendData(0x00);
  223. SendData(0x40);
  224. SendData(0x00);
  225. SendData(0x00);
  226. SendData(0x00);
  227. SendData(0x00);
  228. SendCommand(0x3C); //BorderWavefrom
  229. SendData(0x80);
  230. SendCommand(0x22); //Display Update Sequence Option
  231. SendData(0xC0); // Enable clock and Enable analog
  232. SendCommand(0x20); //Activate Display Update Sequence
  233. WaitUntilIdle();
  234. SetWindows(0, 0, EPD_WIDTH-1, EPD_HEIGHT-1);
  235. SetCursor(0, 0);
  236. } else {
  237. return -1;
  238. }
  239. return 0;
  240. }
  241. /******************************************************************************
  242. function : Software reset
  243. parameter:
  244. ******************************************************************************/
  245. void Epd::Reset(void)
  246. {
  247. DigitalWrite(reset_pin, HIGH);
  248. DelayMs(20);
  249. DigitalWrite(reset_pin, LOW); //module reset
  250. DelayMs(2);
  251. DigitalWrite(reset_pin, HIGH);
  252. DelayMs(20);
  253. }
  254. /******************************************************************************
  255. function : Clear screen
  256. parameter:
  257. ******************************************************************************/
  258. void Epd::Clear(void)
  259. {
  260. int w, h;
  261. w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  262. h = EPD_HEIGHT;
  263. SendCommand(0x24);
  264. for (int j = 0; j < h; j++) {
  265. for (int i = 0; i < w; i++) {
  266. SendData(0xff);
  267. }
  268. }
  269. //DISPLAY REFRESH
  270. SendCommand(0x22);
  271. SendData(0xC7);
  272. SendCommand(0x20);
  273. WaitUntilIdle();
  274. }
  275. /******************************************************************************
  276. function : Sends the image buffer in RAM to e-Paper and displays
  277. parameter:
  278. frame_buffer : Image data
  279. ******************************************************************************/
  280. void Epd::Display(const unsigned char* frame_buffer)
  281. {
  282. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  283. int h = EPD_HEIGHT;
  284. if (frame_buffer != NULL) {
  285. SendCommand(0x24);
  286. for (int j = 0; j < h; j++) {
  287. for (int i = 0; i < w; i++) {
  288. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  289. }
  290. }
  291. }
  292. //DISPLAY REFRESH
  293. SendCommand(0x22);
  294. SendData(0xC7);
  295. SendCommand(0x20);
  296. WaitUntilIdle();
  297. }
  298. /******************************************************************************
  299. function : Refresh a base image
  300. parameter:
  301. frame_buffer : Image data
  302. ******************************************************************************/
  303. void Epd::DisplayPartBaseImage(const unsigned char* frame_buffer)
  304. {
  305. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  306. int h = EPD_HEIGHT;
  307. if (frame_buffer != NULL) {
  308. SendCommand(0x24);
  309. for (int j = 0; j < h; j++) {
  310. for (int i = 0; i < w; i++) {
  311. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  312. }
  313. }
  314. SendCommand(0x26);
  315. for (int j = 0; j < h; j++) {
  316. for (int i = 0; i < w; i++) {
  317. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  318. }
  319. }
  320. }
  321. //DISPLAY REFRESH
  322. SendCommand(0x22);
  323. SendData(0xC7);
  324. SendCommand(0x20);
  325. WaitUntilIdle();
  326. }
  327. /******************************************************************************
  328. function : Sends the image buffer in RAM to e-Paper and partial refresh
  329. parameter:
  330. frame_buffer : Image data
  331. ******************************************************************************/
  332. void Epd::DisplayPart(const unsigned char* )
  333. {
  334. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  335. int h = EPD_HEIGHT;
  336. if (frame_buffer != NULL) {
  337. SendCommand(0x24);
  338. for (int j = 0; j < h; j++) {
  339. for (int i = 0; i < w; i++) {
  340. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  341. }
  342. }
  343. }
  344. //DISPLAY REFRESH
  345. SendCommand(0x22);
  346. SendData(0x0f);
  347. SendCommand(0x20);
  348. WaitUntilIdle();
  349. }
  350. /******************************************************************************
  351. function : Clear screen
  352. parameter:
  353. ******************************************************************************/
  354. void Epd::ClearPart(void)
  355. {
  356. int w, h;
  357. w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  358. h = EPD_HEIGHT;
  359. SendCommand(0x24);
  360. for (int j = 0; j < h; j++) {
  361. for (int i = 0; i < w; i++) {
  362. SendData(0xff);
  363. }
  364. }
  365. //DISPLAY REFRESH
  366. SendCommand(0x22);
  367. SendData(0x0f);
  368. SendCommand(0x20);
  369. WaitUntilIdle();
  370. }
  371. /******************************************************************************
  372. function : Enter sleep mode
  373. parameter:
  374. ******************************************************************************/
  375. void Epd::Sleep()
  376. {
  377. SendCommand(0x10); //enter deep sleep
  378. SendData(0x01);
  379. DelayMs(200);
  380. DigitalWrite(reset_pin, LOW);
  381. }
  382. /* END OF FILE */