epd2in66b.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * @filename : epd2in66b.cpp
  3. * @brief : Implements for e-paper library
  4. * @author : Waveshare
  5. *
  6. * Copyright (C) Waveshare Dec 02 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 "epd2in66b.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;
  37. height = EPD_HEIGHT;
  38. };
  39. /**
  40. * @brief: module reset.
  41. * often used to awaken the module in deep sleep,
  42. * see Epd::Sleep();
  43. */
  44. static void Epd::Reset(void) {
  45. DigitalWrite(reset_pin, HIGH);
  46. DelayMs(200);
  47. DigitalWrite(reset_pin, LOW); //module reset
  48. DelayMs(2);
  49. DigitalWrite(reset_pin, HIGH);
  50. DelayMs(200);
  51. }
  52. /**
  53. * @brief: Initialize the e-Paper register
  54. */
  55. int Epd::Init(void) {
  56. if (IfInit() != 0) {
  57. return -1;
  58. }
  59. Reset();
  60. WaitUntilIdle();
  61. SendCommand(0x12);//soft reset
  62. WaitUntilIdle();
  63. /* Y increment, X increment */
  64. SendCommand(0x11);
  65. SendData(0x03);
  66. /* Set RamX-address Start/End position */
  67. SendCommand(0x44);
  68. SendData(0x00);
  69. SendData(((width-1) >> 3) & 0x1f);
  70. /* Set RamY-address Start/End position */
  71. SendCommand(0x45);
  72. SendData(0);
  73. SendData(0);
  74. SendData((height&0xff));
  75. SendData((height&0x100)>>8);
  76. SendCommand(0x21); // Display update control
  77. SendData(0x00);
  78. SendData(0x80);
  79. SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  80. SendData(0x00);
  81. SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  82. SendData(0x00);
  83. SendData(0x00);
  84. WaitUntilIdle();
  85. return 0;
  86. }
  87. /**
  88. * @brief: basic function for sending commands
  89. */
  90. static void Epd::SendCommand(unsigned char command) {
  91. DigitalWrite(dc_pin, LOW);
  92. SpiTransfer(command);
  93. }
  94. /**
  95. * @brief: basic function for sending data
  96. */
  97. static void Epd::SendData(unsigned char data) {
  98. DigitalWrite(dc_pin, HIGH);
  99. SpiTransfer(data);
  100. }
  101. /**
  102. * @brief: Wait until the busy_pin goes HIGH
  103. */
  104. static void Epd::WaitUntilIdle(void) {
  105. Serial.print("e-Paper busy \r\n ");
  106. UBYTE busy;
  107. do {
  108. DelayMs(20);
  109. busy = DigitalRead(busy_pin);
  110. } while(busy);
  111. DelayMs(20);
  112. Serial.print("e-Paper busy release \r\n ");
  113. }
  114. /******************************************************************************
  115. function : Turn On Display
  116. parameter:
  117. ******************************************************************************/
  118. static void Epd::TurnOnDisplay(void)
  119. {
  120. SendCommand(0x20);
  121. WaitUntilIdle();
  122. }
  123. /******************************************************************************
  124. function : Display Array data
  125. ******************************************************************************/
  126. void Epd::DisplayFrame(const UBYTE *Image, const UBYTE *ImageRed) {
  127. UWORD Width, Height;
  128. Width = (width % 8 == 0)? (width / 8 ): (width / 8 + 1);
  129. Height = height;
  130. SendCommand(0x24);
  131. for (UWORD j = 0; j <Height; j++) {
  132. for (UWORD i = 0; i <Width; i++) {
  133. SendData(pgm_read_byte(&Image[i + j * Width]));
  134. }
  135. }
  136. SendCommand(0x26);
  137. for (UWORD j = 0; j <Height; j++) {
  138. for (UWORD i = 0; i <Width; i++) {
  139. SendData(~pgm_read_byte(&ImageRed[i + j * Width]));
  140. }
  141. }
  142. TurnOnDisplay();
  143. }
  144. /******************************************************************************
  145. function : Clear Screen
  146. parameter:
  147. mode: 0:just partial mode
  148. 1:clear all
  149. ******************************************************************************/
  150. void Epd::Clear(void) {
  151. UWORD Width, Height;
  152. Width = (width % 8 == 0)? (width / 8 ): (width / 8 + 1);
  153. Height = height;
  154. SendCommand(0x24);
  155. for (UWORD j = 0; j <=Height; j++) {
  156. for (UWORD i = 0; i < Width; i++) {
  157. SendData(0xff);
  158. }
  159. }
  160. SendCommand(0x26);
  161. for (UWORD j = 0; j <=Height; j++) {
  162. for (UWORD i = 0; i < Width; i++) {
  163. SendData(0x00);
  164. }
  165. }
  166. TurnOnDisplay();
  167. }
  168. /**
  169. * @brief: After this command is transmitted, the chip would enter the
  170. * deep-sleep mode to save power.
  171. * The deep sleep mode would return to standby by hardware reset.
  172. * The only one parameter is a check code, the command would be
  173. * You can use EPD_Reset() to awaken
  174. */
  175. void Epd::Sleep(void) {
  176. SendCommand(0X10);
  177. SendData(0x01);
  178. }
  179. /* END OF FILE */