epd13in3k.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /**
  2. * @filename : epd13in3k.cpp
  3. * @brief : Implements for e-paper library
  4. * @author : Yehui from Waveshare
  5. *
  6. * Copyright (C) Waveshare September 25 2023
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documnetation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdlib.h>
  27. #include "epd13in3k.h"
  28. const unsigned char Lut_Partial[]={
  29. 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  30. 0x2A, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31. 0x15, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32. 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  33. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34. 0x00, 0x01, 0x01, 0x01, 0x00,
  35. 0x0A, 0x00, 0x05, 0x00, 0x00,
  36. 0x00, 0x00, 0x00, 0x00, 0x00,
  37. 0x00, 0x00, 0x00, 0x00, 0x00,
  38. 0x00, 0x00, 0x00, 0x00, 0x00,
  39. 0x00, 0x00, 0x00, 0x00, 0x00,
  40. 0x00, 0x00, 0x00, 0x00, 0x00,
  41. 0x00, 0x00, 0x00, 0x00, 0x00,
  42. 0x00, 0x00, 0x00, 0x00, 0x00,
  43. 0x00, 0x00, 0x00, 0x01, 0x01,
  44. 0x22, 0x22, 0x22, 0x22, 0x22,
  45. 0x17, 0x41, 0xA8, 0x32, 0x18,
  46. 0x00, 0x00,
  47. };
  48. const unsigned char LUT_DATA_4Gray[112] = //112bytes
  49. {
  50. 0x80, 0x48, 0x4A, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51. 0x0A, 0x48, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52. 0x88, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  53. 0xA8, 0x48, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  55. 0x07, 0x23, 0x17, 0x02, 0x00,
  56. 0x05, 0x01, 0x05, 0x01, 0x02,
  57. 0x08, 0x02, 0x01, 0x04, 0x04,
  58. 0x00, 0x02, 0x00, 0x02, 0x01,
  59. 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x00, 0x00,
  61. 0x00, 0x00, 0x00, 0x00, 0x00,
  62. 0x00, 0x00, 0x00, 0x00, 0x00,
  63. 0x00, 0x00, 0x00, 0x00, 0x00,
  64. 0x00, 0x00, 0x00, 0x00, 0x01,
  65. 0x22, 0x22, 0x22, 0x22, 0x22,
  66. 0x17, 0x41, 0xA8, 0x32, 0x30,
  67. 0x00, 0x00,
  68. };
  69. Epd::~Epd() {
  70. };
  71. Epd::Epd() {
  72. reset_pin = RST_PIN;
  73. dc_pin = DC_PIN;
  74. cs_pin = CS_PIN;
  75. busy_pin = BUSY_PIN;
  76. width = EPD_WIDTH;
  77. height = EPD_HEIGHT;
  78. };
  79. int Epd::Init(void) {
  80. if (IfInit() != 0) {
  81. return -1;
  82. }
  83. Reset();
  84. ReadBusy();
  85. SendCommand(0x12);
  86. ReadBusy();
  87. SendCommand(0x0C);
  88. SendData(0xAE);
  89. SendData(0xC7);
  90. SendData(0xC3);
  91. SendData(0xC0);
  92. SendData(0x80);
  93. SendCommand(0x01);
  94. SendData(0xA7);
  95. SendData(0x02);
  96. SendData(0x00);
  97. SendCommand(0x11);
  98. SendData(0x03);
  99. SendCommand(0x44);
  100. SendData(0x00);
  101. SendData(0x00);
  102. SendData(0xBF);
  103. SendData(0x03);
  104. SendCommand(0x45);
  105. SendData(0x00);
  106. SendData(0x00);
  107. SendData(0xA7);
  108. SendData(0x02);
  109. SendCommand(0x3C);
  110. SendData(0x05);
  111. SendCommand(0x18);
  112. SendData(0x80);
  113. SendCommand(0x4E);
  114. SendData(0x00);
  115. SendData(0x00);
  116. SendCommand(0x4F);
  117. SendData(0x00);
  118. SendData(0x00);
  119. ReadBusy();
  120. return 0;
  121. }
  122. int Epd::Init_Part(void) {
  123. unsigned int count;
  124. if (IfInit() != 0) {
  125. return -1;
  126. }
  127. Reset();
  128. ReadBusy();
  129. SendCommand(0x3C);
  130. SendData(0x80);
  131. SendCommand(0x32);
  132. for(count = 0; count < 105 ; count++) {
  133. SendData(Lut_Partial[count]);
  134. }
  135. SendCommand(0x03);
  136. SendData(Lut_Partial[105]);
  137. SendCommand(0x04);
  138. SendData(Lut_Partial[106]);
  139. SendData(Lut_Partial[107]);
  140. SendData(Lut_Partial[108]);
  141. SendCommand(0x2C);
  142. SendData(Lut_Partial[109]);
  143. SendCommand(0x37);
  144. SendData(0x00);
  145. SendData(0x00);
  146. SendData(0x00);
  147. SendData(0x00);
  148. SendData(0x00);
  149. SendData(0x40);
  150. SendData(0x00);
  151. SendData(0x00);
  152. SendData(0x00);
  153. SendData(0x00);
  154. SendCommand(0x3C);
  155. SendData(0x80);
  156. SendCommand(0x22);
  157. SendData(0xC0);
  158. SendCommand(0x20);
  159. ReadBusy();
  160. return 0;
  161. }
  162. int Epd::Init_4GRAY(void) {
  163. unsigned int count;
  164. if (IfInit() != 0) {
  165. return -1;
  166. }
  167. ReadBusy();
  168. SendCommand(0x12); //SWRESET
  169. ReadBusy();
  170. SendCommand(0x0C); //set soft start
  171. SendData(0xAE);
  172. SendData(0xC7);
  173. SendData(0xC3);
  174. SendData(0xC0);
  175. SendData(0x80);
  176. SendCommand(0x01);
  177. SendData(0xA7);
  178. SendData(0x02);
  179. SendData(0x00);
  180. SendCommand(0x11);
  181. SendData(0x03);
  182. SendCommand(0x44);
  183. SendData(0x00);
  184. SendData(0x00);
  185. SendData(0xBF);
  186. SendData(0x03);
  187. SendCommand(0x45);
  188. SendData(0x00);
  189. SendData(0x00);
  190. SendData(0xA7);
  191. SendData(0x02);
  192. SendCommand(0x3C); // Border Border setting
  193. SendData(0x00);
  194. SendCommand(0x18); // use the internal temperature sensor
  195. SendData(0x80);
  196. SendCommand(0x4E);
  197. SendData(0x00);
  198. SendData(0x00);
  199. SendCommand(0x4F);
  200. SendData(0x00);
  201. SendData(0x00);
  202. SendCommand(0x32);
  203. for(count = 0; count < 105 ; count++) {
  204. SendData(LUT_DATA_4Gray[count]);
  205. }
  206. SendCommand(0x03);
  207. SendData(LUT_DATA_4Gray[105]);
  208. SendCommand(0x04);
  209. SendData(LUT_DATA_4Gray[106]);
  210. SendData(LUT_DATA_4Gray[107]);
  211. SendData(LUT_DATA_4Gray[108]);
  212. SendCommand(0x2C);
  213. SendData(LUT_DATA_4Gray[109]);
  214. ReadBusy();
  215. return 0;
  216. }
  217. /**
  218. * @brief: basic function for sending commands
  219. */
  220. void Epd::SendCommand(unsigned char command) {
  221. DigitalWrite(dc_pin, LOW);
  222. SpiTransfer(command);
  223. }
  224. /**
  225. * @brief: basic function for sending data
  226. */
  227. void Epd::SendData(unsigned char data) {
  228. DigitalWrite(dc_pin, HIGH);
  229. SpiTransfer(data);
  230. }
  231. /**
  232. * @brief: Wait until the busy_pin goes HIGH
  233. */
  234. void Epd::ReadBusy(void) {
  235. unsigned char busy;
  236. Serial.print("e-Paper Busy\r\n ");
  237. do{
  238. DelayMs(20);
  239. busy = DigitalRead(busy_pin);
  240. }while(busy == 1);
  241. Serial.print("e-Paper Busy Release\r\n ");
  242. DelayMs(20);
  243. }
  244. /**
  245. * @brief: Refresh function
  246. */
  247. void Epd::TurnOnDisplay(void) {
  248. SendCommand(0x22);
  249. SendData(0xF7);
  250. SendCommand(0x20);
  251. ReadBusy();
  252. }
  253. void Epd::TurnOnDisplay_Part(void)
  254. {
  255. SendCommand(0x22); //Display Update Control
  256. SendData(0xCF);
  257. SendCommand(0x20); //Activate Display Update Sequence
  258. ReadBusy();
  259. }
  260. void Epd::TurnOnDisplay_4GRAY(void)
  261. {
  262. SendCommand(0x22);
  263. SendData(0xC7);
  264. SendCommand(0x20);
  265. ReadBusy();
  266. }
  267. /**
  268. * @brief: module reset.
  269. * often used to awaken the module in deep sleep,
  270. * see Epd::Sleep();
  271. */
  272. void Epd::Reset(void) {
  273. DigitalWrite(reset_pin, HIGH);
  274. DelayMs(20);
  275. DigitalWrite(reset_pin, LOW); //module reset
  276. DelayMs(2);
  277. DigitalWrite(reset_pin, HIGH);
  278. DelayMs(20);
  279. }
  280. void Epd::DisplayFrame(const unsigned char* frame_buffer) {
  281. SendCommand(0x24);
  282. for (unsigned long j = 0; j < height; j++) {
  283. for (unsigned long i = 0; i < width/8; i++) {
  284. SendData(frame_buffer[i + j * width/8]);
  285. }
  286. }
  287. TurnOnDisplay();
  288. }
  289. void Epd::Displaypart(const unsigned char* pbuffer, unsigned long xStart, unsigned long yStart,unsigned long Picture_Width,unsigned long Picture_Height) {
  290. SendCommand(0x24);
  291. for (unsigned long j = 0; j < height; j++) {
  292. for (unsigned long i = 0; i < width/8; i++) {
  293. if( (j>=yStart) && (j<yStart+Picture_Height) && (i*8>=xStart) && (i*8<xStart+Picture_Width)){
  294. SendData((pgm_read_byte(&(pbuffer[i-xStart/8 + (Picture_Width)/8*(j-yStart)]))) );
  295. // SendData(0xff);
  296. }else {
  297. SendData(0xFF);
  298. }
  299. }
  300. }
  301. TurnOnDisplay();
  302. }
  303. void Epd::Display_Base(const unsigned char* frame_buffer) {
  304. SendCommand(0x24);
  305. for (unsigned long j = 0; j < height; j++) {
  306. for (unsigned long i = 0; i < width/8; i++) {
  307. SendData(frame_buffer[i + j * width/8]);
  308. }
  309. }
  310. SendCommand(0x26);
  311. for (unsigned long j = 0; j < height; j++) {
  312. for (unsigned long i = 0; i < width/8; i++) {
  313. SendData(frame_buffer[i + j * width/8]);
  314. }
  315. }
  316. TurnOnDisplay();
  317. }
  318. void Epd::Display_Part(unsigned char *Image, unsigned long x, unsigned long y, unsigned long w, unsigned long l)
  319. {
  320. unsigned long i;
  321. unsigned long HEIGHT = l;
  322. unsigned long WIDTH = (w % 8 == 0)? (w / 8 ): (w / 8 + 1);
  323. Reset();
  324. SendCommand(0x18); // use the internal temperature sensor
  325. SendData(0x80);
  326. SendCommand(0x3C); // Border Border setting
  327. SendData(0x80);
  328. SendCommand(0x44);
  329. SendData(x & 0xFF);
  330. SendData((x>>8) & 0x03);
  331. SendData((x+w-1) & 0xFF);
  332. SendData(((x+w-1)>>8) & 0x03);
  333. SendCommand(0x45);
  334. SendData(y & 0xFF);
  335. SendData((y>>8) & 0x03);
  336. SendData((y+l-1) & 0xFF);
  337. SendData(((y+l-1)>>8) & 0x03);
  338. SendCommand(0x4E);
  339. SendData(x & 0xFF);
  340. SendData((x>>8) & 0x03);
  341. SendCommand(0x4F);
  342. SendData(y & 0xFF);
  343. SendData((y>>8) & 0x03);
  344. SendCommand(0x24); //write RAM for black(0)/white (1)
  345. for (unsigned long j = 0; j < HEIGHT; j++) {
  346. for (unsigned long i = 0; i < WIDTH; i++) {
  347. SendData(Image[i + j * WIDTH]);
  348. }
  349. }
  350. TurnOnDisplay_Part();
  351. }
  352. /**
  353. * @brief: After this command is transmitted, the chip would enter the
  354. * deep-sleep mode to save power.
  355. * The deep sleep mode would return to standby by hardware reset.
  356. * The only one parameter is a check code, the command would be
  357. * executed if check code = 0xA5.
  358. * You can use EPD_Reset() to awaken
  359. */
  360. void Epd::Sleep(void) {
  361. SendCommand(0x10);
  362. SendData(0x03);
  363. DelayMs(100);
  364. ReadBusy();
  365. }
  366. void Epd::Clear(void) {
  367. SendCommand(0x24);
  368. for(unsigned long i=0; i<height*width/8; i++) {
  369. SendData(0xFF);
  370. }
  371. TurnOnDisplay();
  372. }
  373. /* END OF FILE */