EPD_2in13g.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*****************************************************************************
  2. * | File : EPD_2in13g.c
  3. * | Author : Waveshare team
  4. * | Function : 2inch13 e-paper (G)
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2023-05-29
  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 "EPD_2in13g.h"
  32. #include <stdlib.h>
  33. Epd::~Epd() {
  34. };
  35. Epd::Epd() {
  36. reset_pin = RST_PIN;
  37. dc_pin = DC_PIN;
  38. cs_pin = CS_PIN;
  39. busy_pin = BUSY_PIN;
  40. WIDTH = EPD_2IN13G_WIDTH;
  41. HEIGHT = EPD_2IN13G_HEIGHT;
  42. };
  43. /******************************************************************************
  44. function : Software reset
  45. parameter:
  46. ******************************************************************************/
  47. void Epd::Reset(void)
  48. {
  49. DigitalWrite(RST_PIN, 1);
  50. DelayMs(200);
  51. DigitalWrite(RST_PIN, 0);
  52. DelayMs(2);
  53. DigitalWrite(RST_PIN, 1);
  54. DelayMs(200);
  55. }
  56. /******************************************************************************
  57. function : send command
  58. parameter:
  59. Reg : Command register
  60. ******************************************************************************/
  61. void Epd::SendCommand(UBYTE Reg)
  62. {
  63. DigitalWrite(DC_PIN, 0);
  64. DigitalWrite(CS_PIN, 0);
  65. SpiTransfer(Reg);
  66. DigitalWrite(CS_PIN, 1);
  67. }
  68. /******************************************************************************
  69. function : send data
  70. parameter:
  71. Data : Write data
  72. ******************************************************************************/
  73. void Epd::SendData(UBYTE Data)
  74. {
  75. DigitalWrite(DC_PIN, 1);
  76. DigitalWrite(CS_PIN, 0);
  77. SpiTransfer(Data);
  78. DigitalWrite(CS_PIN, 1);
  79. }
  80. /******************************************************************************
  81. function : Wait until the busy_pin goes HIGH
  82. parameter:
  83. ******************************************************************************/
  84. void Epd::ReadBusy(void)
  85. {
  86. DelayMs(100);
  87. Serial.println("busy");
  88. while(DigitalRead(BUSY_PIN) != 1) { //HIGH: idle, LOW: busy
  89. DelayMs(100);
  90. }
  91. Serial.println("idie");
  92. }
  93. /******************************************************************************
  94. function : Turn On Display
  95. parameter:
  96. ******************************************************************************/
  97. void Epd::TurnOnDisplay(void)
  98. {
  99. SendCommand(0x12); // DISPLAY_REFRESH
  100. SendData(0x00);
  101. ReadBusy();
  102. }
  103. /******************************************************************************
  104. function : Setting the display window
  105. parameter:
  106. ******************************************************************************/
  107. void Epd::SetWindows(void)
  108. {
  109. SendCommand(0x61); //TRES
  110. SendData(Source_BITS/256); // EPD_2IN13G_WIDTH_H
  111. SendData(Source_BITS%256); // EPD_2IN13G_WIDTH_L
  112. SendData(EPD_2IN13G_HEIGHT/256); // EPD_2IN13G_HEIGHT_H
  113. SendData(EPD_2IN13G_HEIGHT%256); // EPD_2IN13G_HEIGHT_L
  114. }
  115. /******************************************************************************
  116. function : Initialize the e-Paper register
  117. parameter:
  118. ******************************************************************************/
  119. int Epd::Init(void)
  120. {
  121. if (IfInit() != 0) {
  122. return -1;
  123. }
  124. Reset();
  125. ReadBusy();
  126. SendCommand(0x4D);
  127. SendData(0x78);
  128. SendCommand(0x00); //PSR
  129. SendData(0x0F);
  130. SendData(0x29);
  131. SendCommand(0x01); //PWRR
  132. SendData(0x07);
  133. SendData(0x00);
  134. SendCommand(0x03); //POFS
  135. SendData(0x10);
  136. SendData(0x54);
  137. SendData(0x44);
  138. SendCommand(0x06); //BTST_P
  139. SendData(0x05);
  140. SendData(0x00);
  141. SendData(0x3F);
  142. SendData(0x0A);
  143. SendData(0x25);
  144. SendData(0x12);
  145. SendData(0x1A);
  146. SendCommand(0x50); //CDI
  147. SendData(0x37);
  148. SendCommand(0x60); //TCON
  149. SendData(0x02);
  150. SendData(0x02);
  151. SetWindows(); // Setting the display window
  152. SendCommand(0xE7);
  153. SendData(0x1C);
  154. SendCommand(0xE3);
  155. SendData(0x22);
  156. SendCommand(0xB4);
  157. SendData(0xD0);
  158. SendCommand(0xB5);
  159. SendData(0x03);
  160. SendCommand(0xE9);
  161. SendData(0x01);
  162. SendCommand(0x30);
  163. SendData(0x08);
  164. SendCommand(0x04);
  165. ReadBusy();
  166. return 0;
  167. }
  168. /******************************************************************************
  169. function : Clear screen
  170. parameter:
  171. ******************************************************************************/
  172. void Epd::Clear(UBYTE color)
  173. {
  174. UWORD Width, Height;
  175. Width = Source_BITS / 4 ;
  176. Height = EPD_2IN13G_HEIGHT;
  177. SendCommand(0x10);
  178. for (UWORD j = 0; j < Height; j++) {
  179. for (UWORD i = 0; i < Width; i++) {
  180. SendData((color << 6) | (color << 4) | (color << 2) | color);
  181. }
  182. }
  183. TurnOnDisplay();
  184. }
  185. /******************************************************************************
  186. function : Sends the image buffer in RAM to e-Paper and displays
  187. parameter:
  188. ******************************************************************************/
  189. void Epd::Display(const UBYTE *Image)
  190. {
  191. UWORD Width, Height;
  192. Width = (EPD_2IN13G_WIDTH % 4 == 0)? (EPD_2IN13G_WIDTH / 4 ): (EPD_2IN13G_WIDTH / 4 + 1);
  193. Height = EPD_2IN13G_HEIGHT;
  194. SendCommand(0x10);
  195. for (UWORD j = 0; j < Height; j++) {
  196. for (UWORD i = 0; i < (Source_BITS/4); i++) {
  197. if(i < 31 )
  198. SendData(pgm_read_byte(&Image[i + j * Width]));
  199. else SendData(0x00);
  200. }
  201. }
  202. TurnOnDisplay();
  203. }
  204. /******************************************************************************
  205. function : Enter sleep mode
  206. parameter:
  207. ******************************************************************************/
  208. void Epd::Sleep(void)
  209. {
  210. SendCommand(0x02); //power off
  211. ReadBusy(); //waiting for the electronic paper IC to release the idle signal
  212. DelayMs(100); //!!!The delay here is necessary,100mS at least!!!
  213. SendCommand(0x07); //deep sleep
  214. SendData(0XA5);
  215. }