epd7in5b.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * @filename : epd7in5b.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 "epd7in5b.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** image_data) {
  108. unsigned char temp1, temp2;
  109. SendCommand(DATA_START_TRANSMISSION_1);
  110. /**
  111. * Size of a single array cannot be larger than 32K in AVR GCC, therefore
  112. * you have to split the image data (61440 bytes in total) into 2 parts
  113. */
  114. for (int image_data_part = 0; image_data_part < 2; image_data_part++) {
  115. for (long i = 0; i < 30720; i++) {
  116. temp1 = pgm_read_byte(image_data[image_data_part] + i);
  117. for (unsigned char j = 0; j < 4; j++) {
  118. if ((temp1 & 0xC0) == 0xC0) {
  119. temp2 = 0x03; // white
  120. } else if ((temp1 & 0xC0) == 0x00) {
  121. temp2 = 0x00; // black
  122. } else {
  123. temp2 = 0x04; // red
  124. }
  125. temp2 <<= 4;
  126. temp1 <<= 2;
  127. j++;
  128. if((temp1 & 0xC0) == 0xC0) {
  129. temp2 |= 0x03; // white
  130. } else if ((temp1 & 0xC0) == 0x00) {
  131. temp2 |= 0x00; // black
  132. } else {
  133. temp2 |= 0x04; // red
  134. }
  135. temp1 <<= 2;
  136. SendData(temp2);
  137. }
  138. }
  139. }
  140. SendCommand(DISPLAY_REFRESH);
  141. DelayMs(100);
  142. WaitUntilIdle();
  143. }
  144. /**
  145. * @brief: After this command is transmitted, the chip would enter the
  146. * deep-sleep mode to save power.
  147. * The deep sleep mode would return to standby by hardware reset.
  148. * The only one parameter is a check code, the command would be
  149. * executed if check code = 0xA5.
  150. * You can use EPD_Reset() to awaken
  151. */
  152. void Epd::Sleep(void) {
  153. SendCommand(POWER_OFF);
  154. WaitUntilIdle();
  155. SendCommand(DEEP_SLEEP);
  156. SendData(0xa5);
  157. }
  158. /* END OF FILE */