2
0

epd7in5.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * @filename : epd7in5.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 "epd7in5.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(POWER_SETTING);
  44. SendData(0x37);
  45. SendData(0x00);
  46. SendCommand(PANEL_SETTING);
  47. SendData(0xCF);
  48. SendData(0x08);
  49. SendCommand(BOOSTER_SOFT_START);
  50. SendData(0xc7);
  51. SendData(0xcc);
  52. SendData(0x28);
  53. SendCommand(POWER_ON);
  54. WaitUntilIdle();
  55. SendCommand(PLL_CONTROL);
  56. SendData(0x3c);
  57. SendCommand(TEMPERATURE_CALIBRATION);
  58. SendData(0x00);
  59. SendCommand(VCOM_AND_DATA_INTERVAL_SETTING);
  60. SendData(0x77);
  61. SendCommand(TCON_SETTING);
  62. SendData(0x22);
  63. SendCommand(TCON_RESOLUTION);
  64. SendData(0x02); //source 640
  65. SendData(0x80);
  66. SendData(0x01); //gate 384
  67. SendData(0x80);
  68. SendCommand(VCM_DC_SETTING);
  69. SendData(0x1E); //decide by LUT file
  70. SendCommand(0xe5); //FLASH MODE
  71. SendData(0x03);
  72. return 0;
  73. }
  74. /**
  75. * @brief: basic function for sending commands
  76. */
  77. void Epd::SendCommand(unsigned char command) {
  78. DigitalWrite(dc_pin, LOW);
  79. SpiTransfer(command);
  80. }
  81. /**
  82. * @brief: basic function for sending data
  83. */
  84. void Epd::SendData(unsigned char data) {
  85. DigitalWrite(dc_pin, HIGH);
  86. SpiTransfer(data);
  87. }
  88. /**
  89. * @brief: Wait until the busy_pin goes HIGH
  90. */
  91. void Epd::WaitUntilIdle(void) {
  92. while(DigitalRead(busy_pin) == 0) { //0: busy, 1: idle
  93. DelayMs(100);
  94. }
  95. }
  96. /**
  97. * @brief: module reset.
  98. * often used to awaken the module in deep sleep,
  99. * see Epd::Sleep();
  100. */
  101. void Epd::Reset(void) {
  102. DigitalWrite(reset_pin, LOW); //module reset
  103. DelayMs(200);
  104. DigitalWrite(reset_pin, HIGH);
  105. DelayMs(200);
  106. }
  107. void Epd::DisplayFrame(const unsigned char* frame_buffer) {
  108. unsigned char temp1, temp2;
  109. SendCommand(DATA_START_TRANSMISSION_1);
  110. for(long i = 0; i < EPD_WIDTH / 8 * EPD_HEIGHT; i++) {
  111. temp1 = pgm_read_byte(&frame_buffer[i]);
  112. for(unsigned char j = 0; j < 8; j++) {
  113. if(temp1 & 0x80)
  114. temp2 = 0x03;
  115. else
  116. temp2 = 0x00;
  117. temp2 <<= 4;
  118. temp1 <<= 1;
  119. j++;
  120. if(temp1 & 0x80)
  121. temp2 |= 0x03;
  122. else
  123. temp2 |= 0x00;
  124. temp1 <<= 1;
  125. SendData(temp2);
  126. }
  127. }
  128. SendCommand(DISPLAY_REFRESH);
  129. DelayMs(100);
  130. WaitUntilIdle();
  131. }
  132. /**
  133. * @brief: After this command is transmitted, the chip would enter the
  134. * deep-sleep mode to save power.
  135. * The deep sleep mode would return to standby by hardware reset.
  136. * The only one parameter is a check code, the command would be
  137. * executed if check code = 0xA5.
  138. * You can use EPD_Reset() to awaken
  139. */
  140. void Epd::Sleep(void) {
  141. SendCommand(POWER_OFF);
  142. WaitUntilIdle();
  143. SendCommand(DEEP_SLEEP);
  144. SendData(0xa5);
  145. }
  146. /* END OF FILE */