epd5in83b.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * @filename : epd5in83b.cpp
  3. * @brief : Implements for e-paper library
  4. * @author : Yehui from Waveshare
  5. *
  6. * Copyright (C) Waveshare August 10 2017
  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 "epd5in83b.h"
  28. Epd::~Epd()
  29. {
  30. };
  31. Epd::Epd()
  32. {
  33. reset_pin = RST_PIN;
  34. dc_pin = DC_PIN;
  35. cs_pin = CS_PIN;
  36. busy_pin = BUSY_PIN;
  37. width = EPD_WIDTH;
  38. height = EPD_HEIGHT;
  39. };
  40. int Epd::Init(void)
  41. {
  42. if (IfInit() != 0) {
  43. return -1;
  44. }
  45. Reset();
  46. SendCommand(POWER_SETTING);
  47. SendData(0x37);
  48. SendData(0x00);
  49. SendCommand(PANEL_SETTING);
  50. SendData(0xCF);
  51. SendData(0x08);
  52. SendCommand(BOOSTER_SOFT_START);
  53. SendData(0xc7);
  54. SendData(0xcc);
  55. SendData(0x28);
  56. SendCommand(PLL_CONTROL);
  57. SendData(0x3A);
  58. SendCommand(TEMPERATURE_CALIBRATION);
  59. SendData(0x00);
  60. SendCommand(VCOM_AND_DATA_INTERVAL_SETTING);
  61. SendData(0x77);
  62. SendCommand(TCON_SETTING);
  63. SendData(0x22);
  64. SendCommand(TCON_RESOLUTION);
  65. SendData(0x02); //source 600
  66. SendData(0x58);
  67. SendData(0x01); //gate 448
  68. SendData(0xC0);
  69. SendCommand(VCM_DC_SETTING);
  70. SendData(0x20); //decide by LUT file
  71. SendCommand(0xe5); //FLASH MODE
  72. SendData(0x03);
  73. SendCommand(POWER_ON);
  74. WaitUntilIdle();
  75. return 0;
  76. }
  77. /**
  78. * @brief: basic function for sending commands
  79. */
  80. void Epd::SendCommand(unsigned char command)
  81. {
  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. {
  90. DigitalWrite(dc_pin, HIGH);
  91. SpiTransfer(data);
  92. }
  93. /**
  94. * @brief: Wait until the busy_pin goes HIGH
  95. */
  96. void Epd::WaitUntilIdle(void)
  97. {
  98. while(DigitalRead(busy_pin) == 0) { //0: busy, 1: idle
  99. DelayMs(100);
  100. }
  101. }
  102. /**
  103. * @brief: module reset.
  104. * often used to awaken the module in deep sleep,
  105. * see Epd::Sleep();
  106. */
  107. void Epd::Reset(void)
  108. {
  109. DigitalWrite(reset_pin, LOW); //module reset
  110. DelayMs(200);
  111. DigitalWrite(reset_pin, HIGH);
  112. DelayMs(200);
  113. }
  114. void Epd::DisplayFrame(const unsigned char* image_black, const unsigned char* image_red)
  115. {
  116. unsigned char Data_Black, Data_Red, Data;
  117. unsigned int i, j, k;
  118. SendCommand(DATA_START_TRANSMISSION_1);
  119. for(i = 0; i < 448; i++) {
  120. for(j = 0; j < 600 / 8; i++) {
  121. Data_Black = pgm_read_byte(image_black + i*(600 / 8) + k);
  122. Data_Red = pgm_read_byte(image_red + i*(600 / 8) + k);
  123. k = 0;
  124. while (k < 8) {
  125. if ((Data_Red & 0x80) == 0x00) {
  126. Data = 0x04; //red
  127. } else if ((Data_Black & 0x80) == 0x00) {
  128. Data = 0x00; //black
  129. } else {
  130. Data = 0x03; //white
  131. }
  132. Data = (Data << 4) & 0xFF;
  133. Data_Black = (Data_Black << 1) & 0xFF;
  134. Data_Red = (Data_Red << 1) & 0xFF;
  135. k += 1;
  136. if((Data_Red & 0x80) == 0x00) {
  137. Data |= 0x04; //red
  138. } else if ((Data_Black & 0x80) == 0x00) {
  139. Data |= 0x00; //black
  140. } else {
  141. Data |= 0x03; //white
  142. }
  143. Data_Black = (Data_Black << 1) & 0xFF;
  144. Data_Red = (Data_Red << 1) & 0xFF;
  145. SendData(Data);
  146. k += 1;
  147. }
  148. }
  149. }
  150. SendCommand(DISPLAY_REFRESH);
  151. DelayMs(100);
  152. WaitUntilIdle();
  153. }
  154. void Epd::Clean(void)
  155. {
  156. SendCommand(DATA_START_TRANSMISSION_1);
  157. for (long i = 0; i < 448; i++) {
  158. for (long j = 0; j < 600 / 2; j++) {
  159. SendData(0x33);
  160. }
  161. }
  162. SendCommand(DISPLAY_REFRESH);
  163. DelayMs(100);
  164. WaitUntilIdle();
  165. }
  166. void Epd::DisplayOneQuarterFrame(const unsigned char* image_black, const unsigned char* image_red)
  167. {
  168. unsigned char Data_Black, Data_Red, Data;
  169. unsigned int j;
  170. SendCommand(DATA_START_TRANSMISSION_1);
  171. for (long i = 0; i < 224; i++) {
  172. for (long k = 0; k < 38; k++) {//38 => 300 / 8 = 37.5
  173. Data_Black = pgm_read_byte(image_black + i*38 + k);
  174. Data_Red = pgm_read_byte(image_red + i*38 + k);
  175. j = 0;
  176. if(k == 37) {
  177. while (j < 4) {
  178. if ((Data_Red & 0x80) == 0x00) {
  179. Data = 0x04; //red
  180. } else if ((Data_Black & 0x80) == 0x00) {
  181. Data = 0x00; //black
  182. } else {
  183. Data = 0x03; //white
  184. }
  185. Data = (Data << 4) & 0xFF;
  186. Data_Black = (Data_Black << 1) & 0xFF;
  187. Data_Red = (Data_Red << 1) & 0xFF;
  188. j += 1;
  189. if((Data_Red & 0x80) == 0x00) {
  190. Data |= 0x04; //red
  191. } else if ((Data_Black & 0x80) == 0x00) {
  192. Data |= 0x00; //black
  193. } else {
  194. Data |= 0x03; //white
  195. }
  196. Data_Black = (Data_Black << 1) & 0xFF;
  197. Data_Red = (Data_Red << 1) & 0xFF;
  198. SendData(Data);
  199. j += 1;
  200. }
  201. } else {
  202. while (j < 8) {
  203. if ((Data_Red & 0x80) == 0x00) {
  204. Data = 0x04; //red
  205. } else if ((Data_Black & 0x80) == 0x00) {
  206. Data = 0x00; //black
  207. } else {
  208. Data = 0x03; //white
  209. }
  210. Data = (Data << 4) & 0xFF;
  211. Data_Black = (Data_Black << 1) & 0xFF;
  212. Data_Red = (Data_Red << 1) & 0xFF;
  213. j += 1;
  214. if((Data_Red & 0x80) == 0x00) {
  215. Data |= 0x04; //red
  216. } else if ((Data_Black & 0x80) == 0x00) {
  217. Data |= 0x00; //black
  218. } else {
  219. Data |= 0x03; //white
  220. }
  221. Data_Black = (Data_Black << 1) & 0xFF;
  222. Data_Red = (Data_Red << 1) & 0xFF;
  223. SendData(Data);
  224. j += 1;
  225. }
  226. }
  227. }
  228. for (long k = 0; k < 150; k++) { // 1/4 show white
  229. SendData(0x33);
  230. }
  231. }
  232. for (long i = 0; i < 224; i++) {
  233. for (long k = 0; k < 150; k++) { // 1/4 show red
  234. SendData(0x44);
  235. }
  236. for (long k = 0; k < 150; k++) { // 1/4 show black
  237. SendData(0x00);
  238. }
  239. }
  240. SendCommand(DISPLAY_REFRESH);
  241. DelayMs(100);
  242. WaitUntilIdle();
  243. }
  244. /**
  245. * @brief: After this command is transmitted, the chip would enter the
  246. * deep-sleep mode to save power.
  247. * The deep sleep mode would return to standby by hardware reset.
  248. * The only one parameter is a check code, the command would be
  249. * executed if check code = 0xA5.
  250. * You can use EPD_Reset() to awaken
  251. */
  252. void Epd::Sleep(void)
  253. {
  254. SendCommand(POWER_OFF);
  255. WaitUntilIdle();
  256. SendCommand(DEEP_SLEEP);
  257. SendData(0xa5);
  258. }
  259. /* END OF FILE */