fepd2in13.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**
  2. * @filename : epd2in13.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 "fepd2in13.h"
  28. /**
  29. * full screen update LUT
  30. **/
  31. const unsigned char lut_vcomDC[] = {
  32. 0x00, 0x08, 0x00, 0x00, 0x00, 0x02,
  33. 0x60, 0x28, 0x28, 0x00, 0x00, 0x01,
  34. 0x00, 0x14, 0x00, 0x00, 0x00, 0x01,
  35. 0x00, 0x12, 0x12, 0x00, 0x00, 0x01,
  36. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  37. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  38. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  39. 0x00, 0x00,
  40. };
  41. const unsigned char lut_ww[] = {
  42. 0x40, 0x08, 0x00, 0x00, 0x00, 0x02,
  43. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  44. 0x40, 0x14, 0x00, 0x00, 0x00, 0x01,
  45. 0xA0, 0x12, 0x12, 0x00, 0x00, 0x01,
  46. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49. };
  50. const unsigned char lut_bw[] = {
  51. 0x40, 0x17, 0x00, 0x00, 0x00, 0x02,
  52. 0x90, 0x0F, 0x0F, 0x00, 0x00, 0x03,
  53. 0x40, 0x0A, 0x01, 0x00, 0x00, 0x01,
  54. 0xA0, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  55. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  56. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  57. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58. };
  59. const unsigned char lut_wb[] = {
  60. 0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
  61. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  62. 0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
  63. 0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
  64. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  67. };
  68. const unsigned char lut_bb[] = {
  69. 0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
  70. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  71. 0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
  72. 0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
  73. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  74. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  75. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  76. };
  77. Epd::~Epd() {
  78. };
  79. Epd::Epd() {
  80. reset_pin = RST_PIN;
  81. dc_pin = DC_PIN;
  82. cs_pin = CS_PIN;
  83. busy_pin = BUSY_PIN;
  84. width = EPD_WIDTH;
  85. height = EPD_HEIGHT;
  86. };
  87. int Epd::Init() {
  88. Serial.begin(9600);
  89. /* this calls the peripheral hardware interface, see epdif */
  90. if (IfInit() != 0) {
  91. return -1;
  92. }
  93. /* EPD hardware init start */
  94. Reset();
  95. SendCommand(0x01); //POWER SETTING
  96. SendData(0x03);
  97. SendData(0x00);
  98. SendData(0x2b);
  99. SendData(0x2b);
  100. SendData(0x03);
  101. SendCommand(0x06); //boost soft start
  102. SendData(0x17); //A
  103. SendData(0x17); //B
  104. SendData(0x17); //C
  105. SendCommand(0x04);
  106. // WaitUntilIdle();
  107. SendCommand(0x00); //panel setting
  108. SendData(0xbf); //LUT from OTP£¬128x296
  109. SendData(0x0d); //VCOM to 0V fast
  110. SendCommand(0x30); //PLL setting
  111. SendData(0x3a); // 3a 100HZ 29 150Hz 39 200HZ 31 171HZ
  112. SendCommand(0x61); //resolution setting
  113. SendData(EPD_WIDTH);
  114. SendData((EPD_HEIGHT >> 8) & 0xff);
  115. SendData(EPD_HEIGHT& 0xff);
  116. SendCommand(0x82); //vcom_DC setting
  117. SendData(0x28);
  118. /* EPD hardware init end */
  119. return 0;
  120. }
  121. /**
  122. * @brief: basic function for sending commands
  123. */
  124. void Epd::SendCommand(unsigned char command) {
  125. DigitalWrite(dc_pin, LOW);
  126. SpiTransfer(command);
  127. }
  128. /**
  129. * @brief: basic function for sending data
  130. */
  131. void Epd::SendData(unsigned char data) {
  132. DigitalWrite(dc_pin, HIGH);
  133. SpiTransfer(data);
  134. }
  135. /**
  136. * @brief: Wait until the busy_pin goes LOW
  137. */
  138. void Epd::WaitUntilIdle(void) {
  139. while(DigitalRead(busy_pin) == HIGH) { //LOW: idle, HIGH: busy
  140. DelayMs(100);
  141. }
  142. }
  143. /**
  144. * @brief: module reset.
  145. * often used to awaken the module in deep sleep,
  146. * see Epd::Sleep();
  147. */
  148. void Epd::Reset(void) {
  149. DigitalWrite(reset_pin, LOW); //module reset
  150. DelayMs(200);
  151. DigitalWrite(reset_pin, HIGH);
  152. DelayMs(200);
  153. }
  154. /**
  155. * @brief: set the look-up table register
  156. */
  157. void Epd::SetFullReg(void)
  158. {
  159. SendCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  160. SendData(0x97); //WBmode:VBDF 17|D7 VBDW 97 VBDB 57 WBRmode:VBDF F7 VBDW 77 VBDB 37 VBDR B7
  161. unsigned int count;
  162. SendCommand(0x20);
  163. for(count=0; count<44; count++) {
  164. SendData(lut_vcomDC[count]);
  165. }
  166. SendCommand(0x21);
  167. for(count=0; count<42; count++) {
  168. SendData(lut_ww[count]);
  169. }
  170. SendCommand(0x22);
  171. for(count=0; count<42; count++) {
  172. SendData(lut_bw[count]);
  173. }
  174. SendCommand(0x23);
  175. for(count=0; count<42; count++) {
  176. SendData(lut_wb[count]);
  177. }
  178. SendCommand(0x24);
  179. for(count=0; count<42; count++) {
  180. SendData(lut_bb[count]);
  181. }
  182. }
  183. void Epd::SetFrameMemory(const unsigned char* image_buffer) {
  184. unsigned char Width, Height, data;
  185. Width = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  186. Height = EPD_HEIGHT;
  187. SendCommand(0x10);
  188. for (int j = 0; j < Height; j++) {
  189. for (int i = 0; i < Width; i++) {
  190. SendData(0x00);
  191. }
  192. }
  193. DelayMs(10);
  194. SendCommand(0x13);
  195. for (int j = 0; j < Height; j++) {
  196. for (int i = 0; i < Width; i++) {
  197. data = pgm_read_byte(&image_buffer[i + j * Width]);
  198. SendData(data);
  199. // Serial.print(data);
  200. }
  201. }
  202. DelayMs(10);
  203. SetFullReg();
  204. DisplayFrame();
  205. }
  206. /**
  207. * @brief: clear the frame memory with the specified color.
  208. * this won't update the display.
  209. */
  210. void Epd::ClearFrameMemory() {
  211. unsigned char Width, Height;
  212. Width = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  213. Height = EPD_HEIGHT;
  214. SendCommand(0x10);
  215. for (int j = 0; j < Height; j++) {
  216. for (int i = 0; i < Width; i++) {
  217. SendData(0x00);
  218. }
  219. }
  220. DelayMs(10);
  221. SendCommand(0x13);
  222. for (int j = 0; j < Height; j++) {
  223. for (int i = 0; i < Width; i++) {
  224. SendData(0xFF);
  225. }
  226. }
  227. DelayMs(10);
  228. SetFullReg();
  229. DisplayFrame();
  230. }
  231. /**
  232. * @brief: update the display
  233. * there are 2 memory areas embedded in the e-paper display
  234. * but once this function is called,
  235. * the the next action of SetFrameMemory or ClearFrame will
  236. * set the other memory area.
  237. */
  238. void Epd::DisplayFrame(void) {
  239. SendCommand(0x12);
  240. DelayMs(100);
  241. WaitUntilIdle();
  242. }
  243. /**
  244. * @brief: After this command is transmitted, the chip would enter the
  245. * deep-sleep mode to save power.
  246. * The deep sleep mode would return to standby by hardware reset.
  247. * You can use Epd::Init() to awaken
  248. */
  249. void Epd::Sleep() {
  250. SendCommand(0X50);
  251. SendData(0xf7);
  252. SendCommand(0X02); //power off
  253. SendCommand(0X07); //deep sleep
  254. SendData(0xA5);
  255. }
  256. /* END OF FILE */