2
0

epd5in79.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /**
  2. * @filename : epd5in79.h
  3. * @brief : Header file for e-paper library epd5in83.cpp
  4. * @author : MyMX from Waveshare
  5. *
  6. * Copyright (C) Waveshare 2024/03/06
  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 "epd5in79.h"
  28. Epd::~Epd() {
  29. };
  30. Epd::Epd() {
  31. reset_pin = RST_PIN;
  32. dc_pin = DC_PIN;
  33. cs_pin = CS_PIN;
  34. busy_pin = BUSY_PIN;
  35. width = EPD_WIDTH;
  36. height = EPD_HEIGHT;
  37. };
  38. int Epd::Init(void) {
  39. if (IfInit() != 0) {
  40. return -1;
  41. }
  42. Reset();
  43. ReadBusy(); //waiting for the electronic paper IC to release the idle signal
  44. SendCommand(0x12); //POWER ON
  45. ReadBusy(); //waiting for the electronic paper IC to release the idle signal
  46. SendCommand(0x11);
  47. SendData(0x01);
  48. SendCommand(0x44); // Set Ram X- address Start / End position
  49. SendData(0x00); // XStart, POR = 00h
  50. SendData(0x31); //400/8-1
  51. SendCommand(0x45); // Set Ram Y- address Start / End position
  52. SendData(0x0f);
  53. SendData(0x01); //300-1
  54. SendData(0x00); // YEnd L
  55. SendData(0x00); // YEnd H
  56. SendCommand(0x4e);
  57. SendData(0x00);
  58. SendCommand(0x4f);
  59. SendData(0x0f);
  60. SendData(0x01);
  61. ReadBusy();
  62. SendCommand(0x91);
  63. SendData(0x00);
  64. SendCommand(0xC4); // Set Ram X- address Start / End position
  65. SendData(0x31); // XStart, POR = 00h
  66. SendData(0x00); //400/8-1
  67. SendCommand(0xC5); // Set Ram Y- address Start / End position
  68. SendData(0x0f);
  69. SendData(0x01); //300-1
  70. SendData(0x00); // YEnd L
  71. SendData(0x00); // YEnd H
  72. SendCommand(0xCE);
  73. SendData(0x31);
  74. SendCommand(0xCF);
  75. SendData(0x0f);
  76. SendData(0x01);
  77. ReadBusy();
  78. return 0;
  79. }
  80. int Epd::Init_Fast(void) {
  81. if (IfInit() != 0) {
  82. return -1;
  83. }
  84. Reset();
  85. ReadBusy(); //waiting for the electronic paper IC to release the idle signal
  86. SendCommand(0x12); //POWER ON
  87. ReadBusy(); //waiting for the electronic paper IC to release the idle signal
  88. SendCommand(0x18); //Read built-in temperature sensor
  89. SendData(0x80);
  90. SendCommand(0x22); // Load temperature value
  91. SendData(0xB1);
  92. SendCommand(0x20);
  93. ReadBusy();
  94. SendCommand(0x1A); // Write to temperature register
  95. SendData(0x64);
  96. SendData(0x00);
  97. SendCommand(0x22); // Load temperature value
  98. SendData(0x91);
  99. SendCommand(0x20);
  100. ReadBusy();
  101. SendCommand(0x11);
  102. SendData(0x01);
  103. SendCommand(0x44); // Set Ram X- address Start / End position
  104. SendData(0x00); // XStart, POR = 00h
  105. SendData(0x31); //400/8-1
  106. SendCommand(0x45); // Set Ram Y- address Start / End position
  107. SendData(0x0f);
  108. SendData(0x01); //300-1
  109. SendData(0x00); // YEnd L
  110. SendData(0x00); // YEnd H
  111. SendCommand(0x4e);
  112. SendData(0x00);
  113. SendCommand(0x4f);
  114. SendData(0x0f);
  115. SendData(0x01);
  116. ReadBusy();
  117. SendCommand(0x91);
  118. SendData(0x00);
  119. SendCommand(0xC4); // Set Ram X- address Start / End position
  120. SendData(0x31); // XStart, POR = 00h
  121. SendData(0x00); //400/8-1
  122. SendCommand(0xC5); // Set Ram Y- address Start / End position
  123. SendData(0x0f);
  124. SendData(0x01); //300-1
  125. SendData(0x00); // YEnd L
  126. SendData(0x00); // YEnd H
  127. SendCommand(0xCe);
  128. SendData(0x31);
  129. SendCommand(0xCf);
  130. SendData(0x0f);
  131. SendData(0x01);
  132. ReadBusy();
  133. return 0;
  134. }
  135. /**
  136. * @brief: basic function for sending commands
  137. */
  138. void Epd::SendCommand(unsigned char command) {
  139. DigitalWrite(dc_pin, LOW);
  140. SpiTransfer(command);
  141. }
  142. /**
  143. * @brief: basic function for sending data
  144. */
  145. void Epd::SendData(unsigned char data) {
  146. DigitalWrite(dc_pin, HIGH);
  147. SpiTransfer(data);
  148. }
  149. /**
  150. * @brief: Wait until the busy_pin goes HIGH
  151. */
  152. void Epd::ReadBusy(void) {
  153. while(DigitalRead(busy_pin) == 1) { //0: busy, 1: idle
  154. DelayMs(100);
  155. }
  156. }
  157. /**
  158. * @brief: module reset.
  159. * often used to awaken the module in deep sleep,
  160. * see Epd::Sleep();
  161. */
  162. void Epd::Reset(void) {
  163. DigitalWrite(reset_pin, LOW); //module reset
  164. DelayMs(200);
  165. DigitalWrite(reset_pin, HIGH);
  166. DelayMs(200);
  167. }
  168. /******************************************************************************
  169. function : Turn On Display
  170. parameter:
  171. ******************************************************************************/
  172. void Epd::TurnOnDisplay(void)
  173. {
  174. SendCommand(0x22); //Display Update Control
  175. SendData(0xF7);
  176. SendCommand(0x20); //Activate Display Update Sequence
  177. ReadBusy();
  178. }
  179. void Epd::TurnOnDisplay_Fast(void)
  180. {
  181. SendCommand(0x22); //Display Update Control
  182. SendData(0xC7);
  183. SendCommand(0x20); //Activate Display Update Sequence
  184. ReadBusy();
  185. }
  186. void Epd::TurnOnDisplay_Part(void)
  187. {
  188. SendCommand(0x22); //Display Update Control
  189. SendData(0xFF);
  190. SendCommand(0x20); //Activate Display Update Sequence
  191. ReadBusy();
  192. }
  193. void Epd::TurnOnDisplay_4GRAY(void)
  194. {
  195. SendCommand(0x22);
  196. SendData(0xCF);
  197. SendCommand(0x20);
  198. ReadBusy();
  199. }
  200. void Epd::Display(const unsigned char* frame_buffer) {
  201. int Width, Width1, Height;
  202. Width =(width % 16 == 0)?(width / 16 ):(width / 16 + 1);
  203. Width1 =(width % 8 == 0)?(width / 8 ):(width / 8 + 1);
  204. Height = height;
  205. // M part 396*272
  206. SendCommand(0x24);
  207. for (int j = 0; j < Height; j++) {
  208. for (int i = 0; i < Width; i++) {
  209. SendData(pgm_read_byte(&(frame_buffer[j * Width1 + i])));
  210. }
  211. }
  212. SendCommand(0X26);
  213. for (int j = 0; j < Height; j++) {
  214. for (int i = 0; i < Width; i++) {
  215. SendData(0x00);
  216. }
  217. }
  218. // S part 396*272
  219. SendCommand(0xA4);
  220. for (int j = 0; j < Height; j++) {
  221. for (int i = 0; i < Width; i++) {
  222. SendData(pgm_read_byte(&(frame_buffer[j * Width1 + i + Width - 1])));
  223. }
  224. }
  225. SendCommand(0XA6);
  226. for (int j = 0; j < Height; j++) {
  227. for (int i = 0; i < Width; i++) {
  228. SendData(0x00);
  229. }
  230. }
  231. TurnOnDisplay();
  232. }
  233. void Epd::Display_Fast(const unsigned char* frame_buffer) {
  234. int Width, Width1, Height;
  235. Width =(width % 16 == 0)?(width / 16 ):(width / 16 + 1);
  236. Width1 =(width % 8 == 0)?(width / 8 ):(width / 8 + 1);
  237. Height = height;
  238. // M part 396*272
  239. SendCommand(0x24);
  240. for (int j = 0; j < Height; j++) {
  241. for (int i = 0; i < Width; i++) {
  242. SendData(frame_buffer[j * Width1 + i]);
  243. }
  244. }
  245. SendCommand(0X26);
  246. for (int j = 0; j < Height; j++) {
  247. for (int i = 0; i < Width; i++) {
  248. SendData(0x00);
  249. }
  250. }
  251. // S part 396*272
  252. SendCommand(0xA4);
  253. for (int j = 0; j < Height; j++) {
  254. for (int i = 0; i < Width; i++) {
  255. SendData(frame_buffer[j * Width1 + i + Width - 1]);
  256. }
  257. }
  258. SendCommand(0XA6);
  259. for (int j = 0; j < Height; j++) {
  260. for (int i = 0; i < Width; i++) {
  261. SendData(0x00);
  262. }
  263. }
  264. TurnOnDisplay_Fast();
  265. }
  266. void Epd::Clear(void) {
  267. SendCommand(0x24);
  268. // M part 396*272
  269. SendCommand(0x24);
  270. for(int i=0; i<13600; i++)
  271. {
  272. SendData(0xFF);
  273. }
  274. SendCommand(0X26);
  275. for(int i=0; i<13600; i++)
  276. {
  277. SendData(0x00);
  278. }
  279. // S part 396*272
  280. SendCommand(0xA4);
  281. for(int i=0; i<13600; i++)
  282. {
  283. SendData(0xFF);
  284. }
  285. SendCommand(0XA6);
  286. for(int i=0; i<13600; i++)
  287. {
  288. SendData(0x00);
  289. }
  290. TurnOnDisplay();
  291. }
  292. void Epd::Sleep(void) {
  293. SendCommand(0X10);
  294. SendData(0x01);
  295. }
  296. /* END OF FILE */