2
0

epd7in5_HD.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * @filename : epd7in5.cpp
  3. * @brief : Implements for e-paper library
  4. * @author : Yehui from Waveshare
  5. *
  6. * Copyright (C) Waveshare August 10 2017
  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 "epd7in5_HD.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. // SendCommand(0x01);
  44. // SendData(0x07);
  45. WaitUntilIdle();
  46. SendCommand(0x12); //SWRESET
  47. WaitUntilIdle();
  48. SendCommand(0x46); // Auto Write Red RAM
  49. SendData(0xf7);
  50. WaitUntilIdle();
  51. SendCommand(0x47); // Auto Write B/W RAM
  52. SendData(0xf7);
  53. WaitUntilIdle();
  54. SendCommand(0x0C); // Soft start setting
  55. SendData(0xAE);
  56. SendData(0xC7);
  57. SendData(0xC3);
  58. SendData(0xC0);
  59. SendData(0x40);
  60. SendCommand(0x01); // Set MUX as 527
  61. SendData(0xAF);
  62. SendData(0x02);
  63. SendData(0x01);//0x01
  64. SendCommand(0x11); // Data entry mode
  65. SendData(0x01);
  66. SendCommand(0x44);
  67. SendData(0x00); // RAM x address start at 0
  68. SendData(0x00);
  69. SendData(0x6F);
  70. SendData(0x03);
  71. SendCommand(0x45);
  72. SendData(0xAF);
  73. SendData(0x02);
  74. SendData(0x00);
  75. SendData(0x00);
  76. SendCommand(0x3C); // VBD
  77. SendData(0x05); // LUT1, for white
  78. SendCommand(0x18);
  79. SendData(0X80);
  80. SendCommand(0x22);
  81. SendData(0XB1); //Load Temperature and waveform setting.
  82. SendCommand(0x20);
  83. WaitUntilIdle();
  84. SendCommand(0x4E); // set RAM x address count to 0;
  85. SendData(0x00);
  86. SendData(0x00);
  87. SendCommand(0x4F);
  88. SendData(0x00);
  89. SendData(0x00);
  90. return 0;
  91. }
  92. /**
  93. * @brief: basic function for sending commands
  94. */
  95. void Epd::SendCommand(unsigned char command) {
  96. DigitalWrite(dc_pin, LOW);
  97. SpiTransfer(command);
  98. }
  99. /**
  100. * @brief: basic function for sending data
  101. */
  102. void Epd::SendData(unsigned char data) {
  103. DigitalWrite(dc_pin, HIGH);
  104. SpiTransfer(data);
  105. }
  106. /**
  107. * @brief: Wait until the busy_pin goes HIGH
  108. */
  109. void Epd::WaitUntilIdle(void) {
  110. unsigned char busy;
  111. do{
  112. DelayMs(10);
  113. }while(DigitalRead(busy_pin) == 1);
  114. DelayMs(200);
  115. }
  116. /**
  117. * @brief: module reset.
  118. * often used to awaken the module in deep sleep,
  119. * see Epd::Sleep();
  120. */
  121. void Epd::Reset(void) {
  122. DigitalWrite(reset_pin, LOW); //module reset
  123. DelayMs(4);
  124. DigitalWrite(reset_pin, HIGH);
  125. DelayMs(200);
  126. }
  127. void Epd::DisplayFrame(const unsigned char* frame_buffer) {
  128. SendCommand(0x13);
  129. for (unsigned long j = 0; j < height; j++) {
  130. for (unsigned long i = 0; i < width; i++) {
  131. SendData(~frame_buffer[i + j * width]);
  132. }
  133. }
  134. SendCommand(0x12);
  135. DelayMs(100);
  136. WaitUntilIdle();
  137. }
  138. void Epd::Displaypart(const unsigned char* pbuffer, unsigned long xStart, unsigned long yStart,unsigned long Picture_Width,unsigned long Picture_Height) {
  139. SendCommand(0x4F);
  140. SendData(0x00);
  141. SendData(0x00);
  142. SendCommand(0x24);
  143. int * padd;
  144. // xStart = xStart/8;
  145. // xStart = xStart*8;
  146. for (unsigned long j = 0; j < height; j++) {
  147. for (unsigned long i = 0; i < width/8; i++) {
  148. if( (j>=yStart) && (j<yStart+Picture_Height) && (i*8>=xStart) && (i*8<xStart+Picture_Width)){
  149. SendData((pgm_read_byte(&(pbuffer[i-xStart/8 + (Picture_Width)/8*(j-yStart)]))) );
  150. // SendData(0xff);
  151. }else {
  152. SendData(0xff);
  153. // SendData(0xff);
  154. }
  155. }
  156. }
  157. SendCommand(0x22);
  158. SendData(0xF7);//Load LUT from MCU(0x32)
  159. SendCommand(0x20);
  160. DelayMs(10);
  161. WaitUntilIdle();
  162. }
  163. /**
  164. * @brief: After this command is transmitted, the chip would enter the
  165. * deep-sleep mode to save power.
  166. * The deep sleep mode would return to standby by hardware reset.
  167. * The only one parameter is a check code, the command would be
  168. * executed if check code = 0xA5.
  169. * You can use EPD_Reset() to awaken
  170. */
  171. void Epd::Sleep(void) {
  172. SendCommand(0x10);
  173. SendData(0x01);
  174. }
  175. void Epd::Clear(void) {
  176. SendCommand(0x4F);
  177. SendData(0x00);
  178. SendData(0x00);
  179. SendCommand(0x24);
  180. for(unsigned long i=0; i<height*width/8; i++) {
  181. SendData(0xff);
  182. }
  183. SendCommand(0x26);
  184. for(unsigned long i=0; i<height*width/8; i++) {
  185. SendData(0x00);
  186. }
  187. SendCommand(0x22);
  188. SendData(0xF7);//Load LUT from MCU(0x32)
  189. SendCommand(0x20);
  190. DelayMs(10);
  191. WaitUntilIdle();
  192. }
  193. /* END OF FILE */