epd2in7b.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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, HIGH);
  120. DelayMs(200);
  121. DigitalWrite(reset_pin, LOW);
  122. DelayMs(10);
  123. DigitalWrite(reset_pin, HIGH);
  124. DelayMs(200);
  125. }
  126. /**
  127. * @brief: set the look-up tables
  128. */
  129. void Epd::SetLut(void) {
  130. unsigned int count;
  131. SendCommand(LUT_FOR_VCOM); //vcom
  132. for(count = 0; count < 44; count++) {
  133. SendData(lut_vcom_dc[count]);
  134. }
  135. SendCommand(LUT_WHITE_TO_WHITE); //ww --
  136. for(count = 0; count < 42; count++) {
  137. SendData(lut_ww[count]);
  138. }
  139. SendCommand(LUT_BLACK_TO_WHITE); //bw r
  140. for(count = 0; count < 42; count++) {
  141. SendData(lut_bw[count]);
  142. }
  143. SendCommand(LUT_WHITE_TO_BLACK); //wb w
  144. for(count = 0; count < 42; count++) {
  145. SendData(lut_bb[count]);
  146. }
  147. SendCommand(LUT_BLACK_TO_BLACK); //bb b
  148. for(count = 0; count < 42; count++) {
  149. SendData(lut_wb[count]);
  150. }
  151. }
  152. /**
  153. * @brief: transmit partial data to the SRAM
  154. */
  155. void Epd::TransmitPartial(const unsigned char* buffer_black, const unsigned char* buffer_red, int x, int y, int w, int l) {
  156. if (buffer_black != NULL) {
  157. SendCommand(PARTIAL_DATA_START_TRANSMISSION_1);
  158. SendData(x >> 8);
  159. SendData(x & 0xf8); // x should be the multiple of 8, the last 3 bit will always be ignored
  160. SendData(y >> 8);
  161. SendData(y & 0xff);
  162. SendData(w >> 8);
  163. SendData(w & 0xf8); // w (width) should be the multiple of 8, the last 3 bit will always be ignored
  164. SendData(l >> 8);
  165. SendData(l & 0xff);
  166. DelayMs(2);
  167. for(int i = 0; i < w / 8 * l; i++) {
  168. SendData(buffer_black[i]);
  169. }
  170. DelayMs(2);
  171. }
  172. if (buffer_red != NULL) {
  173. SendCommand(PARTIAL_DATA_START_TRANSMISSION_2);
  174. SendData(x >> 8);
  175. SendData(x & 0xf8); // x should be the multiple of 8, the last 3 bit will always be ignored
  176. SendData(y >> 8);
  177. SendData(y & 0xff);
  178. SendData(w >> 8);
  179. SendData(w & 0xf8); // w (width) should be the multiple of 8, the last 3 bit will always be ignored
  180. SendData(l >> 8);
  181. SendData(l & 0xff);
  182. DelayMs(2);
  183. for(int i = 0; i < w / 8 * l; i++) {
  184. SendData(buffer_red[i]);
  185. }
  186. DelayMs(2);
  187. }
  188. }
  189. /**
  190. * @brief: transmit partial data to the black part of SRAM
  191. */
  192. void Epd::TransmitPartialBlack(const unsigned char* buffer_black, int x, int y, int w, int l) {
  193. if (buffer_black != NULL) {
  194. SendCommand(PARTIAL_DATA_START_TRANSMISSION_1);
  195. SendData(x >> 8);
  196. SendData(x & 0xf8); // x should be the multiple of 8, the last 3 bit will always be ignored
  197. SendData(y >> 8);
  198. SendData(y & 0xff);
  199. SendData(w >> 8);
  200. SendData(w & 0xf8); // w (width) should be the multiple of 8, the last 3 bit will always be ignored
  201. SendData(l >> 8);
  202. SendData(l & 0xff);
  203. DelayMs(2);
  204. for(int i = 0; i < w / 8 * l; i++) {
  205. SendData(buffer_black[i]);
  206. }
  207. DelayMs(2);
  208. }
  209. }
  210. /**
  211. * @brief: transmit partial data to the red part of SRAM
  212. */
  213. void Epd::TransmitPartialRed(const unsigned char* buffer_red, int x, int y, int w, int l) {
  214. if (buffer_red != NULL) {
  215. SendCommand(PARTIAL_DATA_START_TRANSMISSION_2);
  216. SendData(x >> 8);
  217. SendData(x & 0xf8); // x should be the multiple of 8, the last 3 bit will always be ignored
  218. SendData(y >> 8);
  219. SendData(y & 0xff);
  220. SendData(w >> 8);
  221. SendData(w & 0xf8); // w (width) should be the multiple of 8, the last 3 bit will always be ignored
  222. SendData(l >> 8);
  223. SendData(l & 0xff);
  224. DelayMs(2);
  225. for(int i = 0; i < w / 8 * l; i++) {
  226. SendData(buffer_red[i]);
  227. }
  228. DelayMs(2);
  229. }
  230. }
  231. /**
  232. * @brief: refreshes a specific part of the display
  233. */
  234. void Epd::RefreshPartial(int x, int y, int w, int l) {
  235. SendCommand(PARTIAL_DISPLAY_REFRESH);
  236. SendData(x >> 8);
  237. SendData(x & 0xf8); // x should be the multiple of 8, the last 3 bit will always be ignored
  238. SendData(y >> 8);
  239. SendData(y & 0xff);
  240. SendData(w >> 8);
  241. SendData(w & 0xf8); // w (width) should be the multiple of 8, the last 3 bit will always be ignored
  242. SendData(l >> 8);
  243. SendData(l & 0xff);
  244. WaitUntilIdle();
  245. }
  246. /**
  247. * @brief: refresh and displays the frame
  248. */
  249. void Epd::DisplayFrame(const unsigned char* frame_buffer_black, const unsigned char* frame_buffer_red) {
  250. SendCommand(TCON_RESOLUTION);
  251. SendData(width >> 8);
  252. SendData(width & 0xff); //176
  253. SendData(height >> 8);
  254. SendData(height & 0xff); //264
  255. if (frame_buffer_black != NULL) {
  256. SendCommand(DATA_START_TRANSMISSION_1);
  257. DelayMs(2);
  258. for(int i = 0; i < width * height / 8; i++) {
  259. SendData(pgm_read_byte(&frame_buffer_black[i]));
  260. }
  261. DelayMs(2);
  262. }
  263. if (frame_buffer_red != NULL) {
  264. SendCommand(DATA_START_TRANSMISSION_2);
  265. DelayMs(2);
  266. for(int i = 0; i < width * height / 8; i++) {
  267. SendData(pgm_read_byte(&frame_buffer_red[i]));
  268. }
  269. DelayMs(2);
  270. }
  271. SendCommand(DISPLAY_REFRESH);
  272. WaitUntilIdle();
  273. }
  274. /**
  275. * @brief: clear the frame data from the SRAM, this won't refresh the display
  276. */
  277. void Epd::ClearFrame(void) {
  278. SendCommand(TCON_RESOLUTION);
  279. SendData(width >> 8);
  280. SendData(width & 0xff); //176
  281. SendData(height >> 8);
  282. SendData(height & 0xff); //264
  283. SendCommand(DATA_START_TRANSMISSION_1);
  284. DelayMs(2);
  285. for(int i = 0; i < width * height / 8; i++) {
  286. SendData(0x00);
  287. }
  288. DelayMs(2);
  289. SendCommand(DATA_START_TRANSMISSION_2);
  290. DelayMs(2);
  291. for(int i = 0; i < width * height / 8; i++) {
  292. SendData(0x00);
  293. }
  294. DelayMs(2);
  295. }
  296. /**
  297. * @brief: This displays the frame data from SRAM
  298. */
  299. void Epd::DisplayFrame(void) {
  300. SendCommand(DISPLAY_REFRESH);
  301. WaitUntilIdle();
  302. }
  303. /**
  304. * @brief: After this command is transmitted, the chip would enter the deep-sleep mode to save power.
  305. * The deep sleep mode would return to standby by hardware reset. The only one parameter is a
  306. * check code, the command would be executed if check code = 0xA5.
  307. * You can use Epd::Reset() to awaken and use Epd::Init() to initialize.
  308. */
  309. void Epd::Sleep() {
  310. SendCommand(DEEP_SLEEP);
  311. SendData(0xa5);
  312. }
  313. const unsigned char lut_vcom_dc[] =
  314. {
  315. 0x00, 0x00,
  316. 0x00, 0x1A, 0x1A, 0x00, 0x00, 0x01,
  317. 0x00, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  318. 0x00, 0x0E, 0x01, 0x0E, 0x01, 0x10,
  319. 0x00, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  320. 0x00, 0x04, 0x10, 0x00, 0x00, 0x05,
  321. 0x00, 0x03, 0x0E, 0x00, 0x00, 0x0A,
  322. 0x00, 0x23, 0x00, 0x00, 0x00, 0x01
  323. };
  324. //R21H
  325. const unsigned char lut_ww[] =
  326. {
  327. 0x90, 0x1A, 0x1A, 0x00, 0x00, 0x01,
  328. 0x40, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  329. 0x84, 0x0E, 0x01, 0x0E, 0x01, 0x10,
  330. 0x80, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  331. 0x00, 0x04, 0x10, 0x00, 0x00, 0x05,
  332. 0x00, 0x03, 0x0E, 0x00, 0x00, 0x0A,
  333. 0x00, 0x23, 0x00, 0x00, 0x00, 0x01
  334. };
  335. //R22H r
  336. const unsigned char lut_bw[] =
  337. {
  338. 0xA0, 0x1A, 0x1A, 0x00, 0x00, 0x01,
  339. 0x00, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  340. 0x84, 0x0E, 0x01, 0x0E, 0x01, 0x10,
  341. 0x90, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  342. 0xB0, 0x04, 0x10, 0x00, 0x00, 0x05,
  343. 0xB0, 0x03, 0x0E, 0x00, 0x00, 0x0A,
  344. 0xC0, 0x23, 0x00, 0x00, 0x00, 0x01
  345. };
  346. //R23H w
  347. const unsigned char lut_bb[] =
  348. {
  349. 0x90, 0x1A, 0x1A, 0x00, 0x00, 0x01,
  350. 0x40, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  351. 0x84, 0x0E, 0x01, 0x0E, 0x01, 0x10,
  352. 0x80, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  353. 0x00, 0x04, 0x10, 0x00, 0x00, 0x05,
  354. 0x00, 0x03, 0x0E, 0x00, 0x00, 0x0A,
  355. 0x00, 0x23, 0x00, 0x00, 0x00, 0x01
  356. };
  357. //R24H b
  358. const unsigned char lut_wb[] =
  359. {
  360. 0x90, 0x1A, 0x1A, 0x00, 0x00, 0x01,
  361. 0x20, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  362. 0x84, 0x0E, 0x01, 0x0E, 0x01, 0x10,
  363. 0x10, 0x0A, 0x0A, 0x00, 0x00, 0x08,
  364. 0x00, 0x04, 0x10, 0x00, 0x00, 0x05,
  365. 0x00, 0x03, 0x0E, 0x00, 0x00, 0x0A,
  366. 0x00, 0x23, 0x00, 0x00, 0x00, 0x01
  367. };
  368. /* END OF FILE */