2
0

epd4in01f.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*****************************************************************************
  2. * | File : EPD_4in01f.c
  3. * | Author : Waveshare team
  4. * | Function : 4.01inch e-paper
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2020-12-25
  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 "epd4in01f.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_4IN01F_BusyHigh();
  54. SendCommand(0x00);
  55. SendData(0x2f);
  56. SendData(0x00);
  57. SendCommand(0x01);
  58. SendData(0x37);
  59. SendData(0x00);
  60. SendData(0x05);
  61. SendData(0x05);
  62. SendCommand(0x03);
  63. SendData(0x00);
  64. SendCommand(0x06);
  65. SendData(0xC7);
  66. SendData(0xC7);
  67. SendData(0x1D);
  68. SendCommand(0x41);
  69. SendData(0x00);
  70. SendCommand(0x50);
  71. SendData(0x37);
  72. SendCommand(0x60);
  73. SendData(0x22);
  74. SendCommand(0x61);
  75. SendData(0x02);
  76. SendData(0x80);
  77. SendData(0x01);
  78. SendData(0x90);
  79. SendCommand(0xE3);
  80. SendData(0xAA);
  81. return 0;
  82. }
  83. /**
  84. * @brief: basic function for sending commands
  85. */
  86. void Epd::SendCommand(unsigned char command) {
  87. DigitalWrite(dc_pin, LOW);
  88. SpiTransfer(command);
  89. }
  90. /**
  91. * @brief: basic function for sending data
  92. */
  93. void Epd::SendData(unsigned char data) {
  94. DigitalWrite(dc_pin, HIGH);
  95. SpiTransfer(data);
  96. }
  97. void Epd::EPD_4IN01F_BusyHigh(void)// If BUSYN=0 then waiting
  98. {
  99. while(!(DigitalRead(BUSY_PIN)));
  100. }
  101. void Epd::EPD_4IN01F_BusyLow(void)// If BUSYN=1 then waiting
  102. {
  103. while(DigitalRead(BUSY_PIN));
  104. }
  105. /**
  106. * @brief: module reset.
  107. * often used to awaken the module in deep sleep,
  108. * see Epd::Sleep();
  109. */
  110. void Epd::Reset(void) {
  111. DigitalWrite(reset_pin, HIGH);
  112. DelayMs(200);
  113. DigitalWrite(reset_pin, LOW); //module reset
  114. DelayMs(1);
  115. DigitalWrite(reset_pin, HIGH);
  116. DelayMs(200);
  117. }
  118. /******************************************************************************
  119. function : Sends the image buffer in RAM to e-Paper and displays
  120. parameter:
  121. ******************************************************************************/
  122. void Epd::EPD_4IN01F_Display(const UBYTE *image) {
  123. unsigned long i,j;
  124. SendCommand(0x61);//Set Resolution setting
  125. SendData(0x02);
  126. SendData(0x80);
  127. SendData(0x01);
  128. SendData(0x90);
  129. SendCommand(0x10);
  130. for(i=0; i<height; i++) {
  131. for(j=0; j<width/2; j++) {
  132. SendData(image[j+((width/2)*i)]);
  133. }
  134. }
  135. SendCommand(0x04);//0x04
  136. EPD_4IN01F_BusyHigh();
  137. SendCommand(0x12);//0x12
  138. EPD_4IN01F_BusyHigh();
  139. SendCommand(0x02); //0x02
  140. EPD_4IN01F_BusyLow();
  141. DelayMs(200);
  142. }
  143. /******************************************************************************
  144. function : Sends the part image buffer in RAM to e-Paper and displays
  145. parameter:
  146. ******************************************************************************/
  147. void Epd::EPD_4IN01F_Display_part(const UBYTE *image, UWORD xstart, UWORD ystart,
  148. UWORD image_width, UWORD image_heigh)
  149. {
  150. unsigned long i,j;
  151. SendCommand(0x61);//Set Resolution setting
  152. SendData(0x02);
  153. SendData(0x80);
  154. SendData(0x01);
  155. SendData(0x90);
  156. SendCommand(0x10);
  157. for(i=0; i<height; i++) {
  158. for(j=0; j< width/2; j++) {
  159. if(i<image_heigh+ystart && i>=ystart && j<(image_width+xstart)/2 && j>=xstart/2) {
  160. SendData(pgm_read_byte(&image[(j-xstart/2) + (image_width/2*(i-ystart))]));
  161. }
  162. else {
  163. SendData(0x11);
  164. }
  165. }
  166. }
  167. SendCommand(0x04);//0x04
  168. EPD_4IN01F_BusyHigh();
  169. SendCommand(0x12);//0x12
  170. EPD_4IN01F_BusyHigh();
  171. SendCommand(0x02); //0x02
  172. EPD_4IN01F_BusyLow();
  173. DelayMs(200);
  174. }
  175. /******************************************************************************
  176. function :
  177. Clear screen
  178. ******************************************************************************/
  179. void Epd::Clear(UBYTE color) {
  180. SendCommand(0x61);//Set Resolution setting
  181. SendData(0x02);
  182. SendData(0x80);
  183. SendData(0x01);
  184. SendData(0x90);
  185. SendCommand(0x10);
  186. for(int i=0; i<width/2; i++) {
  187. for(int j=0; j<height; j++) {
  188. SendData((color<<4)|color);
  189. }
  190. }
  191. SendCommand(0x04);//0x04
  192. EPD_4IN01F_BusyHigh();
  193. SendCommand(0x12);//0x12
  194. EPD_4IN01F_BusyHigh();
  195. SendCommand(0x02); //0x02
  196. EPD_4IN01F_BusyLow();
  197. DelayMs(500);
  198. }
  199. /**
  200. * @brief: After this command is transmitted, the chip would enter the
  201. * deep-sleep mode to save power.
  202. * The deep sleep mode would return to standby by hardware reset.
  203. * The only one parameter is a check code, the command would be
  204. * You can use EPD_Reset() to awaken
  205. */
  206. void Epd::Sleep(void) {
  207. DelayMs(100);
  208. SendCommand(0x07);
  209. SendData(0xA5);
  210. DelayMs(100);
  211. DigitalWrite(RST_PIN, 0); // Reset
  212. }
  213. /* END OF FILE */