epd2in9_V2.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /**
  2. * @filename : epd2in9_V2.cpp
  3. * @brief : Implements for e-paper library
  4. * @author :
  5. *
  6. * Copyright (C) Waveshare Nov 9 2020
  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 "epd2in9_V2.h"
  28. unsigned char WF_PARTIAL_2IN9[159] =
  29. {
  30. 0x0,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  31. 0x80,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  32. 0x40,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  33. 0x0,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  34. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  35. 0x0A,0x0,0x0,0x0,0x0,0x0,0x2,
  36. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  37. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  38. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  39. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  40. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  41. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  42. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  43. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  44. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  45. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  46. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  47. 0x22,0x22,0x22,0x22,0x22,0x22,0x0,0x0,0x0,
  48. 0x22,0x17,0x41,0xB0,0x32,0x36,
  49. };
  50. Epd::~Epd() {
  51. };
  52. Epd::Epd() {
  53. reset_pin = RST_PIN;
  54. dc_pin = DC_PIN;
  55. cs_pin = CS_PIN;
  56. busy_pin = BUSY_PIN;
  57. width = EPD_WIDTH;
  58. height = EPD_HEIGHT;
  59. };
  60. int Epd::Init() {
  61. /* this calls the peripheral hardware interface, see epdif */
  62. if (IfInit() != 0) {
  63. return -1;
  64. }
  65. Reset();
  66. /* EPD hardware init start */
  67. WaitUntilIdle();
  68. SendCommand(0x12); //SWRESET
  69. WaitUntilIdle();
  70. SendCommand(0x01); //Driver output control
  71. SendData(0x27);
  72. SendData(0x01);
  73. SendData(0x00);
  74. SendCommand(0x11); //data entry mode
  75. SendData(0x03);
  76. SetMemoryArea(0, 0, width-1, height-1);
  77. SendCommand(0x21); // Display update control
  78. SendData(0x00);
  79. SendData(0x80);
  80. SetMemoryPointer(0, 0);
  81. WaitUntilIdle();
  82. /* EPD hardware init end */
  83. return 0;
  84. }
  85. /**
  86. * @brief: basic function for sending commands
  87. */
  88. void Epd::SendCommand(unsigned char command) {
  89. DigitalWrite(dc_pin, LOW);
  90. DigitalWrite(cs_pin, LOW);
  91. SpiTransfer(command);
  92. DigitalWrite(cs_pin, HIGH);
  93. }
  94. /**
  95. * @brief: basic function for sending data
  96. */
  97. void Epd::SendData(unsigned char data) {
  98. DigitalWrite(dc_pin, HIGH);
  99. DigitalWrite(cs_pin, LOW);
  100. SpiTransfer(data);
  101. DigitalWrite(cs_pin, HIGH);
  102. }
  103. /**
  104. * @brief: Wait until the busy_pin goes LOW
  105. */
  106. void Epd::WaitUntilIdle(void) {
  107. while(1) { //=1 BUSY
  108. if(DigitalRead(busy_pin)==LOW)
  109. break;
  110. DelayMs(50);
  111. }
  112. DelayMs(50);
  113. }
  114. /**
  115. * @brief: module reset.
  116. * often used to awaken the module in deep sleep,
  117. * see Epd::Sleep();
  118. */
  119. void Epd::Reset(void) {
  120. DigitalWrite(reset_pin, HIGH);
  121. DelayMs(200);
  122. DigitalWrite(reset_pin, LOW); //module reset
  123. DelayMs(5);
  124. DigitalWrite(reset_pin, HIGH);
  125. DelayMs(200);
  126. }
  127. /**
  128. * @brief: put an image buffer to the frame memory.
  129. * this won't update the display.
  130. */
  131. void Epd::SetFrameMemory(
  132. const unsigned char* image_buffer,
  133. int x,
  134. int y,
  135. int image_width,
  136. int image_height
  137. ) {
  138. int x_end;
  139. int y_end;
  140. if (
  141. image_buffer == NULL ||
  142. x < 0 || image_width < 0 ||
  143. y < 0 || image_height < 0
  144. ) {
  145. return;
  146. }
  147. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  148. x &= 0xF8;
  149. image_width &= 0xF8;
  150. if (x + image_width >= this->width) {
  151. x_end = this->width - 1;
  152. } else {
  153. x_end = x + image_width - 1;
  154. }
  155. if (y + image_height >= this->height) {
  156. y_end = this->height - 1;
  157. } else {
  158. y_end = y + image_height - 1;
  159. }
  160. SetMemoryArea(x, y, x_end, y_end);
  161. SetMemoryPointer(x, y);
  162. SendCommand(0x24);
  163. /* send the image data */
  164. for (int j = 0; j < y_end - y + 1; j++) {
  165. for (int i = 0; i < (x_end - x + 1) / 8; i++) {
  166. SendData(image_buffer[i + j * (image_width / 8)]);
  167. }
  168. }
  169. }
  170. void Epd::SetFrameMemory_Partial(
  171. const unsigned char* image_buffer,
  172. int x,
  173. int y,
  174. int image_width,
  175. int image_height
  176. ) {
  177. int x_end;
  178. int y_end;
  179. if (
  180. image_buffer == NULL ||
  181. x < 0 || image_width < 0 ||
  182. y < 0 || image_height < 0
  183. ) {
  184. return;
  185. }
  186. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  187. x &= 0xF8;
  188. image_width &= 0xF8;
  189. if (x + image_width >= this->width) {
  190. x_end = this->width - 1;
  191. } else {
  192. x_end = x + image_width - 1;
  193. }
  194. if (y + image_height >= this->height) {
  195. y_end = this->height - 1;
  196. } else {
  197. y_end = y + image_height - 1;
  198. }
  199. DigitalWrite(reset_pin, LOW);
  200. DelayMs(5);
  201. DigitalWrite(reset_pin, HIGH);
  202. DelayMs(10);
  203. SetLut();
  204. SendCommand(0x37);
  205. SendData(0x00);
  206. SendData(0x00);
  207. SendData(0x00);
  208. SendData(0x00);
  209. SendData(0x00);
  210. SendData(0x40);
  211. SendData(0x00);
  212. SendData(0x00);
  213. SendData(0x00);
  214. SendData(0x00);
  215. SendCommand(0x3C); //BorderWavefrom
  216. SendData(0x80);
  217. SendCommand(0x22);
  218. SendData(0xC0);
  219. SendCommand(0x20);
  220. WaitUntilIdle();
  221. SetMemoryArea(x, y, x_end, y_end);
  222. SetMemoryPointer(x, y);
  223. SendCommand(0x24);
  224. /* send the image data */
  225. for (int j = 0; j < y_end - y + 1; j++) {
  226. for (int i = 0; i < (x_end - x + 1) / 8; i++) {
  227. SendData(image_buffer[i + j * (image_width / 8)]);
  228. }
  229. }
  230. }
  231. /**
  232. * @brief: put an image buffer to the frame memory.
  233. * this won't update the display.
  234. *
  235. * Question: When do you use this function instead of
  236. * void SetFrameMemory(
  237. * const unsigned char* image_buffer,
  238. * int x,
  239. * int y,
  240. * int image_width,
  241. * int image_height
  242. * );
  243. * Answer: SetFrameMemory with parameters only reads image data
  244. * from the RAM but not from the flash in AVR chips (for AVR chips,
  245. * you have to use the function pgm_read_byte to read buffers
  246. * from the flash).
  247. */
  248. void Epd::SetFrameMemory(const unsigned char* image_buffer) {
  249. SetMemoryArea(0, 0, this->width - 1, this->height - 1);
  250. SetMemoryPointer(0, 0);
  251. SendCommand(0x24);
  252. /* send the image data */
  253. for (int i = 0; i < this->width / 8 * this->height; i++) {
  254. SendData(pgm_read_byte(&image_buffer[i]));
  255. }
  256. }
  257. void Epd::SetFrameMemory_Base(const unsigned char* image_buffer) {
  258. SetMemoryArea(0, 0, this->width - 1, this->height - 1);
  259. SetMemoryPointer(0, 0);
  260. SendCommand(0x24);
  261. /* send the image data */
  262. for (int i = 0; i < this->width / 8 * this->height; i++) {
  263. SendData(pgm_read_byte(&image_buffer[i]));
  264. }
  265. SendCommand(0x26);
  266. /* send the image data */
  267. for (int i = 0; i < this->width / 8 * this->height; i++) {
  268. SendData(pgm_read_byte(&image_buffer[i]));
  269. }
  270. }
  271. /**
  272. * @brief: clear the frame memory with the specified color.
  273. * this won't update the display.
  274. */
  275. void Epd::ClearFrameMemory(unsigned char color) {
  276. SetMemoryArea(0, 0, this->width - 1, this->height - 1);
  277. SetMemoryPointer(0, 0);
  278. SendCommand(0x24);
  279. /* send the color data */
  280. for (int i = 0; i < this->width / 8 * this->height; i++) {
  281. SendData(color);
  282. }
  283. }
  284. /**
  285. * @brief: update the display
  286. * there are 2 memory areas embedded in the e-paper display
  287. * but once this function is called,
  288. * the the next action of SetFrameMemory or ClearFrame will
  289. * set the other memory area.
  290. */
  291. void Epd::DisplayFrame(void) {
  292. SendCommand(0x22);
  293. SendData(0xF7);
  294. SendCommand(0x20);
  295. WaitUntilIdle();
  296. }
  297. void Epd::DisplayFrame_Partial(void) {
  298. SendCommand(0x22);
  299. SendData(0x0F);
  300. SendCommand(0x20);
  301. WaitUntilIdle();
  302. }
  303. void Epd::SetLut(void) {
  304. unsigned char count;
  305. SendCommand(0x32);
  306. for(count=0; count<153; count++)
  307. SendData(WF_PARTIAL_2IN9[count]);
  308. WaitUntilIdle();
  309. }
  310. /**
  311. * @brief: private function to specify the memory area for data R/W
  312. */
  313. void Epd::SetMemoryArea(int x_start, int y_start, int x_end, int y_end) {
  314. SendCommand(0x44);
  315. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  316. SendData((x_start >> 3) & 0xFF);
  317. SendData((x_end >> 3) & 0xFF);
  318. SendCommand(0x45);
  319. SendData(y_start & 0xFF);
  320. SendData((y_start >> 8) & 0xFF);
  321. SendData(y_end & 0xFF);
  322. SendData((y_end >> 8) & 0xFF);
  323. }
  324. /**
  325. * @brief: private function to specify the start point for data R/W
  326. */
  327. void Epd::SetMemoryPointer(int x, int y) {
  328. SendCommand(0x4E);
  329. /* x point must be the multiple of 8 or the last 3 bits will be ignored */
  330. SendData((x >> 3) & 0xFF);
  331. SendCommand(0x4F);
  332. SendData(y & 0xFF);
  333. SendData((y >> 8) & 0xFF);
  334. WaitUntilIdle();
  335. }
  336. /**
  337. * @brief: After this command is transmitted, the chip would enter the
  338. * deep-sleep mode to save power.
  339. * The deep sleep mode would return to standby by hardware reset.
  340. * You can use Epd::Init() to awaken
  341. */
  342. void Epd::Sleep() {
  343. SendCommand(0x10);
  344. SendData(0x01);
  345. WaitUntilIdle();
  346. }
  347. /* END OF FILE */