epd2in9b.cpp 7.8 KB

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