2
0

epd7in5b_V2.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * @filename : epd7in5b_V2.cpp
  3. * @brief : Implements for e-paper library
  4. * @author : Yehui from Waveshare
  5. *
  6. * Copyright (C) Waveshare Nov 30 2020
  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_V2.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); //POWER SETTING
  44. SendData(0x07);
  45. SendData(0x07); //VGH=20V,VGL=-20V
  46. SendData(0x3f); //VDH=15V
  47. SendData(0x3f); //VDL=-15V
  48. SendCommand(0x04); //POWER ON
  49. DelayMs(100);
  50. WaitUntilIdle();
  51. SendCommand(0X00); //PANNEL SETTING
  52. SendData(0x0F); //KW-3f KWR-2F BWROTP 0f BWOTP 1f
  53. SendCommand(0x61); //tres
  54. SendData(0x03); //source 800
  55. SendData(0x20);
  56. SendData(0x01); //gate 480
  57. SendData(0xE0);
  58. SendCommand(0X15);
  59. SendData(0x00);
  60. SendCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  61. SendData(0x11);
  62. SendData(0x07);
  63. SendCommand(0X60); //TCON SETTING
  64. SendData(0x22);
  65. SendCommand(0x65); // Resolution setting
  66. SendData(0x00);
  67. SendData(0x00);//800*480
  68. SendData(0x00);
  69. SendData(0x00);
  70. return 0;
  71. }
  72. /**
  73. * @brief: basic function for sending commands
  74. */
  75. void Epd::SendCommand(unsigned char command) {
  76. DigitalWrite(dc_pin, LOW);
  77. SpiTransfer(command);
  78. }
  79. /**
  80. * @brief: basic function for sending data
  81. */
  82. void Epd::SendData(unsigned char data) {
  83. DigitalWrite(dc_pin, HIGH);
  84. SpiTransfer(data);
  85. }
  86. /**
  87. * @brief: Wait until the busy_pin goes HIGH
  88. */
  89. void Epd::WaitUntilIdle(void) {
  90. unsigned char busy;
  91. do {
  92. SendCommand(0x71);
  93. busy = DigitalRead(busy_pin);
  94. busy =!(busy & 0x01);
  95. }while(busy);
  96. DelayMs(200);
  97. }
  98. /**
  99. * @brief: module reset.
  100. * often used to awaken the module in deep sleep,
  101. * see Epd::Sleep();
  102. */
  103. void Epd::Reset(void) {
  104. DigitalWrite(reset_pin, HIGH);
  105. DelayMs(200);
  106. DigitalWrite(reset_pin, LOW); //module reset
  107. DelayMs(2);
  108. DigitalWrite(reset_pin, HIGH);
  109. DelayMs(200);
  110. }
  111. void Epd::Displaypart(const unsigned char* pbuffer, unsigned long xStart, unsigned long yStart,\
  112. unsigned long Picture_Width, unsigned long Picture_Height, unsigned char Block) {
  113. if(Block == 0){
  114. SendCommand(0x10);
  115. }else if(Block == 1){
  116. SendCommand(0x13);
  117. }
  118. for (unsigned long j = 0; j < height; j++) {
  119. for (unsigned long i = 0; i < width/8; i++) {
  120. if( (j>=yStart) && (j<yStart+Picture_Height) && (i*8>=xStart) && (i*8<xStart+Picture_Width)){
  121. if(Block == 0){
  122. SendData((pgm_read_byte(&(pbuffer[i-xStart/8 + (Picture_Width)/8*(j-yStart)]))) );
  123. }else{
  124. SendData(~(pgm_read_byte(&(pbuffer[i-xStart/8 + (Picture_Width)/8*(j-yStart)]))) );
  125. }
  126. }else {
  127. if(Block == 0){
  128. SendData(0xff);
  129. }else {
  130. SendData(0x00);
  131. }
  132. }
  133. }
  134. }
  135. if(Block == 1){
  136. SendCommand(0x12);
  137. DelayMs(100);
  138. WaitUntilIdle();
  139. }
  140. }
  141. /**
  142. * @brief: After this command is transmitted, the chip would enter the
  143. * deep-sleep mode to save power.
  144. * The deep sleep mode would return to standby by hardware reset.
  145. * The only one parameter is a check code, the command would be
  146. * executed if check code = 0xA5.
  147. * You can use EPD_Reset() to awaken
  148. */
  149. void Epd::Sleep(void) {
  150. SendCommand(0X02);
  151. WaitUntilIdle();
  152. SendCommand(0X07);
  153. SendData(0xa5);
  154. }
  155. /* END OF FILE */