epd7in5b.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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(0x3A);
  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. void Epd::Clean(void) {
  145. SendCommand(DATA_START_TRANSMISSION_1);
  146. for (long i = 0; i < 122880; i++) {
  147. SendData(0x33);
  148. }
  149. SendCommand(DISPLAY_REFRESH);
  150. DelayMs(100);
  151. WaitUntilIdle();
  152. }
  153. void Epd::DisplayOneQuarterFrame(const unsigned char* image_data) {
  154. unsigned char temp1, temp2;
  155. SendCommand(DATA_START_TRANSMISSION_1);
  156. for (long i = 0; i < 192; i++) {
  157. for (long k = 0; k < 80; k++) {
  158. temp1 = pgm_read_byte(image_data + i*80 + k);
  159. for (unsigned char j = 0; j < 4; j++) {
  160. if ((temp1 & 0xC0) == 0xC0) {
  161. temp2 = 0x03; // white
  162. } else if ((temp1 & 0xC0) == 0x00) {
  163. temp2 = 0x00; // black
  164. } else {
  165. temp2 = 0x04; // red
  166. }
  167. temp2 <<= 4;
  168. temp1 <<= 2;
  169. j++;
  170. if((temp1 & 0xC0) == 0xC0) {
  171. temp2 |= 0x03; // white
  172. } else if ((temp1 & 0xC0) == 0x00) {
  173. temp2 |= 0x00; // black
  174. } else {
  175. temp2 |= 0x04; // red
  176. }
  177. temp1 <<= 2;
  178. SendData(temp2);
  179. }
  180. }
  181. for (long k = 0; k < 160; k++) { // 1/4 show white
  182. SendData(0x33);
  183. }
  184. }
  185. for (long i = 0; i < 192; i++) {
  186. for (long k = 0; k < 160; k++) { // 1/4 show red
  187. SendData(0x44);
  188. }
  189. for (long k = 0; k < 160; k++) { // 1/4 show black
  190. SendData(0x00);
  191. }
  192. }
  193. SendCommand(DISPLAY_REFRESH);
  194. DelayMs(100);
  195. WaitUntilIdle();
  196. }
  197. /**
  198. * @brief: After this command is transmitted, the chip would enter the
  199. * deep-sleep mode to save power.
  200. * The deep sleep mode would return to standby by hardware reset.
  201. * The only one parameter is a check code, the command would be
  202. * executed if check code = 0xA5.
  203. * You can use EPD_Reset() to awaken
  204. */
  205. void Epd::Sleep(void) {
  206. SendCommand(POWER_OFF);
  207. WaitUntilIdle();
  208. SendCommand(DEEP_SLEEP);
  209. SendData(0xa5);
  210. }
  211. /* END OF FILE */