epd4in26.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. unsigned int count;
  128. if (IfInit() != 0) {
  129. return -1;
  130. }
  131. Reset();
  132. DelayMs(100);
  133. ReadBusy();
  134. SendCommand(0x12); //SWRESET
  135. ReadBusy();
  136. SendCommand(0x18); // use the internal temperature sensor
  137. SendData(0x80);
  138. SendCommand(0x0C); //set soft start
  139. SendData(0xAE);
  140. SendData(0xC7);
  141. SendData(0xC3);
  142. SendData(0xC0);
  143. SendData(0x80);
  144. SendCommand(0x01); // drive output control
  145. SendData((WIDTH-1)%256); // Y
  146. SendData((WIDTH-1)/256); // Y
  147. SendData(0x02);
  148. SendCommand(0x3C); // Border Border setting
  149. SendData(0x01);
  150. SendCommand(0x11); // data entry mode
  151. SendData(0x01); // X-mode x+ y-
  152. SetWindows(0, HEIGHT-1, WIDTH-1, 0);
  153. SetCursor(0, 0);
  154. ReadBusy();
  155. SendCommand(0x32); //vcom
  156. for(count = 0; count < 105 ; count++) {
  157. SendData(LUT_DATA_4Gray[count]);
  158. }
  159. SendCommand(0x03); //VGH
  160. SendData(LUT_DATA_4Gray[105]);
  161. SendCommand(0x04); //
  162. SendData(LUT_DATA_4Gray[106]); //VSH1
  163. SendData(LUT_DATA_4Gray[107]); //VSH2
  164. SendData(LUT_DATA_4Gray[108]); //VSL
  165. SendCommand(0x2C); //VCOM Voltage
  166. SendData(LUT_DATA_4Gray[109]); //0x1C
  167. return 0;
  168. }
  169. /******************************************************************************
  170. function : Setting the display window
  171. parameter:
  172. ******************************************************************************/
  173. void Epd::SetWindows(unsigned long Xstart, unsigned long Ystart, unsigned long Xend, unsigned long Yend)
  174. {
  175. SendCommand(0x44); // SET_RAM_X_ADDRESS_START_END_POSITION
  176. SendData(Xstart & 0xFF);
  177. SendData((Xstart>>8) & 0x03);
  178. SendData(Xend & 0xFF);
  179. SendData((Xend>>8) & 0x03);
  180. SendCommand(0x45); // SET_RAM_Y_ADDRESS_START_END_POSITION
  181. SendData(Ystart & 0xFF);
  182. SendData((Ystart>>8) & 0x03);
  183. SendData(Yend & 0xFF);
  184. SendData((Yend>>8) & 0x03);
  185. }
  186. /******************************************************************************
  187. function : Set Cursor
  188. parameter:
  189. ******************************************************************************/
  190. void Epd::SetCursor(unsigned long Xstart, unsigned long Ystart)
  191. {
  192. SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  193. SendData(Xstart & 0xFF);
  194. SendData((Xstart>>8) & 0x03);
  195. SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  196. SendData(Ystart & 0xFF);
  197. SendData((Ystart>>8) & 0x03);
  198. }
  199. /**
  200. * @brief: basic function for sending commands
  201. */
  202. void Epd::SendCommand(unsigned char command) {
  203. DigitalWrite(dc_pin, LOW);
  204. SpiTransfer(command);
  205. }
  206. /**
  207. * @brief: basic function for sending data
  208. */
  209. void Epd::SendData(unsigned char data) {
  210. DigitalWrite(dc_pin, HIGH);
  211. SpiTransfer(data);
  212. }
  213. /**
  214. * @brief: Wait until the busy_pin goes HIGH
  215. */
  216. void Epd::ReadBusy(void) {
  217. unsigned char busy;
  218. Serial.print("e-Paper Busy\r\n ");
  219. while(1)
  220. { //=1 BUSY
  221. if(DigitalRead(busy_pin)==0)
  222. break;
  223. DelayMs(20);
  224. }
  225. Serial.print("e-Paper Busy Release\r\n ");
  226. DelayMs(20);
  227. }
  228. /**
  229. * @brief: module reset.
  230. * often used to awaken the module in deep sleep,
  231. * see Epd::Sleep();
  232. */
  233. void Epd::Reset(void) {
  234. DigitalWrite(reset_pin, HIGH);
  235. DelayMs(20);
  236. DigitalWrite(reset_pin, LOW); //module reset
  237. DelayMs(4);
  238. DigitalWrite(reset_pin, HIGH);
  239. DelayMs(20);
  240. }
  241. /******************************************************************************
  242. function : Turn On Display
  243. parameter:
  244. ******************************************************************************/
  245. void Epd::TurnOnDisplay(void)
  246. {
  247. SendCommand(0x22); //Display Update Control
  248. SendData(0xF7);
  249. SendCommand(0x20); //Activate Display Update Sequence
  250. ReadBusy();
  251. }
  252. void Epd::TurnOnDisplay_Fast(void)
  253. {
  254. SendCommand(0x22); //Display Update Control
  255. SendData(0xC7);
  256. SendCommand(0x20); //Activate Display Update Sequence
  257. ReadBusy();
  258. }
  259. void Epd::TurnOnDisplay_Part(void)
  260. {
  261. SendCommand(0x22); //Display Update Control
  262. SendData(0xFF);
  263. SendCommand(0x20); //Activate Display Update Sequence
  264. ReadBusy();
  265. }
  266. void Epd::TurnOnDisplay_4GRAY(void)
  267. {
  268. SendCommand(0x22);
  269. SendData(0xC7);
  270. SendCommand(0x20);
  271. ReadBusy();
  272. }
  273. void Epd::Display(const unsigned char* frame_buffer) {
  274. SendCommand(0x24);
  275. for (unsigned long j = 0; j < HEIGHT; j++) {
  276. for (unsigned long i = 0; i < WIDTH/8; i++) {
  277. SendData(frame_buffer[i + j * WIDTH/8]);
  278. }
  279. }
  280. TurnOnDisplay();
  281. }
  282. void Epd::Displaypart(const unsigned char* pbuffer, unsigned long xStart, unsigned long yStart,unsigned long Picture_Width,unsigned long Picture_Height) {
  283. SendCommand(0x24);
  284. // xStart = xStart/8;
  285. // xStart = xStart*8;
  286. for (unsigned long j = 0; j < HEIGHT; j++) {
  287. for (unsigned long i = 0; i < WIDTH/8; i++) {
  288. if( (j>=yStart) && (j<yStart+Picture_Height) && (i*8>=xStart) && (i*8<xStart+Picture_Width)){
  289. SendData(pgm_read_byte(&(pbuffer[i-xStart/8 + (Picture_Width)/8*(j-yStart)])) );
  290. // SendData(0xff);
  291. }else {
  292. SendData(0xFF);
  293. }
  294. }
  295. }
  296. TurnOnDisplay();
  297. }
  298. void Epd::Display_Base(const unsigned char* frame_buffer) {
  299. SendCommand(0x24);
  300. for (unsigned long j = 0; j < HEIGHT; j++) {
  301. for (unsigned long i = 0; i < WIDTH/8; i++) {
  302. SendData(frame_buffer[i + j * WIDTH/8]);
  303. }
  304. }
  305. SendCommand(0x26);
  306. for (unsigned long j = 0; j < HEIGHT; j++) {
  307. for (unsigned long i = 0; i < WIDTH/8; i++) {
  308. SendData(frame_buffer[i + j * WIDTH/8]);
  309. }
  310. }
  311. TurnOnDisplay();
  312. }
  313. void Epd::Display_Fast(const unsigned char* frame_buffer) {
  314. SendCommand(0x24);
  315. for (unsigned long j = 0; j < HEIGHT; j++) {
  316. for (unsigned long i = 0; i < WIDTH/8; i++) {
  317. SendData(frame_buffer[i + j * WIDTH/8]);
  318. }
  319. }
  320. TurnOnDisplay_Fast();
  321. }
  322. void Epd::Display_Part(unsigned char *Image, unsigned long x, unsigned long y, unsigned long w, unsigned long l)
  323. {
  324. unsigned long i;
  325. unsigned long height = l;
  326. unsigned long width = (w % 8 == 0)? (w / 8 ): (w / 8 + 1);
  327. Reset();
  328. SendCommand(0x18); // use the internal temperature sensor
  329. SendData(0x80);
  330. SendCommand(0x3C); // Border Border setting
  331. SendData(0x80);
  332. SetWindows(x, y, x+w-1, y+l-1);
  333. SetCursor(x, y);
  334. SendCommand(0x24); //write RAM for black(0)/white (1)
  335. for (unsigned long j = 0; j < height; j++) {
  336. for (unsigned long i = 0; i < width; i++) {
  337. SendData(Image[i + j * width]);
  338. }
  339. }
  340. TurnOnDisplay_Part();
  341. }
  342. /**
  343. * @brief: After this command is transmitted, the chip would enter the
  344. * deep-sleep mode to save power.
  345. * The deep sleep mode would return to standby by hardware reset.
  346. * The only one parameter is a check code, the command would be
  347. * executed if check code = 0xA5.
  348. * You can use EPD_Reset() to awaken
  349. */
  350. void Epd::Sleep(void) {
  351. SendCommand(0x10);
  352. SendData(0x03);
  353. DelayMs(100);
  354. ReadBusy();
  355. }
  356. void Epd::Clear(void) {
  357. SendCommand(0x24);
  358. for(unsigned long i=0; i<HEIGHT*WIDTH; i++) {
  359. SendData(0xff);
  360. }
  361. // SendCommand(0x26);
  362. // for(unsigned long i=0; i<HEIGHT*WIDTH; i++) {
  363. // SendData(0xff);
  364. // }
  365. TurnOnDisplay();
  366. }
  367. /* END OF FILE */