epd1in54_V2.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*****************************************************************************
  2. * | File : epd1in54_V2.cpp
  3. * | Author : Waveshare team
  4. * | Function : 1.54inch e-paper V2
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2019-06-24
  9. * | Info :
  10. #
  11. # Permission is hereby granted, free of charge, to any person obtaining a copy
  12. # of this software and associated documnetation files (the "Software"), to deal
  13. # in the Software without restriction, including without limitation the rights
  14. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. # copies of the Software, and to permit persons to whom the Software is
  16. # furished to do so, subject to the following conditions:
  17. #
  18. # The above copyright notice and this permission notice shall be included in
  19. # all copies or substantial portions of the Software.
  20. #
  21. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. # THE SOFTWARE.
  28. #
  29. ******************************************************************************/
  30. #include <stdlib.h>
  31. #include "epd1in54_V2.h"
  32. Epd::~Epd()
  33. {
  34. };
  35. Epd::Epd()
  36. {
  37. reset_pin = RST_PIN;
  38. dc_pin = DC_PIN;
  39. cs_pin = CS_PIN;
  40. busy_pin = BUSY_PIN;
  41. width = EPD_WIDTH;
  42. height = EPD_HEIGHT;
  43. };
  44. /**
  45. * @brief: basic function for sending commands
  46. */
  47. void Epd::SendCommand(unsigned char command)
  48. {
  49. DigitalWrite(dc_pin, LOW);
  50. SpiTransfer(command);
  51. }
  52. /**
  53. * @brief: basic function for sending data
  54. */
  55. void Epd::SendData(unsigned char data)
  56. {
  57. DigitalWrite(dc_pin, HIGH);
  58. SpiTransfer(data);
  59. }
  60. /**
  61. * @brief: Wait until the busy_pin goes HIGH
  62. */
  63. void Epd::WaitUntilIdle(void)
  64. {
  65. while(DigitalRead(busy_pin) == 1) { //LOW: idle, HIGH: busy
  66. DelayMs(100);
  67. }
  68. DelayMs(200);
  69. }
  70. int Epd::HDirInit(void)
  71. {
  72. /* this calls the peripheral hardware interface, see epdif */
  73. if (IfInit() != 0) {
  74. return -1;
  75. }
  76. /* EPD hardware init start */
  77. Reset();
  78. WaitUntilIdle();
  79. SendCommand(0x12); //SWRESET
  80. WaitUntilIdle();
  81. SendCommand(0x01); //Driver output control
  82. SendData(0xC7);
  83. SendData(0x00);
  84. SendData(0x01);
  85. SendCommand(0x11); //data entry mode
  86. SendData(0x01);
  87. SendCommand(0x44); //set Ram-X address start/end position
  88. SendData(0x00);
  89. SendData(0x18); //0x0C-->(18+1)*8=200
  90. SendCommand(0x45); //set Ram-Y address start/end position
  91. SendData(0xC7); //0xC7-->(199+1)=200
  92. SendData(0x00);
  93. SendData(0x00);
  94. SendData(0x00);
  95. SendCommand(0x3C); //BorderWavefrom
  96. SendData(0x01);
  97. SendCommand(0x18);
  98. SendData(0x80);
  99. SendCommand(0x22); // //Load Temperature and waveform setting.
  100. SendData(0XB1);
  101. SendCommand(0x20);
  102. SendCommand(0x4E); // set RAM x address count to 0;
  103. SendData(0x00);
  104. SendCommand(0x4F); // set RAM y address count to 0X199;
  105. SendData(0xC7);
  106. SendData(0x00);
  107. WaitUntilIdle();
  108. /* EPD hardware init end */
  109. return 0;
  110. }
  111. int Epd::LDirInit(void)
  112. {
  113. /* this calls the peripheral hardware interface, see epdif */
  114. if (IfInit() != 0) {
  115. return -1;
  116. }
  117. /* EPD hardware init start */
  118. Reset();
  119. WaitUntilIdle();
  120. SendCommand(0x12); //SWRESET
  121. WaitUntilIdle();
  122. SendCommand(0x01); //Driver output control
  123. SendData(0xC7);
  124. SendData(0x00);
  125. SendData(0x00);
  126. SendCommand(0x11); //data entry mode
  127. SendData(0x03);
  128. SendCommand(0x44); //set Ram-X address start/end position
  129. SendData(0x00);
  130. SendData(0x18); //0x0C-->(18+1)*8=200
  131. SendCommand(0x45); //set Ram-Y address start/end position
  132. SendData(0xC7); //0xC7-->(199+1)=200
  133. SendData(0x00);
  134. SendData(0x00);
  135. SendData(0x00);
  136. SendCommand(0x3C); //BorderWavefrom
  137. SendData(0x01);
  138. SendCommand(0x18);
  139. SendData(0x80);
  140. SendCommand(0x22); // //Load Temperature and waveform setting.
  141. SendData(0XB1);
  142. SendCommand(0x20);
  143. SendCommand(0x4E); // set RAM x address count to 0;
  144. SendData(0x00);
  145. SendCommand(0x4F); // set RAM y address count to 0X199;
  146. SendData(0xC7);
  147. SendData(0x00);
  148. WaitUntilIdle();
  149. /* EPD hardware init end */
  150. return 0;
  151. }
  152. /**
  153. * @brief: module reset.
  154. * often used to awaken the module in deep sleep,
  155. * see Epd::Sleep();
  156. */
  157. void Epd::Reset(void)
  158. {
  159. DigitalWrite(reset_pin, HIGH);
  160. DelayMs(200);
  161. DigitalWrite(reset_pin, LOW); //module reset
  162. DelayMs(20);
  163. DigitalWrite(reset_pin, HIGH);
  164. DelayMs(200);
  165. }
  166. void Epd::Clear(void)
  167. {
  168. int w, h;
  169. w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  170. h = EPD_HEIGHT;
  171. SendCommand(0x24);
  172. for (int j = 0; j < h; j++) {
  173. for (int i = 0; i < w; i++) {
  174. SendData(0xff);
  175. }
  176. }
  177. //DISPLAY REFRESH
  178. SendCommand(0x22);
  179. SendData(0xF7);
  180. SendCommand(0x20);
  181. WaitUntilIdle();
  182. }
  183. void Epd::Display(const unsigned char* frame_buffer)
  184. {
  185. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  186. int h = EPD_HEIGHT;
  187. if (frame_buffer != NULL) {
  188. SendCommand(0x24);
  189. for (int j = 0; j < h; j++) {
  190. for (int i = 0; i < w; i++) {
  191. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  192. }
  193. }
  194. }
  195. //DISPLAY REFRESH
  196. SendCommand(0x22);
  197. SendData(0xF7);
  198. SendCommand(0x20);
  199. WaitUntilIdle();
  200. }
  201. void Epd::DisplayPartBaseImage(const unsigned char* frame_buffer)
  202. {
  203. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  204. int h = EPD_HEIGHT;
  205. if (frame_buffer != NULL) {
  206. SendCommand(0x24);
  207. for (int j = 0; j < h; j++) {
  208. for (int i = 0; i < w; i++) {
  209. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  210. }
  211. }
  212. SendCommand(0x26);
  213. for (int j = 0; j < h; j++) {
  214. for (int i = 0; i < w; i++) {
  215. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  216. }
  217. }
  218. }
  219. //DISPLAY REFRESH
  220. SendCommand(0x22);
  221. SendData(0xFF);
  222. SendCommand(0x20);
  223. WaitUntilIdle();
  224. }
  225. void Epd::DisplayPartBaseWhiteImage(void)
  226. {
  227. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  228. int h = EPD_HEIGHT;
  229. SendCommand(0x24);
  230. for (int j = 0; j < h; j++) {
  231. for (int i = 0; i < w; i++) {
  232. SendData(0xff);
  233. }
  234. }
  235. SendCommand(0x26);
  236. for (int j = 0; j < h; j++) {
  237. for (int i = 0; i < w; i++) {
  238. SendData(0xff);
  239. }
  240. }
  241. //DISPLAY REFRESH
  242. SendCommand(0x22);
  243. SendData(0xFF);
  244. SendCommand(0x20);
  245. WaitUntilIdle();
  246. }
  247. void Epd::DisplayPart(const unsigned char* frame_buffer)
  248. {
  249. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  250. int h = EPD_HEIGHT;
  251. if (frame_buffer != NULL) {
  252. SendCommand(0x24);
  253. for (int j = 0; j < h; j++) {
  254. for (int i = 0; i < w; i++) {
  255. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  256. }
  257. }
  258. }
  259. //DISPLAY REFRESH
  260. SendCommand(0x22);
  261. SendData(0xFF);
  262. SendCommand(0x20);
  263. WaitUntilIdle();
  264. }
  265. /**
  266. * @brief: private function to specify the memory area for data R/W
  267. */
  268. void Epd::SetMemoryArea(int x_start, int y_start, int x_end, int y_end)
  269. {
  270. SendCommand(0x44);
  271. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  272. SendData((x_start >> 3) & 0xFF);
  273. SendData((x_end >> 3) & 0xFF);
  274. SendCommand(0x45);
  275. SendData(y_start & 0xFF);
  276. SendData((y_start >> 8) & 0xFF);
  277. SendData(y_end & 0xFF);
  278. SendData((y_end >> 8) & 0xFF);
  279. }
  280. /**
  281. * @brief: private function to specify the start point for data R/W
  282. */
  283. void Epd::SetMemoryPointer(int x, int y)
  284. {
  285. SendCommand(0x4e);
  286. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  287. SendData((x >> 3) & 0xFF);
  288. SendCommand(0x4F);
  289. SendData(y & 0xFF);
  290. SendData((y >> 8) & 0xFF);
  291. WaitUntilIdle();
  292. }
  293. /**
  294. * @brief: update the display
  295. * there are 2 memory areas embedded in the e-paper display
  296. * but once this function is called,
  297. * the the next action of SetFrameMemory or ClearFrame will
  298. * set the other memory area.
  299. */
  300. void Epd::DisplayFrame(void)
  301. {
  302. //DISPLAY REFRESH
  303. SendCommand(0x22);
  304. SendData(0xF7);
  305. SendCommand(0x20);
  306. WaitUntilIdle();
  307. }
  308. void Epd::DisplayPartFrame(void)
  309. {
  310. SendCommand(0x22);
  311. SendData(0xFF);
  312. SendCommand(0x20);
  313. WaitUntilIdle();
  314. }
  315. void Epd::SetFrameMemory(
  316. const unsigned char* image_buffer,
  317. int x,
  318. int y,
  319. int image_width,
  320. int image_height
  321. )
  322. {
  323. int x_end;
  324. int y_end;
  325. if (
  326. image_buffer == NULL ||
  327. x < 0 || image_width < 0 ||
  328. y < 0 || image_height < 0
  329. ) {
  330. return;
  331. }
  332. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  333. x &= 0xF8;
  334. image_width &= 0xF8;
  335. if (x + image_width >= this->width) {
  336. x_end = this->width - 1;
  337. } else {
  338. x_end = x + image_width - 1;
  339. }
  340. if (y + image_height >= this->height) {
  341. y_end = this->height - 1;
  342. } else {
  343. y_end = y + image_height - 1;
  344. }
  345. SetMemoryArea(x, y, x_end, y_end);
  346. SetMemoryPointer(x, y);
  347. SendCommand(0x24);
  348. /* send the image data */
  349. for (int j = 0; j < y_end - y + 1; j++) {
  350. for (int i = 0; i < (x_end - x + 1) / 8; i++) {
  351. SendData(image_buffer[i + j * (image_width / 8)]);
  352. }
  353. }
  354. }
  355. /**
  356. * @brief: After this command is transmitted, the chip would enter the
  357. * deep-sleep mode to save power.
  358. * The deep sleep mode would return to standby by hardware reset.
  359. * The only one parameter is a check code, the command would be
  360. * executed if check code = 0xA5.
  361. * You can use Epd::Init() to awaken
  362. */
  363. void Epd::Sleep()
  364. {
  365. SendCommand(0x10); //enter deep sleep
  366. SendData(0x01);
  367. DelayMs(200);
  368. DigitalWrite(reset_pin, LOW);
  369. }
  370. /* END OF FILE */