epd3in7.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. static const UBYTE lut_1Gray_A2[] =
  72. {
  73. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //1
  74. 0x0A,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //2
  75. 0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //3
  76. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //4
  77. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //5
  78. 0x00,0x00,0x03,0x05,0x00,0x00,0x00,0x00,0x00,0x00, //6
  79. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //7
  80. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //8
  81. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //9
  82. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //10
  83. 0x22,0x22,0x22,0x22,0x22
  84. };
  85. Epd::~Epd() {
  86. };
  87. Epd::Epd() {
  88. reset_pin = RST_PIN;
  89. dc_pin = DC_PIN;
  90. cs_pin = CS_PIN;
  91. busy_pin = BUSY_PIN;
  92. width = EPD_WIDTH;
  93. height = EPD_HEIGHT;
  94. };
  95. int Epd::Init(void) {
  96. if (IfInit() != 0) {
  97. return -1;
  98. }
  99. Reset();
  100. SendCommand(0x12);
  101. DelayMs(300);
  102. SendCommand(0x46);
  103. SendData(0xF7);
  104. WaitUntilIdle();
  105. SendCommand(0x47);
  106. SendData(0xF7);
  107. WaitUntilIdle();
  108. SendCommand(0x01); // setting gaet number
  109. SendData(0xDF);
  110. SendData(0x01);
  111. SendData(0x00);
  112. SendCommand(0x03); // set gate voltage
  113. SendData(0x00);
  114. SendCommand(0x04); // set source voltage
  115. SendData(0x41);
  116. SendData(0xA8);
  117. SendData(0x32);
  118. SendCommand(0x11); // set data entry sequence
  119. SendData(0x03);
  120. SendCommand(0x3C); // set border
  121. SendData(0x00);
  122. SendCommand(0x0C); // set booster strength
  123. SendData(0xAE);
  124. SendData(0xC7);
  125. SendData(0xC3);
  126. SendData(0xC0);
  127. SendData(0xC0);
  128. SendCommand(0x18); // set internal sensor on
  129. SendData(0x80);
  130. SendCommand(0x2C); // set vcom value
  131. SendData(0x44);
  132. SendCommand(0x37); // set display option, these setting turn on previous function
  133. SendData(0x00);
  134. SendData(0xff);
  135. SendData(0xff);
  136. SendData(0xff);
  137. SendData(0xff);
  138. SendData(0x4f);
  139. SendData(0xff);
  140. SendData(0xff);
  141. SendData(0xff);
  142. SendData(0xff);
  143. SendCommand(0x44); // setting X direction start/end position of RAM
  144. SendData(0x00);
  145. SendData(0x00);
  146. SendData(0x17);
  147. SendData(0x01);
  148. SendCommand(0x45); // setting Y direction start/end position of RAM
  149. SendData(0x00);
  150. SendData(0x00);
  151. SendData(0xDF);
  152. SendData(0x01);
  153. SendCommand(0x22); // Display Update Control 2
  154. SendData(0xCF);
  155. return 0;
  156. }
  157. /**
  158. * @brief: basic function for sending commands
  159. */
  160. void Epd::SendCommand(unsigned char command) {
  161. DigitalWrite(dc_pin, LOW);
  162. SpiTransfer(command);
  163. }
  164. /**
  165. * @brief: basic function for sending data
  166. */
  167. void Epd::SendData(unsigned char data) {
  168. DigitalWrite(dc_pin, HIGH);
  169. SpiTransfer(data);
  170. }
  171. /**
  172. * @brief: Wait until the busy_pin goes HIGH
  173. */
  174. void Epd::WaitUntilIdle(void) {
  175. Serial.print("e-Paper busy \r\n ");
  176. UBYTE busy;
  177. do {
  178. busy = DigitalRead(busy_pin);
  179. } while(busy);
  180. DelayMs(200);
  181. Serial.print("e-Paper busy release \r\n ");
  182. }
  183. /**
  184. * @brief: module reset.
  185. * often used to awaken the module in deep sleep,
  186. * see Epd::Sleep();
  187. */
  188. void Epd::Reset(void) {
  189. DigitalWrite(reset_pin, HIGH);
  190. DelayMs(20);
  191. DigitalWrite(reset_pin, LOW); //module reset
  192. DelayMs(2);
  193. DigitalWrite(reset_pin, HIGH);
  194. DelayMs(20);
  195. }
  196. /******************************************************************************
  197. function : Display
  198. ******************************************************************************/
  199. void Epd::DisplayFrame(const UBYTE *Image, bool isBase) {
  200. UWORD i;
  201. UWORD IMAGE_COUNTER = width * height / 8;
  202. SendCommand(0x4E);
  203. SendData(0x00);
  204. SendData(0x00);
  205. SendCommand(0x4F);
  206. SendData(0x00);
  207. SendData(0x00);
  208. SendCommand(0x24);
  209. for (i = 0; i < IMAGE_COUNTER; i++) {
  210. SendData(pgm_read_byte(&Image[i]));
  211. }
  212. if(isBase) {
  213. SendCommand(0x26);
  214. for (i = 0; i < IMAGE_COUNTER; i++) {
  215. SendData(pgm_read_byte(&Image[i]));
  216. }
  217. }
  218. Load_LUT(1);
  219. SendCommand(0x20);
  220. WaitUntilIdle();
  221. }
  222. /******************************************************************************
  223. function : Partial Display
  224. note:
  225. 1. iwidth must be a multiple of 8
  226. 2. The update will clear other parts of the image data
  227. ******************************************************************************/
  228. void Epd::DisplayFrame_Partial(const UBYTE *Image, UWORD Xstart, UWORD Ystart, UWORD iwidth, UWORD iheight) {
  229. UWORD i, j;
  230. SendCommand(0x4E);
  231. SendData(0x00);
  232. SendData(0x00);
  233. SendCommand(0x4F);
  234. SendData(0x00);
  235. SendData(0x00);
  236. SendCommand(0x24);
  237. for(i=0; i<EPD_WIDTH; i++) {
  238. for(j=0; j<EPD_HEIGHT/8; j++) {
  239. if(i>=Ystart && i<Ystart+iheight && j>=Xstart/8 && j<(Xstart+iwidth)/8)
  240. SendData(Image[(j-Xstart/8) + (i-Ystart)*iwidth/8]);
  241. else
  242. SendData(0xff);
  243. }
  244. }
  245. Load_LUT(0);
  246. SendCommand(0x20);
  247. WaitUntilIdle();
  248. }
  249. /******************************************************************************
  250. function : Sends part the image buffer in RAM to e-Paper and displays
  251. notes:
  252. 1. Xstart must be a multiple of 8
  253. 1. isGC: use GC mode to clear base-image
  254. ******************************************************************************/
  255. void Epd::DisplayFrame_Part(const UBYTE *Image, UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend, bool isGC)
  256. {
  257. UWORD i, Width;
  258. Width = (Xend-Xstart)%8 == 0 ? (Xend-Xstart)/8 : (Xend-Xstart)/8+1;
  259. UWORD IMAGE_COUNTER = Width * (Yend-Ystart);
  260. Xend -= 1;
  261. Yend -= 1;
  262. SendCommand(0x44);
  263. SendData(Xstart & 0xff);
  264. SendData((Xstart>>8) & 0x03);
  265. SendData(Xend & 0xff);
  266. SendData((Xend>>8) & 0x03);
  267. SendCommand(0x45);
  268. SendData(Ystart & 0xff);
  269. SendData((Ystart>>8) & 0x03);
  270. SendData(Yend & 0xff);
  271. SendData((Yend>>8) & 0x03);
  272. SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  273. SendData(Xstart & 0xFF);
  274. SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  275. SendData(Ystart & 0xFF);
  276. SendData((Ystart >> 8) & 0xFF);
  277. SendCommand(0x24);
  278. for (i = 0; i < IMAGE_COUNTER; i++)
  279. {
  280. SendData(Image[i]);
  281. }
  282. if(isGC)
  283. Load_LUT(1);
  284. else
  285. Load_LUT(0);
  286. }
  287. void Epd::TurnOnDisplay(void) {
  288. SendCommand(0x20);
  289. WaitUntilIdle();
  290. }
  291. /******************************************************************************
  292. function : set the look-up tables
  293. parameter:
  294. ******************************************************************************/
  295. void Epd::Load_LUT(UBYTE mode) {
  296. UWORD i;
  297. SendCommand(0x32);
  298. for (i = 0; i < 105; i++) {
  299. if(mode == 1)
  300. SendData(lut_1Gray_GC[i]);
  301. else if(mode == 0)
  302. SendData(lut_1Gray_A2[i]);
  303. }
  304. }
  305. /******************************************************************************
  306. function : Clear Screen
  307. parameter:
  308. mode: 0:just partial mode
  309. 1:clear all
  310. ******************************************************************************/
  311. void Epd::Clear(UBYTE mode) {
  312. UWORD i;
  313. UWORD IMAGE_COUNTER = width * height / 8;
  314. SendCommand(0x4E);
  315. SendData(0x00);
  316. SendData(0x00);
  317. SendCommand(0x4F);
  318. SendData(0x00);
  319. SendData(0x00);
  320. SendCommand(0x24);
  321. for (i = 0; i < IMAGE_COUNTER; i++)
  322. {
  323. SendData(0xff);
  324. }
  325. if(mode)
  326. Load_LUT(1);
  327. else
  328. Load_LUT(0);
  329. SendCommand(0x20);
  330. WaitUntilIdle();
  331. }
  332. /**
  333. * @brief: After this command is transmitted, the chip would enter the
  334. * deep-sleep mode to save power.
  335. * The deep sleep mode would return to standby by hardware reset.
  336. * The only one parameter is a check code, the command would be
  337. * You can use EPD_Reset() to awaken
  338. */
  339. void Epd::Sleep(void) {
  340. SendCommand(0X10); //deep sleep
  341. SendData(0x03);
  342. }
  343. /* END OF FILE */