epd5in79b.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /**
  2. * @filename : epd5in79b.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 "epd5in79b.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. SendCommand(0x91);
  62. SendData(0x00);
  63. SendCommand(0xC4); // Set Ram X- address Start / End position
  64. SendData(0x31); // XStart, POR = 00h
  65. SendData(0x00); //400/8-1
  66. SendCommand(0xC5); // Set Ram Y- address Start / End position
  67. SendData(0x0f);
  68. SendData(0x01); //300-1
  69. SendData(0x00); // YEnd L
  70. SendData(0x00); // YEnd H
  71. SendCommand(0xCe);
  72. SendData(0x31);
  73. SendCommand(0xCf);
  74. SendData(0x0f);
  75. SendData(0x01);
  76. return 0;
  77. }
  78. /**
  79. * @brief: basic function for sending commands
  80. */
  81. void Epd::SendCommand(unsigned char command) {
  82. DigitalWrite(dc_pin, LOW);
  83. SpiTransfer(command);
  84. }
  85. /**
  86. * @brief: basic function for sending data
  87. */
  88. void Epd::SendData(unsigned char data) {
  89. DigitalWrite(dc_pin, HIGH);
  90. SpiTransfer(data);
  91. }
  92. /**
  93. * @brief: Wait until the busy_pin goes HIGH
  94. */
  95. void Epd::ReadBusy(void) {
  96. while(DigitalRead(busy_pin) == 1) { //0: busy, 1: idle
  97. DelayMs(100);
  98. }
  99. }
  100. /**
  101. * @brief: module reset.
  102. * often used to awaken the module in deep sleep,
  103. * see Epd::Sleep();
  104. */
  105. void Epd::Reset(void) {
  106. DigitalWrite(reset_pin, LOW); //module reset
  107. DelayMs(200);
  108. DigitalWrite(reset_pin, HIGH);
  109. DelayMs(200);
  110. }
  111. /******************************************************************************
  112. function : Turn On Display
  113. parameter:
  114. ******************************************************************************/
  115. void Epd::TurnOnDisplay(void)
  116. {
  117. SendCommand(0x22); //Display Update Control
  118. SendData(0xF7);
  119. SendCommand(0x20); //Activate Display Update Sequence
  120. ReadBusy();
  121. }
  122. void Epd::Display(const UBYTE *blackimage, const UBYTE *ryimage) {
  123. int Width, Width1, Height;
  124. Width =(width % 16 == 0)?(width / 16 ):(width / 16 + 1);
  125. Width1 =(width % 8 == 0)?(width / 8 ):(width / 8 + 1);
  126. Height = height;
  127. // M part 396*272
  128. SendCommand(0x24);
  129. for (int j = 0; j < Height; j++) {
  130. for (int i = 0; i < Width; i++) {
  131. SendData(blackimage[j * Width1 + i]);
  132. }
  133. }
  134. SendCommand(0X26);
  135. for (int j = 0; j < Height; j++) {
  136. for (int i = 0; i < Width; i++) {
  137. SendData(~ryimage[j * Width1 + i]);
  138. }
  139. }
  140. // S part 396*272
  141. SendCommand(0xA4);
  142. for (int j = 0; j < Height; j++) {
  143. for (int i = 0; i < Width; i++) {
  144. SendData(blackimage[j * Width1 + i + Width - 1]);
  145. }
  146. }
  147. SendCommand(0XA6);
  148. for (int j = 0; j < Height; j++) {
  149. for (int i = 0; i < Width; i++) {
  150. SendData(~ryimage[j * Width1 + i + Width - 1]);
  151. }
  152. }
  153. TurnOnDisplay();
  154. }
  155. void Epd::Displaypart(const unsigned char* image, unsigned char Block) {
  156. int Width, Height;
  157. Width =(width % 16 == 0)?(width / 16 ):(width / 16 + 1);
  158. Height = height;
  159. int k = 0;
  160. if( Block == 0 )
  161. {
  162. // M part 396*272
  163. SendCommand(0x24);
  164. k = 0;
  165. for (int j = 0; j < Height; j++) {
  166. for (int i = 0; i < Width; i++) {
  167. if(i < 37 && j < 152)
  168. SendData((pgm_read_byte(&image[k++])));
  169. else
  170. SendData(0xff);
  171. }
  172. }
  173. SendCommand(0X26);
  174. for(int i=0; i<13600; i++)
  175. {
  176. SendData(0x00);
  177. }
  178. }
  179. else
  180. {
  181. // M part 396*272
  182. SendCommand(0xA4);
  183. for(int i=0; i<13600; i++)
  184. {
  185. SendData(0xFF);
  186. }
  187. SendCommand(0XA6);
  188. k = 0;
  189. for (int j = 0; j < Height; j++) {
  190. for (int i = 0; i < Width; i++) {
  191. if(i < 37 && j < 152)
  192. SendData(~(pgm_read_byte(&image[k++])));
  193. else
  194. SendData(0x00);
  195. }
  196. }
  197. TurnOnDisplay();
  198. }
  199. }
  200. void Epd::Clear(void) {
  201. // M part 396*272
  202. SendCommand(0x24);
  203. for(int i=0; i<13600; i++)
  204. {
  205. SendData(0xFF);
  206. }
  207. SendCommand(0X26);
  208. for(int i=0; i<13600; i++)
  209. {
  210. SendData(0x00);
  211. }
  212. // S part 396*272
  213. SendCommand(0xA4);
  214. for(int i=0; i<13600; i++)
  215. {
  216. SendData(0xFF);
  217. }
  218. SendCommand(0XA6);
  219. for(int i=0; i<13600; i++)
  220. {
  221. SendData(0x00);
  222. }
  223. TurnOnDisplay();
  224. }
  225. void Epd::Sleep(void) {
  226. SendCommand(0X10);
  227. SendData(0x01);
  228. }
  229. /* END OF FILE */