epd4in26.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /**
  2. * @filename : epd4in26.cpp
  3. * @brief : Implements for e-paper library
  4. * @author : Yehui from Waveshare
  5. *
  6. * Copyright (C) Waveshare 23-12-20
  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 "epd4in26.h"
  28. const unsigned char LUT_DATA_4Gray[112] = //112bytes
  29. {
  30. 0x80, 0x48, 0x4A, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31. 0x0A, 0x48, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32. 0x88, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  33. 0xA8, 0x48, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  35. 0x07, 0x1E, 0x1C, 0x02, 0x00,
  36. 0x05, 0x01, 0x05, 0x01, 0x02,
  37. 0x08, 0x01, 0x01, 0x04, 0x04,
  38. 0x00, 0x02, 0x00, 0x02, 0x01,
  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, 0x00, 0x00,
  44. 0x00, 0x00, 0x00, 0x00, 0x01,
  45. 0x22, 0x22, 0x22, 0x22, 0x22,
  46. 0x17, 0x41, 0xA8, 0x32, 0x30,
  47. 0x00, 0x00,
  48. };
  49. Epd::~Epd() {
  50. };
  51. Epd::Epd() {
  52. reset_pin = RST_PIN;
  53. dc_pin = DC_PIN;
  54. cs_pin = CS_PIN;
  55. busy_pin = BUSY_PIN;
  56. WIDTH = EPD_WIDTH;
  57. HEIGHT = EPD_HEIGHT;
  58. };
  59. int Epd::Init(void) {
  60. if (IfInit() != 0) {
  61. return -1;
  62. }
  63. Reset();
  64. DelayMs(100);
  65. ReadBusy();
  66. SendCommand(0x12); //SWRESET
  67. ReadBusy();
  68. SendCommand(0x18); // use the internal temperature sensor
  69. SendData(0x80);
  70. SendCommand(0x0C); //set soft start
  71. SendData(0xAE);
  72. SendData(0xC7);
  73. SendData(0xC3);
  74. SendData(0xC0);
  75. SendData(0x80);
  76. SendCommand(0x01); // drive output control
  77. SendData((HEIGHT-1)%256); // Y
  78. SendData((HEIGHT-1)/256); // Y
  79. SendData(0x02);
  80. SendCommand(0x3C); // Border Border setting
  81. SendData(0x01);
  82. SendCommand(0x11); // data entry mode
  83. SendData(0x01); // X-mode x+ y-
  84. SetWindows(0, HEIGHT-1, WIDTH-1, 0);
  85. SetCursor(0, 0);
  86. ReadBusy();
  87. return 0;
  88. }
  89. int Epd::Init_Fast(void) {
  90. if (IfInit() != 0) {
  91. return -1;
  92. }
  93. Reset();
  94. DelayMs(100);
  95. ReadBusy();
  96. SendCommand(0x12); //SWRESET
  97. ReadBusy();
  98. SendCommand(0x18); // use the internal temperature sensor
  99. SendData(0x80);
  100. SendCommand(0x0C); //set soft start
  101. SendData(0xAE);
  102. SendData(0xC7);
  103. SendData(0xC3);
  104. SendData(0xC0);
  105. SendData(0x80);
  106. SendCommand(0x01); // drive output control
  107. SendData((HEIGHT-1)%256); // Y
  108. SendData((HEIGHT-1)/256); // Y
  109. SendData(0x02);
  110. SendCommand(0x3C); // Border Border setting
  111. SendData(0x01);
  112. SendCommand(0x11); // data entry mode
  113. SendData(0x01); // X-mode x+ y-
  114. SetWindows(0, HEIGHT-1, WIDTH-1, 0);
  115. SetCursor(0, 0);
  116. ReadBusy();
  117. //TEMP (1.5s)
  118. SendCommand(0x1A);
  119. SendData(0x5A);
  120. SendCommand(0x22);
  121. SendData(0x91);
  122. SendCommand(0x20);
  123. ReadBusy();
  124. return 0;
  125. }
  126. int Epd::Init_4GRAY(void) {
  127. if (IfInit() != 0) {
  128. return -1;
  129. }
  130. Reset();
  131. DelayMs(100);
  132. ReadBusy();
  133. SendCommand(0x12); //SWRESET
  134. ReadBusy();
  135. SendCommand(0x18); // use the internal temperature sensor
  136. SendData(0x80);
  137. SendCommand(0x0C); //set soft start
  138. SendData(0xAE);
  139. SendData(0xC7);
  140. SendData(0xC3);
  141. SendData(0xC0);
  142. SendData(0x80);
  143. SendCommand(0x01); // drive output control
  144. SendData((WIDTH-1)%256); // Y
  145. SendData((WIDTH-1)/256); // Y
  146. SendData(0x02);
  147. SendCommand(0x3C); // Border Border setting
  148. SendData(0x01);
  149. SendCommand(0x11); // data entry mode
  150. SendData(0x01); // X-mode x+ y-
  151. SetWindows(0, HEIGHT-1, WIDTH-1, 0);
  152. SetCursor(0, 0);
  153. ReadBusy();
  154. SetLut();
  155. return 0;
  156. }
  157. /******************************************************************************
  158. function : Setting the display window
  159. parameter:
  160. ******************************************************************************/
  161. void Epd::SetWindows(unsigned long Xstart, unsigned long Ystart, unsigned long Xend, unsigned long Yend)
  162. {
  163. SendCommand(0x44); // SET_RAM_X_ADDRESS_START_END_POSITION
  164. SendData(Xstart & 0xFF);
  165. SendData((Xstart>>8) & 0x03);
  166. SendData(Xend & 0xFF);
  167. SendData((Xend>>8) & 0x03);
  168. SendCommand(0x45); // SET_RAM_Y_ADDRESS_START_END_POSITION
  169. SendData(Ystart & 0xFF);
  170. SendData((Ystart>>8) & 0x03);
  171. SendData(Yend & 0xFF);
  172. SendData((Yend>>8) & 0x03);
  173. }
  174. /******************************************************************************
  175. function : Set Cursor
  176. parameter:
  177. ******************************************************************************/
  178. void Epd::SetCursor(unsigned long Xstart, unsigned long Ystart)
  179. {
  180. SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  181. SendData(Xstart & 0xFF);
  182. SendData((Xstart>>8) & 0x03);
  183. SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  184. SendData(Ystart & 0xFF);
  185. SendData((Ystart>>8) & 0x03);
  186. }
  187. /**
  188. * @brief: basic function for sending commands
  189. */
  190. void Epd::SendCommand(unsigned char command) {
  191. DigitalWrite(dc_pin, LOW);
  192. SpiTransfer(command);
  193. }
  194. /**
  195. * @brief: basic function for sending data
  196. */
  197. void Epd::SendData(unsigned char data) {
  198. DigitalWrite(dc_pin, HIGH);
  199. SpiTransfer(data);
  200. }
  201. /**
  202. * @brief: Wait until the busy_pin goes HIGH
  203. */
  204. void Epd::ReadBusy(void) {
  205. unsigned char busy;
  206. Serial.print("e-Paper Busy\r\n ");
  207. while(1)
  208. { //=1 BUSY
  209. if(DigitalRead(busy_pin)==0)
  210. break;
  211. DelayMs(20);
  212. }
  213. Serial.print("e-Paper Busy Release\r\n ");
  214. DelayMs(20);
  215. }
  216. /**
  217. * @brief: module reset.
  218. * often used to awaken the module in deep sleep,
  219. * see Epd::Sleep();
  220. */
  221. void Epd::Reset(void) {
  222. DigitalWrite(reset_pin, HIGH);
  223. DelayMs(20);
  224. DigitalWrite(reset_pin, LOW); //module reset
  225. DelayMs(4);
  226. DigitalWrite(reset_pin, HIGH);
  227. DelayMs(20);
  228. }
  229. /******************************************************************************
  230. function : Turn On Display
  231. parameter:
  232. ******************************************************************************/
  233. void Epd::TurnOnDisplay(void)
  234. {
  235. SendCommand(0x22); //Display Update Control
  236. SendData(0xF7);
  237. SendCommand(0x20); //Activate Display Update Sequence
  238. ReadBusy();
  239. }
  240. void Epd::TurnOnDisplay_Fast(void)
  241. {
  242. SendCommand(0x22); //Display Update Control
  243. SendData(0xC7);
  244. SendCommand(0x20); //Activate Display Update Sequence
  245. ReadBusy();
  246. }
  247. void Epd::TurnOnDisplay_Part(void)
  248. {
  249. SendCommand(0x22); //Display Update Control
  250. SendData(0xFF);
  251. SendCommand(0x20); //Activate Display Update Sequence
  252. ReadBusy();
  253. }
  254. void Epd::TurnOnDisplay_4GRAY(void)
  255. {
  256. SendCommand(0x22);
  257. SendData(0xC7);
  258. SendCommand(0x20);
  259. ReadBusy();
  260. }
  261. void Epd::Display(const unsigned char* frame_buffer) {
  262. SendCommand(0x24);
  263. for (unsigned long j = 0; j < HEIGHT; j++) {
  264. for (unsigned long i = 0; i < WIDTH/8; i++) {
  265. SendData(frame_buffer[i + j * WIDTH/8]);
  266. }
  267. }
  268. TurnOnDisplay();
  269. }
  270. void Epd::Displaypart(const unsigned char* pbuffer, unsigned long xStart, unsigned long yStart,unsigned long Picture_Width,unsigned long Picture_Height) {
  271. SendCommand(0x24);
  272. // xStart = xStart/8;
  273. // xStart = xStart*8;
  274. for (unsigned long j = 0; j < HEIGHT; j++) {
  275. for (unsigned long i = 0; i < WIDTH/8; i++) {
  276. if( (j>=yStart) && (j<yStart+Picture_Height) && (i*8>=xStart) && (i*8<xStart+Picture_Width)){
  277. SendData(pgm_read_byte(&(pbuffer[i-xStart/8 + (Picture_Width)/8*(j-yStart)])) );
  278. // SendData(0xff);
  279. }else {
  280. SendData(0xFF);
  281. }
  282. }
  283. }
  284. TurnOnDisplay();
  285. }
  286. void Epd::Display_Base(const unsigned char* frame_buffer) {
  287. SendCommand(0x24);
  288. for (unsigned long j = 0; j < HEIGHT; j++) {
  289. for (unsigned long i = 0; i < WIDTH/8; i++) {
  290. SendData(frame_buffer[i + j * WIDTH/8]);
  291. }
  292. }
  293. SendCommand(0x26);
  294. for (unsigned long j = 0; j < HEIGHT; j++) {
  295. for (unsigned long i = 0; i < WIDTH/8; i++) {
  296. SendData(frame_buffer[i + j * WIDTH/8]);
  297. }
  298. }
  299. TurnOnDisplay();
  300. }
  301. void Epd::Display_Fast(const unsigned char* frame_buffer) {
  302. SendCommand(0x24);
  303. for (unsigned long j = 0; j < HEIGHT; j++) {
  304. for (unsigned long i = 0; i < WIDTH/8; i++) {
  305. SendData(frame_buffer[i + j * WIDTH/8]);
  306. }
  307. }
  308. TurnOnDisplay_Fast();
  309. }
  310. void Epd::Display_Part(unsigned char *Image, unsigned long x, unsigned long y, unsigned long w, unsigned long l)
  311. {
  312. unsigned long i;
  313. unsigned long height = l;
  314. unsigned long width = (w % 8 == 0)? (w / 8 ): (w / 8 + 1);
  315. Reset();
  316. SendCommand(0x18); // use the internal temperature sensor
  317. SendData(0x80);
  318. SendCommand(0x3C); // Border Border setting
  319. SendData(0x80);
  320. SetWindows(x, y, x+w-1, y+l-1);
  321. SetCursor(x, y);
  322. SendCommand(0x24); //write RAM for black(0)/white (1)
  323. for (unsigned long j = 0; j < height; j++) {
  324. for (unsigned long i = 0; i < width; i++) {
  325. SendData(Image[i + j * width]);
  326. }
  327. }
  328. TurnOnDisplay_Part();
  329. }
  330. /**
  331. * @brief: After this command is transmitted, the chip would enter the
  332. * deep-sleep mode to save power.
  333. * The deep sleep mode would return to standby by hardware reset.
  334. * The only one parameter is a check code, the command would be
  335. * executed if check code = 0xA5.
  336. * You can use EPD_Reset() to awaken
  337. */
  338. void Epd::Sleep(void) {
  339. SendCommand(0X02);
  340. ReadBusy();
  341. SendCommand(0X07);
  342. SendData(0xA5);
  343. }
  344. void Epd::Clear(void) {
  345. SendCommand(0x24);
  346. for(unsigned long i=0; i<HEIGHT*WIDTH; i++) {
  347. SendData(0xff);
  348. }
  349. // SendCommand(0x26);
  350. // for(unsigned long i=0; i<HEIGHT*WIDTH; i++) {
  351. // SendData(0xff);
  352. // }
  353. TurnOnDisplay();
  354. }
  355. /* END OF FILE */