epd5in83b_V2.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * @filename : epd5in83b_V2.cpp
  3. * @brief : Implements for e-paper library
  4. * @author : Waveshare
  5. *
  6. * Copyright (C) Waveshare July 4 2020
  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 "epd5in83b_V2.h"
  28. #include "imagedata.h"
  29. Epd::~Epd() {
  30. };
  31. Epd::Epd() {
  32. reset_pin = RST_PIN;
  33. dc_pin = DC_PIN;
  34. cs_pin = CS_PIN;
  35. busy_pin = BUSY_PIN;
  36. width = EPD_WIDTH / 8;
  37. height = EPD_HEIGHT;
  38. };
  39. int Epd::Init(void) {
  40. if (IfInit() != 0) {
  41. return -1;
  42. }
  43. Reset();
  44. SendCommand(0x01); //POWER SETTING
  45. SendData (0x07);
  46. SendData (0x07); //VGH=20V,VGL=-20V
  47. SendData (0x3f); //VDH=15V
  48. SendData (0x3f); //VDL=-15V
  49. SendCommand(0x04); //POWER ON
  50. DelayMs(100);
  51. WaitUntilIdle(); //waiting for the electronic paper IC to release the idle signal
  52. SendCommand(0X00); //PANNEL SETTING
  53. SendData(0x0F); //KW-3f KWR-2F BWROTP 0f BWOTP 1f
  54. SendCommand(0x61); //tres
  55. SendData (0x02); //source 648
  56. SendData (0x88);
  57. SendData (0x01); //gate 480
  58. SendData (0xe0);
  59. SendCommand(0X15);
  60. SendData(0x00);
  61. SendCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  62. SendData(0x11);
  63. SendData(0x07);
  64. SendCommand(0X60); //TCON SETTING
  65. SendData(0x22);
  66. return 0;
  67. }
  68. /**
  69. * @brief: basic function for sending commands
  70. */
  71. void Epd::SendCommand(unsigned char command) {
  72. DigitalWrite(dc_pin, LOW);
  73. SpiTransfer(command);
  74. }
  75. /**
  76. * @brief: basic function for sending data
  77. */
  78. void Epd::SendData(unsigned char data) {
  79. DigitalWrite(dc_pin, HIGH);
  80. SpiTransfer(data);
  81. }
  82. /**
  83. * @brief: Wait until the busy_pin goes HIGH
  84. */
  85. void Epd::WaitUntilIdle(void) {
  86. Serial.print("e-Paper busy \r\n ");
  87. UBYTE busy;
  88. do
  89. {
  90. SendCommand(0x71);
  91. busy = DigitalRead(busy_pin);
  92. busy =!(busy & 0x01);
  93. }
  94. while(busy);
  95. DelayMs(200);
  96. Serial.print("e-Paper busy release \r\n ");
  97. }
  98. /**
  99. * @brief: module reset.
  100. * often used to awaken the module in deep sleep,
  101. * see Epd::Sleep();
  102. */
  103. void Epd::Reset(void) {
  104. DigitalWrite(reset_pin, LOW); //module reset
  105. DelayMs(1);
  106. DigitalWrite(reset_pin, HIGH);
  107. DelayMs(200);
  108. }
  109. void Epd::DisplayFrame(const UBYTE *blackimage, const UBYTE *ryimage) {
  110. SendCommand(0x10);
  111. for (UWORD j = 0; j < height; j++) {
  112. for (UWORD i = 0; i < width; i++) {
  113. SendData(pgm_read_byte(&blackimage[i + (j*width)]));
  114. }
  115. }
  116. SendCommand(0x13);
  117. for (UWORD j = 0; j < height; j++) {
  118. for (UWORD i = 0; i < width; i++) {
  119. SendData(pgm_read_byte(&ryimage[i + (j*width)]));
  120. }
  121. }
  122. SendCommand(0x12);
  123. WaitUntilIdle();
  124. }
  125. void Epd::DisplayPicture(const UBYTE *blackimage, UBYTE Block)
  126. {
  127. UDOUBLE i, j;
  128. if(Block == 0){
  129. SendCommand(0x10);
  130. for (j = 0; j < height/2; j++) {
  131. for (i = 0; i < width; i++) {
  132. SendData(pgm_read_byte(&blackimage[i + j * width]));
  133. }
  134. }
  135. }else if(Block == 1){
  136. for (j = 0; j < height/2; j++) {
  137. for (i = 0; i < width; i++) {
  138. SendData(pgm_read_byte(&blackimage[i + j * width]));
  139. }
  140. }
  141. }else if(Block == 2){
  142. SendCommand(0x13);
  143. for (j = 0; j < height/2; j++) {
  144. for (i = 0; i < width; i++) {
  145. SendData(pgm_read_byte(&blackimage[i + j * width]));
  146. }
  147. }
  148. }else if(Block == 3){
  149. for (j = 0; j < height/2; j++) {
  150. for (i = 0; i < width; i++) {
  151. SendData(pgm_read_byte(&blackimage[i + j * width]));
  152. }
  153. }
  154. SendCommand(0x12);
  155. WaitUntilIdle();
  156. }
  157. }
  158. void Epd::Clear(void) {
  159. //send black data
  160. SendCommand(0x10);
  161. for (UWORD j = 0; j < height; j++) {
  162. for (UWORD i = 0; i < width; i++) {
  163. SendData(0xff);
  164. }
  165. }
  166. //send red data
  167. SendCommand(0x13);
  168. for (UWORD j = 0; j < height; j++) {
  169. for (UWORD i = 0; i < width; i++) {
  170. SendData(0x00);
  171. }
  172. }
  173. SendCommand(0x12);
  174. WaitUntilIdle();
  175. }
  176. /**
  177. * @brief: After this command is transmitted, the chip would enter the
  178. * deep-sleep mode to save power.
  179. * The deep sleep mode would return to standby by hardware reset.
  180. * The only one parameter is a check code, the command would be
  181. * You can use EPD_Reset() to awaken
  182. */
  183. void Epd::Sleep(void) {
  184. SendCommand(0x02); // POWER_OFF
  185. WaitUntilIdle();
  186. SendCommand(0x07); // DEEP_SLEEP
  187. SendData(0xA5); // check code
  188. }
  189. /* END OF FILE */