epd2in9b_V4.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /**
  2. * @filename : epd2in9b_V4.cpp
  3. * @brief : Implements for e-paper library
  4. * @author : Waveshare
  5. *
  6. * Copyright (C) Waveshare 2023-12-20
  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 "epd2in9b_V4.h"
  28. #include "imagedata.h"
  29. Epd::~Epd() {
  30. };
  31. Epd::Epd() {
  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(void) {
  40. if (IfInit() != 0) {
  41. return -1;
  42. }
  43. Reset();
  44. ReadBusy();
  45. SendCommand(0x12);
  46. ReadBusy();
  47. SendCommand(0x01); //Driver output control
  48. SendData((height-1)%256);
  49. SendData((height-1)/256);
  50. SendData(0x00);
  51. SendCommand(0x11); //data entry mode
  52. SendData(0x03);
  53. SendCommand(0x44); //set Ram-X address start/end position
  54. SendData(0x00);
  55. SendData(width/8-1);
  56. SendCommand(0x45); //set Ram-Y address start/end position
  57. SendData(0x00);
  58. SendData(0x00);
  59. SendData((height-1)%256);
  60. SendData((height-1)/256);
  61. SendCommand(0x3C); //BorderWavefrom
  62. SendData(0x05);
  63. SendCommand(0x21); // Display update control
  64. SendData(0x00);
  65. SendData(0x80);
  66. SendCommand(0x18); //Read built-in temperature sensor
  67. SendData(0x80);
  68. SendCommand(0x4E); // set RAM x address count to 0;
  69. SendData(0x00);
  70. SendCommand(0x4F); // set RAM y address count to 0X199;
  71. SendData(0x00);
  72. SendData(0x00);
  73. ReadBusy();
  74. return 0;
  75. }
  76. int Epd::Init_Fast(void) {
  77. if (IfInit() != 0) {
  78. return -1;
  79. }
  80. Reset();
  81. ReadBusy();
  82. SendCommand(0x12); //SWRESET
  83. ReadBusy();
  84. SendCommand(0x18); //Read built-in temperature sensor
  85. SendData(0x80);
  86. SendCommand(0x22); // Load temperature value
  87. SendData(0xB1);
  88. SendCommand(0x20);
  89. ReadBusy();
  90. SendCommand(0x1A); // Write to temperature register
  91. SendData(0x5a); // 90
  92. SendData(0x00);
  93. SendCommand(0x22); // Load temperature value
  94. SendData(0x91);
  95. SendCommand(0x20);
  96. ReadBusy();
  97. SendCommand(0x01); //Driver output control
  98. SendData((height-1)%256);
  99. SendData((height-1)/256);
  100. SendData(0x00);
  101. SendCommand(0x11); //data entry mode
  102. SendData(0x03);
  103. SendCommand(0x44); //set Ram-X address start/end position
  104. SendData(0x00);
  105. SendData(width/8-1);
  106. SendCommand(0x45); //set Ram-Y address start/end position
  107. SendData(0x00);
  108. SendData(0x00);
  109. SendData((height-1)%256);
  110. SendData((height-1)/256);
  111. SendCommand(0x4E); // set RAM x address count to 0;
  112. SendData(0x00);
  113. SendCommand(0x4F); // set RAM y address count to 0X199;
  114. SendData(0x00);
  115. SendData(0x00);
  116. ReadBusy();
  117. return 0;
  118. }
  119. /**
  120. * @brief: basic function for sending commands
  121. */
  122. void Epd::SendCommand(unsigned char command) {
  123. DigitalWrite(dc_pin, LOW);
  124. SpiTransfer(command);
  125. }
  126. /**
  127. * @brief: basic function for sending data
  128. */
  129. void Epd::SendData(unsigned char data) {
  130. DigitalWrite(dc_pin, HIGH);
  131. SpiTransfer(data);
  132. }
  133. /**
  134. * @brief: Wait until the busy_pin goes HIGH
  135. */
  136. void Epd::ReadBusy(void) {
  137. unsigned char busy;
  138. Serial.print("e-Paper busy \r\n ");
  139. while(1)
  140. {
  141. if(DigitalRead(busy_pin) == 0)
  142. break;
  143. DelayMs(50);
  144. }
  145. Serial.print("e-Paper busy release \r\n ");
  146. DelayMs(200);
  147. }
  148. /**
  149. * @brief: module reset.
  150. * often used to awaken the module in deep sleep,
  151. * see Epd::Sleep();
  152. */
  153. void Epd::Reset(void) {
  154. DigitalWrite(reset_pin, HIGH);
  155. DelayMs(200);
  156. DigitalWrite(reset_pin, LOW); //module reset
  157. DelayMs(5);
  158. DigitalWrite(reset_pin, HIGH);
  159. DelayMs(200);
  160. }
  161. /******************************************************************************
  162. function : Turn On Display
  163. parameter:
  164. ******************************************************************************/
  165. void Epd::TurnOnDisplay(void)
  166. {
  167. SendCommand(0x22); //Display Update Control
  168. SendData(0xF7);
  169. SendCommand(0x20); //Activate Display Update Sequence
  170. ReadBusy();
  171. }
  172. void Epd::TurnOnDisplay_Base(void)
  173. {
  174. SendCommand(0x22); //Display Update Control
  175. SendData(0xF4);
  176. SendCommand(0x20); //Activate Display Update Sequence
  177. ReadBusy();
  178. }
  179. void Epd::TurnOnDisplay_Partial(void)
  180. {
  181. SendCommand(0x22); //Display Update Control
  182. SendData(0x1C);
  183. SendCommand(0x20); //Activate Display Update Sequence
  184. ReadBusy();
  185. }
  186. void Epd::TurnOnDisplay_Fast(void)
  187. {
  188. SendCommand(0x22); //Display Update Control
  189. SendData(0xC7);
  190. SendCommand(0x20); //Activate Display Update Sequence
  191. ReadBusy();
  192. }
  193. void Epd::Display(const UBYTE *blackimage, const UBYTE *ryimage) {
  194. UBYTE k;
  195. SendCommand(0x24);
  196. for (UWORD j = 0; j < height; j++) {
  197. for (UWORD i = 0; i < width/8; i++) {
  198. SendData(pgm_read_byte(&blackimage[i + (j*width/8)]));
  199. }
  200. }
  201. SendCommand(0x26);
  202. for (UWORD j = 0; j < height; j++) {
  203. for (UWORD i = 0; i < width/8; i++) {
  204. k = pgm_read_byte(&ryimage[i + (j*width/8)]);
  205. SendData(~k);
  206. }
  207. }
  208. TurnOnDisplay();
  209. }
  210. void Epd::Display_Fast(const UBYTE *blackimage, const UBYTE *ryimage) {
  211. UBYTE k;
  212. SendCommand(0x24);
  213. for (UWORD j = 0; j < height; j++) {
  214. for (UWORD i = 0; i < width/8; i++) {
  215. SendData(pgm_read_byte(&blackimage[i + (j*width/8)]));
  216. }
  217. }
  218. SendCommand(0x26);
  219. for (UWORD j = 0; j < height; j++) {
  220. for (UWORD i = 0; i < width/8; i++) {
  221. k = pgm_read_byte(&ryimage[i + (j*width/8)]);
  222. SendData(~k);
  223. }
  224. }
  225. TurnOnDisplay_Fast();
  226. }
  227. void Epd::Clear() {
  228. //send black data
  229. SendCommand(0x24);
  230. for (UWORD j = 0; j < height; j++) {
  231. for (UWORD i = 0; i < width/8; i++) {
  232. SendData(0xff);
  233. }
  234. }
  235. //send red data
  236. SendCommand(0x26);
  237. for (UWORD j = 0; j < height; j++) {
  238. for (UWORD i = 0; i < width/8; i++) {
  239. SendData(0x00);
  240. }
  241. }
  242. TurnOnDisplay_Base();
  243. SendCommand(0x26);
  244. for (UWORD j = 0; j < height; j++) {
  245. for (UWORD i = 0; i < width/8; i++) {
  246. SendData(0xff);
  247. }
  248. }
  249. }
  250. void Epd::Clear_Base() {
  251. //send black data
  252. SendCommand(0x10);
  253. for (UWORD j = 0; j < height; j++) {
  254. for (UWORD i = 0; i < width/8; i++) {
  255. SendData(0xff);
  256. }
  257. }
  258. //send red data
  259. SendCommand(0x26);
  260. for (UWORD j = 0; j < height; j++) {
  261. for (UWORD i = 0; i < width/8; i++) {
  262. SendData(0x00);
  263. }
  264. }
  265. TurnOnDisplay();
  266. }
  267. //Partial refresh display
  268. void Epd::Partial(const UBYTE *Image, UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
  269. {
  270. if((Xstart % 8 + Xend % 8 == 8 && Xstart % 8 > Xend % 8) || Xstart % 8 + Xend % 8 == 0 || (Xend - Xstart)%8 == 0)
  271. {
  272. Xstart = Xstart / 8 ;
  273. Xend = Xend / 8;
  274. }
  275. else
  276. {
  277. Xstart = Xstart / 8 ;
  278. Xend = Xend % 8 == 0 ? Xend / 8 : Xend / 8 + 1;
  279. }
  280. UWORD i, Width;
  281. Width = Xend - Xstart;
  282. UWORD IMAGE_COUNTER = Width * (Yend-Ystart);
  283. Xend -= 1;
  284. Yend -= 1;
  285. SendCommand(0x44); // set RAM x address start/end, in page 35
  286. SendData(Xstart & 0xff); // RAM x address start at 00h;
  287. SendData(Xend & 0xff); // RAM x address end at 0fh(15+1)*8->128
  288. SendCommand(0x45); // set RAM y address start/end, in page 35
  289. SendData(Ystart & 0xff); // RAM y address start at 0127h;
  290. SendData((Ystart>>8) & 0x01); // RAM y address start at 0127h;
  291. SendData(Yend & 0xff); // RAM y address end at 00h;
  292. SendData((Yend>>8) & 0x01);
  293. SendCommand(0x4E); // set RAM x address count to 0;
  294. SendData(Xstart & 0xff);
  295. SendCommand(0x4F); // set RAM y address count to 0X127;
  296. SendData(Ystart & 0xff);
  297. SendData((Ystart>>8) & 0x01);
  298. SendCommand(0x24); //Write Black and White image to RAM
  299. for (i = 0; i < IMAGE_COUNTER; i++) {
  300. SendData(pgm_read_byte(&Image[i]));
  301. }
  302. TurnOnDisplay_Partial();
  303. }
  304. /**
  305. * @brief: After this command is transmitted, the chip would enter the
  306. * deep-sleep mode to save power.
  307. * The deep sleep mode would return to standby by hardware reset.
  308. * The only one parameter is a check code, the command would be
  309. * You can use EPD_Reset() to awaken
  310. */
  311. void Epd::Sleep(void) {
  312. SendCommand(0x10); // DEEP_SLEEP
  313. SendData(0x01); // check code
  314. }
  315. /* END OF FILE */