epd2in9._c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /**
  2. * @filename : epd2in9.c
  3. * @brief : Implements for e-paper library
  4. * @author : Yehui from Waveshare
  5. *
  6. * Copyright (C) Waveshare September 12 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 "epd1in54.h"
  28. #include "epd2in9.h"
  29. #include "epdif.h"
  30. int EPD_Init(EPD* epd, const unsigned char* lut) {
  31. epd->reset_pin = RST_PIN;
  32. epd->dc_pin = DC_PIN;
  33. epd->cs_pin = CS_PIN;
  34. epd->busy_pin = BUSY_PIN;
  35. epd->width = EPD_WIDTH;
  36. epd->height = EPD_HEIGHT;
  37. /* this calls the peripheral hardware interface, see epdif */
  38. if (EpdInitCallback() != 0) {
  39. return -1;
  40. }
  41. epd->lut = lut;
  42. /* EPD hardware init start */
  43. EPD_Reset(epd);
  44. EPD_SendCommand(epd, DRIVER_OUTPUT_CONTROL);
  45. EPD_SendData(epd, (EPD_HEIGHT - 1) & 0xFF);
  46. EPD_SendData(epd, ((EPD_HEIGHT - 1) >> 8) & 0xFF);
  47. EPD_SendData(epd, 0x00); // GD = 0; SM = 0; TB = 0;
  48. EPD_SendCommand(epd, BOOSTER_SOFT_START_CONTROL);
  49. EPD_SendData(epd, 0xD7);
  50. EPD_SendData(epd, 0xD6);
  51. EPD_SendData(epd, 0x9D);
  52. EPD_SendCommand(epd, WRITE_VCOM_REGISTER);
  53. EPD_SendData(epd, 0x9A); // VCOM 7C
  54. // EPD_SendData(epd, 0xA8); // VCOM 7C
  55. EPD_SendCommand(epd, SET_DUMMY_LINE_PERIOD);
  56. EPD_SendData(epd, 0x1A); // 4 dummy lines per gate
  57. EPD_SendCommand(epd, SET_GATE_TIME);
  58. EPD_SendData(epd, 0x08); // 2us per line
  59. EPD_SendCommand(epd, DATA_ENTRY_MODE_SETTING);
  60. EPD_SendData(epd, 0x03); // X increment; Y increment
  61. // EPD_DelayMs(epd, 10);
  62. // EPD_WaitUntilIdle(epd);
  63. EPD_SetLut(epd, epd->lut);
  64. /* EPD hardware init end */
  65. return 0;
  66. }
  67. /**
  68. * @brief: this calls the corresponding function from epdif.h
  69. * usually there is no need to change this function
  70. */
  71. void EPD_DigitalWrite(EPD* epd, int pin, int value) {
  72. EpdDigitalWriteCallback(pin, value);
  73. }
  74. /**
  75. * @brief: this calls the corresponding function from epdif.h
  76. * usually there is no need to change this function
  77. */
  78. int EPD_DigitalRead(EPD* epd, int pin) {
  79. return EpdDigitalReadCallback(pin);
  80. }
  81. /**
  82. * @brief: this calls the corresponding function from epdif.h
  83. * usually there is no need to change this function
  84. */
  85. void EPD_DelayMs(EPD* epd, unsigned int delaytime) { // 1ms
  86. EpdDelayMsCallback(delaytime);
  87. }
  88. /**
  89. * @brief: basic function for sending commands
  90. */
  91. void EPD_SendCommand(EPD* epd, unsigned char command) {
  92. EPD_DigitalWrite(epd, epd->dc_pin, LOW);
  93. EpdSpiTransferCallback(command);
  94. }
  95. /**
  96. * @brief: basic function for sending data
  97. */
  98. void EPD_SendData(EPD* epd, unsigned char data) {
  99. EPD_DigitalWrite(epd, epd->dc_pin, HIGH);
  100. EpdSpiTransferCallback(data);
  101. }
  102. /**
  103. * @brief: Wait until the busy_pin goes LOW
  104. */
  105. void EPD_WaitUntilIdle(EPD* epd) {
  106. while(EPD_DigitalRead(epd, epd->busy_pin) == HIGH) { //0: busy, 1: idle
  107. EPD_DelayMs(epd, 100);
  108. }
  109. }
  110. /**
  111. * @brief: module reset.
  112. * often used to awaken the module in deep sleep,
  113. * see EPD::Sleep();
  114. */
  115. void EPD_Reset(EPD* epd) {
  116. EPD_DigitalWrite(epd, epd->reset_pin, LOW); //module reset
  117. EPD_DelayMs(epd, 30);
  118. EPD_DigitalWrite(epd, epd->reset_pin, HIGH);
  119. EPD_DelayMs(epd, 30);
  120. }
  121. /**
  122. * @brief: put an image buffer to the frame memory.
  123. * this won't update the display.
  124. */
  125. void EPD_SetFrameMemory(
  126. EPD* epd,
  127. const unsigned char* image_buffer,
  128. int x,
  129. int y,
  130. int image_width,
  131. int image_height
  132. ) {
  133. int x_end;
  134. int y_end;
  135. if (
  136. image_buffer == NULL ||
  137. x < 0 || image_width < 0 ||
  138. y < 0 || image_height < 0
  139. ) {
  140. return;
  141. }
  142. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  143. x &= 0xF8;
  144. image_width &= 0xF8;
  145. if (x + image_width >= epd->width) {
  146. x_end = epd->width - 1;
  147. } else {
  148. x_end = x + image_width - 1;
  149. }
  150. if (y + image_height >= epd->height) {
  151. y_end = epd->height - 1;
  152. } else {
  153. y_end = y + image_height - 1;
  154. }
  155. EPD_SetMemoryArea(epd, x, y, x_end, y_end);
  156. EPD_SetMemoryPointer(epd, x, y);
  157. EPD_SendCommand(epd, WRITE_RAM);
  158. /* send the image data */
  159. for (int j = 0; j < y_end - y + 1; j++) {
  160. for (int i = 0; i < (x_end - x + 1) / 8; i++) {
  161. EPD_SendData(epd, image_buffer[i + j * (image_width / 8)]);
  162. }
  163. }
  164. }
  165. /**
  166. * @brief: clear the frame memory with the specified color.
  167. * this won't update the display.
  168. */
  169. void EPD_ClearFrameMemory(EPD* epd, unsigned char color) {
  170. EPD_SetMemoryArea(epd, 0, 0, epd->width - 1, epd->height - 1);
  171. EPD_SetMemoryPointer(epd, 0, 0);
  172. EPD_SendCommand(epd, WRITE_RAM);
  173. /* send the color data */
  174. for (int i = 0; i < epd->width / 8 * epd->height; i++) {
  175. EPD_SendData(epd, color);
  176. }
  177. }
  178. /**
  179. * @brief: update the display
  180. * there are 2 memory areas embedded in the e-paper display
  181. * but once this function is called,
  182. * the the next action of SetFrameMemory or ClearFrame will
  183. * set the other memory area.
  184. */
  185. void EPD_DisplayFrame(EPD* epd) {
  186. EPD_SendCommand(epd, DISPLAY_UPDATE_CONTROL_2);
  187. EPD_SendData(epd, 0xC4);
  188. EPD_SendCommand(epd, MASTER_ACTIVATION);
  189. EPD_SendCommand(epd, TERMINATE_FRAME_READ_WRITE);
  190. EPD_WaitUntilIdle(epd);
  191. }
  192. /**
  193. * @brief: After this command is transmitted, the chip would enter the
  194. * deep-sleep mode to save power.
  195. * The deep sleep mode would return to standby by hardware reset.
  196. * You can use EPD_Init() to awaken
  197. */
  198. void EPD_Sleep(EPD* epd) {
  199. EPD_SendCommand(epd, DEEP_SLEEP_MODE);
  200. EPD_WaitUntilIdle(epd);
  201. }
  202. /**
  203. * @brief: set the look-up tables
  204. */
  205. static void EPD_SetLut(EPD* epd, const unsigned char* lut) {
  206. epd->lut = lut;
  207. EPD_SendCommand(epd, WRITE_LUT_REGISTER);
  208. /* the length of look-up table is 30 bytes */
  209. for (int i = 0; i < 30; i++) {
  210. EPD_SendData(epd, epd->lut[i]);
  211. }
  212. }
  213. /**
  214. * @brief: private function to specify the memory area for data R/W
  215. */
  216. static void EPD_SetMemoryArea(EPD* epd, int x_start, int y_start, int x_end, int y_end) {
  217. EPD_SendCommand(epd, SET_RAM_X_ADDRESS_START_END_POSITION);
  218. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  219. EPD_SendData(epd, (x_start >> 3) & 0xFF);
  220. EPD_SendData(epd, (x_end >> 3) & 0xFF);
  221. EPD_SendCommand(epd, SET_RAM_Y_ADDRESS_START_END_POSITION);
  222. EPD_SendData(epd, y_start & 0xFF);
  223. EPD_SendData(epd, (y_start >> 8) & 0xFF);
  224. EPD_SendData(epd, y_end & 0xFF);
  225. EPD_SendData(epd, (y_end >> 8) & 0xFF);
  226. }
  227. /**
  228. * @brief: private function to specify the start point for data R/W
  229. */
  230. static void EPD_SetMemoryPointer(EPD* epd, int x, int y) {
  231. EPD_SendCommand(epd, SET_RAM_X_ADDRESS_COUNTER);
  232. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  233. EPD_SendData(epd, (x >> 3) & 0xFF);
  234. EPD_SendCommand(epd, SET_RAM_Y_ADDRESS_COUNTER);
  235. EPD_SendData(epd, y & 0xFF);
  236. EPD_SendData(epd, (y >> 8) & 0xFF);
  237. EPD_WaitUntilIdle(epd);
  238. }
  239. const unsigned char lut_full_update[] =
  240. {
  241. 0x02, 0x02, 0x01, 0x11, 0x12, 0x12, 0x22, 0x22,
  242. 0x66, 0x69, 0x69, 0x59, 0x58, 0x99, 0x99, 0x88,
  243. 0x00, 0x00, 0x00, 0x00, 0xF8, 0xB4, 0x13, 0x51,
  244. 0x35, 0x51, 0x51, 0x19, 0x01, 0x00
  245. };
  246. const unsigned char lut_partial_update[] =
  247. {
  248. 0x10, 0x18, 0x18, 0x08, 0x18, 0x18, 0x08, 0x00,
  249. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  250. 0x00, 0x00, 0x00, 0x00, 0x13, 0x14, 0x44, 0x12,
  251. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  252. };
  253. /* END OF FILE */