epd5in65f.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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(0x40);
  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 : show 7 kind of color block
  122. parameter:
  123. ******************************************************************************/
  124. void Epd::EPD_5IN65F_Show7Block(void)
  125. {
  126. unsigned long i,j,k;
  127. unsigned char const Color_seven[8] =
  128. {EPD_5IN65F_BLACK,EPD_5IN65F_BLUE,EPD_5IN65F_GREEN,EPD_5IN65F_ORANGE,
  129. EPD_5IN65F_RED,EPD_5IN65F_YELLOW,EPD_5IN65F_WHITE,EPD_5IN65F_WHITE};
  130. SendCommand(0x61);//Set Resolution setting
  131. SendData(0x02);
  132. SendData(0x58);
  133. SendData(0x01);
  134. SendData(0xC0);
  135. SendCommand(0x10);
  136. for(i=0; i<224; i++) {
  137. for(k = 0 ; k < 4; k ++) {
  138. for(j = 0 ; j < 75; j ++) {
  139. SendData((Color_seven[k]<<4) |Color_seven[k]);
  140. }
  141. }
  142. }
  143. for(i=0; i<224; i++) {
  144. for(k = 4 ; k < 8; k ++) {
  145. for(j = 0 ; j < 75; j ++) {
  146. SendData((Color_seven[k]<<4) |Color_seven[k]);
  147. }
  148. }
  149. }
  150. SendCommand(0x04);//0x04
  151. EPD_5IN65F_BusyHigh();
  152. SendCommand(0x12);//0x12
  153. EPD_5IN65F_BusyHigh();
  154. SendCommand(0x02); //0x02
  155. EPD_5IN65F_BusyLow();
  156. DelayMs(200);
  157. }
  158. /******************************************************************************
  159. function : Sends the image buffer in RAM to e-Paper and displays
  160. parameter:
  161. ******************************************************************************/
  162. void Epd::EPD_5IN65F_Display(const UBYTE *image) {
  163. unsigned long i,j;
  164. SendCommand(0x61);//Set Resolution setting
  165. SendData(0x02);
  166. SendData(0x58);
  167. SendData(0x01);
  168. SendData(0xC0);
  169. SendCommand(0x10);
  170. for(i=0; i<height; i++) {
  171. for(j=0; j<width/2; j++)
  172. SendData(image[j+((width/2)*i)]);
  173. }
  174. SendCommand(0x04);//0x04
  175. EPD_5IN65F_BusyHigh();
  176. SendCommand(0x12);//0x12
  177. EPD_5IN65F_BusyHigh();
  178. SendCommand(0x02); //0x02
  179. EPD_5IN65F_BusyLow();
  180. DelayMs(200);
  181. }
  182. /******************************************************************************
  183. function : Sends the part image buffer in RAM to e-Paper and displays
  184. parameter:
  185. ******************************************************************************/
  186. void Epd::EPD_5IN65F_Display_part(const UBYTE *image, UWORD xstart, UWORD ystart,
  187. UWORD image_width, UWORD image_heigh)
  188. {
  189. unsigned long i,j;
  190. SendCommand(0x61);//Set Resolution setting
  191. SendData(0x02);
  192. SendData(0x58);
  193. SendData(0x01);
  194. SendData(0xC0);
  195. SendCommand(0x10);
  196. for(i=0; i<height; i++) {
  197. for(j=0; j< width/2; j++)
  198. if(i<image_heigh+ystart && i>=ystart && j<(image_width+xstart)/2 && j>=xstart/2) {
  199. SendData(pgm_read_byte(&image[(j-xstart/2) + (image_width/2*(i-ystart))]));
  200. }
  201. else {
  202. SendData(0x11);
  203. }
  204. }
  205. SendCommand(0x04);//0x04
  206. EPD_5IN65F_BusyHigh();
  207. SendCommand(0x12);//0x12
  208. EPD_5IN65F_BusyHigh();
  209. SendCommand(0x02); //0x02
  210. EPD_5IN65F_BusyLow();
  211. DelayMs(200);
  212. }
  213. /******************************************************************************
  214. function :
  215. Clear screen
  216. ******************************************************************************/
  217. void Epd::Clear(UBYTE color) {
  218. SendCommand(0x61);//Set Resolution setting
  219. SendData(0x02);
  220. SendData(0x58);
  221. SendData(0x01);
  222. SendData(0xC0);
  223. SendCommand(0x10);
  224. for(int i=0; i<width/2; i++) {
  225. for(int j=0; j<height; j++)
  226. SendData((color<<4)|color);
  227. }
  228. SendCommand(0x04);//0x04
  229. EPD_5IN65F_BusyHigh();
  230. SendCommand(0x12);//0x12
  231. EPD_5IN65F_BusyHigh();
  232. SendCommand(0x02); //0x02
  233. EPD_5IN65F_BusyLow();
  234. DelayMs(500);
  235. }
  236. /**
  237. * @brief: After this command is transmitted, the chip would enter the
  238. * deep-sleep mode to save power.
  239. * The deep sleep mode would return to standby by hardware reset.
  240. * The only one parameter is a check code, the command would be
  241. * You can use EPD_Reset() to awaken
  242. */
  243. void Epd::Sleep(void) {
  244. DelayMs(100);
  245. SendCommand(0x07);
  246. SendData(0xA5);
  247. DelayMs(100);
  248. DigitalWrite(RST_PIN, 0); // Reset
  249. }
  250. /* END OF FILE */