epd4in2b_V2.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /**
  2. * @filename : epd4in2b_V2.cpp
  3. * @brief : Implements for Dual-color e-paper library
  4. * @author : Yehui from Waveshare
  5. *
  6. * Copyright (C) Waveshare Nov 25 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 "epd4in2b_V2.h"
  28. Epd::~Epd() {
  29. };
  30. Epd::Epd() {
  31. flag=0;
  32. reset_pin = RST_PIN;
  33. dc_pin = DC_PIN;
  34. cs_pin = CS_PIN;
  35. busy_pin = BUSY_PIN;
  36. width = EPD_WIDTH;
  37. height = EPD_HEIGHT;
  38. };
  39. int Epd::Init_old(void) {
  40. Reset();
  41. SendCommand(0x04);
  42. ReadBusy();
  43. SendCommand(0x00);
  44. SendData(0x0F); // LUT from OTP
  45. // /* EPD hardware init end */
  46. return 0;
  47. }
  48. int Epd::Init_new(void) {
  49. Reset();
  50. ReadBusy();
  51. SendCommand(0x12);
  52. ReadBusy();
  53. SendCommand(0x3C); //BorderWavefrom
  54. SendData(0x05);
  55. SendCommand(0x18); //Read built-in temperature sensor
  56. SendData(0x80);
  57. SendCommand(0x11); //data entry mode
  58. SendData(0x03);
  59. SendCommand(0x44); //set Ram-X address start/end position
  60. SendData(0x00);
  61. SendData(width/8-1);
  62. SendCommand(0x45); //set Ram-Y address start/end position
  63. SendData(0x00);
  64. SendData(0x00);
  65. SendData((height-1)%256);
  66. SendData((height-1)/256);
  67. SendCommand(0x4E); // set RAM x address count to 0;
  68. SendData(0x00);
  69. SendCommand(0x4F); // set RAM y address count to 0X199;
  70. SendData(0x00);
  71. SendData(0x00);
  72. ReadBusy();
  73. return 0;
  74. }
  75. int Epd::Init(void) {
  76. unsigned char i=0;
  77. /* this calls the peripheral hardware interface, see epdif */
  78. if (IfInit() != 0) {
  79. return -1;
  80. }
  81. /* EPD hardware init start */
  82. Reset();
  83. EPD_GPIO_Init();
  84. digitalWrite(dc_pin, 0);
  85. EPD_SendData(0x2F);
  86. DelayMs(50);
  87. digitalWrite(dc_pin, 1);
  88. i = EPD_ReadData();
  89. printf("%02x\n",i);
  90. EPD_SPI_Init();
  91. if(i == 0x01)
  92. {
  93. flag = 0;
  94. Init_new();
  95. }
  96. else
  97. {
  98. flag = 1;
  99. Init_old();
  100. }
  101. // /* EPD hardware init end */
  102. return 0;
  103. }
  104. /**
  105. * @brief: basic function for sending commands
  106. */
  107. void Epd::SendCommand(unsigned char command) {
  108. DigitalWrite(dc_pin, LOW);
  109. SpiTransfer(command);
  110. }
  111. /**
  112. * @brief: basic function for sending data
  113. */
  114. void Epd::SendData(unsigned char data) {
  115. DigitalWrite(dc_pin, HIGH);
  116. SpiTransfer(data);
  117. }
  118. /**
  119. * @brief: Wait until the busy_pin goes HIGH
  120. */
  121. void Epd::ReadBusy(void) {
  122. if(flag == 0)
  123. {
  124. while(DigitalRead(busy_pin) == 1) {
  125. DelayMs(100);
  126. }
  127. }
  128. else
  129. {
  130. while(DigitalRead(busy_pin) == 0) {
  131. DelayMs(100);
  132. }
  133. }
  134. }
  135. /**
  136. * @brief: module reset.
  137. * often used to awaken the module in deep sleep,
  138. * see Epd::Sleep();
  139. */
  140. void Epd::Reset(void) {
  141. DigitalWrite(reset_pin, HIGH);
  142. DelayMs(200);
  143. DigitalWrite(reset_pin, LOW);
  144. DelayMs(2);
  145. DigitalWrite(reset_pin, HIGH);
  146. DelayMs(200);
  147. }
  148. /**
  149. * @brief: transmit partial data to the black part of SRAM
  150. */
  151. void Epd::Display_Window_Black(const UBYTE *image, UBYTE count) {
  152. UBYTE k;
  153. if(count == 0 && flag == 0)
  154. SendCommand(0x24);
  155. else if(count == 0)
  156. SendCommand(0x10);
  157. for (UWORD j = 0; j < 100; j++) {
  158. for (UWORD i = 0; i < 50; i++) {
  159. if(i<16)
  160. {
  161. SendData(image[i + (j*16)]);
  162. }
  163. else
  164. {
  165. SendData(0xFF);
  166. }
  167. }
  168. }
  169. }
  170. /**
  171. * @brief: transmit partial data to the red part of SRAM
  172. */
  173. void Epd::Display_Window_Red_new(const UBYTE *image, UBYTE count) {
  174. UBYTE k;
  175. if(count == 0)
  176. SendCommand(0x26);
  177. for (UWORD j = 0; j < 100; j++) {
  178. for (UWORD i = 0; i < 50; i++) {
  179. if(i<16)
  180. {
  181. SendData(~image[i + (j*16)]);
  182. }
  183. else
  184. {
  185. SendData(0x00);
  186. }
  187. }
  188. }
  189. }
  190. void Epd::Display_Window_Red_old(const UBYTE *image, UBYTE count) {
  191. UBYTE k;
  192. if(count == 0)
  193. SendCommand(0x13);
  194. for (UWORD j = 0; j < 100; j++) {
  195. for (UWORD i = 0; i < 50; i++) {
  196. if(i<16)
  197. {
  198. SendData(image[i + (j*16)]);
  199. }
  200. else
  201. {
  202. SendData(0xFF);
  203. }
  204. }
  205. }
  206. }
  207. void Epd::Display_Window_Red(const UBYTE *image, UBYTE count) {
  208. if(flag == 0)
  209. Display_Window_Red_new(image, count);
  210. else
  211. Display_Window_Red_old(image, count);
  212. }
  213. void Epd::DisplayFrame(void) {
  214. if(flag == 0)
  215. {
  216. SendCommand(0x22);
  217. SendData(0xF7);
  218. SendCommand(0x20);
  219. ReadBusy();
  220. }
  221. else
  222. {
  223. SendCommand(0x12);
  224. DelayMs(100);
  225. ReadBusy();
  226. }
  227. }
  228. /**
  229. * @brief: refresh and displays the frame
  230. */
  231. void Epd::Display_old(const UBYTE *blackimage, const UBYTE *ryimage) {
  232. UBYTE k;
  233. SendCommand(0x10);
  234. for (UWORD j = 0; j < height; j++) {
  235. for (UWORD i = 0; i < width/8; i++) {
  236. SendData(pgm_read_byte(&blackimage[i + (j*width/8)]));
  237. }
  238. }
  239. SendCommand(0x13);
  240. for (UWORD j = 0; j < height; j++) {
  241. for (UWORD i = 0; i < width/8; i++) {
  242. SendData(pgm_read_byte(&ryimage[i + (j*width/8)]));
  243. }
  244. }
  245. SendCommand(0x12);
  246. DelayMs(100);
  247. ReadBusy();
  248. }
  249. void Epd::Display_new(const UBYTE *blackimage, const UBYTE *ryimage) {
  250. UBYTE k;
  251. SendCommand(0x24);
  252. for (UWORD j = 0; j < height; j++) {
  253. for (UWORD i = 0; i < width/8; i++) {
  254. SendData(pgm_read_byte(&blackimage[i + (j*width/8)]));
  255. }
  256. }
  257. SendCommand(0x26);
  258. for (UWORD j = 0; j < height; j++) {
  259. for (UWORD i = 0; i < width/8; i++) {
  260. k = pgm_read_byte(&ryimage[i + (j*width/8)]);
  261. SendData(~k);
  262. }
  263. }
  264. SendCommand(0x22);
  265. SendData(0xF7);
  266. SendCommand(0x20);
  267. ReadBusy();
  268. }
  269. void Epd::Display(const UBYTE *blackimage, const UBYTE *ryimage) {
  270. if(flag == 0)
  271. Display_new(blackimage, ryimage);
  272. else
  273. Display_old(blackimage, ryimage);
  274. }
  275. /**
  276. * @brief: clear the frame data from the SRAM, this won't refresh the display
  277. */
  278. void Epd::Clear_new() {
  279. //send black data
  280. SendCommand(0x24);
  281. for (UWORD j = 0; j < height; j++) {
  282. for (UWORD i = 0; i < width/8; i++) {
  283. SendData(0xff);
  284. }
  285. }
  286. //send red data
  287. SendCommand(0x26);
  288. for (UWORD j = 0; j < height; j++) {
  289. for (UWORD i = 0; i < width/8; i++) {
  290. SendData(0x00);
  291. }
  292. }
  293. SendCommand(0x22);
  294. SendData(0xF7);
  295. SendCommand(0x20);
  296. ReadBusy();
  297. }
  298. void Epd::Clear_old() {
  299. //send black data
  300. SendCommand(0x10);
  301. for (UWORD j = 0; j < height; j++) {
  302. for (UWORD i = 0; i < width/8; i++) {
  303. SendData(0xff);
  304. }
  305. }
  306. //send red data
  307. SendCommand(0x13);
  308. for (UWORD j = 0; j < height; j++) {
  309. for (UWORD i = 0; i < width/8; i++) {
  310. SendData(0xff);
  311. }
  312. }
  313. SendCommand(0x12);
  314. DelayMs(100);
  315. ReadBusy();
  316. }
  317. void Epd::Clear() {
  318. if(flag == 0)
  319. Clear_new();
  320. else
  321. Clear_old();
  322. }
  323. /**
  324. * @brief: After this command is transmitted, the chip would enter the deep-sleep mode to save power.
  325. * The deep sleep mode would return to standby by hardware reset. The only one parameter is a
  326. * check code, the command would be executed if check code = 0xA5.
  327. * You can use Epd::Reset() to awaken and use Epd::Init() to initialize.
  328. */
  329. void Epd::Sleep_new(void) {
  330. SendCommand(0x10);
  331. SendData(0x01);
  332. }
  333. void Epd::Sleep_old() {
  334. SendCommand(0X50);
  335. SendData(0xf7);
  336. SendCommand(0x02);
  337. ReadBusy();
  338. SendCommand(0x07);
  339. SendData(0XA5);
  340. }
  341. void Epd::Sleep(void) {
  342. if(flag == 0)
  343. Sleep_new();
  344. else
  345. Sleep_old();
  346. }
  347. /* END OF FILE */