epd2in66g.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "epd2in66g.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. ReadBusyH();
  45. SendCommand(0x4D);
  46. SendData(0x78);
  47. SendCommand(0x00); //PSR
  48. SendData(0x0F);
  49. SendData(0x29);
  50. SendCommand(0x01); //PWRR
  51. SendData(0x07);
  52. SendData(0x00);
  53. SendCommand(0x03); //POFS
  54. SendData(0x10);
  55. SendData(0x54);
  56. SendData(0x44);
  57. SendCommand(0x06); //BTST_P
  58. SendData(0x05);
  59. SendData(0x00);
  60. SendData(0x3F);
  61. SendData(0x0A);
  62. SendData(0x25);
  63. SendData(0x12);
  64. SendData(0x1A);
  65. SendCommand(0x50); //CDI
  66. SendData(0x37);
  67. SendCommand(0x60); //TCON
  68. SendData(0x02);
  69. SendData(0x02);
  70. SendCommand(0x61); //TRES
  71. SendData(WIDTH/256); // Source_BITS_H
  72. SendData(WIDTH%256); // Source_BITS_L
  73. SendData(HEIGHT/256); // Gate_BITS_H
  74. SendData(HEIGHT%256); // Gate_BITS_L
  75. SendCommand(0xE7);
  76. SendData(0x1C);
  77. SendCommand(0xE3);
  78. SendData(0x22);
  79. SendCommand(0xB4);
  80. SendData(0xD0);
  81. SendCommand(0xB5);
  82. SendData(0x03);
  83. SendCommand(0xE9);
  84. SendData(0x01);
  85. SendCommand(0x30);
  86. SendData(0x08);
  87. SendCommand(0x04);
  88. ReadBusyH();
  89. return 0;
  90. }
  91. /**
  92. * @brief: basic function for sending commands
  93. */
  94. void Epd::SendCommand(unsigned char command) {
  95. DigitalWrite(dc_pin, LOW);
  96. SpiTransfer(command);
  97. }
  98. /**
  99. * @brief: basic function for sending data
  100. */
  101. void Epd::SendData(unsigned char data) {
  102. DigitalWrite(dc_pin, HIGH);
  103. SpiTransfer(data);
  104. }
  105. /**
  106. * @brief: Wait until the busy_pin goes LOW
  107. */
  108. void Epd::ReadBusyH(void) {
  109. Serial.print("e-Paper busy H\r\n ");
  110. while(DigitalRead(busy_pin) == LOW) { //LOW: busy, HIGH: idle
  111. DelayMs(5);
  112. }
  113. Serial.print("e-Paper busy release H\r\n ");
  114. }
  115. void Epd::ReadBusyL(void) {
  116. Serial.print("e-Paper busy L\r\n ");
  117. while(DigitalRead(busy_pin) == HIGH) { //LOW: idle, HIGH: busy
  118. DelayMs(5);
  119. }
  120. Serial.print("e-Paper busy release L\r\n ");
  121. }
  122. /**
  123. * @brief: module reset.
  124. * often used to awaken the module in deep sleep,
  125. * see Epd::Sleep();
  126. */
  127. void Epd::Reset(void) {
  128. DigitalWrite(reset_pin, HIGH);
  129. DelayMs(20);
  130. DigitalWrite(reset_pin, LOW); //module reset
  131. DelayMs(2);
  132. DigitalWrite(reset_pin, HIGH);
  133. DelayMs(20);
  134. }
  135. /******************************************************************************
  136. function : Turn On Display
  137. parameter:
  138. ******************************************************************************/
  139. void Epd::TurnOnDisplay(void)
  140. {
  141. SendCommand(0x12); // DISPLAY_REFRESH
  142. SendData(0x00);
  143. ReadBusyH();
  144. }
  145. /******************************************************************************
  146. function : Clear screen
  147. parameter:
  148. ******************************************************************************/
  149. void Epd::Clear(UBYTE color)
  150. {
  151. UWORD Width, Height;
  152. Width = (WIDTH % 4 == 0)? (WIDTH / 4 ): (WIDTH / 4 + 1);
  153. Height = HEIGHT;
  154. SendCommand(0x10);
  155. for (UWORD j = 0; j < Height; j++) {
  156. for (UWORD i = 0; i < Width; i++) {
  157. SendData((color<<6) | (color<<4) | (color<<2) | color);
  158. }
  159. }
  160. TurnOnDisplay();
  161. }
  162. /******************************************************************************
  163. function : Sends the image buffer in RAM to e-Paper and displays
  164. parameter:
  165. ******************************************************************************/
  166. void Epd::Display(UBYTE *Image)
  167. {
  168. UWORD Width, Height;
  169. Width = (WIDTH % 4 == 0)? (WIDTH / 4 ): (WIDTH / 4 + 1);
  170. Height = HEIGHT;
  171. SendCommand(0x10);
  172. for (UWORD j = 0; j < Height; j++) {
  173. for (UWORD i = 0; i < Width; i++) {
  174. SendData(pgm_read_byte(&Image[i + j * Width]));
  175. }
  176. }
  177. TurnOnDisplay();
  178. }
  179. void Epd::Display_part(UBYTE *Image, UWORD xstart, UWORD ystart, UWORD image_width, UWORD image_height)
  180. {
  181. UWORD Width, Height, i, j;
  182. Width = (WIDTH % 4 == 0)? (WIDTH / 4 ): (WIDTH / 4 + 1);
  183. Height = HEIGHT;
  184. SendCommand(0x10);
  185. for(i=0; i<Height; i++) {
  186. for(j=0; j< Width; j++) {
  187. if(i<image_height+ystart && i>=ystart && j<(image_width+xstart)/4 && j>=xstart/4) {
  188. SendData(pgm_read_byte(&Image[(j-xstart/4) + (image_width/4*(i-ystart))]));
  189. }
  190. else {
  191. SendData(0x55);
  192. }
  193. }
  194. }
  195. TurnOnDisplay();
  196. }
  197. /******************************************************************************
  198. function : Enter sleep mode
  199. parameter:
  200. ******************************************************************************/
  201. void Epd::Sleep(void)
  202. {
  203. SendCommand(0x02); // POWER_OFF
  204. SendData(0X00);
  205. ReadBusyH();
  206. SendCommand(0x07); // DEEP_SLEEP
  207. SendData(0XA5);
  208. }
  209. /* END OF FILE */