epd2in9b_V3.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * @filename : epd2in9b_V3.cpp
  3. * @brief : Implements for e-paper library
  4. * @author : Waveshare
  5. *
  6. * Copyright (C) Waveshare Dec 3 2020
  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 "epd2in9b_V3.h"
  28. #include "imagedata.h"
  29. Epd::~Epd() {
  30. };
  31. Epd::Epd() {
  32. reset_pin = RST_PIN;
  33. dc_pin = DC_PIN;
  34. cs_pin = CS_PIN;
  35. busy_pin = BUSY_PIN;
  36. width = EPD_WIDTH / 8;
  37. height = EPD_HEIGHT;
  38. };
  39. int Epd::Init(void) {
  40. if (IfInit() != 0) {
  41. return -1;
  42. }
  43. Reset();
  44. SendCommand(0x04);//power on
  45. WaitUntilIdle();//waiting for the electronic paper IC to release the idle signal
  46. SendCommand(0x00);//panel setting
  47. SendData(0x0f);//default data
  48. SendData(0x89);//128x296,Temperature sensor, boost and other related timing settings
  49. SendCommand(0x61);//Display resolution setting
  50. SendData (0x80);
  51. SendData (0x01);
  52. SendData (0x28);
  53. SendCommand(0X50);//VCOM AND DATA INTERVAL SETTING
  54. SendData(0x77);//WBmode:VBDF 17|D7 VBDW 97 VBDB 57
  55. //WBRmode:VBDF F7 VBDW 77 VBDB 37 VBDR B7
  56. return 0;
  57. }
  58. /**
  59. * @brief: basic function for sending commands
  60. */
  61. void Epd::SendCommand(unsigned char command) {
  62. DigitalWrite(dc_pin, LOW);
  63. SpiTransfer(command);
  64. }
  65. /**
  66. * @brief: basic function for sending data
  67. */
  68. void Epd::SendData(unsigned char data) {
  69. DigitalWrite(dc_pin, HIGH);
  70. SpiTransfer(data);
  71. }
  72. /**
  73. * @brief: Wait until the busy_pin goes HIGH
  74. */
  75. void Epd::WaitUntilIdle(void) {
  76. unsigned char busy;
  77. Serial.print("e-Paper busy \r\n ");
  78. do
  79. {
  80. SendCommand(0x71);
  81. busy = DigitalRead(busy_pin);
  82. busy =!(busy & 0x01);
  83. }
  84. while(busy);
  85. Serial.print("e-Paper busy release \r\n ");
  86. DelayMs(200);
  87. }
  88. /**
  89. * @brief: module reset.
  90. * often used to awaken the module in deep sleep,
  91. * see Epd::Sleep();
  92. */
  93. void Epd::Reset(void) {
  94. DigitalWrite(reset_pin, HIGH);
  95. DelayMs(200);
  96. DigitalWrite(reset_pin, LOW); //module reset
  97. DelayMs(5);
  98. DigitalWrite(reset_pin, HIGH);
  99. DelayMs(200);
  100. }
  101. void Epd::DisplayFrame(const UBYTE *blackimage, const UBYTE *ryimage) {
  102. SendCommand(0x10);
  103. for (UWORD j = 0; j < height; j++) {
  104. for (UWORD i = 0; i < width; i++) {
  105. SendData(pgm_read_byte(&blackimage[i + (j*width)]));
  106. }
  107. }
  108. SendCommand(0x92);
  109. SendCommand(0x13);
  110. for (UWORD j = 0; j < height; j++) {
  111. for (UWORD i = 0; i < width; i++) {
  112. SendData(pgm_read_byte(&ryimage[i + (j*width)]));
  113. }
  114. }
  115. SendCommand(0x92);
  116. SendCommand(0x12);
  117. WaitUntilIdle();
  118. }
  119. void Epd::Clear(void) {
  120. //send black data
  121. SendCommand(0x10);
  122. for (UWORD j = 0; j < height; j++) {
  123. for (UWORD i = 0; i < width; i++) {
  124. SendData(0xff);
  125. }
  126. }
  127. //send red data
  128. SendCommand(0x13);
  129. for (UWORD j = 0; j < height; j++) {
  130. for (UWORD i = 0; i < width; i++) {
  131. SendData(0xff);
  132. }
  133. }
  134. SendCommand(0x12);
  135. WaitUntilIdle();
  136. }
  137. /**
  138. * @brief: After this command is transmitted, the chip would enter the
  139. * deep-sleep mode to save power.
  140. * The deep sleep mode would return to standby by hardware reset.
  141. * The only one parameter is a check code, the command would be
  142. * You can use EPD_Reset() to awaken
  143. */
  144. void Epd::Sleep(void) {
  145. SendCommand(0x02); // POWER_OFF
  146. WaitUntilIdle();
  147. SendCommand(0x07); // DEEP_SLEEP
  148. SendData(0xA5); // check code
  149. }
  150. /* END OF FILE */