2
0

epd1in54_V2.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. // High Direction
  71. int Epd::HDirInit(void)
  72. {
  73. /* this calls the peripheral hardware interface, see epdif */
  74. if (IfInit() != 0) {
  75. return -1;
  76. }
  77. /* EPD hardware init start */
  78. Reset();
  79. WaitUntilIdle();
  80. SendCommand(0x12); //SWRESET
  81. WaitUntilIdle();
  82. SendCommand(0x01); //Driver output control
  83. SendData(0xC7);
  84. SendData(0x00);
  85. SendData(0x01);
  86. SendCommand(0x11); //data entry mode
  87. SendData(0x01);
  88. SendCommand(0x44); //set Ram-X address start/end position
  89. SendData(0x00);
  90. SendData(0x18); //0x0C-->(18+1)*8=200
  91. SendCommand(0x45); //set Ram-Y address start/end position
  92. SendData(0xC7); //0xC7-->(199+1)=200
  93. SendData(0x00);
  94. SendData(0x00);
  95. SendData(0x00);
  96. SendCommand(0x3C); //BorderWavefrom
  97. SendData(0x01);
  98. SendCommand(0x18);
  99. SendData(0x80);
  100. SendCommand(0x22); // //Load Temperature and waveform setting.
  101. SendData(0XB1);
  102. SendCommand(0x20);
  103. SendCommand(0x4E); // set RAM x address count to 0;
  104. SendData(0x00);
  105. SendCommand(0x4F); // set RAM y address count to 0X199;
  106. SendData(0xC7);
  107. SendData(0x00);
  108. WaitUntilIdle();
  109. /* EPD hardware init end */
  110. return 0;
  111. }
  112. // Low Direction
  113. int Epd::LDirInit(void)
  114. {
  115. /* this calls the peripheral hardware interface, see epdif */
  116. if (IfInit() != 0) {
  117. return -1;
  118. }
  119. /* EPD hardware init start */
  120. Reset();
  121. WaitUntilIdle();
  122. SendCommand(0x12); //SWRESET
  123. WaitUntilIdle();
  124. SendCommand(0x01); //Driver output control
  125. SendData(0xC7);
  126. SendData(0x00);
  127. SendData(0x00);
  128. SendCommand(0x11); //data entry mode
  129. SendData(0x03);
  130. SendCommand(0x44);
  131. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  132. SendData((0 >> 3) & 0xFF);
  133. SendData((200 >> 3) & 0xFF);
  134. SendCommand(0x45);
  135. SendData(0 & 0xFF);
  136. SendData((0 >> 8) & 0xFF);
  137. SendData(200 & 0xFF);
  138. SendData((200 >> 8) & 0xFF);
  139. SendCommand(0x3C); //BorderWavefrom
  140. SendData(0x01);
  141. SendCommand(0x18);
  142. SendData(0x80);
  143. SendCommand(0x22); // //Load Temperature and waveform setting.
  144. SendData(0XB1);
  145. SendCommand(0x20);
  146. SendCommand(0x4E); // set RAM x address count to 0;
  147. SendData(0x00);
  148. SendCommand(0x4F); // set RAM y address count to 0X199;
  149. SendData(0xC7);
  150. SendData(0x00);
  151. WaitUntilIdle();
  152. /* EPD hardware init end */
  153. return 0;
  154. }
  155. /**
  156. * @brief: module reset.
  157. * often used to awaken the module in deep sleep,
  158. * see Epd::Sleep();
  159. */
  160. void Epd::Reset(void)
  161. {
  162. DigitalWrite(reset_pin, HIGH);
  163. DelayMs(200);
  164. DigitalWrite(reset_pin, LOW); //module reset
  165. DelayMs(10);
  166. DigitalWrite(reset_pin, HIGH);
  167. DelayMs(200);
  168. }
  169. void Epd::Clear(void)
  170. {
  171. int w, h;
  172. w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  173. h = EPD_HEIGHT;
  174. SendCommand(0x24);
  175. for (int j = 0; j < h; j++) {
  176. for (int i = 0; i < w; i++) {
  177. SendData(0xff);
  178. }
  179. }
  180. //DISPLAY REFRESH
  181. DisplayFrame();
  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. DisplayFrame();
  197. }
  198. void Epd::DisplayPartBaseImage(const unsigned char* frame_buffer)
  199. {
  200. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  201. int h = EPD_HEIGHT;
  202. if (frame_buffer != NULL) {
  203. SendCommand(0x24);
  204. for (int j = 0; j < h; j++) {
  205. for (int i = 0; i < w; i++) {
  206. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  207. }
  208. }
  209. SendCommand(0x26);
  210. for (int j = 0; j < h; j++) {
  211. for (int i = 0; i < w; i++) {
  212. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  213. }
  214. }
  215. }
  216. //DISPLAY REFRESH
  217. DisplayFrame();
  218. }
  219. void Epd::DisplayPartBaseWhiteImage(void)
  220. {
  221. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  222. int h = EPD_HEIGHT;
  223. SendCommand(0x24);
  224. for (int j = 0; j < h; j++) {
  225. for (int i = 0; i < w; i++) {
  226. SendData(0xff);
  227. }
  228. }
  229. SendCommand(0x26);
  230. for (int j = 0; j < h; j++) {
  231. for (int i = 0; i < w; i++) {
  232. SendData(0xff);
  233. }
  234. }
  235. //DISPLAY REFRESH
  236. DisplayFrame();
  237. }
  238. void Epd::DisplayPart(const unsigned char* frame_buffer)
  239. {
  240. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  241. int h = EPD_HEIGHT;
  242. if (frame_buffer != NULL) {
  243. SendCommand(0x24);
  244. for (int j = 0; j < h; j++) {
  245. for (int i = 0; i < w; i++) {
  246. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  247. }
  248. }
  249. }
  250. //DISPLAY REFRESH
  251. DisplayPartFrame();
  252. }
  253. /**
  254. * @brief: private function to specify the memory area for data R/W
  255. */
  256. void Epd::SetMemoryArea(int x_start, int y_start, int x_end, int y_end)
  257. {
  258. SendCommand(0x44);
  259. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  260. SendData((x_start >> 3) & 0xFF);
  261. SendData((x_end >> 3) & 0xFF);
  262. SendCommand(0x45);
  263. SendData(y_start & 0xFF);
  264. SendData((y_start >> 8) & 0xFF);
  265. SendData(y_end & 0xFF);
  266. SendData((y_end >> 8) & 0xFF);
  267. }
  268. /**
  269. * @brief: private function to specify the start point for data R/W
  270. */
  271. void Epd::SetMemoryPointer(int x, int y)
  272. {
  273. SendCommand(0x4e);
  274. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  275. SendData((x >> 3) & 0xFF);
  276. SendCommand(0x4F);
  277. SendData(y & 0xFF);
  278. SendData((y >> 8) & 0xFF);
  279. WaitUntilIdle();
  280. }
  281. /**
  282. * @brief: update the display
  283. * there are 2 memory areas embedded in the e-paper display
  284. * but once this function is called,
  285. * the the next action of SetFrameMemory or ClearFrame will
  286. * set the other memory area.
  287. */
  288. void Epd::DisplayFrame(void)
  289. {
  290. //DISPLAY REFRESH
  291. SendCommand(0x22);
  292. SendData(0xF7);
  293. SendCommand(0x20);
  294. WaitUntilIdle();
  295. }
  296. void Epd::DisplayPartFrame(void)
  297. {
  298. SendCommand(0x22);
  299. SendData(0xFF);
  300. SendCommand(0x20);
  301. WaitUntilIdle();
  302. }
  303. void Epd::SetFrameMemory(
  304. const unsigned char* image_buffer,
  305. int x,
  306. int y,
  307. int image_width,
  308. int image_height
  309. )
  310. {
  311. int x_end;
  312. int y_end;
  313. DigitalWrite(reset_pin, LOW); //module reset
  314. DelayMs(10);
  315. DigitalWrite(reset_pin, HIGH);
  316. DelayMs(10);
  317. SendCommand(0x3c);
  318. SendData(0x80);
  319. if (
  320. image_buffer == NULL ||
  321. x < 0 || image_width < 0 ||
  322. y < 0 || image_height < 0
  323. ) {
  324. return;
  325. }
  326. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  327. x &= 0xF8;
  328. image_width &= 0xF8;
  329. if (x + image_width >= this->width) {
  330. x_end = this->width - 1;
  331. } else {
  332. x_end = x + image_width - 1;
  333. }
  334. if (y + image_height >= this->height) {
  335. y_end = this->height - 1;
  336. } else {
  337. y_end = y + image_height - 1;
  338. }
  339. SetMemoryArea(x, y, x_end, y_end);
  340. SetMemoryPointer(x, y);
  341. SendCommand(0x24);
  342. /* send the image data */
  343. for (int j = 0; j < y_end - y + 1; j++) {
  344. for (int i = 0; i < (x_end - x + 1) / 8; i++) {
  345. SendData(image_buffer[i + j * (image_width / 8)]);
  346. }
  347. }
  348. }
  349. /**
  350. * @brief: After this command is transmitted, the chip would enter the
  351. * deep-sleep mode to save power.
  352. * The deep sleep mode would return to standby by hardware reset.
  353. * The only one parameter is a check code, the command would be
  354. * executed if check code = 0xA5.
  355. * You can use Epd::Init() to awaken
  356. */
  357. void Epd::Sleep()
  358. {
  359. SendCommand(0x10); //enter deep sleep
  360. SendData(0x01);
  361. DelayMs(200);
  362. DigitalWrite(reset_pin, LOW);
  363. }
  364. /* END OF FILE */