epd2in9b_V2.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @filename : epd2in9b_V2.cpp
  3. * @brief : Implements for e-paper library
  4. * @author : Waveshare
  5. *
  6. * Copyright (C) Waveshare July 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_V2.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, LOW); //module reset
  95. DelayMs(10);
  96. DigitalWrite(reset_pin, HIGH);
  97. DelayMs(200);
  98. }
  99. void Epd::DisplayFrame(const UBYTE *blackimage, const UBYTE *ryimage) {
  100. SendCommand(0x10);
  101. for (UWORD j = 0; j < height; j++) {
  102. for (UWORD i = 0; i < width; i++) {
  103. SendData(pgm_read_byte(&blackimage[i + (j*width)]));
  104. }
  105. }
  106. SendCommand(0x92);
  107. SendCommand(0x13);
  108. for (UWORD j = 0; j < height; j++) {
  109. for (UWORD i = 0; i < width; i++) {
  110. SendData(pgm_read_byte(&ryimage[i + (j*width)]));
  111. }
  112. }
  113. SendCommand(0x92);
  114. SendCommand(0x12);
  115. WaitUntilIdle();
  116. }
  117. void Epd::Clear(void) {
  118. //send black data
  119. SendCommand(0x10);
  120. for (UWORD j = 0; j < height; j++) {
  121. for (UWORD i = 0; i < width; i++) {
  122. SendData(0xff);
  123. }
  124. }
  125. //send red data
  126. SendCommand(0x13);
  127. for (UWORD j = 0; j < height; j++) {
  128. for (UWORD i = 0; i < width; i++) {
  129. SendData(0xff);
  130. }
  131. }
  132. SendCommand(0x12);
  133. WaitUntilIdle();
  134. }
  135. /**
  136. * @brief: After this command is transmitted, the chip would enter the
  137. * deep-sleep mode to save power.
  138. * The deep sleep mode would return to standby by hardware reset.
  139. * The only one parameter is a check code, the command would be
  140. * You can use EPD_Reset() to awaken
  141. */
  142. void Epd::Sleep(void) {
  143. SendCommand(0x02); // POWER_OFF
  144. WaitUntilIdle();
  145. SendCommand(0x07); // DEEP_SLEEP
  146. SendData(0xA5); // check code
  147. }
  148. /* END OF FILE */