epd4in2b.cpp 7.4 KB

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