epd1in54_V2.cpp 9.2 KB

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