2
0

epd2in9.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * @filename : epd2in9.cpp
  3. * @brief : Implements for e-paper library
  4. * @author : Yehui from Waveshare
  5. *
  6. * Copyright (C) Waveshare September 9 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 "epd2in9.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(const unsigned char* lut) {
  39. /* this calls the peripheral hardware interface, see epdif */
  40. if (IfInit() != 0) {
  41. return -1;
  42. }
  43. /* EPD hardware init start */
  44. this->lut = lut;
  45. Reset();
  46. SendCommand(DRIVER_OUTPUT_CONTROL);
  47. SendData((EPD_HEIGHT - 1) & 0xFF);
  48. SendData(((EPD_HEIGHT - 1) >> 8) & 0xFF);
  49. SendData(0x00); // GD = 0; SM = 0; TB = 0;
  50. SendCommand(BOOSTER_SOFT_START_CONTROL);
  51. SendData(0xD7);
  52. SendData(0xD6);
  53. SendData(0x9D);
  54. SendCommand(WRITE_VCOM_REGISTER);
  55. SendData(0xA8); // VCOM 7C
  56. SendCommand(SET_DUMMY_LINE_PERIOD);
  57. SendData(0x1A); // 4 dummy lines per gate
  58. SendCommand(SET_GATE_TIME);
  59. SendData(0x08); // 2us per line
  60. SendCommand(DATA_ENTRY_MODE_SETTING);
  61. SendData(0x03); // X increment; Y increment
  62. SetLut(this->lut);
  63. /* EPD hardware init end */
  64. return 0;
  65. }
  66. /**
  67. * @brief: basic function for sending commands
  68. */
  69. void Epd::SendCommand(unsigned char command) {
  70. DigitalWrite(dc_pin, LOW);
  71. SpiTransfer(command);
  72. }
  73. /**
  74. * @brief: basic function for sending data
  75. */
  76. void Epd::SendData(unsigned char data) {
  77. DigitalWrite(dc_pin, HIGH);
  78. SpiTransfer(data);
  79. }
  80. /**
  81. * @brief: Wait until the busy_pin goes LOW
  82. */
  83. void Epd::WaitUntilIdle(void) {
  84. while(DigitalRead(busy_pin) == HIGH) { //LOW: idle, HIGH: busy
  85. DelayMs(100);
  86. }
  87. }
  88. /**
  89. * @brief: module reset.
  90. * often used to awaken the module in deep sleep,
  91. * see Epd::Sleep();
  92. */
  93. void Epd::Reset(void) {
  94. DigitalWrite(reset_pin, LOW); //module reset
  95. DelayMs(200);
  96. DigitalWrite(reset_pin, HIGH);
  97. DelayMs(200);
  98. }
  99. /**
  100. * @brief: set the look-up table register
  101. */
  102. void Epd::SetLut(const unsigned char* lut) {
  103. this->lut = lut;
  104. SendCommand(WRITE_LUT_REGISTER);
  105. /* the length of look-up table is 30 bytes */
  106. for (int i = 0; i < 30; i++) {
  107. SendData(this->lut[i]);
  108. }
  109. }
  110. /**
  111. * @brief: put an image buffer to the frame memory.
  112. * this won't update the display.
  113. */
  114. void Epd::SetFrameMemory(
  115. const unsigned char* image_buffer,
  116. int x,
  117. int y,
  118. int image_width,
  119. int image_height
  120. ) {
  121. int x_end;
  122. int y_end;
  123. if (
  124. image_buffer == NULL ||
  125. x < 0 || image_width < 0 ||
  126. y < 0 || image_height < 0
  127. ) {
  128. return;
  129. }
  130. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  131. x &= 0xF8;
  132. image_width &= 0xF8;
  133. if (x + image_width >= this->width) {
  134. x_end = this->width - 1;
  135. } else {
  136. x_end = x + image_width - 1;
  137. }
  138. if (y + image_height >= this->height) {
  139. y_end = this->height - 1;
  140. } else {
  141. y_end = y + image_height - 1;
  142. }
  143. SetMemoryArea(x, y, x_end, y_end);
  144. SetMemoryPointer(x, y);
  145. SendCommand(WRITE_RAM);
  146. /* send the image data */
  147. for (int j = 0; j < y_end - y + 1; j++) {
  148. for (int i = 0; i < (x_end - x + 1) / 8; i++) {
  149. SendData(image_buffer[i + j * (image_width / 8)]);
  150. }
  151. }
  152. }
  153. /**
  154. * @brief: put an image buffer to the frame memory.
  155. * this won't update the display.
  156. *
  157. * Question: When do you use this function instead of
  158. * void SetFrameMemory(
  159. * const unsigned char* image_buffer,
  160. * int x,
  161. * int y,
  162. * int image_width,
  163. * int image_height
  164. * );
  165. * Answer: SetFrameMemory with parameters only reads image data
  166. * from the RAM but not from the flash in AVR chips (for AVR chips,
  167. * you have to use the function pgm_read_byte to read buffers
  168. * from the flash).
  169. */
  170. void Epd::SetFrameMemory(const unsigned char* image_buffer) {
  171. SetMemoryArea(0, 0, this->width - 1, this->height - 1);
  172. SetMemoryPointer(0, 0);
  173. SendCommand(WRITE_RAM);
  174. /* send the image data */
  175. for (int i = 0; i < this->width / 8 * this->height; i++) {
  176. SendData(pgm_read_byte(&image_buffer[i]));
  177. }
  178. }
  179. /**
  180. * @brief: clear the frame memory with the specified color.
  181. * this won't update the display.
  182. */
  183. void Epd::ClearFrameMemory(unsigned char color) {
  184. SetMemoryArea(0, 0, this->width - 1, this->height - 1);
  185. SetMemoryPointer(0, 0);
  186. SendCommand(WRITE_RAM);
  187. /* send the color data */
  188. for (int i = 0; i < this->width / 8 * this->height; i++) {
  189. SendData(color);
  190. }
  191. }
  192. /**
  193. * @brief: update the display
  194. * there are 2 memory areas embedded in the e-paper display
  195. * but once this function is called,
  196. * the the next action of SetFrameMemory or ClearFrame will
  197. * set the other memory area.
  198. */
  199. void Epd::DisplayFrame(void) {
  200. SendCommand(DISPLAY_UPDATE_CONTROL_2);
  201. SendData(0xC4);
  202. SendCommand(MASTER_ACTIVATION);
  203. SendCommand(TERMINATE_FRAME_READ_WRITE);
  204. WaitUntilIdle();
  205. }
  206. /**
  207. * @brief: private function to specify the memory area for data R/W
  208. */
  209. void Epd::SetMemoryArea(int x_start, int y_start, int x_end, int y_end) {
  210. SendCommand(SET_RAM_X_ADDRESS_START_END_POSITION);
  211. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  212. SendData((x_start >> 3) & 0xFF);
  213. SendData((x_end >> 3) & 0xFF);
  214. SendCommand(SET_RAM_Y_ADDRESS_START_END_POSITION);
  215. SendData(y_start & 0xFF);
  216. SendData((y_start >> 8) & 0xFF);
  217. SendData(y_end & 0xFF);
  218. SendData((y_end >> 8) & 0xFF);
  219. }
  220. /**
  221. * @brief: private function to specify the start point for data R/W
  222. */
  223. void Epd::SetMemoryPointer(int x, int y) {
  224. SendCommand(SET_RAM_X_ADDRESS_COUNTER);
  225. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  226. SendData((x >> 3) & 0xFF);
  227. SendCommand(SET_RAM_Y_ADDRESS_COUNTER);
  228. SendData(y & 0xFF);
  229. SendData((y >> 8) & 0xFF);
  230. WaitUntilIdle();
  231. }
  232. /**
  233. * @brief: After this command is transmitted, the chip would enter the
  234. * deep-sleep mode to save power.
  235. * The deep sleep mode would return to standby by hardware reset.
  236. * You can use Epd::Init() to awaken
  237. */
  238. void Epd::Sleep() {
  239. SendCommand(DEEP_SLEEP_MODE);
  240. SendData(0x01);
  241. // WaitUntilIdle();
  242. }
  243. const unsigned char lut_full_update[] = {
  244. 0x50, 0xAA, 0x55, 0xAA, 0x11, 0x00,
  245. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  246. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  247. 0x00, 0x00, 0xFF, 0xFF, 0x1F, 0x00,
  248. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  249. };
  250. const unsigned char lut_partial_update[] = {
  251. 0x10, 0x18, 0x18, 0x08, 0x18, 0x18,
  252. 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  253. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  254. 0x00, 0x00, 0x13, 0x14, 0x44, 0x12,
  255. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  256. };
  257. /* END OF FILE */