2
0

epd4in01f.cpp 6.3 KB

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