epd5in83_V2.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * @filename : epd5in83_V2.h
  3. * @brief : Header file for e-paper library epd5in83_V2.cpp
  4. * @author : MyMX from Waveshare
  5. *
  6. * Copyright (C) Waveshare Nov 09 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 "epd5in83_V2.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(void) {
  39. if (IfInit() != 0) {
  40. return -1;
  41. }
  42. Reset();
  43. SendCommand(0x01); //POWER SETTING
  44. SendData (0x07);
  45. SendData (0x07); //VGH=20V,VGL=-20V
  46. SendData (0x3f); //VDH=15V
  47. SendData (0x3f); //VDL=-15V
  48. SendCommand(0x04); //POWER ON
  49. DelayMs(100);
  50. WaitUntilIdle(); //waiting for the electronic paper IC to release the idle signal
  51. SendCommand(0X00); //PANNEL SETTING
  52. SendData(0x1F); //KW-3f KWR-2F BWROTP 0f BWOTP 1f
  53. SendCommand(0x61); //tres
  54. SendData (0x02); //source 648
  55. SendData (0x88);
  56. SendData (0x01); //gate 480
  57. SendData (0xE0);
  58. SendCommand(0X15);
  59. SendData(0x00);
  60. SendCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  61. SendData(0x10);
  62. SendData(0x07);
  63. SendCommand(0X60); //TCON SETTING
  64. SendData(0x22);
  65. return 0;
  66. }
  67. /**
  68. * @brief: basic function for sending commands
  69. */
  70. void Epd::SendCommand(unsigned char command) {
  71. DigitalWrite(dc_pin, LOW);
  72. SpiTransfer(command);
  73. }
  74. /**
  75. * @brief: basic function for sending data
  76. */
  77. void Epd::SendData(unsigned char data) {
  78. DigitalWrite(dc_pin, HIGH);
  79. SpiTransfer(data);
  80. }
  81. /**
  82. * @brief: Wait until the busy_pin goes HIGH
  83. */
  84. void Epd::WaitUntilIdle(void) {
  85. do {
  86. SendCommand(0x71);
  87. DelayMs(50);
  88. }
  89. while(!DigitalRead(busy_pin));
  90. DelayMs(50);
  91. }
  92. /**
  93. * @brief: module reset.
  94. * often used to awaken the module in deep sleep,
  95. * see Epd::Sleep();
  96. */
  97. void Epd::Reset(void) {
  98. DigitalWrite(reset_pin, HIGH);
  99. DelayMs(200);
  100. DigitalWrite(reset_pin, LOW); //module reset
  101. DelayMs(5);
  102. DigitalWrite(reset_pin, HIGH);
  103. DelayMs(200);
  104. }
  105. void Epd::DisplayFrame(const unsigned char* frame_buffer, char part) {
  106. /**
  107. * Size of a single array cannot be larger than 32K in AVR GCC, therefore
  108. * you have to split the image data () into 2 parts
  109. */
  110. unsigned int Width, Height, i, j;
  111. Width = (width % 8 == 0)? (width / 8 ): (width / 8 + 1);
  112. Height = height;
  113. if(part == 1) {
  114. SendCommand(0x10);
  115. for(i=0;i<Height;i++) {
  116. for(j=0; j<Width; j++) {
  117. SendData(0x00);
  118. }
  119. }
  120. SendCommand(0x13);
  121. }
  122. for(i=0;i<Height/2;i++) {
  123. for(j=0; j<Width; j++) {
  124. SendData(~pgm_read_byte(&frame_buffer[i*Width + j]));
  125. }
  126. }
  127. if(part == 2)
  128. TurnOnDisplay();
  129. }
  130. void Epd::Clear(void)
  131. {
  132. unsigned int Width, Height, i;
  133. Width = (width % 8 == 0)? (width / 8 ): (width / 8 + 1);
  134. Height = height;
  135. SendCommand(0x10);
  136. for(i=0; i<Width*Height; i++) {
  137. SendData(0x00);
  138. }
  139. SendCommand(0x13);
  140. for(i=0; i<Width*Height; i++) {
  141. SendData(0x00);
  142. }
  143. TurnOnDisplay();
  144. }
  145. void Epd::TurnOnDisplay(void)
  146. {
  147. SendCommand(0x12);
  148. DelayMs(100);
  149. WaitUntilIdle();
  150. }
  151. /**
  152. * @brief: After this command is transmitted, the chip would enter the
  153. * deep-sleep mode to save power.
  154. * The deep sleep mode would return to standby by hardware reset.
  155. * The only one parameter is a check code, the command would be
  156. * executed if check code = 0xA5.
  157. * You can use EPD_Reset() to awaken
  158. */
  159. void Epd::Sleep(void) {
  160. SendCommand(0X02);
  161. WaitUntilIdle();
  162. SendCommand(0X07);
  163. SendData(0xa5);
  164. }
  165. /* END OF FILE */