GxEPD2_EPD.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Display Library for SPI e-paper panels from Dalian Good Display and boards from Waveshare.
  2. // Requires HW SPI and Adafruit_GFX. Caution: these e-papers require 3.3V supply AND data lines!
  3. //
  4. // based on Demo Example from Good Display: http://www.good-display.com/download_list/downloadcategoryid=34&isMode=false.html
  5. //
  6. // Author: Jean-Marc Zingg
  7. //
  8. // Version: see library.properties
  9. //
  10. // Library: https://github.com/ZinggJM/GxEPD2
  11. #include "GxEPD2_EPD.h"
  12. #if defined(ESP8266) || defined(ESP32)
  13. #include <pgmspace.h>
  14. #else
  15. #include <avr/pgmspace.h>
  16. #endif
  17. GxEPD2_EPD::GxEPD2_EPD(int8_t cs, int8_t dc, int8_t rst, int8_t busy, int8_t busy_level, uint32_t busy_timeout,
  18. uint16_t w, uint16_t h, GxEPD2::Panel p, bool c, bool pu, bool fpu) :
  19. WIDTH(w), HEIGHT(h), panel(p), hasColor(c), hasPartialUpdate(pu), hasFastPartialUpdate(fpu),
  20. _cs(cs), _dc(dc), _rst(rst), _busy(busy), _busy_level(busy_level), _busy_timeout(busy_timeout), _diag_enabled(false),
  21. _spi_settings(4000000, MSBFIRST, SPI_MODE0)
  22. {
  23. }
  24. void GxEPD2_EPD::init(uint32_t serial_diag_bitrate)
  25. {
  26. if (serial_diag_bitrate > 0)
  27. {
  28. Serial.begin(serial_diag_bitrate);
  29. _diag_enabled = true;
  30. }
  31. if (_cs >= 0)
  32. {
  33. digitalWrite(_cs, HIGH);
  34. pinMode(_cs, OUTPUT);
  35. }
  36. if (_dc >= 0)
  37. {
  38. digitalWrite(_dc, HIGH);
  39. pinMode(_dc, OUTPUT);
  40. }
  41. if (_rst >= 0)
  42. {
  43. digitalWrite(_rst, HIGH);
  44. pinMode(_rst, OUTPUT);
  45. delay(20);
  46. digitalWrite(_rst, LOW);
  47. delay(20);
  48. digitalWrite(_rst, HIGH);
  49. delay(200);
  50. }
  51. if (_busy >= 0)
  52. {
  53. pinMode(_busy, INPUT);
  54. }
  55. SPI.begin();
  56. // SPI.setDataMode(SPI_MODE0);
  57. // SPI.setBitOrder(MSBFIRST);
  58. //#if defined(SPI_HAS_TRANSACTION)
  59. // // true also for STM32F1xx Boards
  60. // SPISettings settings(4000000, MSBFIRST, SPI_MODE0);
  61. // SPI.beginTransaction(settings);
  62. // SPI.endTransaction();
  63. // //Serial.println("SPI has Transaction");
  64. //#elif defined(ESP8266) || defined(ESP32)
  65. // SPI.setFrequency(4000000);
  66. //#endif
  67. }
  68. void GxEPD2_EPD::_waitWhileBusy(const char* comment, uint16_t busy_time)
  69. {
  70. if (_busy >= 0)
  71. {
  72. unsigned long start = micros();
  73. while (1)
  74. {
  75. if (digitalRead(_busy) != _busy_level) break;
  76. delay(1);
  77. if (micros() - start > _busy_timeout)
  78. {
  79. Serial.println("Busy Timeout!");
  80. break;
  81. }
  82. }
  83. if (comment)
  84. {
  85. #if !defined(DISABLE_DIAGNOSTIC_OUTPUT)
  86. if (_diag_enabled)
  87. {
  88. unsigned long elapsed = micros() - start;
  89. Serial.print(comment);
  90. Serial.print(" : ");
  91. Serial.println(elapsed);
  92. }
  93. #endif
  94. }
  95. (void) start;
  96. }
  97. else delay(busy_time);
  98. }
  99. void GxEPD2_EPD::_writeCommand(uint8_t c)
  100. {
  101. SPI.beginTransaction(_spi_settings);
  102. if (_dc >= 0) digitalWrite(_dc, LOW);
  103. if (_cs >= 0) digitalWrite(_cs, LOW);
  104. SPI.transfer(c);
  105. if (_cs >= 0) digitalWrite(_cs, HIGH);
  106. if (_dc >= 0) digitalWrite(_dc, HIGH);
  107. SPI.endTransaction();
  108. }
  109. void GxEPD2_EPD::_writeData(uint8_t d)
  110. {
  111. SPI.beginTransaction(_spi_settings);
  112. if (_cs >= 0) digitalWrite(_cs, LOW);
  113. SPI.transfer(d);
  114. if (_cs >= 0) digitalWrite(_cs, HIGH);
  115. SPI.endTransaction();
  116. }
  117. void GxEPD2_EPD::_writeData(const uint8_t* data, uint16_t n)
  118. {
  119. SPI.beginTransaction(_spi_settings);
  120. if (_cs >= 0) digitalWrite(_cs, LOW);
  121. for (uint8_t i = 0; i < n; i++)
  122. {
  123. SPI.transfer(*data++);
  124. }
  125. if (_cs >= 0) digitalWrite(_cs, HIGH);
  126. SPI.endTransaction();
  127. }
  128. void GxEPD2_EPD::_writeDataPGM(const uint8_t* data, uint16_t n)
  129. {
  130. SPI.beginTransaction(_spi_settings);
  131. if (_cs >= 0) digitalWrite(_cs, LOW);
  132. for (uint8_t i = 0; i < n; i++)
  133. {
  134. SPI.transfer(pgm_read_byte(&*data++));
  135. }
  136. if (_cs >= 0) digitalWrite(_cs, HIGH);
  137. SPI.endTransaction();
  138. }
  139. void GxEPD2_EPD::_writeCommandData(const uint8_t* pCommandData, uint8_t datalen)
  140. {
  141. SPI.beginTransaction(_spi_settings);
  142. if (_dc >= 0) digitalWrite(_dc, LOW);
  143. if (_cs >= 0) digitalWrite(_cs, LOW);
  144. SPI.transfer(*pCommandData++);
  145. if (_dc >= 0) digitalWrite(_dc, HIGH);
  146. for (uint8_t i = 0; i < datalen - 1; i++) // sub the command
  147. {
  148. SPI.transfer(*pCommandData++);
  149. }
  150. if (_cs >= 0) digitalWrite(_cs, HIGH);
  151. SPI.endTransaction();
  152. }
  153. void GxEPD2_EPD::_writeCommandDataPGM(const uint8_t* pCommandData, uint8_t datalen)
  154. {
  155. SPI.beginTransaction(_spi_settings);
  156. if (_dc >= 0) digitalWrite(_dc, LOW);
  157. if (_cs >= 0) digitalWrite(_cs, LOW);
  158. SPI.transfer(pgm_read_byte(&*pCommandData++));
  159. if (_dc >= 0) digitalWrite(_dc, HIGH);
  160. for (uint8_t i = 0; i < datalen - 1; i++) // sub the command
  161. {
  162. SPI.transfer(pgm_read_byte(&*pCommandData++));
  163. }
  164. if (_cs >= 0) digitalWrite(_cs, HIGH);
  165. SPI.endTransaction();
  166. }