epd4in2b_V2.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * @filename : epd4in2b_V2.cpp
  3. * @brief : Implements for Dual-color e-paper library
  4. * @author : Yehui from Waveshare
  5. *
  6. * Copyright (C) Waveshare Nov 25 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 "epd4in2b_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. SendCommand(POWER_ON);
  46. WaitUntilIdle();
  47. SendCommand(PANEL_SETTING);
  48. SendData(0x0F); // LUT from OTP
  49. /* EPD hardware init end */
  50. return 0;
  51. }
  52. /**
  53. * @brief: basic function for sending commands
  54. */
  55. void Epd::SendCommand(unsigned char command) {
  56. DigitalWrite(dc_pin, LOW);
  57. SpiTransfer(command);
  58. }
  59. /**
  60. * @brief: basic function for sending data
  61. */
  62. void Epd::SendData(unsigned char data) {
  63. DigitalWrite(dc_pin, HIGH);
  64. SpiTransfer(data);
  65. }
  66. /**
  67. * @brief: Wait until the busy_pin goes HIGH
  68. */
  69. void Epd::WaitUntilIdle(void) {
  70. while(DigitalRead(busy_pin) == 0) { //0: busy, 1: idle
  71. DelayMs(100);
  72. }
  73. }
  74. /**
  75. * @brief: module reset.
  76. * often used to awaken the module in deep sleep,
  77. * see Epd::Sleep();
  78. */
  79. void Epd::Reset(void) {
  80. DigitalWrite(reset_pin, HIGH);
  81. DelayMs(200);
  82. DigitalWrite(reset_pin, LOW);
  83. DelayMs(2);
  84. DigitalWrite(reset_pin, HIGH);
  85. DelayMs(200);
  86. }
  87. /**
  88. * @brief: transmit partial data to the SRAM
  89. */
  90. void Epd::SetPartialWindow(const unsigned char* buffer_black, const unsigned char* buffer_red, int x, int y, int w, int l) {
  91. SendCommand(PARTIAL_IN);
  92. SendCommand(PARTIAL_WINDOW);
  93. SendData(x >> 8);
  94. SendData(x & 0xf8); // x should be the multiple of 8, the last 3 bit will always be ignored
  95. SendData(((x & 0xf8) + w - 1) >> 8);
  96. SendData(((x & 0xf8) + w - 1) | 0x07);
  97. SendData(y >> 8);
  98. SendData(y & 0xff);
  99. SendData((y + l - 1) >> 8);
  100. SendData((y + l - 1) & 0xff);
  101. SendData(0x01); // Gates scan both inside and outside of the partial window. (default)
  102. DelayMs(2);
  103. SendCommand(DATA_START_TRANSMISSION_1);
  104. if (buffer_black != NULL) {
  105. for(int i = 0; i < w / 8 * l; i++) {
  106. SendData(buffer_black[i]);
  107. }
  108. }
  109. DelayMs(2);
  110. SendCommand(DATA_START_TRANSMISSION_2);
  111. if (buffer_red != NULL) {
  112. for(int i = 0; i < w / 8 * l; i++) {
  113. SendData(buffer_red[i]);
  114. }
  115. }
  116. DelayMs(2);
  117. SendCommand(PARTIAL_OUT);
  118. }
  119. /**
  120. * @brief: transmit partial data to the black part of SRAM
  121. */
  122. void Epd::SetPartialWindowBlack(const unsigned char* buffer_black, int x, int y, int w, int l) {
  123. SendCommand(PARTIAL_IN);
  124. SendCommand(PARTIAL_WINDOW);
  125. SendData(x >> 8);
  126. SendData(x & 0xf8); // x should be the multiple of 8, the last 3 bit will always be ignored
  127. SendData(((x & 0xf8) + w - 1) >> 8);
  128. SendData(((x & 0xf8) + w - 1) | 0x07);
  129. SendData(y >> 8);
  130. SendData(y & 0xff);
  131. SendData((y + l - 1) >> 8);
  132. SendData((y + l - 1) & 0xff);
  133. SendData(0x01); // Gates scan both inside and outside of the partial window. (default)
  134. DelayMs(2);
  135. SendCommand(DATA_START_TRANSMISSION_1);
  136. if (buffer_black != NULL) {
  137. for(int i = 0; i < w / 8 * l; i++) {
  138. SendData(buffer_black[i]);
  139. }
  140. }
  141. DelayMs(2);
  142. SendCommand(PARTIAL_OUT);
  143. }
  144. /**
  145. * @brief: transmit partial data to the red part of SRAM
  146. */
  147. void Epd::SetPartialWindowRed(const unsigned char* buffer_red, int x, int y, int w, int l) {
  148. SendCommand(PARTIAL_IN);
  149. SendCommand(PARTIAL_WINDOW);
  150. SendData(x >> 8);
  151. SendData(x & 0xf8); // x should be the multiple of 8, the last 3 bit will always be ignored
  152. SendData(((x & 0xf8) + w - 1) >> 8);
  153. SendData(((x & 0xf8) + w - 1) | 0x07);
  154. SendData(y >> 8);
  155. SendData(y & 0xff);
  156. SendData((y + l - 1) >> 8);
  157. SendData((y + l - 1) & 0xff);
  158. SendData(0x01); // Gates scan both inside and outside of the partial window. (default)
  159. DelayMs(2);
  160. SendCommand(DATA_START_TRANSMISSION_2);
  161. if (buffer_red != NULL) {
  162. for(int i = 0; i < w / 8 * l; i++) {
  163. SendData(buffer_red[i]);
  164. }
  165. }
  166. DelayMs(2);
  167. SendCommand(PARTIAL_OUT);
  168. }
  169. /**
  170. * @brief: refresh and displays the frame
  171. */
  172. void Epd::DisplayFrame(const unsigned char* frame_black, const unsigned char* frame_red) {
  173. if (frame_black != NULL) {
  174. SendCommand(DATA_START_TRANSMISSION_1);
  175. DelayMs(2);
  176. for (int i = 0; i < this->width / 8 * this->height; i++) {
  177. SendData(pgm_read_byte(&frame_black[i]));
  178. }
  179. DelayMs(2);
  180. }
  181. if (frame_red != NULL) {
  182. SendCommand(DATA_START_TRANSMISSION_2);
  183. DelayMs(2);
  184. for (int i = 0; i < this->width / 8 * this->height; i++) {
  185. SendData(pgm_read_byte(&frame_red[i]));
  186. }
  187. DelayMs(2);
  188. }
  189. SendCommand(DISPLAY_REFRESH);
  190. WaitUntilIdle();
  191. }
  192. /**
  193. * @brief: clear the frame data from the SRAM, this won't refresh the display
  194. */
  195. void Epd::ClearFrame(void) {
  196. SendCommand(DATA_START_TRANSMISSION_1);
  197. DelayMs(2);
  198. for(int i = 0; i < width / 8 * height; i++) {
  199. SendData(0xFF);
  200. }
  201. DelayMs(2);
  202. SendCommand(DATA_START_TRANSMISSION_2);
  203. DelayMs(2);
  204. for(int i = 0; i < width / 8 * height; i++) {
  205. SendData(0xFF);
  206. }
  207. DelayMs(2);
  208. }
  209. /**
  210. * @brief: This displays the frame data from SRAM
  211. */
  212. void Epd::DisplayFrame(void) {
  213. SendCommand(DISPLAY_REFRESH);
  214. DelayMs(100);
  215. WaitUntilIdle();
  216. }
  217. /**
  218. * @brief: After this command is transmitted, the chip would enter the deep-sleep mode to save power.
  219. * The deep sleep mode would return to standby by hardware reset. The only one parameter is a
  220. * check code, the command would be executed if check code = 0xA5.
  221. * You can use Epd::Reset() to awaken and use Epd::Init() to initialize.
  222. */
  223. void Epd::Sleep() {
  224. SendCommand(VCOM_AND_DATA_INTERVAL_SETTING);
  225. SendData(0xF7); // border floating
  226. SendCommand(POWER_OFF);
  227. WaitUntilIdle();
  228. SendCommand(DEEP_SLEEP);
  229. SendData(0xA5); // check code
  230. }
  231. /* END OF FILE */