2
0

epd7in3g.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /**
  2. * @filename : epd7in3g.cpp
  3. * @brief : Implements for e-paper library
  4. * @author : Waveshare
  5. *
  6. * Copyright (C) Waveshare 2022/7/22
  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 "epd7in3g.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. DelayMs(30);
  46. SendCommand(0xAA);
  47. SendData(0x49);
  48. SendData(0x55);
  49. SendData(0x20);
  50. SendData(0x08);
  51. SendData(0x09);
  52. SendData(0x18);
  53. SendCommand(0x01);
  54. SendData(0x3F);
  55. SendCommand(0x00);
  56. SendData(0x4F);
  57. SendData(0x69);
  58. SendCommand(0x05);
  59. SendData(0x40);
  60. SendData(0x1F);
  61. SendData(0x1F);
  62. SendData(0x2C);
  63. SendCommand(0x08);
  64. SendData(0x6F);
  65. SendData(0x1F);
  66. SendData(0x1F);
  67. SendData(0x22);
  68. //===================
  69. //20211212
  70. //First setting
  71. SendCommand(0x06);
  72. SendData(0x6F);
  73. SendData(0x1F);
  74. SendData(0x14);
  75. SendData(0x14);
  76. //===================
  77. SendCommand(0x03);
  78. SendData(0x00);
  79. SendData(0x54);
  80. SendData(0x00);
  81. SendData(0x44);
  82. SendCommand(0x60);
  83. SendData(0x02);
  84. SendData(0x00);
  85. //Please notice that PLL must be set for version 2 IC
  86. SendCommand(0x30);
  87. SendData(0x08);
  88. SendCommand(0x50);
  89. SendData(0x3F);
  90. SendCommand(0x61);
  91. SendData(0x03);
  92. SendData(0x20);
  93. SendData(0x01);
  94. SendData(0xE0);
  95. SendCommand(0xE3);
  96. SendData(0x2F);
  97. SendCommand(0x84);
  98. SendData(0x01);
  99. return 0;
  100. }
  101. /**
  102. * @brief: basic function for sending commands
  103. */
  104. void Epd::SendCommand(unsigned char command) {
  105. DigitalWrite(dc_pin, LOW);
  106. SpiTransfer(command);
  107. }
  108. /**
  109. * @brief: basic function for sending data
  110. */
  111. void Epd::SendData(unsigned char data) {
  112. DigitalWrite(dc_pin, HIGH);
  113. SpiTransfer(data);
  114. }
  115. /**
  116. * @brief: Wait until the busy_pin goes LOW
  117. */
  118. void Epd::ReadBusyH(void) {
  119. Serial.print("e-Paper busy H\r\n ");
  120. while(DigitalRead(busy_pin) == LOW) { //LOW: busy, HIGH: idle
  121. DelayMs(5);
  122. }
  123. Serial.print("e-Paper busy release H\r\n ");
  124. }
  125. void Epd::ReadBusyL(void) {
  126. Serial.print("e-Paper busy L\r\n ");
  127. while(DigitalRead(busy_pin) == HIGH) { //LOW: idle, HIGH: busy
  128. DelayMs(5);
  129. }
  130. Serial.print("e-Paper busy release L\r\n ");
  131. }
  132. /**
  133. * @brief: module reset.
  134. * often used to awaken the module in deep sleep,
  135. * see Epd::Sleep();
  136. */
  137. void Epd::Reset(void) {
  138. DigitalWrite(reset_pin, HIGH);
  139. DelayMs(20);
  140. DigitalWrite(reset_pin, LOW); //module reset
  141. DelayMs(2);
  142. DigitalWrite(reset_pin, HIGH);
  143. DelayMs(20);
  144. }
  145. /******************************************************************************
  146. function : Turn On Display
  147. parameter:
  148. ******************************************************************************/
  149. void Epd::TurnOnDisplay(void)
  150. {
  151. SendCommand(0x12); // DISPLAY_REFRESH
  152. SendData(0x01);
  153. ReadBusyH();
  154. SendCommand(0x02); // POWER_OFF
  155. SendData(0X00);
  156. ReadBusyH();
  157. }
  158. /******************************************************************************
  159. function : Clear screen
  160. parameter:
  161. ******************************************************************************/
  162. void Epd::Clear(UBYTE color)
  163. {
  164. UWORD Width, Height;
  165. Width = (WIDTH % 4 == 0)? (WIDTH / 4 ): (WIDTH / 4 + 1);
  166. Height = HEIGHT;
  167. SendCommand(0x04);
  168. ReadBusyH();
  169. SendCommand(0x10);
  170. for (UWORD j = 0; j < Height; j++) {
  171. for (UWORD i = 0; i < Width; i++) {
  172. SendData((color<<6) | (color<<4) | (color<<2) | color);
  173. }
  174. }
  175. TurnOnDisplay();
  176. }
  177. /******************************************************************************
  178. function : Sends the image buffer in RAM to e-Paper and displays
  179. parameter:
  180. ******************************************************************************/
  181. void Epd::Display(UBYTE *Image)
  182. {
  183. UWORD Width, Height;
  184. Width = (WIDTH % 4 == 0)? (WIDTH / 4 ): (WIDTH / 4 + 1);
  185. Height = HEIGHT;
  186. SendCommand(0x04);
  187. ReadBusyH();
  188. SendCommand(0x10);
  189. for (UWORD j = 0; j < Height; j++) {
  190. for (UWORD i = 0; i < Width; i++) {
  191. SendData(pgm_read_byte(&Image[i + j * Width]));
  192. }
  193. }
  194. TurnOnDisplay();
  195. }
  196. void Epd::Display_part(UBYTE *Image, UWORD xstart, UWORD ystart, UWORD image_width, UWORD image_height)
  197. {
  198. UWORD Width, Height, i, j;
  199. Width = (WIDTH % 4 == 0)? (WIDTH / 4 ): (WIDTH / 4 + 1);
  200. Height = HEIGHT;
  201. SendCommand(0x04);
  202. ReadBusyH();
  203. SendCommand(0x10);
  204. for(i=0; i<Height; i++) {
  205. for(j=0; j< Width; j++) {
  206. if(i<image_height+ystart && i>=ystart && j<(image_width+xstart)/4 && j>=xstart/4) {
  207. SendData(pgm_read_byte(&Image[(j-xstart/4) + (image_width/4*(i-ystart))]));
  208. }
  209. else {
  210. SendData(0x55);
  211. }
  212. }
  213. }
  214. TurnOnDisplay();
  215. }
  216. /******************************************************************************
  217. function : Enter sleep mode
  218. parameter:
  219. ******************************************************************************/
  220. void Epd::Sleep(void)
  221. {
  222. SendCommand(0x02); // POWER_OFF
  223. SendData(0X00);
  224. SendCommand(0x07); // DEEP_SLEEP
  225. SendData(0XA5);
  226. }
  227. /* END OF FILE */