epd3in0g.cpp 6.0 KB

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