epd5in65f.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*****************************************************************************
  2. * | File : EPD_5in65f.c
  3. * | Author : Waveshare team
  4. * | Function : 5.65inch e-paper
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2020-07-08
  9. * | Info :
  10. * -----------------------------------------------------------------------------
  11. #
  12. # Permission is hereby granted, free of charge, to any person obtaining a copy
  13. # of this software and associated documnetation files (the "Software"), to deal
  14. # in the Software without restriction, including without limitation the rights
  15. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. # copies of the Software, and to permit persons to whom the Software is
  17. # furished to do so, subject to the following conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included in
  20. # all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. # THE SOFTWARE.
  29. #
  30. ******************************************************************************/
  31. #include <stdlib.h>
  32. #include "epd5in65f.h"
  33. #include "imagedata.h"
  34. Epd::~Epd() {
  35. };
  36. Epd::Epd() {
  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. function : Initialize the e-Paper register
  46. parameter:
  47. ******************************************************************************/
  48. int Epd::Init(void) {
  49. if (IfInit() != 0) {
  50. return -1;
  51. }
  52. Reset();
  53. EPD_5IN65F_BusyHigh();
  54. SendCommand(0x00);
  55. SendData(0xEF);
  56. SendData(0x08);
  57. SendCommand(0x01);
  58. SendData(0x37);
  59. SendData(0x00);
  60. SendData(0x23);
  61. SendData(0x23);
  62. SendCommand(0x03);
  63. SendData(0x00);
  64. SendCommand(0x06);
  65. SendData(0xC7);
  66. SendData(0xC7);
  67. SendData(0x1D);
  68. SendCommand(0x30);
  69. SendData(0x3C);
  70. SendCommand(0x41);
  71. SendData(0x00);
  72. SendCommand(0x50);
  73. SendData(0x37);
  74. SendCommand(0x60);
  75. SendData(0x22);
  76. SendCommand(0x61);
  77. SendData(0x02);
  78. SendData(0x58);
  79. SendData(0x01);
  80. SendData(0xC0);
  81. SendCommand(0xE3);
  82. SendData(0xAA);
  83. DelayMs(100);
  84. SendCommand(0x50);
  85. SendData(0x37);
  86. return 0;
  87. }
  88. /**
  89. * @brief: basic function for sending commands
  90. */
  91. void Epd::SendCommand(unsigned char command) {
  92. DigitalWrite(dc_pin, LOW);
  93. SpiTransfer(command);
  94. }
  95. /**
  96. * @brief: basic function for sending data
  97. */
  98. void Epd::SendData(unsigned char data) {
  99. DigitalWrite(dc_pin, HIGH);
  100. SpiTransfer(data);
  101. }
  102. void Epd::EPD_5IN65F_BusyHigh(void)// If BUSYN=0 then waiting
  103. {
  104. while(!(DigitalRead(BUSY_PIN)));
  105. }
  106. void Epd::EPD_5IN65F_BusyLow(void)// If BUSYN=1 then waiting
  107. {
  108. while(DigitalRead(BUSY_PIN));
  109. }
  110. /**
  111. * @brief: module reset.
  112. * often used to awaken the module in deep sleep,
  113. * see Epd::Sleep();
  114. */
  115. void Epd::Reset(void) {
  116. DigitalWrite(reset_pin, LOW); //module reset
  117. DelayMs(1);
  118. DigitalWrite(reset_pin, HIGH);
  119. DelayMs(200);
  120. }
  121. /******************************************************************************
  122. function : Sends the image buffer in RAM to e-Paper and displays
  123. parameter:
  124. ******************************************************************************/
  125. void Epd::EPD_5IN65F_Display(const UBYTE *image) {
  126. unsigned long i,j;
  127. SendCommand(0x61);//Set Resolution setting
  128. SendData(0x02);
  129. SendData(0x58);
  130. SendData(0x01);
  131. SendData(0xC0);
  132. SendCommand(0x10);
  133. for(i=0; i<height; i++) {
  134. for(j=0; j<width/2; j++) {
  135. SendData(image[j+((width/2)*i)]);
  136. }
  137. }
  138. SendCommand(0x04);//0x04
  139. EPD_5IN65F_BusyHigh();
  140. SendCommand(0x12);//0x12
  141. EPD_5IN65F_BusyHigh();
  142. SendCommand(0x02); //0x02
  143. EPD_5IN65F_BusyLow();
  144. DelayMs(200);
  145. }
  146. /******************************************************************************
  147. function : Sends the part image buffer in RAM to e-Paper and displays
  148. parameter:
  149. ******************************************************************************/
  150. void Epd::EPD_5IN65F_Display_part(const UBYTE *image, UWORD xstart, UWORD ystart,
  151. UWORD image_width, UWORD image_heigh)
  152. {
  153. unsigned long i,j;
  154. SendCommand(0x61);//Set Resolution setting
  155. SendData(0x02);
  156. SendData(0x58);
  157. SendData(0x01);
  158. SendData(0xC0);
  159. SendCommand(0x10);
  160. for(i=0; i<height; i++) {
  161. for(j=0; j< width/2; j++) {
  162. if(i<image_heigh+ystart && i>=ystart && j<(image_width+xstart)/2 && j>=xstart/2) {
  163. SendData(pgm_read_byte(&image[(j-xstart/2) + (image_width/2*(i-ystart))]));
  164. }
  165. else {
  166. SendData(0x11);
  167. }
  168. }
  169. }
  170. SendCommand(0x04);//0x04
  171. EPD_5IN65F_BusyHigh();
  172. SendCommand(0x12);//0x12
  173. EPD_5IN65F_BusyHigh();
  174. SendCommand(0x02); //0x02
  175. EPD_5IN65F_BusyLow();
  176. DelayMs(200);
  177. }
  178. /******************************************************************************
  179. function :
  180. Clear screen
  181. ******************************************************************************/
  182. void Epd::Clear(UBYTE color) {
  183. SendCommand(0x61);//Set Resolution setting
  184. SendData(0x02);
  185. SendData(0x58);
  186. SendData(0x01);
  187. SendData(0xC0);
  188. SendCommand(0x10);
  189. for(int i=0; i<width/2; i++) {
  190. for(int j=0; j<height; j++) {
  191. SendData((color<<4)|color);
  192. }
  193. }
  194. SendCommand(0x04);//0x04
  195. EPD_5IN65F_BusyHigh();
  196. SendCommand(0x12);//0x12
  197. EPD_5IN65F_BusyHigh();
  198. SendCommand(0x02); //0x02
  199. EPD_5IN65F_BusyLow();
  200. DelayMs(500);
  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. * You can use EPD_Reset() to awaken
  208. */
  209. void Epd::Sleep(void) {
  210. DelayMs(100);
  211. SendCommand(0x07);
  212. SendData(0xA5);
  213. DelayMs(100);
  214. DigitalWrite(RST_PIN, 0); // Reset
  215. }
  216. /* END OF FILE */