2
0

epd5in83.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @filename : epd5in83.h
  3. * @brief : Header file for e-paper library epd5in83.cpp
  4. * @author : MyMX from Waveshare
  5. *
  6. * Copyright (C) Waveshare April 6 2018
  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 "epd5in83.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. SendCommand(POWER_SETTING);
  44. SendData(0x37);
  45. SendData(0x00);
  46. SendCommand(PANEL_SETTING);
  47. SendData(0xCF);
  48. SendData(0x08);
  49. SendCommand(BOOSTER_SOFT_START);
  50. SendData(0xc7);
  51. SendData(0xcc);
  52. SendData(0x28);
  53. SendCommand(POWER_ON);
  54. WaitUntilIdle();
  55. SendCommand(PLL_CONTROL);
  56. SendData(0x3c);
  57. SendCommand(TEMPERATURE_CALIBRATION);
  58. SendData(0x00);
  59. SendCommand(VCOM_AND_DATA_INTERVAL_SETTING);
  60. SendData(0x77);
  61. SendCommand(TCON_SETTING);
  62. SendData(0x22);
  63. SendCommand(TCON_RESOLUTION);
  64. SendData(width >> 8); //source 640
  65. SendData(width & 0xff);
  66. SendData(height >> 8); //gate 384
  67. SendData(height & 0xff);
  68. SendCommand(VCM_DC_SETTING);
  69. SendData(0x1E); //decide by LUT file
  70. SendCommand(0xe5); //FLASH MODE
  71. SendData(0x03);
  72. return 0;
  73. }
  74. /**
  75. * @brief: basic function for sending commands
  76. */
  77. void Epd::SendCommand(unsigned char command) {
  78. DigitalWrite(dc_pin, LOW);
  79. SpiTransfer(command);
  80. }
  81. /**
  82. * @brief: basic function for sending data
  83. */
  84. void Epd::SendData(unsigned char data) {
  85. DigitalWrite(dc_pin, HIGH);
  86. SpiTransfer(data);
  87. }
  88. /**
  89. * @brief: Wait until the busy_pin goes HIGH
  90. */
  91. void Epd::WaitUntilIdle(void) {
  92. while(DigitalRead(busy_pin) == 0) { //0: busy, 1: idle
  93. DelayMs(100);
  94. }
  95. }
  96. /**
  97. * @brief: module reset.
  98. * often used to awaken the module in deep sleep,
  99. * see Epd::Sleep();
  100. */
  101. void Epd::Reset(void) {
  102. DigitalWrite(reset_pin, LOW); //module reset
  103. DelayMs(200);
  104. DigitalWrite(reset_pin, HIGH);
  105. DelayMs(200);
  106. }
  107. void Epd::DisplayFrame(const unsigned char** frame_buffer) {
  108. unsigned char temp1, temp2;
  109. SendCommand(DATA_START_TRANSMISSION_1);
  110. /**
  111. * Size of a single array cannot be larger than 32K in AVR GCC, therefore
  112. * you have to split the image data (33600 bytes in total) into 2 parts
  113. */
  114. for (int image_data_part = 0; image_data_part < 2; image_data_part++) {
  115. for(long i = 0; i < width / 8 * height /2; i++) {
  116. temp1 = pgm_read_byte(frame_buffer[image_data_part] + i);
  117. for(unsigned char j = 0; j < 8; j++) {
  118. if(temp1 & 0x80)
  119. temp2 = 0x03;
  120. else
  121. temp2 = 0x00;
  122. temp2 <<= 4;
  123. temp1 <<= 1;
  124. j++;
  125. if(temp1 & 0x80)
  126. temp2 |= 0x03;
  127. else
  128. temp2 |= 0x00;
  129. temp1 <<= 1;
  130. SendData(temp2);
  131. }
  132. }
  133. }
  134. SendCommand(DISPLAY_REFRESH);
  135. DelayMs(100);
  136. WaitUntilIdle();
  137. }
  138. /**
  139. * @brief: After this command is transmitted, the chip would enter the
  140. * deep-sleep mode to save power.
  141. * The deep sleep mode would return to standby by hardware reset.
  142. * The only one parameter is a check code, the command would be
  143. * executed if check code = 0xA5.
  144. * You can use EPD_Reset() to awaken
  145. */
  146. void Epd::Sleep(void) {
  147. SendCommand(POWER_OFF);
  148. WaitUntilIdle();
  149. SendCommand(DEEP_SLEEP);
  150. SendData(0xa5);
  151. }
  152. /* END OF FILE */