epd1in54_V2.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*****************************************************************************
  2. * | File : epd1in54_V2.cpp
  3. * | Author : Waveshare team
  4. * | Function : 1.54inch e-paper V2
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2019-06-24
  9. * | Info :
  10. #
  11. # Permission is hereby granted, free of charge, to any person obtaining a copy
  12. # of this software and associated documnetation files (the "Software"), to deal
  13. # in the Software without restriction, including without limitation the rights
  14. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. # copies of the Software, and to permit persons to whom the Software is
  16. # furished to do so, subject to the following conditions:
  17. #
  18. # The above copyright notice and this permission notice shall be included in
  19. # all copies or substantial portions of the Software.
  20. #
  21. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. # THE SOFTWARE.
  28. #
  29. ******************************************************************************/
  30. #include <stdlib.h>
  31. #include "epd1in54_V2.h"
  32. Epd::~Epd()
  33. {
  34. };
  35. Epd::Epd()
  36. {
  37. reset_pin = RST_PIN;
  38. dc_pin = DC_PIN;
  39. cs_pin = CS_PIN;
  40. busy_pin = BUSY_PIN;
  41. width = EPD_WIDTH;
  42. height = EPD_HEIGHT;
  43. };
  44. /**
  45. * @brief: basic function for sending commands
  46. */
  47. void Epd::SendCommand(unsigned char command)
  48. {
  49. DigitalWrite(dc_pin, LOW);
  50. SpiTransfer(command);
  51. }
  52. /**
  53. * @brief: basic function for sending data
  54. */
  55. void Epd::SendData(unsigned char data)
  56. {
  57. DigitalWrite(dc_pin, HIGH);
  58. SpiTransfer(data);
  59. }
  60. /**
  61. * @brief: Wait until the busy_pin goes HIGH
  62. */
  63. void Epd::WaitUntilIdle(void)
  64. {
  65. while(DigitalRead(busy_pin) == 1) { //LOW: idle, HIGH: busy
  66. DelayMs(100);
  67. }
  68. DelayMs(200);
  69. }
  70. int Epd::Init(void)
  71. {
  72. /* this calls the peripheral hardware interface, see epdif */
  73. if (IfInit() != 0) {
  74. return -1;
  75. }
  76. /* EPD hardware init start */
  77. Reset();
  78. WaitUntilIdle();
  79. SendCommand(0x12); //SWRESET
  80. WaitUntilIdle();
  81. SendCommand(0x01); //Driver output control
  82. SendData(0xC7);
  83. SendData(0x00);
  84. SendData(0x01);
  85. SendCommand(0x11); //data entry mode
  86. SendData(0x01);
  87. SendCommand(0x44); //set Ram-X address start/end position
  88. SendData(0x00);
  89. SendData(0x18); //0x0C-->(18+1)*8=200
  90. SendCommand(0x45); //set Ram-Y address start/end position
  91. SendData(0xC7); //0xC7-->(199+1)=200
  92. SendData(0x00);
  93. SendData(0x00);
  94. SendData(0x00);
  95. SendCommand(0x3C); //BorderWavefrom
  96. SendData(0x01);
  97. SendCommand(0x18);
  98. SendData(0x80);
  99. SendCommand(0x22); // //Load Temperature and waveform setting.
  100. SendData(0XB1);
  101. SendCommand(0x20);
  102. SendCommand(0x4E); // set RAM x address count to 0;
  103. SendData(0x00);
  104. SendCommand(0x4F); // set RAM y address count to 0X199;
  105. SendData(0xC7);
  106. SendData(0x00);
  107. WaitUntilIdle();
  108. /* EPD hardware init end */
  109. return 0;
  110. }
  111. /**
  112. * @brief: module reset.
  113. * often used to awaken the module in deep sleep,
  114. * see Epd::Sleep();
  115. */
  116. void Epd::Reset(void)
  117. {
  118. DigitalWrite(reset_pin, HIGH);
  119. DelayMs(200);
  120. DigitalWrite(reset_pin, LOW); //module reset
  121. DelayMs(10);
  122. DigitalWrite(reset_pin, HIGH);
  123. DelayMs(200);
  124. }
  125. void Epd::Clear(void)
  126. {
  127. int w, h;
  128. w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  129. h = EPD_HEIGHT;
  130. SendCommand(0x24);
  131. for (int j = 0; j < h; j++) {
  132. for (int i = 0; i < w; i++) {
  133. SendData(0xff);
  134. }
  135. }
  136. //DISPLAY REFRESH
  137. SendCommand(0x22);
  138. SendData(0xF7);
  139. SendCommand(0x20);
  140. WaitUntilIdle();
  141. }
  142. void Epd::Display(const unsigned char* frame_buffer)
  143. {
  144. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  145. int h = EPD_HEIGHT;
  146. if (frame_buffer != NULL) {
  147. SendCommand(0x24);
  148. for (int j = 0; j < h; j++) {
  149. for (int i = 0; i < w; i++) {
  150. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  151. }
  152. }
  153. }
  154. //DISPLAY REFRESH
  155. SendCommand(0x22);
  156. SendData(0xF7);
  157. SendCommand(0x20);
  158. WaitUntilIdle();
  159. }
  160. void Epd::DisplayPartBaseImage(const unsigned char* frame_buffer)
  161. {
  162. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  163. int h = EPD_HEIGHT;
  164. if (frame_buffer != NULL) {
  165. SendCommand(0x24);
  166. for (int j = 0; j < h; j++) {
  167. for (int i = 0; i < w; i++) {
  168. SendData(frame_buffer[i + j * w]);
  169. }
  170. }
  171. SendCommand(0x26);
  172. for (int j = 0; j < h; j++) {
  173. for (int i = 0; i < w; i++) {
  174. SendData(frame_buffer[i + j * w]);
  175. }
  176. }
  177. }
  178. //DISPLAY REFRESH
  179. SendCommand(0x22);
  180. SendData(0xFF);
  181. SendCommand(0x20);
  182. WaitUntilIdle();
  183. }
  184. void Epd::DisplayPart(const unsigned char* frame_buffer)
  185. {
  186. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  187. int h = EPD_HEIGHT;
  188. if (frame_buffer != NULL) {
  189. SendCommand(0x24);
  190. for (int j = 0; j < h; j++) {
  191. for (int i = 0; i < w; i++) {
  192. SendData(frame_buffer[i + j * w]);
  193. }
  194. }
  195. }
  196. //DISPLAY REFRESH
  197. SendCommand(0x22);
  198. SendData(0xFF);
  199. SendCommand(0x20);
  200. WaitUntilIdle();
  201. }
  202. /**
  203. * @brief: After this command is transmitted, the chip would enter the
  204. * deep-sleep mode to save power.
  205. * The deep sleep mode would return to standby by hardware reset.
  206. * The only one parameter is a check code, the command would be
  207. * executed if check code = 0xA5.
  208. * You can use Epd::Init() to awaken
  209. */
  210. void Epd::Sleep()
  211. {
  212. SendCommand(0x10); //enter deep sleep
  213. SendData(0x01);
  214. DelayMs(200);
  215. DigitalWrite(reset_pin, LOW);
  216. }
  217. /* END OF FILE */