epd1in54b_V2.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * @filename : epd1in54b.cpp
  3. * @brief : Implements for e-paper library
  4. * @author : Yehui from Waveshare
  5. *
  6. * Copyright (C) Waveshare August 10 2017
  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 "epd1in54b_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. /* this calls the peripheral hardware interface, see epdif */
  40. if (IfInit() != 0) {
  41. return -1;
  42. }
  43. /* EPD hardware init start */
  44. Reset();
  45. WaitUntilIdle();
  46. SendCommand(0x12); //SWRESET
  47. WaitUntilIdle();
  48. SendCommand(0x01); //Driver output control
  49. SendData(0xC7);
  50. SendData(0x00);
  51. SendData(0x01);
  52. SendCommand(0x11); //data entry mode
  53. SendData(0x01);
  54. SendCommand(0x44); //set Ram-X address start/end position
  55. SendData(0x00);
  56. SendData(0x18); //0x18-->(24+1)*8=200
  57. SendCommand(0x45); //set Ram-Y address start/end position
  58. SendData(0xC7); //0xC7-->(199+1)=200
  59. SendData(0x00);
  60. SendData(0x00);
  61. SendData(0x00);
  62. SendCommand(0x3C); //BorderWavefrom
  63. SendData(0x05);
  64. SendCommand(0x18); //Read built-in temperature sensor
  65. SendData(0x80);
  66. SendCommand(0x4E); // set RAM x address count to 0;
  67. SendData(0x00);
  68. SendCommand(0x4F); // set RAM y address count to 0X199;
  69. SendData(0xC7);
  70. SendData(0x00);
  71. WaitUntilIdle();
  72. /* EPD hardware init end */
  73. return 0;
  74. }
  75. /**
  76. * @brief: basic function for sending commands
  77. */
  78. void Epd::SendCommand(unsigned char command) {
  79. DigitalWrite(dc_pin, LOW);
  80. SpiTransfer(command);
  81. }
  82. /**
  83. * @brief: basic function for sending data
  84. */
  85. void Epd::SendData(unsigned char data) {
  86. DigitalWrite(dc_pin, HIGH);
  87. SpiTransfer(data);
  88. }
  89. /**
  90. * @brief: Wait until the busy_pin goes HIGH
  91. */
  92. void Epd::WaitUntilIdle(void) {
  93. while(1) {
  94. if(DigitalRead(busy_pin) == 0)
  95. break;
  96. DelayMs(100);
  97. }
  98. }
  99. /**
  100. * @brief: module reset.
  101. * often used to awaken the module in deep sleep,
  102. * see Epd::Sleep();
  103. */
  104. void Epd::Reset(void) {
  105. DigitalWrite(reset_pin, HIGH);
  106. DelayMs(200);
  107. DigitalWrite(reset_pin, LOW); //module reset
  108. DelayMs(10);
  109. DigitalWrite(reset_pin, HIGH);
  110. DelayMs(200);
  111. }
  112. /**
  113. * @brief: set the look-up tables
  114. */
  115. void Epd::DisplayFrame(const unsigned char* frame_buffer_black, const unsigned char* frame_buffer_red) {
  116. unsigned char temp;
  117. unsigned int i;
  118. SendCommand(0x24);
  119. for(i=0;i<this->width * this->height / 8;i++)
  120. {
  121. SendData(pgm_read_byte(&frame_buffer_black[i]));
  122. }
  123. SendCommand(0x26);
  124. for(i=0;i<this->width * this->height / 8;i++)
  125. {
  126. SendData(~pgm_read_byte(&frame_buffer_red[i]));
  127. }
  128. SendCommand(0x22);
  129. SendData(0xF7);
  130. SendCommand(0x20);
  131. WaitUntilIdle();
  132. }
  133. void Epd::DisplayClear() {
  134. unsigned char temp;
  135. unsigned int i;
  136. SendCommand(0x24);
  137. for(i=0;i<this->width * this->height / 8;i++)
  138. {
  139. SendData(0xff);
  140. }
  141. SendCommand(0x26);
  142. for(i=0;i<this->width * this->height / 8;i++)
  143. {
  144. SendData(0x00);
  145. }
  146. SendCommand(0x22);
  147. SendData(0xF7);
  148. SendCommand(0x20);
  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::Init() to awaken
  158. */
  159. void Epd::Sleep() {
  160. SendCommand(0x10); //power setting
  161. SendData(0x01); //gate switch to external
  162. DelayMs(100);
  163. }
  164. /* END OF FILE */