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. }
  87. /**
  88. * @brief: basic function for sending commands
  89. */
  90. void Epd::SendCommand(unsigned char command) {
  91. DigitalWrite(dc_pin, LOW);
  92. SpiTransfer(command);
  93. }
  94. /**
  95. * @brief: basic function for sending data
  96. */
  97. void Epd::SendData(unsigned char data) {
  98. DigitalWrite(dc_pin, HIGH);
  99. SpiTransfer(data);
  100. }
  101. void Epd::EPD_5IN65F_BusyHigh(void)// If BUSYN=0 then waiting
  102. {
  103. while(!(DigitalRead(BUSY_PIN)));
  104. }
  105. void Epd::EPD_5IN65F_BusyLow(void)// If BUSYN=1 then waiting
  106. {
  107. while(DigitalRead(BUSY_PIN));
  108. }
  109. /**
  110. * @brief: module reset.
  111. * often used to awaken the module in deep sleep,
  112. * see Epd::Sleep();
  113. */
  114. void Epd::Reset(void) {
  115. DigitalWrite(reset_pin, LOW); //module reset
  116. DelayMs(1);
  117. DigitalWrite(reset_pin, HIGH);
  118. DelayMs(200);
  119. }
  120. /******************************************************************************
  121. function : Sends the image buffer in RAM to e-Paper and displays
  122. parameter:
  123. ******************************************************************************/
  124. void Epd::EPD_5IN65F_Display(const UBYTE *image) {
  125. unsigned long i,j;
  126. SendCommand(0x61);//Set Resolution setting
  127. SendData(0x02);
  128. SendData(0x58);
  129. SendData(0x01);
  130. SendData(0xC0);
  131. SendCommand(0x10);
  132. for(i=0; i<height; i++) {
  133. for(j=0; j<width/2; j++) {
  134. SendData(image[j+((width/2)*i)]);
  135. }
  136. }
  137. SendCommand(0x04);//0x04
  138. EPD_5IN65F_BusyHigh();
  139. SendCommand(0x12);//0x12
  140. EPD_5IN65F_BusyHigh();
  141. SendCommand(0x02); //0x02
  142. EPD_5IN65F_BusyLow();
  143. DelayMs(200);
  144. }
  145. /******************************************************************************
  146. function : Sends the part image buffer in RAM to e-Paper and displays
  147. parameter:
  148. ******************************************************************************/
  149. void Epd::EPD_5IN65F_Display_part(const UBYTE *image, UWORD xstart, UWORD ystart,
  150. UWORD image_width, UWORD image_heigh)
  151. {
  152. unsigned long i,j;
  153. SendCommand(0x61);//Set Resolution setting
  154. SendData(0x02);
  155. SendData(0x58);
  156. SendData(0x01);
  157. SendData(0xC0);
  158. SendCommand(0x10);
  159. for(i=0; i<height; i++) {
  160. for(j=0; j< width/2; j++) {
  161. if(i<image_heigh+ystart && i>=ystart && j<(image_width+xstart)/2 && j>=xstart/2) {
  162. SendData(pgm_read_byte(&image[(j-xstart/2) + (image_width/2*(i-ystart))]));
  163. }
  164. else {
  165. SendData(0x11);
  166. }
  167. }
  168. }
  169. SendCommand(0x04);//0x04
  170. EPD_5IN65F_BusyHigh();
  171. SendCommand(0x12);//0x12
  172. EPD_5IN65F_BusyHigh();
  173. SendCommand(0x02); //0x02
  174. EPD_5IN65F_BusyLow();
  175. DelayMs(200);
  176. }
  177. /******************************************************************************
  178. function :
  179. Clear screen
  180. ******************************************************************************/
  181. void Epd::Clear(UBYTE color) {
  182. SendCommand(0x61);//Set Resolution setting
  183. SendData(0x02);
  184. SendData(0x58);
  185. SendData(0x01);
  186. SendData(0xC0);
  187. SendCommand(0x10);
  188. for(int i=0; i<width/2; i++) {
  189. for(int j=0; j<height; j++) {
  190. SendData((color<<4)|color);
  191. }
  192. }
  193. SendCommand(0x04);//0x04
  194. EPD_5IN65F_BusyHigh();
  195. SendCommand(0x12);//0x12
  196. EPD_5IN65F_BusyHigh();
  197. SendCommand(0x02); //0x02
  198. EPD_5IN65F_BusyLow();
  199. DelayMs(500);
  200. }
  201. /**
  202. * @brief: After this command is transmitted, the chip would enter the
  203. * deep-sleep mode to save power.
  204. * The deep sleep mode would return to standby by hardware reset.
  205. * The only one parameter is a check code, the command would be
  206. * You can use EPD_Reset() to awaken
  207. */
  208. void Epd::Sleep(void) {
  209. DelayMs(100);
  210. SendCommand(0x07);
  211. SendData(0xA5);
  212. DelayMs(100);
  213. DigitalWrite(RST_PIN, 0); // Reset
  214. }
  215. /* END OF FILE */