epd5in79g.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /**
  2. * @filename : epd2in66g.cpp
  3. * @brief : Implements for e-paper library
  4. * @author : Waveshare
  5. *
  6. * Copyright (C) Waveshare 2022/08/17
  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 "epd5in79g.h"
  28. Epd::~Epd() {
  29. };
  30. Epd::Epd() {
  31. reset_pin = RST_PIN;
  32. dc_pin = DC_PIN;
  33. cs_pin = CS_PIN;
  34. busy_pin = BUSY_PIN;
  35. WIDTH = EPD_WIDTH;
  36. HEIGHT = EPD_HEIGHT;
  37. };
  38. int Epd::Init() {
  39. /* this calls the peripheral hardware interface, see epdif */
  40. if (IfInit() != 0) {
  41. return -1;
  42. }
  43. Reset();
  44. SendCommand(0xA2); //********************
  45. SendData(0x01);
  46. SendCommand(0x00); //0x00
  47. SendData(0x03);
  48. SendData(0x29);
  49. SendCommand(0xA2); //********************
  50. SendData(0x02);
  51. SendCommand(0x00); //0x00
  52. SendData(0x07);
  53. SendData(0x29);
  54. SendCommand(0xA2); //********************
  55. SendData(0x00);
  56. SendCommand(0x50); //
  57. SendData(0x97);
  58. SendCommand(0x61); //0x61
  59. SendData(0x01);
  60. SendData(0x8c);
  61. SendData(0x01);
  62. SendData(0x10);
  63. SendCommand(0x06); //0x06
  64. SendData(0x38);
  65. SendData(0x38);
  66. SendData(0x38);
  67. SendData(0x00); //////////////////////////////////////////
  68. SendCommand(0xE9); //0xE0
  69. SendData(0x01);
  70. SendCommand(0xE0); //0xE0
  71. SendData(0x01);
  72. SendCommand(0x04);
  73. ReadBusyH(); //while(1);
  74. return 0;
  75. }
  76. /**
  77. * @brief: basic function for sending commands
  78. */
  79. void Epd::SendCommand(unsigned char command) {
  80. DigitalWrite(dc_pin, LOW);
  81. SpiTransfer(command);
  82. }
  83. /**
  84. * @brief: basic function for sending data
  85. */
  86. void Epd::SendData(unsigned char data) {
  87. DigitalWrite(dc_pin, HIGH);
  88. SpiTransfer(data);
  89. }
  90. /**
  91. * @brief: Wait until the busy_pin goes LOW
  92. */
  93. void Epd::ReadBusyH(void) {
  94. Serial.print("e-Paper busy H\r\n ");
  95. while(DigitalRead(busy_pin) == LOW) { //LOW: busy, HIGH: idle
  96. DelayMs(5);
  97. }
  98. Serial.print("e-Paper busy release H\r\n ");
  99. }
  100. void Epd::ReadBusyL(void) {
  101. Serial.print("e-Paper busy L\r\n ");
  102. while(DigitalRead(busy_pin) == HIGH) { //LOW: idle, HIGH: busy
  103. DelayMs(5);
  104. }
  105. Serial.print("e-Paper busy release L\r\n ");
  106. }
  107. /**
  108. * @brief: module reset.
  109. * often used to awaken the module in deep sleep,
  110. * see Epd::Sleep();
  111. */
  112. void Epd::Reset(void) {
  113. DigitalWrite(reset_pin, HIGH);
  114. DelayMs(20);
  115. DigitalWrite(reset_pin, LOW); //module reset
  116. DelayMs(2);
  117. DigitalWrite(reset_pin, HIGH);
  118. DelayMs(20);
  119. }
  120. /******************************************************************************
  121. function : Turn On Display
  122. parameter:
  123. ******************************************************************************/
  124. void Epd::TurnOnDisplay(void)
  125. {
  126. SendCommand(0xA2);
  127. SendData(0x00);
  128. SendCommand(0x12);
  129. SendData(0x00);
  130. ReadBusyH();
  131. }
  132. /******************************************************************************
  133. function : Clear screen
  134. parameter:
  135. ******************************************************************************/
  136. void Epd::Clear(UBYTE color)
  137. {
  138. UWORD Width, Height;
  139. Width = (WIDTH % 8 == 0)? (WIDTH / 8 ): (WIDTH / 8 + 1);
  140. Height = HEIGHT;
  141. SendCommand(0xA2); //********************
  142. SendData(0x01);
  143. SendCommand(0x10);
  144. for (UWORD j = 0; j < Height; j++) {
  145. for (UWORD i = 0; i < Width; i++) {
  146. SendData((color<<6) | (color<<4) | (color<<2) | color);
  147. }
  148. }
  149. SendCommand(0xA2); //********************
  150. SendData(0x02);
  151. SendCommand(0x10);
  152. for (UWORD j = 0; j < Height; j++) {
  153. for (UWORD i = 0; i < Width; i++) {
  154. SendData((color<<6) | (color<<4) | (color<<2) | color);
  155. }
  156. }
  157. TurnOnDisplay();
  158. }
  159. /******************************************************************************
  160. function : Sends the image buffer in RAM to e-Paper and displays
  161. parameter:
  162. ******************************************************************************/
  163. void Epd::Display(UBYTE *Image)
  164. {
  165. UWORD Width, Height, Width1;
  166. Width1 = (WIDTH % 4 == 0)? (WIDTH / 4 ): (WIDTH / 4 + 1);
  167. Width =(WIDTH % 8 == 0)?(WIDTH / 8 ):(WIDTH / 8 + 1);
  168. Height = HEIGHT;
  169. SendCommand(0xA2); //********************
  170. SendData(0x01);
  171. SendCommand(0x10);
  172. for (UWORD j = 0; j < Height/2; j++) {
  173. for (UWORD i = 0; i < Width; i++) {
  174. SendData(pgm_read_byte(&Image[i + j * Width1]));
  175. }
  176. for (UWORD i = 0; i < Width; i++) {
  177. SendData(pgm_read_byte(&Image[i + (Height - j - 1) * Width1]));
  178. }
  179. }
  180. SendCommand(0xA2); //********************
  181. SendData(0x02);
  182. SendCommand(0x10);
  183. for (UWORD j = 0; j < Height; j++) {
  184. for (UWORD i = 0; i < Width; i++) {
  185. SendData(pgm_read_byte(&Image[j * Width1 + i + Width]));
  186. }
  187. for (UWORD i = 0; i < Width; i++) {
  188. SendData(pgm_read_byte(&Image[(Height - j - 1) * Width1 + i + Width]));
  189. }
  190. }
  191. TurnOnDisplay();
  192. }
  193. void Epd::Display_part(UBYTE *Image, UWORD xstart, UWORD ystart, UWORD image_width, UWORD image_height)
  194. {
  195. UWORD Width, Width1, Width2, Height, i, j, xend, yend;
  196. Width = (WIDTH % 8 == 0)? (WIDTH / 8 ): (WIDTH / 8 + 1);
  197. Height = HEIGHT;
  198. xend = xstart + image_width;
  199. yend = ystart + image_height;
  200. if(xstart>395)
  201. {
  202. xstart = xstart - 396 ;
  203. xend = xstart + image_width;
  204. SendCommand(0xA2);
  205. SendData(0x01);
  206. SendCommand(0x10);
  207. for (UWORD j = 0; j < Height/2; j++) {
  208. for (UWORD i = 0; i < Width; i++) {
  209. if(j >= ystart && i < xend/4 && i >= xstart/4)
  210. SendData(pgm_read_byte(&Image[(j - ystart) * image_width/4 + i - xstart/4]));
  211. else
  212. SendData(0x55);
  213. }
  214. for (UWORD i = 0; i < Width; i++) {
  215. if((Height - j) < yend && i < xend/4 && i >= xstart/4)
  216. SendData(pgm_read_byte(&Image[(yend - (Height - j)) * image_width/4 + i- xstart/4]));
  217. else
  218. SendData(0x55);
  219. }
  220. }
  221. SendCommand(0xA2);
  222. SendData(0x02);
  223. SendCommand(0x10);
  224. for (UWORD j = 0; j < Height; j++) {
  225. for (UWORD i = 0; i < Width; i++) {
  226. SendData(0x55);
  227. }
  228. }
  229. }
  230. else if(xend<396)
  231. {
  232. xend = xstart + image_width;
  233. SendCommand(0xA2);
  234. SendData(0x01);
  235. SendCommand(0x10);
  236. for (UWORD j = 0; j < Height; j++) {
  237. for (UWORD i = 0; i < Width; i++) {
  238. SendData(0x55);
  239. }
  240. }
  241. SendCommand(0xA2);
  242. SendData(0x02);
  243. SendCommand(0x10);
  244. for (UWORD j = 0; j < Height/2; j++) {
  245. for (UWORD i = 0; i < Width; i++) {
  246. if(j >= ystart && i < xend/4 && i >= xstart/4)
  247. SendData(pgm_read_byte(&Image[(j - ystart) * image_width/4 + i - xstart/4]));
  248. else
  249. SendData(0x55);
  250. }
  251. for (UWORD i = 0; i < Width; i++) {
  252. if((Height - j) < yend && i < xend/4 && i >= xstart/4)
  253. SendData(pgm_read_byte(&Image[(yend - (Height - j)) * image_width/4 + i- xstart/4]));
  254. else
  255. SendData(0x55);
  256. }
  257. }
  258. }
  259. else
  260. {
  261. xend = xstart + image_width;
  262. Width1 = ((395 - xstart) % 4 == 0)?((395 - xstart) / 4 ):((395 - xstart) / 4 + 1);
  263. Width2 = ((xend - 396) % 4 == 0)?((xend - 396) / 4 ):((xend - 396) / 4 + 1);
  264. SendCommand(0xA2);
  265. SendData(0x01);
  266. SendCommand(0x10);
  267. for (UWORD j = 0; j < Height/2; j++) {
  268. for (UWORD i = 0; i < Width; i++) {
  269. if(j >= ystart && i < Width2)
  270. SendData(pgm_read_byte(&Image[(j - ystart) * image_width/4 + i + Width1]));
  271. else
  272. SendData(0x55);
  273. }
  274. for (UWORD i = 0; i < Width; i++) {
  275. if((Height - j) < yend && i < Width2)
  276. SendData(pgm_read_byte(&Image[(yend - (Height - j)) * image_width/4 + i + Width1]));
  277. else
  278. SendData(0x55);
  279. }
  280. }
  281. SendCommand(0xA2);
  282. SendData(0x02);
  283. SendCommand(0x10);
  284. for (UWORD j = 0; j < Height/2; j++) {
  285. for (UWORD i = 0; i < Width; i++) {
  286. if(j >= ystart && i >= xstart/4)
  287. SendData(pgm_read_byte(&Image[(j - ystart) * image_width/4 + i - xstart/4]));
  288. else
  289. SendData(0x55);
  290. }
  291. for (UWORD i = 0; i < Width; i++) {
  292. if((Height - j) < yend && i >= xstart/4)
  293. SendData(pgm_read_byte(&Image[(yend - (Height - j)) * image_width/4 + i- xstart/4]));
  294. else
  295. SendData(0x55);
  296. }
  297. }
  298. }
  299. TurnOnDisplay();
  300. }
  301. /******************************************************************************
  302. function : Enter sleep mode
  303. parameter:
  304. ******************************************************************************/
  305. void Epd::Sleep(void)
  306. {
  307. // SendCommand(0x02); // POWER_OFF
  308. // SendData(0X00);
  309. // ReadBusyH();
  310. SendCommand(0x07); // DEEP_SLEEP
  311. SendData(0XA5);
  312. }
  313. /* END OF FILE */