epd1in54b.cpp 7.1 KB

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