epd1in54_V2.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. SendCommand(0x22);
  180. SendData(0xF7);
  181. SendCommand(0x20);
  182. WaitUntilIdle();
  183. }
  184. void Epd::Display(const unsigned char* frame_buffer)
  185. {
  186. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  187. int h = EPD_HEIGHT;
  188. if (frame_buffer != NULL) {
  189. SendCommand(0x24);
  190. for (int j = 0; j < h; j++) {
  191. for (int i = 0; i < w; i++) {
  192. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  193. }
  194. }
  195. }
  196. //DISPLAY REFRESH
  197. SendCommand(0x22);
  198. SendData(0xF7);
  199. SendCommand(0x20);
  200. WaitUntilIdle();
  201. }
  202. void Epd::DisplayPartBaseImage(const unsigned char* frame_buffer)
  203. {
  204. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  205. int h = EPD_HEIGHT;
  206. if (frame_buffer != NULL) {
  207. SendCommand(0x24);
  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. SendCommand(0x26);
  214. for (int j = 0; j < h; j++) {
  215. for (int i = 0; i < w; i++) {
  216. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  217. }
  218. }
  219. }
  220. //DISPLAY REFRESH
  221. SendCommand(0x22);
  222. SendData(0xFF);
  223. SendCommand(0x20);
  224. WaitUntilIdle();
  225. }
  226. void Epd::DisplayPartBaseWhiteImage(void)
  227. {
  228. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  229. int h = EPD_HEIGHT;
  230. SendCommand(0x24);
  231. for (int j = 0; j < h; j++) {
  232. for (int i = 0; i < w; i++) {
  233. SendData(0xff);
  234. }
  235. }
  236. SendCommand(0x26);
  237. for (int j = 0; j < h; j++) {
  238. for (int i = 0; i < w; i++) {
  239. SendData(0xff);
  240. }
  241. }
  242. //DISPLAY REFRESH
  243. SendCommand(0x22);
  244. SendData(0xFF);
  245. SendCommand(0x20);
  246. WaitUntilIdle();
  247. }
  248. void Epd::DisplayPart(const unsigned char* frame_buffer)
  249. {
  250. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  251. int h = EPD_HEIGHT;
  252. if (frame_buffer != NULL) {
  253. SendCommand(0x24);
  254. for (int j = 0; j < h; j++) {
  255. for (int i = 0; i < w; i++) {
  256. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  257. }
  258. }
  259. }
  260. //DISPLAY REFRESH
  261. SendCommand(0x22);
  262. SendData(0xFF);
  263. SendCommand(0x20);
  264. WaitUntilIdle();
  265. }
  266. /**
  267. * @brief: private function to specify the memory area for data R/W
  268. */
  269. void Epd::SetMemoryArea(int x_start, int y_start, int x_end, int y_end)
  270. {
  271. SendCommand(0x44);
  272. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  273. SendData((x_start >> 3) & 0xFF);
  274. SendData((x_end >> 3) & 0xFF);
  275. SendCommand(0x45);
  276. SendData(y_start & 0xFF);
  277. SendData((y_start >> 8) & 0xFF);
  278. SendData(y_end & 0xFF);
  279. SendData((y_end >> 8) & 0xFF);
  280. }
  281. /**
  282. * @brief: private function to specify the start point for data R/W
  283. */
  284. void Epd::SetMemoryPointer(int x, int y)
  285. {
  286. SendCommand(0x4e);
  287. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  288. SendData((x >> 3) & 0xFF);
  289. SendCommand(0x4F);
  290. SendData(y & 0xFF);
  291. SendData((y >> 8) & 0xFF);
  292. WaitUntilIdle();
  293. }
  294. /**
  295. * @brief: update the display
  296. * there are 2 memory areas embedded in the e-paper display
  297. * but once this function is called,
  298. * the the next action of SetFrameMemory or ClearFrame will
  299. * set the other memory area.
  300. */
  301. void Epd::DisplayFrame(void)
  302. {
  303. //DISPLAY REFRESH
  304. SendCommand(0x22);
  305. SendData(0xF7);
  306. SendCommand(0x20);
  307. WaitUntilIdle();
  308. }
  309. void Epd::DisplayPartFrame(void)
  310. {
  311. SendCommand(0x22);
  312. SendData(0xFF);
  313. SendCommand(0x20);
  314. WaitUntilIdle();
  315. }
  316. void Epd::SetFrameMemory(
  317. const unsigned char* image_buffer,
  318. int x,
  319. int y,
  320. int image_width,
  321. int image_height
  322. )
  323. {
  324. int x_end;
  325. int y_end;
  326. if (
  327. image_buffer == NULL ||
  328. x < 0 || image_width < 0 ||
  329. y < 0 || image_height < 0
  330. ) {
  331. return;
  332. }
  333. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  334. x &= 0xF8;
  335. image_width &= 0xF8;
  336. if (x + image_width >= this->width) {
  337. x_end = this->width - 1;
  338. } else {
  339. x_end = x + image_width - 1;
  340. }
  341. if (y + image_height >= this->height) {
  342. y_end = this->height - 1;
  343. } else {
  344. y_end = y + image_height - 1;
  345. }
  346. SetMemoryArea(x, y, x_end, y_end);
  347. SetMemoryPointer(x, y);
  348. SendCommand(0x24);
  349. /* send the image data */
  350. for (int j = 0; j < y_end - y + 1; j++) {
  351. for (int i = 0; i < (x_end - x + 1) / 8; i++) {
  352. SendData(image_buffer[i + j * (image_width / 8)]);
  353. }
  354. }
  355. }
  356. /**
  357. * @brief: After this command is transmitted, the chip would enter the
  358. * deep-sleep mode to save power.
  359. * The deep sleep mode would return to standby by hardware reset.
  360. * The only one parameter is a check code, the command would be
  361. * executed if check code = 0xA5.
  362. * You can use Epd::Init() to awaken
  363. */
  364. void Epd::Sleep()
  365. {
  366. SendCommand(0x10); //enter deep sleep
  367. SendData(0x01);
  368. DelayMs(200);
  369. DigitalWrite(reset_pin, LOW);
  370. }
  371. /* END OF FILE */