epd2in13_V3.cpp 13 KB

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