epd2in9.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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_2IN9F_Init(EPD* epd, const unsigned char* lut) {
  31. epd->reset_pin = EPD_RST_PIN;
  32. epd->dc_pin = EPD_DC_PIN;
  33. epd->cs_pin = EPD_CS_PIN;
  34. epd->busy_pin = EPD_BUSY_PIN;
  35. epd->width = EPD_2IN9F_WIDTH;
  36. epd->height = EPD_2IN9F_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_2IN9F_Reset(epd);
  44. EPD_2IN9F_SendCommand(epd, DRIVER_OUTPUT_CONTROL);
  45. EPD_2IN9F_SendData(epd, (EPD_2IN9F_HEIGHT - 1) & 0xFF);
  46. EPD_2IN9F_SendData(epd, ((EPD_2IN9F_HEIGHT - 1) >> 8) & 0xFF);
  47. EPD_2IN9F_SendData(epd, 0x00); // GD = 0; SM = 0; TB = 0;
  48. EPD_2IN9F_SendCommand(epd, BOOSTER_SOFT_START_CONTROL);
  49. EPD_2IN9F_SendData(epd, 0xD7);
  50. EPD_2IN9F_SendData(epd, 0xD6);
  51. EPD_2IN9F_SendData(epd, 0x9D);
  52. EPD_2IN9F_SendCommand(epd, WRITE_VCOM_REGISTER);
  53. EPD_2IN9F_SendData(epd, 0x9A); // VCOM 7C
  54. // EPD_2IN9F_SendData(epd, 0xA8); // VCOM 7C
  55. EPD_2IN9F_SendCommand(epd, SET_DUMMY_LINE_PERIOD);
  56. EPD_2IN9F_SendData(epd, 0x1A); // 4 dummy lines per gate
  57. EPD_2IN9F_SendCommand(epd, SET_GATE_TIME);
  58. EPD_2IN9F_SendData(epd, 0x08); // 2us per line
  59. EPD_2IN9F_SendCommand(epd, DATA_ENTRY_MODE_SETTING);
  60. EPD_2IN9F_SendData(epd, 0x03); // X increment; Y increment
  61. // EPD_2IN9F_DelayMs(epd, 10);
  62. // EPD_2IN9F_WaitUntilIdle(epd);
  63. EPD_2IN9F_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_2IN9F_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_2IN9F_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_2IN9F_DelayMs(EPD* epd, unsigned int delaytime) { // 1ms
  86. EpdDelayMsCallback(delaytime);
  87. }
  88. /**
  89. * @brief: basic function for sending commands
  90. */
  91. void EPD_2IN9F_SendCommand(EPD* epd, unsigned char command) {
  92. EPD_2IN9F_DigitalWrite(epd, epd->dc_pin, LOW);
  93. EpdSpiTransferCallback(command);
  94. }
  95. /**
  96. * @brief: basic function for sending data
  97. */
  98. void EPD_2IN9F_SendData(EPD* epd, unsigned char data) {
  99. EPD_2IN9F_DigitalWrite(epd, epd->dc_pin, HIGH);
  100. EpdSpiTransferCallback(data);
  101. }
  102. /**
  103. * @brief: Wait until the busy_pin goes LOW
  104. */
  105. void EPD_2IN9F_WaitUntilIdle(EPD* epd) {
  106. while(EPD_2IN9F_DigitalRead(epd, epd->busy_pin) == HIGH) { //0: busy, 1: idle
  107. EPD_2IN9F_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_2IN9F_Reset(EPD* epd) {
  116. EPD_2IN9F_DigitalWrite(epd, epd->reset_pin, LOW); //module reset
  117. EPD_2IN9F_DelayMs(epd, 30);
  118. EPD_2IN9F_DigitalWrite(epd, epd->reset_pin, HIGH);
  119. EPD_2IN9F_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_2IN9F_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_2IN9F_SetMemoryArea(epd, x, y, x_end, y_end);
  156. EPD_2IN9F_SetMemoryPointer(epd, x, y);
  157. EPD_2IN9F_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_2IN9F_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_2IN9F_ClearFrameMemory(EPD* epd, unsigned char color) {
  170. EPD_2IN9F_SetMemoryArea(epd, 0, 0, epd->width - 1, epd->height - 1);
  171. EPD_2IN9F_SetMemoryPointer(epd, 0, 0);
  172. EPD_2IN9F_SendCommand(epd, WRITE_RAM);
  173. /* send the color data */
  174. for (int i = 0; i < epd->width / 8 * epd->height; i++) {
  175. EPD_2IN9F_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_2IN9F_DisplayFrame(EPD* epd) {
  186. EPD_2IN9F_SendCommand(epd, DISPLAY_UPDATE_CONTROL_2);
  187. EPD_2IN9F_SendData(epd, 0xC4);
  188. EPD_2IN9F_SendCommand(epd, MASTER_ACTIVATION);
  189. EPD_2IN9F_SendCommand(epd, TERMINATE_FRAME_READ_WRITE);
  190. EPD_2IN9F_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_2IN9F_Init() to awaken
  197. */
  198. void EPD_2IN9F_Sleep(EPD* epd) {
  199. EPD_2IN9F_SendCommand(epd, DEEP_SLEEP_MODE);
  200. EPD_2IN9F_WaitUntilIdle(epd);
  201. }
  202. /**
  203. * @brief: set the look-up tables
  204. */
  205. static void EPD_2IN9F_SetLut(EPD* epd, const unsigned char* lut) {
  206. epd->lut = lut;
  207. EPD_2IN9F_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_2IN9F_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_2IN9F_SetMemoryArea(EPD* epd, int x_start, int y_start, int x_end, int y_end) {
  217. EPD_2IN9F_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_2IN9F_SendData(epd, (x_start >> 3) & 0xFF);
  220. EPD_2IN9F_SendData(epd, (x_end >> 3) & 0xFF);
  221. EPD_2IN9F_SendCommand(epd, SET_RAM_Y_ADDRESS_START_END_POSITION);
  222. EPD_2IN9F_SendData(epd, y_start & 0xFF);
  223. EPD_2IN9F_SendData(epd, (y_start >> 8) & 0xFF);
  224. EPD_2IN9F_SendData(epd, y_end & 0xFF);
  225. EPD_2IN9F_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_2IN9F_SetMemoryPointer(EPD* epd, int x, int y) {
  231. EPD_2IN9F_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_2IN9F_SendData(epd, (x >> 3) & 0xFF);
  234. EPD_2IN9F_SendCommand(epd, SET_RAM_Y_ADDRESS_COUNTER);
  235. EPD_2IN9F_SendData(epd, y & 0xFF);
  236. EPD_2IN9F_SendData(epd, (y >> 8) & 0xFF);
  237. EPD_2IN9F_WaitUntilIdle(epd);
  238. }
  239. static 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. // static 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 */
  254. //-----------------------------
  255. #define UBYTE uint8_t
  256. UBYTE EPD_2IN9FF_Init(void);
  257. void EPD_2IN9FF_Clear(void);
  258. void EPD_2IN9FF_Display(UBYTE *Image);
  259. void EPD_2IN9FF_Sleep(void);
  260. #define BUSY_Pin (18)
  261. #define RST_Pin (7)
  262. #define DC_Pin (9)
  263. #define SPI_CS_Pin (16)
  264. static EPD _epd;
  265. UBYTE EPD_2IN9FF_Init(void) {
  266. _epd.busy_pin = BUSY_Pin;
  267. _epd.reset_pin = RST_PIN;
  268. _epd.dc_pin = DC_PIN;
  269. _epd.cs_pin = SPI_CS_Pin;
  270. _epd.width = EPD_2IN9F_WIDTH;
  271. _epd.height = EPD_2IN9F_HEIGHT;
  272. EPD_2IN9F_Init(&_epd, lut_full_update);
  273. return 0;
  274. }
  275. void EPD_2IN9FF_Clear(void) {
  276. ;
  277. }
  278. void EPD_2IN9FF_Display(UBYTE *Image) {
  279. EPD_2IN9F_SetFrameMemory(&_epd, Image, 0, 0, EPD_2IN9F_WIDTH, EPD_2IN9F_HEIGHT);
  280. EPD_2IN9F_DisplayFrame(&_epd);
  281. }
  282. void EPD_2IN9FF_Sleep(void) {
  283. EPD_2IN9F_Sleep(&_epd);
  284. }