epd2in7b.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /**
  2. * @filename : epd2in7b.cpp
  3. * @brief : Implements for Dual-color e-paper library
  4. * @author : Yehui from Waveshare
  5. *
  6. * Copyright (C) Waveshare August 10 2017
  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 "epd2in7b.h"
  28. Epd::~Epd() {
  29. };
  30. Epd::Epd() {
  31. reset_pin = RST_PIN;
  32. dc_pin = DC_PIN;
  33. cs_pin = CS_PIN;
  34. busy_pin = BUSY_PIN;
  35. width = EPD_WIDTH;
  36. height = EPD_HEIGHT;
  37. };
  38. int Epd::Init(void) {
  39. /* this calls the peripheral hardware interface, see epdif */
  40. if (IfInit() != 0) {
  41. return -1;
  42. }
  43. /* EPD hardware init start */
  44. Reset();
  45. SendCommand(POWER_ON);
  46. WaitUntilIdle();
  47. SendCommand(PANEL_SETTING);
  48. SendData(0xaf); //KW-BF KWR-AF BWROTP 0f
  49. SendCommand(PLL_CONTROL);
  50. SendData(0x3a); //3A 100HZ 29 150Hz 39 200HZ 31 171HZ
  51. SendCommand(POWER_SETTING);
  52. SendData(0x03); // VDS_EN, VDG_EN
  53. SendData(0x00); // VCOM_HV, VGHL_LV[1], VGHL_LV[0]
  54. SendData(0x2b); // VDH
  55. SendData(0x2b); // VDL
  56. SendData(0x09); // VDHR
  57. SendCommand(BOOSTER_SOFT_START);
  58. SendData(0x07);
  59. SendData(0x07);
  60. SendData(0x17);
  61. // Power optimization
  62. SendCommand(0xF8);
  63. SendData(0x60);
  64. SendData(0xA5);
  65. // Power optimization
  66. SendCommand(0xF8);
  67. SendData(0x89);
  68. SendData(0xA5);
  69. // Power optimization
  70. SendCommand(0xF8);
  71. SendData(0x90);
  72. SendData(0x00);
  73. // Power optimization
  74. SendCommand(0xF8);
  75. SendData(0x93);
  76. SendData(0x2A);
  77. // Power optimization
  78. SendCommand(0xF8);
  79. SendData(0x73);
  80. SendData(0x41);
  81. SendCommand(VCM_DC_SETTING_REGISTER);
  82. SendData(0x12);
  83. SendCommand(VCOM_AND_DATA_INTERVAL_SETTING);
  84. SendData(0x87); // define by OTP
  85. SetLut();
  86. SendCommand(PARTIAL_DISPLAY_REFRESH);
  87. SendData(0x00);
  88. /* EPD hardware init end */
  89. return 0;
  90. }
  91. /**
  92. * @brief: basic function for sending commands
  93. */
  94. void Epd::SendCommand(unsigned char command) {
  95. DigitalWrite(dc_pin, LOW);
  96. SpiTransfer(command);
  97. }
  98. /**
  99. * @brief: basic function for sending data
  100. */
  101. void Epd::SendData(unsigned char data) {
  102. DigitalWrite(dc_pin, HIGH);
  103. SpiTransfer(data);
  104. }
  105. /**
  106. * @brief: Wait until the busy_pin goes HIGH
  107. */
  108. void Epd::WaitUntilIdle(void) {
  109. while(DigitalRead(busy_pin) == 0) { //0: busy, 1: idle
  110. DelayMs(100);
  111. }
  112. }
  113. /**
  114. * @brief: module reset.
  115. * often used to awaken the module in deep sleep,
  116. * see Epd::Sleep();
  117. */
  118. void Epd::Reset(void) {
  119. DigitalWrite(reset_pin, LOW);
  120. DelayMs(200);
  121. DigitalWrite(reset_pin, HIGH);
  122. DelayMs(200);
  123. }
  124. /**
  125. * @brief: set the look-up tables
  126. */
  127. void Epd::SetLut(void) {
  128. unsigned int count;
  129. SendCommand(LUT_FOR_VCOM); //vcom
  130. for(count = 0; count < 44; count++) {
  131. SendData(lut_vcom_dc[count]);
  132. }
  133. SendCommand(LUT_WHITE_TO_WHITE); //ww --
  134. for(count = 0; count < 42; count++) {
  135. SendData(lut_ww[count]);
  136. }
  137. SendCommand(LUT_BLACK_TO_WHITE); //bw r
  138. for(count = 0; count < 42; count++) {
  139. SendData(lut_bw[count]);
  140. }
  141. SendCommand(LUT_WHITE_TO_BLACK); //wb w
  142. for(count = 0; count < 42; count++) {
  143. SendData(lut_bb[count]);
  144. }
  145. SendCommand(LUT_BLACK_TO_BLACK); //bb b
  146. for(count = 0; count < 42; count++) {
  147. SendData(lut_wb[count]);
  148. }
  149. }
  150. /**
  151. * @brief: transmit partial data to the SRAM
  152. */
  153. void Epd::TransmitPartial(const unsigned char* buffer_black, const unsigned char* buffer_red, int x, int y, int w, int l) {
  154. if (buffer_black != NULL) {
  155. SendCommand(PARTIAL_DATA_START_TRANSMISSION_1);
  156. SendData(x >> 8);
  157. SendData(x & 0xf8); // x should be the multiple of 8, the last 3 bit will always be ignored
  158. SendData(y >> 8);
  159. SendData(y & 0xff);
  160. SendData(w >> 8);
  161. SendData(w & 0xf8); // w (width) should be the multiple of 8, the last 3 bit will always be ignored
  162. SendData(l >> 8);
  163. SendData(l & 0xff);
  164. DelayMs(2);
  165. for(int i = 0; i < w / 8 * l; i++) {
  166. SendData(buffer_black[i]);
  167. }
  168. DelayMs(2);
  169. }
  170. if (buffer_red != NULL) {
  171. SendCommand(PARTIAL_DATA_START_TRANSMISSION_2);
  172. SendData(x >> 8);
  173. SendData(x & 0xf8); // x should be the multiple of 8, the last 3 bit will always be ignored
  174. SendData(y >> 8);
  175. SendData(y & 0xff);
  176. SendData(w >> 8);
  177. SendData(w & 0xf8); // w (width) should be the multiple of 8, the last 3 bit will always be ignored
  178. SendData(l >> 8);
  179. SendData(l & 0xff);
  180. DelayMs(2);
  181. for(int i = 0; i < w / 8 * l; i++) {
  182. SendData(buffer_red[i]);
  183. }
  184. DelayMs(2);
  185. }
  186. }
  187. /**
  188. * @brief: transmit partial data to the black part of SRAM
  189. */
  190. void Epd::TransmitPartialBlack(const unsigned char* buffer_black, int x, int y, int w, int l) {
  191. if (buffer_black != NULL) {
  192. SendCommand(PARTIAL_DATA_START_TRANSMISSION_1);
  193. SendData(x >> 8);
  194. SendData(x & 0xf8); // x should be the multiple of 8, the last 3 bit will always be ignored
  195. SendData(y >> 8);
  196. SendData(y & 0xff);
  197. SendData(w >> 8);
  198. SendData(w & 0xf8); // w (width) should be the multiple of 8, the last 3 bit will always be ignored
  199. SendData(l >> 8);
  200. SendData(l & 0xff);
  201. DelayMs(2);
  202. for(int i = 0; i < w / 8 * l; i++) {
  203. SendData(buffer_black[i]);
  204. }
  205. DelayMs(2);
  206. }
  207. }
  208. /**
  209. * @brief: transmit partial data to the red part of SRAM
  210. */
  211. void Epd::TransmitPartialRed(const unsigned char* buffer_red, int x, int y, int w, int l) {
  212. if (buffer_red != NULL) {
  213. SendCommand(PARTIAL_DATA_START_TRANSMISSION_2);
  214. SendData(x >> 8);
  215. SendData(x & 0xf8); // x should be the multiple of 8, the last 3 bit will always be ignored
  216. SendData(y >> 8);
  217. SendData(y & 0xff);
  218. SendData(w >> 8);
  219. SendData(w & 0xf8); // w (width) should be the multiple of 8, the last 3 bit will always be ignored
  220. SendData(l >> 8);
  221. SendData(l & 0xff);
  222. DelayMs(2);
  223. for(int i = 0; i < w / 8 * l; i++) {
  224. SendData(buffer_red[i]);
  225. }
  226. DelayMs(2);
  227. }
  228. }
  229. /**
  230. * @brief: refreshes a specific part of the display
  231. */
  232. void Epd::RefreshPartial(int x, int y, int w, int l) {
  233. SendCommand(PARTIAL_DISPLAY_REFRESH);
  234. SendData(x >> 8);
  235. SendData(x & 0xf8); // x should be the multiple of 8, the last 3 bit will always be ignored
  236. SendData(y >> 8);
  237. SendData(y & 0xff);
  238. SendData(w >> 8);
  239. SendData(w & 0xf8); // w (width) should be the multiple of 8, the last 3 bit will always be ignored
  240. SendData(l >> 8);
  241. SendData(l & 0xff);
  242. WaitUntilIdle();
  243. }
  244. /**
  245. * @brief: refresh and displays the frame
  246. */
  247. void Epd::DisplayFrame(const unsigned char* frame_buffer_black, const unsigned char* frame_buffer_red) {
  248. SendCommand(TCON_RESOLUTION);
  249. SendData(width >> 8);
  250. SendData(width & 0xff); //176
  251. SendData(height >> 8);
  252. SendData(height & 0xff); //264
  253. if (frame_buffer_black != NULL) {
  254. SendCommand(DATA_START_TRANSMISSION_1);
  255. DelayMs(2);
  256. for(int i = 0; i < width * height / 8; i++) {
  257. SendData(pgm_read_byte(&frame_buffer_black[i]));
  258. }
  259. DelayMs(2);
  260. }
  261. if (frame_buffer_red != NULL) {
  262. SendCommand(DATA_START_TRANSMISSION_2);
  263. DelayMs(2);
  264. for(int i = 0; i < width * height / 8; i++) {
  265. SendData(pgm_read_byte(&frame_buffer_red[i]));
  266. }
  267. DelayMs(2);
  268. }
  269. SendCommand(DISPLAY_REFRESH);
  270. WaitUntilIdle();
  271. }
  272. /**
  273. * @brief: clear the frame data from the SRAM, this won't refresh the display
  274. */
  275. void Epd::ClearFrame(void) {
  276. SendCommand(TCON_RESOLUTION);
  277. SendData(width >> 8);
  278. SendData(width & 0xff); //176
  279. SendData(height >> 8);
  280. SendData(height & 0xff); //264
  281. SendCommand(DATA_START_TRANSMISSION_1);
  282. DelayMs(2);
  283. for(int i = 0; i < width * height / 8; i++) {
  284. SendData(0x00);
  285. }
  286. DelayMs(2);
  287. SendCommand(DATA_START_TRANSMISSION_2);
  288. DelayMs(2);
  289. for(int i = 0; i < width * height / 8; i++) {
  290. SendData(0x00);
  291. }
  292. DelayMs(2);
  293. }
  294. /**
  295. * @brief: This displays the frame data from SRAM
  296. */
  297. void Epd::DisplayFrame(void) {
  298. SendCommand(DISPLAY_REFRESH);
  299. WaitUntilIdle();
  300. }
  301. /**
  302. * @brief: After this command is transmitted, the chip would enter the deep-sleep mode to save power.
  303. * The deep sleep mode would return to standby by hardware reset. The only one parameter is a
  304. * check code, the command would be executed if check code = 0xA5.
  305. * You can use Epd::Reset() to awaken and use Epd::Init() to initialize.
  306. */
  307. void Epd::Sleep() {
  308. SendCommand(DEEP_SLEEP);
  309. SendData(0xa5);
  310. }
  311. const unsigned char lut_vcom_dc[] =
  312. {
  313. 0x00, 0x00,
  314. 0x00, 0x1A, 0x1A, 0x00, 0x00, 0x01,
  315. 0x00, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  316. 0x00, 0x0E, 0x01, 0x0E, 0x01, 0x10,
  317. 0x00, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  318. 0x00, 0x04, 0x10, 0x00, 0x00, 0x05,
  319. 0x00, 0x03, 0x0E, 0x00, 0x00, 0x0A,
  320. 0x00, 0x23, 0x00, 0x00, 0x00, 0x01
  321. };
  322. //R21H
  323. const unsigned char lut_ww[] =
  324. {
  325. 0x90, 0x1A, 0x1A, 0x00, 0x00, 0x01,
  326. 0x40, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  327. 0x84, 0x0E, 0x01, 0x0E, 0x01, 0x10,
  328. 0x80, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  329. 0x00, 0x04, 0x10, 0x00, 0x00, 0x05,
  330. 0x00, 0x03, 0x0E, 0x00, 0x00, 0x0A,
  331. 0x00, 0x23, 0x00, 0x00, 0x00, 0x01
  332. };
  333. //R22H r
  334. const unsigned char lut_bw[] =
  335. {
  336. 0xA0, 0x1A, 0x1A, 0x00, 0x00, 0x01,
  337. 0x00, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  338. 0x84, 0x0E, 0x01, 0x0E, 0x01, 0x10,
  339. 0x90, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  340. 0xB0, 0x04, 0x10, 0x00, 0x00, 0x05,
  341. 0xB0, 0x03, 0x0E, 0x00, 0x00, 0x0A,
  342. 0xC0, 0x23, 0x00, 0x00, 0x00, 0x01
  343. };
  344. //R23H w
  345. const unsigned char lut_bb[] =
  346. {
  347. 0x90, 0x1A, 0x1A, 0x00, 0x00, 0x01,
  348. 0x40, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  349. 0x84, 0x0E, 0x01, 0x0E, 0x01, 0x10,
  350. 0x80, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  351. 0x00, 0x04, 0x10, 0x00, 0x00, 0x05,
  352. 0x00, 0x03, 0x0E, 0x00, 0x00, 0x0A,
  353. 0x00, 0x23, 0x00, 0x00, 0x00, 0x01
  354. };
  355. //R24H b
  356. const unsigned char lut_wb[] =
  357. {
  358. 0x90, 0x1A, 0x1A, 0x00, 0x00, 0x01,
  359. 0x20, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  360. 0x84, 0x0E, 0x01, 0x0E, 0x01, 0x10,
  361. 0x10, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  362. 0x00, 0x04, 0x10, 0x00, 0x00, 0x05,
  363. 0x00, 0x03, 0x0E, 0x00, 0x00, 0x0A,
  364. 0x00, 0x23, 0x00, 0x00, 0x00, 0x01
  365. };
  366. /* END OF FILE */