epd13in3k.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * @filename : epd13in3k.cpp
  3. * @brief : Implements for e-paper library
  4. * @author : Yehui from Waveshare
  5. *
  6. * Copyright (C) Waveshare September 25 2023
  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 "epd13in3k.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(void) {
  39. if (IfInit() != 0) {
  40. return -1;
  41. }
  42. Reset();
  43. ReadBusy();
  44. SendCommand(0x12);
  45. ReadBusy();
  46. SendCommand(0x0C);
  47. SendData(0xAE);
  48. SendData(0xC7);
  49. SendData(0xC3);
  50. SendData(0xC0);
  51. SendData(0x80);
  52. SendCommand(0x01);
  53. SendData(0xA7);
  54. SendData(0x02);
  55. SendData(0x00);
  56. SendCommand(0x11);
  57. SendData(0x03);
  58. SendCommand(0x44);
  59. SendData(0x00);
  60. SendData(0x00);
  61. SendData(0xBF);
  62. SendData(0x03);
  63. SendCommand(0x45);
  64. SendData(0x00);
  65. SendData(0x00);
  66. SendData(0xA7);
  67. SendData(0x02);
  68. SendCommand(0x3C);
  69. SendData(0x05);
  70. SendCommand(0x18);
  71. SendData(0x80);
  72. SendCommand(0x4E);
  73. SendData(0x00);
  74. SendCommand(0x4F);
  75. SendData(0x00);
  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 HIGH
  95. */
  96. void Epd::ReadBusy(void) {
  97. unsigned char busy;
  98. Serial.print("e-Paper Busy\r\n ");
  99. do{
  100. DelayMs(20);
  101. busy = DigitalRead(busy_pin);
  102. }while(busy == 1);
  103. Serial.print("e-Paper Busy Release\r\n ");
  104. DelayMs(20);
  105. }
  106. /**
  107. * @brief: Refresh function
  108. */
  109. void Epd::TurnOnDisplay(void) {
  110. SendCommand(0x22);
  111. SendData(0xF7);
  112. SendCommand(0x20);
  113. ReadBusy();
  114. }
  115. /**
  116. * @brief: module reset.
  117. * often used to awaken the module in deep sleep,
  118. * see Epd::Sleep();
  119. */
  120. void Epd::Reset(void) {
  121. DigitalWrite(reset_pin, HIGH);
  122. DelayMs(20);
  123. DigitalWrite(reset_pin, LOW); //module reset
  124. DelayMs(4);
  125. DigitalWrite(reset_pin, HIGH);
  126. DelayMs(20);
  127. }
  128. void Epd::DisplayFrame(const unsigned char* frame_buffer) {
  129. SendCommand(0x24);
  130. for (unsigned long j = 0; j < height; j++) {
  131. for (unsigned long i = 0; i < width/8; i++) {
  132. SendData(frame_buffer[i + j * width/8]);
  133. }
  134. }
  135. TurnOnDisplay();
  136. }
  137. void Epd::Displaypart(const unsigned char* pbuffer, unsigned long xStart, unsigned long yStart,unsigned long Picture_Width,unsigned long Picture_Height) {
  138. SendCommand(0x24);
  139. for (unsigned long j = 0; j < height; j++) {
  140. for (unsigned long i = 0; i < width/8; i++) {
  141. if( (j>=yStart) && (j<yStart+Picture_Height) && (i*8>=xStart) && (i*8<xStart+Picture_Width)){
  142. SendData((pgm_read_byte(&(pbuffer[i-xStart/8 + (Picture_Width)/8*(j-yStart)]))) );
  143. // SendData(0xff);
  144. }else {
  145. SendData(0xFF);
  146. }
  147. }
  148. }
  149. TurnOnDisplay();
  150. }
  151. /**
  152. * @brief: After this command is transmitted, the chip would enter the
  153. * deep-sleep mode to save power.
  154. * The deep sleep mode would return to standby by hardware reset.
  155. * The only one parameter is a check code, the command would be
  156. * executed if check code = 0xA5.
  157. * You can use EPD_Reset() to awaken
  158. */
  159. void Epd::Sleep(void) {
  160. SendCommand(0x10);
  161. SendData(0x01);
  162. DelayMs(100);
  163. ReadBusy();
  164. }
  165. void Epd::Clear(void) {
  166. SendCommand(0x24);
  167. for(unsigned long i=0; i<height*width/8; i++) {
  168. SendData(0xFF);
  169. }
  170. TurnOnDisplay();
  171. }
  172. /* END OF FILE */