2
0

epd7in5b_HD.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * @filename : epd7in5b.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 "epd7in5b_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(0x12); //SWRESET
  44. WaitUntilIdle(); //waiting for the electronic paper IC to release the idle signal
  45. SendCommand(0x46); // Auto Write RAM
  46. SendData(0xF7);
  47. WaitUntilIdle(); //waiting for the electronic paper IC to release the idle signal
  48. SendCommand(0x47); // Auto Write RAM
  49. SendData(0xF7);
  50. WaitUntilIdle(); //waiting for the electronic paper IC to release the idle signal
  51. SendCommand(0x0C); // Soft start setting
  52. SendData(0xAE);
  53. SendData(0xC7);
  54. SendData(0xC3);
  55. SendData(0xC0);
  56. SendData(0x40);
  57. SendCommand(0x01); // Set MUX as 527
  58. SendData(0xAF);
  59. SendData(0x02);
  60. SendData(0x01);
  61. SendCommand(0x11); // Data entry mode
  62. SendData(0x01);
  63. SendCommand(0x44);
  64. SendData(0x00); // RAM x address start at 0
  65. SendData(0x00);
  66. SendData(0x6F); // RAM x address end at 36Fh -> 879
  67. SendData(0x03);
  68. SendCommand(0x45);
  69. SendData(0xAF); // RAM y address start at 20Fh;
  70. SendData(0x02);
  71. SendData(0x00); // RAM y address end at 00h;
  72. SendData(0x00);
  73. SendCommand(0x3C); // VBD
  74. SendData(0x01); // LUT1, for white
  75. SendCommand(0x18);
  76. SendData(0X80);
  77. SendCommand(0x22);
  78. SendData(0XB1); //Load Temperature and waveform setting.
  79. SendCommand(0x20);
  80. WaitUntilIdle(); //waiting for the electronic paper IC to release the idle signal
  81. SendCommand(0x4E);
  82. SendData(0x00);
  83. SendData(0x00);
  84. SendCommand(0x4F);
  85. SendData(0xAF);
  86. SendData(0x02);
  87. return 0;
  88. }
  89. /**
  90. * @brief: basic function for sending commands
  91. */
  92. void Epd::SendCommand(unsigned char command) {
  93. DigitalWrite(dc_pin, LOW);
  94. SpiTransfer(command);
  95. }
  96. /**
  97. * @brief: basic function for sending data
  98. */
  99. void Epd::SendData(unsigned char data) {
  100. DigitalWrite(dc_pin, HIGH);
  101. SpiTransfer(data);
  102. }
  103. /**
  104. * @brief: Wait until the busy_pin goes HIGH
  105. */
  106. void Epd::WaitUntilIdle(void) {
  107. unsigned char busy;
  108. while(DigitalRead(busy_pin)){
  109. // DelayMs(10);
  110. }
  111. DelayMs(200);
  112. }
  113. /**
  114. * @brief: module reset.
  115. * often used to awaken the module in deep sleep,
  116. * see Epd::Sleep();
  117. */
  118. void Epd::Reset(void) {
  119. DigitalWrite(reset_pin, LOW); //module reset
  120. DelayMs(2);
  121. DigitalWrite(reset_pin, HIGH);
  122. DelayMs(200);
  123. }
  124. void Epd::Displaypart(const unsigned char* pbuffer, unsigned long xStart, unsigned long yStart,\
  125. unsigned long Picture_Width, unsigned long Picture_Height, unsigned char Block) {
  126. SendCommand(0x4F);
  127. SendData(0xAf);
  128. SendData(0x02);
  129. if(Block == 0){
  130. SendCommand(0x24);
  131. }else if(Block == 1){
  132. SendCommand(0x26);
  133. }
  134. for (unsigned long j = 0; j < height; j++) {
  135. for (unsigned long i = 0; i < width/8; i++) {
  136. if( (j>=yStart) && (j<yStart+Picture_Height) && (i*8>=xStart) && (i*8<xStart+Picture_Width)){
  137. if(Block == 0){
  138. SendData((pgm_read_byte(&(pbuffer[i-xStart/8 + (Picture_Width)/8*(j-yStart)]))) );
  139. }else{
  140. SendData(~(pgm_read_byte(&(pbuffer[i-xStart/8 + (Picture_Width)/8*(j-yStart)]))) );
  141. }
  142. }else {
  143. if(Block == 0){
  144. SendData(0xff);
  145. }else {
  146. SendData(0x00);
  147. }
  148. }
  149. }
  150. }
  151. if(Block == 1){
  152. SendCommand(0x22);
  153. SendData(0xC7);
  154. SendCommand(0x20);
  155. DelayMs(100);
  156. WaitUntilIdle();
  157. }
  158. }
  159. /**
  160. * @brief: After this command is transmitted, the chip would enter the
  161. * deep-sleep mode to save power.
  162. * The deep sleep mode would return to standby by hardware reset.
  163. * The only one parameter is a check code, the command would be
  164. * executed if check code = 0xA5.
  165. * You can use EPD_Reset() to awaken
  166. */
  167. void Epd::Sleep(void) {
  168. SendCommand(0x10); //deep sleep
  169. SendData(0x01);
  170. }
  171. /* END OF FILE */