2
0

epd2in66.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * @filename : epd2in66.cpp
  3. * @brief : Implements for e-paper library
  4. * @author : Waveshare
  5. *
  6. * Copyright (C) Waveshare July 29 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 "epd2in66.h"
  28. #include "imagedata.h"
  29. static const UBYTE WF_PARTIAL[159] =
  30. {
  31. 0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32. 0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,
  33. 0x00,0x00,0x00,0x00,0x40,0x40,0x00,0x00,0x00,0x00,
  34. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,
  35. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  36. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  37. 0x0A,0x00,0x00,0x00,0x00,0x00,0x02,0x01,0x00,0x00,
  38. 0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,
  39. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  40. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  42. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  43. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45. 0x00,0x00,0x00,0x00,0x22,0x22,0x22,0x22,0x22,0x22,
  46. 0x00,0x00,0x00,0x22,0x17,0x41,0xB0,0x32,0x36,
  47. };
  48. Epd::~Epd() {
  49. };
  50. Epd::Epd() {
  51. reset_pin = RST_PIN;
  52. dc_pin = DC_PIN;
  53. cs_pin = CS_PIN;
  54. busy_pin = BUSY_PIN;
  55. width = EPD_WIDTH;
  56. height = EPD_HEIGHT;
  57. };
  58. /**
  59. * @brief: module reset.
  60. * often used to awaken the module in deep sleep,
  61. * see Epd::Sleep();
  62. */
  63. static void Epd::Reset(void) {
  64. DigitalWrite(reset_pin, LOW); //module reset
  65. DelayMs(1);
  66. DigitalWrite(reset_pin, HIGH);
  67. DelayMs(200);
  68. }
  69. /**
  70. * @brief: Initialize the e-Paper register
  71. */
  72. int Epd::Init(void) {
  73. if (IfInit() != 0) {
  74. return -1;
  75. }
  76. Reset();
  77. WaitUntilIdle();
  78. SendCommand(0x12);//soft reset
  79. WaitUntilIdle();
  80. /* Y increment, X increment */
  81. SendCommand(0x11);
  82. SendData(0x03);
  83. /* Set RamX-address Start/End position */
  84. SendCommand(0x44);
  85. SendData(0x01);
  86. SendData((width % 8 == 0)? (width / 8 ): (width / 8 + 1) );
  87. /* Set RamY-address Start/End position */
  88. SendCommand(0x45);
  89. SendData(0);
  90. SendData(0);
  91. SendData((height&0xff));
  92. SendData((height&0x100)>>8);
  93. WaitUntilIdle();
  94. return 0;
  95. }
  96. /**
  97. * @brief: Initialize the e-Paper register(Partial display)
  98. */
  99. int Epd::Init_Partial(void) {
  100. Reset();
  101. WaitUntilIdle();
  102. SendCommand(0x12);//soft reset
  103. WaitUntilIdle();
  104. Load_LUT();
  105. SendCommand(0x37);
  106. SendData(0x00);
  107. SendData(0x00);
  108. SendData(0x00);
  109. SendData(0x00);
  110. SendData(0x00);
  111. SendData(0x40);
  112. SendData(0x00);
  113. SendData(0x00);
  114. SendData(0x00);
  115. SendData(0x00);
  116. /* Y increment, X increment */
  117. SendCommand(0x11);
  118. SendData(0x03);
  119. /* Set RamX-address Start/End position */
  120. SendCommand(0x44);
  121. SendData(0x01);
  122. SendData((width % 8 == 0)? (width / 8 ): (width / 8 + 1) );
  123. /* Set RamY-address Start/End position */
  124. SendCommand(0x45);
  125. SendData(0);
  126. SendData(0);
  127. SendData((height&0xff));
  128. SendData((height&0x100)>>8);
  129. SendCommand(0x3C);
  130. SendData(0x80);
  131. SendCommand(0x22);
  132. SendData(0xcf);
  133. SendCommand(0x20);
  134. WaitUntilIdle();
  135. return 0;
  136. }
  137. /**
  138. * @brief: basic function for sending commands
  139. */
  140. static void Epd::SendCommand(unsigned char command) {
  141. DigitalWrite(dc_pin, LOW);
  142. SpiTransfer(command);
  143. }
  144. /**
  145. * @brief: basic function for sending data
  146. */
  147. static void Epd::SendData(unsigned char data) {
  148. DigitalWrite(dc_pin, HIGH);
  149. SpiTransfer(data);
  150. }
  151. /**
  152. * @brief: Wait until the busy_pin goes HIGH
  153. */
  154. static void Epd::WaitUntilIdle(void) {
  155. Serial.print("e-Paper busy \r\n ");
  156. UBYTE busy;
  157. do
  158. {
  159. busy = DigitalRead(busy_pin);
  160. }
  161. while(busy);
  162. DelayMs(200);
  163. Serial.print("e-Paper busy release \r\n ");
  164. }
  165. /******************************************************************************
  166. function : Turn On Display
  167. parameter:
  168. ******************************************************************************/
  169. static void Epd::TurnOnDisplay(void)
  170. {
  171. SendCommand(0x20);
  172. WaitUntilIdle();
  173. }
  174. /******************************************************************************
  175. function : Display Array data
  176. ******************************************************************************/
  177. void Epd::DisplayFrame(const UBYTE *Image) {
  178. UWORD Width, Height;
  179. Width = (width % 8 == 0)? (width / 8 ): (width / 8 + 1);
  180. Height = height;
  181. SendCommand(0x24);
  182. for (UWORD j = 0; j <Height; j++) {
  183. for (UWORD i = 0; i <Width; i++) {
  184. SendData(pgm_read_byte(&Image[i + j * Width]));
  185. }
  186. }
  187. TurnOnDisplay();
  188. }
  189. /******************************************************************************
  190. function : Partial Display
  191. ******************************************************************************/
  192. void Epd::DisplayFrame_part(const UBYTE *Image, UWORD Xstart, UWORD Ystart, UWORD iwidth, UWORD iheight) {
  193. UWORD Width, Height;
  194. Width = (width % 8 == 0)? (width / 8 ): (width / 8 + 1);
  195. Height = height;
  196. SendCommand(0x24);
  197. for (UWORD j = 0; j<Height; j++) {
  198. for (UWORD i = 0; i <Width; i++) {
  199. if(j>=Ystart && j<Ystart+iheight && i>=Xstart/8 && i<(Xstart+iwidth)/8)
  200. SendData(Image[(i-Xstart/8) + (j-Ystart)*iwidth/8]);
  201. else
  202. SendData(0xff);
  203. }
  204. }
  205. TurnOnDisplay();
  206. }
  207. /******************************************************************************
  208. function : set the look-up tables
  209. parameter:
  210. ******************************************************************************/
  211. static void Epd::Load_LUT(void) {
  212. UWORD i;
  213. SendCommand(0x32);
  214. for (i = 0; i < 153; i++) {
  215. SendData(WF_PARTIAL[i]);
  216. }
  217. WaitUntilIdle();
  218. }
  219. /******************************************************************************
  220. function : Clear Screen
  221. parameter:
  222. mode: 0:just partial mode
  223. 1:clear all
  224. ******************************************************************************/
  225. void Epd::Clear(void) {
  226. UWORD Width, Height;
  227. Width = (width % 8 == 0)? (width / 8 ): (width / 8 + 1);
  228. Height = height;
  229. SendCommand(0x24);
  230. for (UWORD j = 0; j <=Height; j++) {
  231. for (UWORD i = 0; i < Width; i++) {
  232. SendData(0xff);
  233. }
  234. }
  235. TurnOnDisplay();
  236. }
  237. /**
  238. * @brief: After this command is transmitted, the chip would enter the
  239. * deep-sleep mode to save power.
  240. * The deep sleep mode would return to standby by hardware reset.
  241. * The only one parameter is a check code, the command would be
  242. * You can use EPD_Reset() to awaken
  243. */
  244. void Epd::Sleep(void) {
  245. SendCommand(0X10);
  246. SendData(0x01);
  247. }
  248. /* END OF FILE */