epd1in54c.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**
  2. * @filename : epd1in54c.cpp
  3. * @brief : Implements for 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 "epd1in54c.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(POWER_SETTING);
  46. SendData(0x07);
  47. SendData(0x00);
  48. SendData(0x08);
  49. SendData(0x00);
  50. SendCommand(BOOSTER_SOFT_START);
  51. SendData(0x17);
  52. SendData(0x17);
  53. SendData(0x17);
  54. SendCommand(POWER_ON);
  55. WaitUntilIdle();
  56. SendCommand(PANEL_SETTING);
  57. SendData(0x0f);
  58. SendData(0x0d);
  59. SendCommand(VCOM_AND_DATA_INTERVAL_SETTING);
  60. SendData(0xF7);
  61. // SendCommand(PLL_CONTROL);
  62. // SendData(0x39);
  63. SendCommand(TCON_RESOLUTION);
  64. SendData(0x98);
  65. SendData(0x00);
  66. SendData(0x98);
  67. SendCommand(VCM_DC_SETTING_REGISTER);
  68. SendData(0xf7);
  69. SetLutBw();
  70. SetLutRed();
  71. /* EPD hardware init end */
  72. return 0;
  73. }
  74. /**
  75. * @brief: basic function for sending commands
  76. */
  77. void Epd::SendCommand(unsigned char command) {
  78. DigitalWrite(dc_pin, LOW);
  79. SpiTransfer(command);
  80. }
  81. /**
  82. * @brief: basic function for sending data
  83. */
  84. void Epd::SendData(unsigned char data) {
  85. DigitalWrite(dc_pin, HIGH);
  86. SpiTransfer(data);
  87. }
  88. /**
  89. * @brief: Wait until the busy_pin goes HIGH
  90. */
  91. void Epd::WaitUntilIdle(void) {
  92. while(DigitalRead(busy_pin) == 0) { //0: busy, 1: idle
  93. DelayMs(100);
  94. }
  95. }
  96. /**
  97. * @brief: module reset.
  98. * often used to awaken the module in deep sleep,
  99. * see Epd::Sleep();
  100. */
  101. void Epd::Reset(void) {
  102. DigitalWrite(reset_pin, LOW); //module reset
  103. DelayMs(200);
  104. DigitalWrite(reset_pin, HIGH);
  105. DelayMs(200);
  106. }
  107. /**
  108. * @brief: set the look-up tables
  109. */
  110. void Epd::SetLutBw(void) {
  111. unsigned int count;
  112. SendCommand(0x20); //g vcom
  113. for(count = 0; count < 15; count++) {
  114. SendData(lut_vcom0[count]);
  115. }
  116. SendCommand(0x21); //g ww --
  117. for(count = 0; count < 15; count++) {
  118. SendData(lut_w[count]);
  119. }
  120. SendCommand(0x22); //g bw r
  121. for(count = 0; count < 15; count++) {
  122. SendData(lut_b[count]);
  123. }
  124. SendCommand(0x23); //g wb w
  125. for(count = 0; count < 15; count++) {
  126. SendData(lut_g1[count]);
  127. }
  128. SendCommand(0x24); //g bb b
  129. for(count = 0; count < 15; count++) {
  130. SendData(lut_g2[count]);
  131. }
  132. }
  133. void Epd::SetLutRed(void) {
  134. unsigned int count;
  135. SendCommand(0x25);
  136. for(count = 0; count < 15; count++) {
  137. SendData(lut_vcom1[count]);
  138. }
  139. SendCommand(0x26);
  140. for(count = 0; count < 15; count++) {
  141. SendData(lut_red0[count]);
  142. }
  143. SendCommand(0x27);
  144. for(count = 0; count < 15; count++) {
  145. SendData(lut_red1[count]);
  146. }
  147. }
  148. void Epd::DisplayFrame(const unsigned char* frame_buffer_black, const unsigned char* frame_buffer_red) {
  149. if (frame_buffer_black != NULL) {
  150. SendCommand(DATA_START_TRANSMISSION_1);
  151. DelayMs(2);
  152. for (int i = 0; i < this->width * this->height / 8; i++) {
  153. SendData(pgm_read_byte(&frame_buffer_black[i]));
  154. }
  155. DelayMs(2);
  156. }
  157. if (frame_buffer_red != NULL) {
  158. SendCommand(DATA_START_TRANSMISSION_2);
  159. DelayMs(2);
  160. for (int i = 0; i < this->width * this->height / 8; i++) {
  161. SendData(pgm_read_byte(&frame_buffer_red[i]));
  162. }
  163. DelayMs(2);
  164. }
  165. SendCommand(DISPLAY_REFRESH);
  166. WaitUntilIdle();
  167. }
  168. /**
  169. * @brief: After this command is transmitted, the chip would enter the
  170. * deep-sleep mode to save power.
  171. * The deep sleep mode would return to standby by hardware reset.
  172. * The only one parameter is a check code, the command would be
  173. * executed if check code = 0xA5.
  174. * You can use Epd::Init() to awaken
  175. */
  176. void Epd::Sleep() {
  177. SendCommand(VCOM_AND_DATA_INTERVAL_SETTING);
  178. SendData(0x17);
  179. SendCommand(VCM_DC_SETTING_REGISTER); //to solve Vcom drop
  180. SendData(0x00);
  181. SendCommand(POWER_SETTING); //power setting
  182. SendData(0x02); //gate switch to external
  183. SendData(0x00);
  184. SendData(0x00);
  185. SendData(0x00);
  186. WaitUntilIdle();
  187. SendCommand(POWER_OFF); //power off
  188. }
  189. const unsigned char lut_vcom0[] =
  190. {
  191. 0x0E, 0x14, 0x01, 0x0A, 0x06, 0x04, 0x0A, 0x0A,
  192. 0x0F, 0x03, 0x03, 0x0C, 0x06, 0x0A, 0x00
  193. };
  194. const unsigned char lut_w[] =
  195. {
  196. 0x0E, 0x14, 0x01, 0x0A, 0x46, 0x04, 0x8A, 0x4A,
  197. 0x0F, 0x83, 0x43, 0x0C, 0x86, 0x0A, 0x04
  198. };
  199. const unsigned char lut_b[] =
  200. {
  201. 0x0E, 0x14, 0x01, 0x8A, 0x06, 0x04, 0x8A, 0x4A,
  202. 0x0F, 0x83, 0x43, 0x0C, 0x06, 0x4A, 0x04
  203. };
  204. const unsigned char lut_g1[] =
  205. {
  206. 0x8E, 0x94, 0x01, 0x8A, 0x06, 0x04, 0x8A, 0x4A,
  207. 0x0F, 0x83, 0x43, 0x0C, 0x06, 0x0A, 0x04
  208. };
  209. const unsigned char lut_g2[] =
  210. {
  211. 0x8E, 0x94, 0x01, 0x8A, 0x06, 0x04, 0x8A, 0x4A,
  212. 0x0F, 0x83, 0x43, 0x0C, 0x06, 0x0A, 0x04
  213. };
  214. const unsigned char lut_vcom1[] =
  215. {
  216. 0x03, 0x1D, 0x01, 0x01, 0x08, 0x23, 0x37, 0x37,
  217. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  218. };
  219. const unsigned char lut_red0[] =
  220. {
  221. 0x83, 0x5D, 0x01, 0x81, 0x48, 0x23, 0x77, 0x77,
  222. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  223. };
  224. const unsigned char lut_red1[] =
  225. {
  226. 0x03, 0x1D, 0x01, 0x01, 0x08, 0x23, 0x37, 0x37,
  227. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  228. };
  229. /* END OF FILE */