epd2in13_V4.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*****************************************************************************
  2. * | File : epd2in13_V4.cpp
  3. * | Author : Waveshare team
  4. * | Function : 2.13inch e-paper V4
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2023-6-25
  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 "epd2in13_V4.h"
  32. Epd::~Epd()
  33. {
  34. };
  35. /******************************************************************************
  36. function : Pin definition
  37. parameter:
  38. ******************************************************************************/
  39. Epd::Epd()
  40. {
  41. reset_pin = RST_PIN;
  42. dc_pin = DC_PIN;
  43. cs_pin = CS_PIN;
  44. busy_pin = BUSY_PIN;
  45. width = EPD_WIDTH;
  46. height = EPD_HEIGHT;
  47. bufwidth = 128/8; //16
  48. bufheight = 63;
  49. };
  50. /******************************************************************************
  51. function : send command
  52. parameter:
  53. command : Command register
  54. ******************************************************************************/
  55. void Epd::SendCommand(unsigned char command)
  56. {
  57. DigitalWrite(dc_pin, LOW);
  58. SpiTransfer(command);
  59. }
  60. /******************************************************************************
  61. function : send data
  62. parameter:
  63. Data : Write data
  64. ******************************************************************************/
  65. void Epd::SendData(unsigned char data)
  66. {
  67. DigitalWrite(dc_pin, HIGH);
  68. SpiTransfer(data);
  69. }
  70. /******************************************************************************
  71. function : Wait until the busy_pin goes LOW
  72. parameter:
  73. ******************************************************************************/
  74. void Epd::WaitUntilIdle(void)
  75. {
  76. while(1) { //LOW: idle, HIGH: busy
  77. if(DigitalRead(busy_pin) == 0)
  78. break;
  79. DelayMs(10);
  80. }
  81. }
  82. /******************************************************************************
  83. function : Setting the display window
  84. parameter:
  85. Xstart : X-axis starting position
  86. Ystart : Y-axis starting position
  87. Xend : End position of X-axis
  88. Yend : End position of Y-axis
  89. ******************************************************************************/
  90. void Epd::SetWindows(unsigned char Xstart, unsigned char Ystart, unsigned char Xend, unsigned char Yend)
  91. {
  92. SendCommand(0x44); // SET_RAM_X_ADDRESS_START_END_POSITION
  93. SendData((Xstart>>3) & 0xFF);
  94. SendData((Xend>>3) & 0xFF);
  95. SendCommand(0x45); // SET_RAM_Y_ADDRESS_START_END_POSITION
  96. SendData(Ystart & 0xFF);
  97. SendData((Ystart >> 8) & 0xFF);
  98. SendData(Yend & 0xFF);
  99. SendData((Yend >> 8) & 0xFF);
  100. }
  101. /******************************************************************************
  102. function : Set Cursor
  103. parameter:
  104. Xstart : X-axis starting position
  105. Ystart : Y-axis starting position
  106. ******************************************************************************/
  107. void Epd::SetCursor(unsigned char Xstart, unsigned char Ystart)
  108. {
  109. SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  110. SendData(Xstart & 0xFF);
  111. SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  112. SendData(Ystart & 0xFF);
  113. SendData((Ystart >> 8) & 0xFF);
  114. }
  115. /******************************************************************************
  116. function : Initialize the e-Paper register
  117. parameter:
  118. Mode : Mode selection
  119. ******************************************************************************/
  120. int Epd::Init(char Mode)
  121. {
  122. /* this calls the peripheral hardware interface, see epdif */
  123. if (IfInit() != 0) {
  124. return -1;
  125. }
  126. Reset();
  127. int count;
  128. if(Mode == FULL) {
  129. WaitUntilIdle();
  130. SendCommand(0x12); // soft reset
  131. WaitUntilIdle();
  132. SendCommand(0x01); //Driver output control
  133. SendData(0xF9);
  134. SendData(0x00);
  135. SendData(0x00);
  136. SendCommand(0x11); //data entry mode
  137. SendData(0x03);
  138. SetWindows(0, 0, EPD_WIDTH-1, EPD_HEIGHT-1);
  139. SetCursor(0, 0);
  140. SendCommand(0x3C); //BorderWavefrom
  141. SendData(0x05);
  142. SendCommand(0x21); // Display update control
  143. SendData(0x00);
  144. SendData(0x80);
  145. SendCommand(0x18); //Read built-in temperature sensor
  146. SendData(0x80);
  147. WaitUntilIdle();
  148. }
  149. else if(Mode == FAST) {
  150. WaitUntilIdle();
  151. SendCommand(0x12); // soft reset
  152. WaitUntilIdle();
  153. SendCommand(0x18); // Read built-in temperature sensor
  154. SendData(0x80);
  155. SendCommand(0x11); // data entry mode
  156. SendData(0x03);
  157. SetWindows(0, 0, EPD_WIDTH-1, EPD_HEIGHT-1);
  158. SetCursor(0, 0);
  159. SendCommand(0x22); // Load temperature value
  160. SendData(0xB1);
  161. SendCommand(0x20);
  162. WaitUntilIdle();
  163. SendCommand(0x1A); // Write to temperature register
  164. SendData(0x64);
  165. SendData(0x00);
  166. SendCommand(0x22); // Load temperature value
  167. SendData(0x91);
  168. SendCommand(0x20);
  169. WaitUntilIdle();
  170. }
  171. else if(Mode == PART) {
  172. DigitalWrite(reset_pin, LOW); //module reset
  173. DelayMs(1);
  174. DigitalWrite(reset_pin, HIGH);
  175. SendCommand(0x3C); //BorderWavefrom
  176. SendData(0x80);
  177. SendCommand(0x01); //Driver output control
  178. SendData(0xF9);
  179. SendData(0x00);
  180. SendData(0x00);
  181. SendCommand(0x11); // data entry mode
  182. SendData(0x03);
  183. SetWindows(0, 0, EPD_WIDTH-1, EPD_HEIGHT-1);
  184. SetCursor(0, 0);
  185. } else {
  186. return -1;
  187. }
  188. return 0;
  189. }
  190. /******************************************************************************
  191. function : Software reset
  192. parameter:
  193. ******************************************************************************/
  194. void Epd::Reset(void)
  195. {
  196. DigitalWrite(reset_pin, HIGH);
  197. DelayMs(20);
  198. DigitalWrite(reset_pin, LOW); //module reset
  199. DelayMs(2);
  200. DigitalWrite(reset_pin, HIGH);
  201. DelayMs(20);
  202. this->count = 0;
  203. }
  204. /******************************************************************************
  205. function : Clear screen
  206. parameter:
  207. ******************************************************************************/
  208. void Epd::Clear(void)
  209. {
  210. int w, h;
  211. w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  212. h = EPD_HEIGHT;
  213. SendCommand(0x24);
  214. for (int j = 0; j < h; j++) {
  215. for (int i = 0; i < w; i++) {
  216. SendData(0xff);
  217. }
  218. }
  219. //DISPLAY REFRESH
  220. SendCommand(0x22);
  221. SendData(0xf7);
  222. SendCommand(0x20);
  223. WaitUntilIdle();
  224. }
  225. /******************************************************************************
  226. function : Sends the image buffer in RAM to e-Paper and displays
  227. parameter:
  228. frame_buffer : Image data
  229. ******************************************************************************/
  230. void Epd::Display(const unsigned char* frame_buffer)
  231. {
  232. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  233. int h = EPD_HEIGHT;
  234. if (frame_buffer != NULL) {
  235. SendCommand(0x24);
  236. for (int j = 0; j < h; j++) {
  237. for (int i = 0; i < w; i++) {
  238. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  239. }
  240. }
  241. }
  242. //DISPLAY REFRESH
  243. SendCommand(0x22);
  244. SendData(0xf7);
  245. SendCommand(0x20);
  246. WaitUntilIdle();
  247. }
  248. void Epd::Display1(const unsigned char* frame_buffer) {
  249. if(this->count == 0){
  250. SendCommand(0x24);
  251. this->count++;
  252. }else if(this->count > 0 && this->count < 4 ){
  253. this->count++;
  254. }
  255. for(int i = 0; i < this->bufwidth * this->bufheight; i++){
  256. SendData(frame_buffer[i]);
  257. }
  258. if(this->count == 4){
  259. SendCommand(0x22);
  260. SendData(0xf7);
  261. SendCommand(0x20);
  262. WaitUntilIdle();
  263. this->count = 0;
  264. }
  265. }
  266. /******************************************************************************
  267. function : Sends the image buffer in RAM to e-Paper and fast displays
  268. parameter:
  269. frame_buffer : Image data
  270. ******************************************************************************/
  271. void Epd::Display_Fast(const unsigned char* frame_buffer)
  272. {
  273. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  274. int h = EPD_HEIGHT;
  275. if (frame_buffer != NULL) {
  276. SendCommand(0x24);
  277. for (int j = 0; j < h; j++) {
  278. for (int i = 0; i < w; i++) {
  279. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  280. }
  281. }
  282. }
  283. //DISPLAY REFRESH
  284. SendCommand(0x22);
  285. SendData(0xC7);
  286. SendCommand(0x20);
  287. WaitUntilIdle();
  288. }
  289. /******************************************************************************
  290. function : Refresh a base image
  291. parameter:
  292. frame_buffer : Image data
  293. ******************************************************************************/
  294. void Epd::DisplayPartBaseImage(const unsigned char* frame_buffer)
  295. {
  296. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  297. int h = EPD_HEIGHT;
  298. if (frame_buffer != NULL) {
  299. SendCommand(0x24);
  300. for (int j = 0; j < h; j++) {
  301. for (int i = 0; i < w; i++) {
  302. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  303. }
  304. }
  305. SendCommand(0x26);
  306. for (int j = 0; j < h; j++) {
  307. for (int i = 0; i < w; i++) {
  308. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  309. }
  310. }
  311. }
  312. //DISPLAY REFRESH
  313. SendCommand(0x22);
  314. SendData(0xf7);
  315. SendCommand(0x20);
  316. WaitUntilIdle();
  317. }
  318. /******************************************************************************
  319. function : Sends the image buffer in RAM to e-Paper and partial refresh
  320. parameter:
  321. frame_buffer : Image data
  322. ******************************************************************************/
  323. void Epd::DisplayPart(const unsigned char* frame_buffer)
  324. {
  325. int w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  326. int h = EPD_HEIGHT;
  327. if (frame_buffer != NULL) {
  328. SendCommand(0x24);
  329. for (int j = 0; j < h; j++) {
  330. for (int i = 0; i < w; i++) {
  331. SendData(pgm_read_byte(&frame_buffer[i + j * w]));
  332. }
  333. }
  334. }
  335. //DISPLAY REFRESH
  336. SendCommand(0x22);
  337. SendData(0xff);
  338. SendCommand(0x20);
  339. WaitUntilIdle();
  340. }
  341. /******************************************************************************
  342. function : Clear screen
  343. parameter:
  344. ******************************************************************************/
  345. void Epd::ClearPart(void)
  346. {
  347. int w, h;
  348. w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);
  349. h = EPD_HEIGHT;
  350. SendCommand(0x24);
  351. for (int j = 0; j < h; j++) {
  352. for (int i = 0; i < w; i++) {
  353. SendData(0xff);
  354. }
  355. }
  356. //DISPLAY REFRESH
  357. SendCommand(0x22);
  358. SendData(0x0f);
  359. SendCommand(0x20);
  360. WaitUntilIdle();
  361. }
  362. /******************************************************************************
  363. function : Enter sleep mode
  364. parameter:
  365. ******************************************************************************/
  366. void Epd::Sleep()
  367. {
  368. SendCommand(0x10); //enter deep sleep
  369. SendData(0x01);
  370. DelayMs(200);
  371. DigitalWrite(reset_pin, LOW);
  372. }
  373. /* END OF FILE */