epd2in13b_V3.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * @filename : epd2in9b.cpp
  3. * @brief : Implements for Dual-color 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 "epd2in13b_V3.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. SendCommand(0x04);
  46. WaitUntilIdle();//waiting for the electronic paper IC to release the idle signal
  47. SendCommand(0x00);//panel setting
  48. SendData(0x0f);//LUT from OTP,128x296
  49. SendData(0x89);//Temperature sensor, boost and other related timing settings
  50. SendCommand(0x61);//resolution setting
  51. SendData (0x68);
  52. SendData (0x00);
  53. SendData (0xD4);
  54. SendCommand(0X50);//VCOM AND DATA INTERVAL SETTING
  55. SendData(0x77);//WBmode:VBDF 17|D7 VBDW 97 VBDB 57
  56. //WBRmode:VBDF F7 VBDW 77 VBDB 37 VBDR B7;
  57. return 0;
  58. }
  59. /**
  60. * @brief: basic function for sending commands
  61. */
  62. void Epd::SendCommand(unsigned char command) {
  63. DigitalWrite(dc_pin, LOW);
  64. SpiTransfer(command);
  65. }
  66. /**
  67. * @brief: basic function for sending data
  68. */
  69. void Epd::SendData(unsigned char data) {
  70. DigitalWrite(dc_pin, HIGH);
  71. SpiTransfer(data);
  72. }
  73. /**
  74. * @brief: Wait until the busy_pin goes HIGH
  75. */
  76. void Epd::WaitUntilIdle(void) {
  77. SendCommand(0x71);
  78. while(DigitalRead(busy_pin) == 0) { //0: busy, 1: idle
  79. SendCommand(0x71);
  80. DelayMs(50);
  81. }
  82. DelayMs(200);
  83. }
  84. /**
  85. * @brief: module reset.
  86. * often used to awaken the module in deep sleep,
  87. * see Epd::Sleep();
  88. */
  89. void Epd::Reset(void) {
  90. DigitalWrite(reset_pin, HIGH);
  91. DelayMs(200);
  92. DigitalWrite(reset_pin, LOW);
  93. DelayMs(10);
  94. DigitalWrite(reset_pin, HIGH);
  95. DelayMs(200);
  96. this->count = 0;
  97. }
  98. /**
  99. * @brief: refresh and displays the frame
  100. */
  101. void Epd::DisplayFrame(const unsigned char* frame_buffer_black, const unsigned char* frame_buffer_red) {
  102. if (frame_buffer_black != NULL) {
  103. SendCommand(0x10);
  104. for (int i = 0; i < this->width * this->height / 8; i++) {
  105. SendData(pgm_read_byte(&frame_buffer_black[i]));
  106. }
  107. DelayMs(2);
  108. }
  109. if (frame_buffer_red != NULL) {
  110. SendCommand(0x13);
  111. for (int i = 0; i < this->width * this->height / 8; i++) {
  112. SendData(pgm_read_byte(&frame_buffer_red[i]));
  113. }
  114. }
  115. SendCommand(0x12);
  116. DelayMs(100);
  117. WaitUntilIdle();
  118. this->count = 0;
  119. }
  120. void Epd::Display(const unsigned char* frame_buffer) {
  121. // 1378
  122. if(this->count == 0){
  123. SendCommand(0x10);
  124. this->count++;
  125. }else if(this->count > 0 && this->count < 4 ){
  126. this->count++;
  127. }else if(this->count == 4){
  128. SendCommand(0x13);
  129. this->count++;
  130. }else if(this->count > 4 && this->count < 8 ){
  131. this->count++;
  132. }
  133. for(int i = 0; i < this->width * this->height / 8 / 4; i++){
  134. SendData(frame_buffer[i]);
  135. }
  136. if(this->count == 8){
  137. SendCommand(0x12);
  138. DelayMs(100);
  139. WaitUntilIdle();
  140. this->count = 0;
  141. }
  142. }
  143. /**
  144. * @brief: clear the frame data from the SRAM, this won't refresh the display
  145. */
  146. void Epd::ClearFrame(void) {
  147. SendCommand(0x10);
  148. for(int i = 0; i < width * height / 8; i++) {
  149. SendData(0xFF);
  150. }
  151. DelayMs(2);
  152. SendCommand(0x13);
  153. for(int i = 0; i < width * height / 8; i++) {
  154. SendData(0xFF);
  155. }
  156. SendCommand(0x12);
  157. DelayMs(100);
  158. WaitUntilIdle();
  159. this->count = 0;
  160. }
  161. /**
  162. * @brief: This displays the frame data from SRAM
  163. */
  164. void Epd::DisplayFrame(void) {
  165. this->count = 0;
  166. SendCommand(0x12);
  167. DelayMs(100);
  168. WaitUntilIdle();
  169. }
  170. /**
  171. * @brief: After this command is transmitted, the chip would enter the deep-sleep mode to save power.
  172. * The deep sleep mode would return to standby by hardware reset. The only one parameter is a
  173. * check code, the command would be executed if check code = 0xA5.
  174. * You can use Epd::Reset() to awaken and use Epd::Init() to initialize.
  175. */
  176. void Epd::Sleep() {
  177. SendCommand(0X50);
  178. SendData(0xf7);
  179. SendCommand(0X02);//power off
  180. WaitUntilIdle();//waiting for the electronic paper IC to release the idle signal
  181. SendCommand(0X07);//deep sleep
  182. SendData(0xA5);
  183. this->count = 0;
  184. }
  185. /* END OF FILE */