epd2in7b_V2.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * @filename : epd2in7b_V2.cpp
  3. * @brief : Implements for Dual-color e-paper library
  4. * @author :
  5. *
  6. * Copyright (C) Waveshare Feb 19 2021
  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 "epd2in7b_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. /* this calls the peripheral hardware interface, see epdif */
  40. if (IfInit() != 0) {
  41. return -1;
  42. }
  43. /* EPD hardware init start */
  44. Reset();
  45. WaitUntilIdle();
  46. SendCommand(0x12);
  47. WaitUntilIdle();
  48. SendCommand(0x00);
  49. SendData(0x27);
  50. SendData(0x01);
  51. SendData(0x00);
  52. SendCommand(0x11);
  53. SendData(0x03);
  54. SetWindows(0, 0, width-1, height-1);
  55. SetCursor(0, 0);
  56. /* EPD hardware init end */
  57. return 0;
  58. }
  59. /**
  60. * @brief: basic function for sending commands
  61. */
  62. void Epd::SendCommand(unsigned char command) {
  63. DigitalWrite(dc_pin, LOW);
  64. SpiTransfer(command);
  65. }
  66. /**
  67. * @brief: basic function for sending data
  68. */
  69. void Epd::SendData(unsigned char data) {
  70. DigitalWrite(dc_pin, HIGH);
  71. SpiTransfer(data);
  72. }
  73. /**
  74. * @brief: Wait until the busy_pin goes HIGH
  75. */
  76. void Epd::WaitUntilIdle(void) {
  77. while(DigitalRead(busy_pin) == 1) { //0: busy, 1: idle
  78. DelayMs(10);
  79. }
  80. }
  81. /******************************************************************************
  82. function : Setting the display window
  83. parameter:
  84. ******************************************************************************/
  85. void Epd::SetWindows(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
  86. {
  87. SendCommand(0x44); // SET_RAM_X_ADDRESS_START_END_POSITION
  88. SendData((Xstart>>3) & 0xFF);
  89. SendData((Xend>>3) & 0xFF);
  90. SendCommand(0x45); // SET_RAM_Y_ADDRESS_START_END_POSITION
  91. SendData(Ystart & 0xFF);
  92. SendData((Ystart >> 8) & 0xFF);
  93. SendData(Yend & 0xFF);
  94. SendData((Yend >> 8) & 0xFF);
  95. }
  96. /******************************************************************************
  97. function : Set Cursor
  98. parameter:
  99. ******************************************************************************/
  100. void Epd::SetCursor(UWORD Xstart, UWORD Ystart)
  101. {
  102. SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  103. SendData(Xstart & 0xFF);
  104. SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  105. SendData(Ystart & 0xFF);
  106. SendData((Ystart >> 8) & 0xFF);
  107. }
  108. /**
  109. * @brief: module reset.
  110. * often used to awaken the module in deep sleep,
  111. * see Epd::Sleep();
  112. */
  113. void Epd::Reset(void) {
  114. DigitalWrite(reset_pin, HIGH);
  115. DelayMs(200);
  116. DigitalWrite(reset_pin, LOW);
  117. DelayMs(2);
  118. DigitalWrite(reset_pin, HIGH);
  119. DelayMs(200);
  120. }
  121. /**
  122. * @brief: transmit partial data to the black part of SRAM
  123. */
  124. void Epd::TransmitPartialBlack(const unsigned char* buffer_black, int x, int y, int w, int l) {
  125. if (buffer_black != NULL) {
  126. int width = (w%8 == 0 ? w/8 : w/8+1);
  127. SetWindows(x, y, w+x-1, l+y-1);
  128. SetCursor(x, y);
  129. SendCommand(0x24);
  130. DelayMs(2);
  131. for(int i = 0; i < width * l; i++) {
  132. SendData(~buffer_black[i]);
  133. }
  134. DelayMs(2);
  135. }
  136. }
  137. /**
  138. * @brief: transmit partial data to the red part of SRAM
  139. */
  140. void Epd::TransmitPartialRed(const unsigned char* buffer_red, int x, int y, int w, int l) {
  141. if (buffer_red != NULL) {
  142. int width = (w%8 == 0 ? w/8 : w/8+1);
  143. SetWindows(x, y, w+x-1, l+y-1);
  144. SetCursor(x, y);
  145. SendCommand(0x26);
  146. DelayMs(2);
  147. for(int i = 0; i < width * l; i++) {
  148. SendData(buffer_red[i]);
  149. }
  150. DelayMs(2);
  151. }
  152. }
  153. /**
  154. * @brief: refresh and displays the frame
  155. */
  156. void Epd::DisplayFrame(const unsigned char* frame_buffer_black, const unsigned char* frame_buffer_red) {
  157. if (frame_buffer_black != NULL) {
  158. SendCommand(0x24);
  159. DelayMs(2);
  160. for(int i = 0; i < width * height / 8; i++) {
  161. SendData(~pgm_read_byte(&frame_buffer_black[i]));
  162. }
  163. DelayMs(2);
  164. }
  165. if (frame_buffer_red != NULL) {
  166. SendCommand(0x26);
  167. DelayMs(2);
  168. for(int i = 0; i < width * height / 8; i++) {
  169. SendData(pgm_read_byte(&frame_buffer_red[i]));
  170. }
  171. DelayMs(2);
  172. }
  173. DisplayFrame();
  174. }
  175. /**
  176. * @brief: clear the frame data from the SRAM, this won't refresh the display
  177. */
  178. void Epd::ClearFrame(void) {
  179. SendCommand(0x24);
  180. DelayMs(2);
  181. for(int i = 0; i < width * height / 8; i++) {
  182. SendData(0xff);
  183. }
  184. DelayMs(2);
  185. SendCommand(0x26);
  186. DelayMs(2);
  187. for(int i = 0; i < width * height / 8; i++) {
  188. SendData(0x00);
  189. }
  190. DelayMs(2);
  191. DisplayFrame();
  192. }
  193. /**
  194. * @brief: This displays the frame data from SRAM
  195. */
  196. void Epd::DisplayFrame(void) {
  197. SendCommand(0x20);
  198. WaitUntilIdle();
  199. }
  200. /**
  201. * @brief: After this command is transmitted, the chip would enter the deep-sleep mode to save power.
  202. * The deep sleep mode would return to standby by hardware reset. The only one parameter is a
  203. * check code, the command would be executed if check code = 0xA5.
  204. * You can use Epd::Reset() to awaken and use Epd::Init() to initialize.
  205. */
  206. void Epd::Sleep() {
  207. SendCommand(0x10);
  208. SendData(0x01);
  209. }
  210. /* END OF FILE */