2
0

epd2in13b.cpp 7.7 KB

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