epd3in7.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**
  2. * @filename : epd3in7.cpp
  3. * @brief : Implements for e-paper library
  4. * @author : Waveshare
  5. *
  6. * Copyright (C) Waveshare July 17 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 "epd3in7.h"
  28. #include "imagedata.h"
  29. static const UBYTE lut_4Gray_GC[] =
  30. {
  31. 0x2A,0x06,0x15,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//1
  32. 0x28,0x06,0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//2
  33. 0x20,0x06,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//3
  34. 0x14,0x06,0x28,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//4
  35. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//5
  36. 0x00,0x02,0x02,0x0A,0x00,0x00,0x00,0x08,0x08,0x02,//6
  37. 0x00,0x02,0x02,0x0A,0x00,0x00,0x00,0x00,0x00,0x00,//7
  38. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//8
  39. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//9
  40. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//10
  41. 0x22,0x22,0x22,0x22,0x22
  42. };
  43. static const UBYTE lut_1Gray_GC[] =
  44. {
  45. 0x2A,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//1
  46. 0x05,0x2A,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//2
  47. 0x2A,0x15,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//3
  48. 0x05,0x0A,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//4
  49. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//5
  50. 0x00,0x02,0x03,0x0A,0x00,0x02,0x06,0x0A,0x05,0x00,//6
  51. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//7
  52. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//8
  53. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//9
  54. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//10
  55. 0x22,0x22,0x22,0x22,0x22
  56. };
  57. static const UBYTE lut_1Gray_DU[] =
  58. {
  59. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//1
  60. 0x01,0x2A,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  61. 0x0A,0x55,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//3
  62. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  63. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//5
  64. 0x00,0x00,0x05,0x05,0x00,0x05,0x03,0x05,0x05,0x00,
  65. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//7
  66. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  67. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//9
  68. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  69. 0x22,0x22,0x22,0x22,0x22
  70. };
  71. Epd::~Epd() {
  72. };
  73. Epd::Epd() {
  74. reset_pin = RST_PIN;
  75. dc_pin = DC_PIN;
  76. cs_pin = CS_PIN;
  77. busy_pin = BUSY_PIN;
  78. width = EPD_WIDTH;
  79. height = EPD_HEIGHT;
  80. };
  81. int Epd::Init(void) {
  82. if (IfInit() != 0) {
  83. return -1;
  84. }
  85. Reset();
  86. SendCommand(0x12);
  87. DelayMs(300);
  88. SendCommand(0x46);
  89. SendData(0xF7);
  90. WaitUntilIdle();
  91. SendCommand(0x47);
  92. SendData(0xF7);
  93. WaitUntilIdle();
  94. SendCommand(0x01); // setting gaet number
  95. SendData(0xDF);
  96. SendData(0x01);
  97. SendData(0x00);
  98. SendCommand(0x03); // set gate voltage
  99. SendData(0x00);
  100. SendCommand(0x04); // set source voltage
  101. SendData(0x41);
  102. SendData(0xA8);
  103. SendData(0x32);
  104. SendCommand(0x11); // set data entry sequence
  105. SendData(0x03);
  106. SendCommand(0x3C); // set border
  107. SendData(0x00);
  108. SendCommand(0x0C); // set booster strength
  109. SendData(0xAE);
  110. SendData(0xC7);
  111. SendData(0xC3);
  112. SendData(0xC0);
  113. SendData(0xC0);
  114. SendCommand(0x18); // set internal sensor on
  115. SendData(0x80);
  116. SendCommand(0x2C); // set vcom value
  117. SendData(0x44);
  118. SendCommand(0x37); // set display option, these setting turn on previous function
  119. SendData(0x00);
  120. SendData(0xff);
  121. SendData(0xff);
  122. SendData(0xff);
  123. SendData(0xff);
  124. SendData(0x4f);
  125. SendData(0xff);
  126. SendData(0xff);
  127. SendData(0xff);
  128. SendData(0xff);
  129. SendCommand(0x44); // setting X direction start/end position of RAM
  130. SendData(0x00);
  131. SendData(0x00);
  132. SendData(0x17);
  133. SendData(0x01);
  134. SendCommand(0x45); // setting Y direction start/end position of RAM
  135. SendData(0x00);
  136. SendData(0x00);
  137. SendData(0xDF);
  138. SendData(0x01);
  139. SendCommand(0x22); // Display Update Control 2
  140. SendData(0xCF);
  141. return 0;
  142. }
  143. /**
  144. * @brief: basic function for sending commands
  145. */
  146. void Epd::SendCommand(unsigned char command) {
  147. DigitalWrite(dc_pin, LOW);
  148. SpiTransfer(command);
  149. }
  150. /**
  151. * @brief: basic function for sending data
  152. */
  153. void Epd::SendData(unsigned char data) {
  154. DigitalWrite(dc_pin, HIGH);
  155. SpiTransfer(data);
  156. }
  157. /**
  158. * @brief: Wait until the busy_pin goes HIGH
  159. */
  160. void Epd::WaitUntilIdle(void) {
  161. Serial.print("e-Paper busy \r\n ");
  162. UBYTE busy;
  163. do
  164. {
  165. busy = DigitalRead(busy_pin);
  166. }
  167. while(busy);
  168. DelayMs(200);
  169. Serial.print("e-Paper busy release \r\n ");
  170. }
  171. /**
  172. * @brief: module reset.
  173. * often used to awaken the module in deep sleep,
  174. * see Epd::Sleep();
  175. */
  176. void Epd::Reset(void) {
  177. DigitalWrite(reset_pin, LOW); //module reset
  178. DelayMs(1);
  179. DigitalWrite(reset_pin, HIGH);
  180. DelayMs(200);
  181. }
  182. /******************************************************************************
  183. function : Display
  184. ******************************************************************************/
  185. void Epd::DisplayFrame(const UBYTE *Image) {
  186. UWORD i;
  187. UWORD IMAGE_COUNTER = width * height / 8;
  188. SendCommand(0x4E);
  189. SendData(0x00);
  190. SendData(0x00);
  191. SendCommand(0x4F);
  192. SendData(0x00);
  193. SendData(0x00);
  194. SendCommand(0x24);
  195. for (i = 0; i < IMAGE_COUNTER; i++)
  196. {
  197. SendData(pgm_read_byte(&Image[i]));
  198. }
  199. Load_LUT(1);
  200. SendCommand(0x20);
  201. WaitUntilIdle();
  202. }
  203. /******************************************************************************
  204. function : Partial Display
  205. ******************************************************************************/
  206. void Epd::DisplayFrame_Partial(const UBYTE *Image, UWORD Xstart, UWORD Ystart, UWORD iwidth, UWORD iheight) {
  207. UWORD i, j;
  208. SendCommand(0x4E);
  209. SendData(0x00);
  210. SendData(0x00);
  211. SendCommand(0x4F);
  212. SendData(0x00);
  213. SendData(0x00);
  214. SendCommand(0x24);
  215. for(i=0; i<EPD_WIDTH; i++) {
  216. for(j=0; j<EPD_HEIGHT/8; j++) {
  217. if(i>=Ystart && i<Ystart+iheight && j>=Xstart/8 && j<(Xstart+iwidth)/8)
  218. SendData(Image[(j-Xstart/8) + (i-Ystart)*iwidth/8]);
  219. else
  220. SendData(0xff);
  221. }
  222. }
  223. Load_LUT(0);
  224. SendCommand(0x20);
  225. WaitUntilIdle();
  226. }
  227. /******************************************************************************
  228. function : set the look-up tables
  229. parameter:
  230. ******************************************************************************/
  231. void Epd::Load_LUT(UBYTE mode) {
  232. UWORD i;
  233. SendCommand(0x32);
  234. for (i = 0; i < 105; i++) {
  235. if(mode == 1)
  236. SendData(lut_1Gray_GC[i]);
  237. else if(mode == 0)
  238. SendData(lut_1Gray_DU[i]);
  239. }
  240. }
  241. /******************************************************************************
  242. function : Clear Screen
  243. parameter:
  244. mode: 0:just partial mode
  245. 1:clear all
  246. ******************************************************************************/
  247. void Epd::Clear(UBYTE mode) {
  248. UWORD i;
  249. UWORD IMAGE_COUNTER = width * height / 8;
  250. SendCommand(0x4E);
  251. SendData(0x00);
  252. SendData(0x00);
  253. SendCommand(0x4F);
  254. SendData(0x00);
  255. SendData(0x00);
  256. SendCommand(0x24);
  257. for (i = 0; i < IMAGE_COUNTER; i++)
  258. {
  259. SendData(0xff);
  260. }
  261. if(mode)
  262. Load_LUT(1);
  263. else
  264. Load_LUT(0);
  265. SendCommand(0x20);
  266. WaitUntilIdle();
  267. }
  268. /**
  269. * @brief: After this command is transmitted, the chip would enter the
  270. * deep-sleep mode to save power.
  271. * The deep sleep mode would return to standby by hardware reset.
  272. * The only one parameter is a check code, the command would be
  273. * You can use EPD_Reset() to awaken
  274. */
  275. void Epd::Sleep(void) {
  276. SendCommand(0X50);
  277. SendData(0xf7);
  278. SendCommand(0X02); //power off
  279. SendCommand(0X07); //deep sleep
  280. SendData(0xA5);
  281. }
  282. /* END OF FILE */