epd1in64g.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**
  2. * @filename : epd1in64g.cpp
  3. * @brief : Implements for e-paper library
  4. * @author : Waveshare
  5. *
  6. * Copyright (C) Waveshare 2022/7/22
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documnetation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdlib.h>
  27. #include "epd1in64g.h"
  28. Epd::~Epd() {
  29. };
  30. Epd::Epd() {
  31. reset_pin = RST_PIN;
  32. dc_pin = DC_PIN;
  33. cs_pin = CS_PIN;
  34. busy_pin = BUSY_PIN;
  35. WIDTH = EPD_WIDTH;
  36. HEIGHT = EPD_HEIGHT;
  37. };
  38. int Epd::Init() {
  39. /* this calls the peripheral hardware interface, see epdif */
  40. if (IfInit() != 0) {
  41. return -1;
  42. }
  43. Reset();
  44. SendCommand(0x66);
  45. SendData(0x49);
  46. SendData(0x55);
  47. SendData(0x13);
  48. SendData(0x5D);
  49. SendCommand(0x66);
  50. SendData(0x49);
  51. SendData(0x55);
  52. SendCommand(0xB0);
  53. SendData(0x03);//1 boost 20211113
  54. SendCommand(0x00);
  55. SendData(0x4F);
  56. SendData(0x6B);
  57. SendCommand(0x03);
  58. SendData(0x00);
  59. SendCommand(0xF0);
  60. SendData(0xF6);
  61. SendData(0x0D);
  62. SendData(0x00);
  63. SendData(0x00);
  64. SendData(0x00);
  65. //20220303
  66. SendCommand(0x06);
  67. SendData(0xCF);
  68. SendData(0xDF);
  69. SendData(0x0F);
  70. SendCommand(0x41);
  71. SendData(0x00);
  72. SendCommand(0x50);
  73. SendData(0x30);
  74. SendCommand(0x60);
  75. SendData(0x0C);
  76. SendData(0x05);
  77. SendCommand(0x61);
  78. SendData(0xA8);
  79. SendData(0x00);
  80. SendData(0xA8);
  81. SendCommand(0x84);
  82. SendData(0x01);
  83. return 0;
  84. }
  85. /**
  86. * @brief: basic function for sending commands
  87. */
  88. void Epd::SendCommand(unsigned char command) {
  89. DigitalWrite(dc_pin, LOW);
  90. SpiTransfer(command);
  91. }
  92. /**
  93. * @brief: basic function for sending data
  94. */
  95. void Epd::SendData(unsigned char data) {
  96. DigitalWrite(dc_pin, HIGH);
  97. SpiTransfer(data);
  98. }
  99. /**
  100. * @brief: Wait until the busy_pin goes LOW
  101. */
  102. void Epd::ReadBusyH(void) {
  103. Serial.print("e-Paper busy H\r\n ");
  104. while(DigitalRead(busy_pin) == LOW) { //LOW: busy, HIGH: idle
  105. DelayMs(5);
  106. }
  107. Serial.print("e-Paper busy release H\r\n ");
  108. }
  109. void Epd::ReadBusyL(void) {
  110. Serial.print("e-Paper busy L\r\n ");
  111. while(DigitalRead(busy_pin) == HIGH) { //LOW: idle, HIGH: busy
  112. DelayMs(5);
  113. }
  114. Serial.print("e-Paper busy release L\r\n ");
  115. }
  116. /**
  117. * @brief: module reset.
  118. * often used to awaken the module in deep sleep,
  119. * see Epd::Sleep();
  120. */
  121. void Epd::Reset(void) {
  122. DigitalWrite(reset_pin, HIGH);
  123. DelayMs(20);
  124. DigitalWrite(reset_pin, LOW); //module reset
  125. DelayMs(2);
  126. DigitalWrite(reset_pin, HIGH);
  127. DelayMs(20);
  128. }
  129. /******************************************************************************
  130. function : Turn On Display
  131. parameter:
  132. ******************************************************************************/
  133. void Epd::TurnOnDisplay(void)
  134. {
  135. SendCommand(0x12); // DISPLAY_REFRESH
  136. SendData(0x01);
  137. ReadBusyH();
  138. SendCommand(0x02); // POWER_OFF
  139. SendData(0X00);
  140. ReadBusyH();
  141. }
  142. /******************************************************************************
  143. function : Clear screen
  144. parameter:
  145. ******************************************************************************/
  146. void Epd::Clear(UBYTE color)
  147. {
  148. UWORD Width, Height;
  149. Width = (WIDTH % 4 == 0)? (WIDTH / 4 ): (WIDTH / 4 + 1);
  150. Height = HEIGHT;
  151. SendCommand(0x68);
  152. SendData(0x01);
  153. SendCommand(0x04);
  154. ReadBusyH();
  155. SendCommand(0x10);
  156. for (UWORD j = 0; j < Height; j++) {
  157. for (UWORD i = 0; i < Width; i++) {
  158. for(UBYTE k = 0; k < 4; k++) {
  159. SendData(color);
  160. }
  161. }
  162. }
  163. SendCommand(0x68);
  164. SendData(0x00);
  165. TurnOnDisplay();
  166. }
  167. /******************************************************************************
  168. function : Sends the image buffer in RAM to e-Paper and displays
  169. parameter:
  170. ******************************************************************************/
  171. void Epd::Display(UBYTE *Image)
  172. {
  173. UWORD Width, Height;
  174. Width = (WIDTH % 4 == 0)? (WIDTH / 4 ): (WIDTH / 4 + 1);
  175. Height = HEIGHT;
  176. SendCommand(0x68);
  177. SendData(0x01);
  178. SendCommand(0x04);
  179. ReadBusyH();
  180. SendCommand(0x10);
  181. for (UWORD j = 0; j < Height; j++) {
  182. for (UWORD i = 0; i < Width; i++) {
  183. SendData(pgm_read_byte(&Image[i + j * Width]));
  184. }
  185. }
  186. TurnOnDisplay();
  187. }
  188. void Epd::Display_part(UBYTE *Image, UWORD xstart, UWORD ystart, UWORD image_width, UWORD image_heigh)
  189. {
  190. UWORD Width, Height;
  191. Width = (WIDTH % 4 == 0)? (WIDTH / 4 ): (WIDTH / 4 + 1);
  192. Height = HEIGHT;
  193. SendCommand(0x68);
  194. SendData(0x01);
  195. SendCommand(0x04);
  196. ReadBusyH();
  197. SendCommand(0x10);
  198. for (UWORD i = 0; i < Height; i++) {
  199. for (UWORD j = 0; j < Width; j++) {
  200. if((j >= xstart/4) && (j < (image_width + xstart)/4) && (i >= ystart) && (i <= (ystart + image_heigh)) )
  201. {
  202. SendData(Image[(i-ystart) * image_width/4 + j - xstart/4]);
  203. // Serial.print(Image[(i-ystart) * image_width/8 + j - xstart], HEX);
  204. // Serial.print(" ");
  205. }
  206. else
  207. {
  208. SendData(0x55);
  209. }
  210. }
  211. }
  212. TurnOnDisplay();
  213. }
  214. /******************************************************************************
  215. function : Enter sleep mode
  216. parameter:
  217. ******************************************************************************/
  218. void Epd::Sleep(void)
  219. {
  220. SendCommand(0x02); // POWER_OFF
  221. SendData(0X00);
  222. SendCommand(0x07); // DEEP_SLEEP
  223. SendData(0XA5);
  224. }
  225. /* END OF FILE */