GxEPD2_EPD.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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.e-paper-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(int16_t cs, int16_t dc, int16_t rst, int16_t busy, int16_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. _pSPIx(&SPI), _spi_settings(4000000, MSBFIRST, SPI_MODE0)
  22. {
  23. _initial_write = true;
  24. _initial_refresh = true;
  25. _power_is_on = false;
  26. _using_partial_mode = false;
  27. _hibernating = false;
  28. _reset_duration = 10;
  29. _busy_callback = 0;
  30. _busy_callback_parameter = 0;
  31. }
  32. void GxEPD2_EPD::init(uint32_t serial_diag_bitrate)
  33. {
  34. init(serial_diag_bitrate, true, 10, false);
  35. }
  36. void GxEPD2_EPD::init(uint32_t serial_diag_bitrate, bool initial, uint16_t reset_duration, bool pulldown_rst_mode)
  37. {
  38. _initial_write = initial;
  39. _initial_refresh = initial;
  40. _pulldown_rst_mode = pulldown_rst_mode;
  41. _power_is_on = false;
  42. _using_partial_mode = false;
  43. _hibernating = false;
  44. _reset_duration = reset_duration;
  45. if (serial_diag_bitrate > 0)
  46. {
  47. Serial.begin(serial_diag_bitrate);
  48. _diag_enabled = true;
  49. }
  50. if (_cs >= 0)
  51. {
  52. digitalWrite(_cs, HIGH);
  53. pinMode(_cs, OUTPUT);
  54. }
  55. if (_dc >= 0)
  56. {
  57. digitalWrite(_dc, HIGH);
  58. pinMode(_dc, OUTPUT);
  59. }
  60. _reset();
  61. if (_busy >= 0)
  62. {
  63. pinMode(_busy, INPUT);
  64. }
  65. _pSPIx->begin();
  66. if (_busy == MISO) // may be overridden, to be verified
  67. {
  68. pinMode(_busy, INPUT);
  69. }
  70. if (_dc == MISO) // may be overridden, TTGO T5 V2.66
  71. {
  72. pinMode(_dc, OUTPUT);
  73. }
  74. }
  75. void GxEPD2_EPD::setBusyCallback(void (*busyCallback)(const void*), const void* busy_callback_parameter)
  76. {
  77. _busy_callback = busyCallback;
  78. _busy_callback_parameter = busy_callback_parameter;
  79. }
  80. void GxEPD2_EPD::selectSPI(SPIClass& spi, SPISettings spi_settings)
  81. {
  82. _pSPIx = &spi;
  83. _spi_settings = spi_settings;
  84. }
  85. void GxEPD2_EPD::_reset()
  86. {
  87. if (_rst >= 0)
  88. {
  89. if (_pulldown_rst_mode)
  90. {
  91. digitalWrite(_rst, LOW);
  92. pinMode(_rst, OUTPUT);
  93. delay(_reset_duration);
  94. pinMode(_rst, INPUT_PULLUP);
  95. delay(_reset_duration > 10 ? _reset_duration : 10);
  96. }
  97. else
  98. {
  99. digitalWrite(_rst, HIGH); // NEEDED for Waveshare "clever" reset circuit, power controller before reset pulse
  100. pinMode(_rst, OUTPUT);
  101. delay(10); // NEEDED for Waveshare "clever" reset circuit, at least delay(2);
  102. digitalWrite(_rst, LOW);
  103. delay(_reset_duration);
  104. digitalWrite(_rst, HIGH);
  105. delay(_reset_duration > 10 ? _reset_duration : 10);
  106. }
  107. _hibernating = false;
  108. }
  109. }
  110. void GxEPD2_EPD::_waitWhileBusy(const char* comment, uint16_t busy_time)
  111. {
  112. if (_busy >= 0)
  113. {
  114. delay(1); // add some margin to become active
  115. unsigned long start = micros();
  116. while (1)
  117. {
  118. if (digitalRead(_busy) != _busy_level) break;
  119. if (_busy_callback) _busy_callback(_busy_callback_parameter);
  120. else delay(1);
  121. if (digitalRead(_busy) != _busy_level) break;
  122. if (micros() - start > _busy_timeout)
  123. {
  124. Serial.println("Busy Timeout!");
  125. break;
  126. }
  127. #if defined(ESP8266) || defined(ESP32)
  128. yield(); // avoid wdt
  129. #endif
  130. }
  131. if (comment)
  132. {
  133. #if !defined(DISABLE_DIAGNOSTIC_OUTPUT)
  134. if (_diag_enabled)
  135. {
  136. unsigned long elapsed = micros() - start;
  137. Serial.print(comment);
  138. Serial.print(" : ");
  139. Serial.println(elapsed);
  140. }
  141. #endif
  142. }
  143. (void) start;
  144. }
  145. else delay(busy_time);
  146. }
  147. void GxEPD2_EPD::_writeCommand(uint8_t c)
  148. {
  149. _pSPIx->beginTransaction(_spi_settings);
  150. if (_dc >= 0) digitalWrite(_dc, LOW);
  151. if (_cs >= 0) digitalWrite(_cs, LOW);
  152. _pSPIx->transfer(c);
  153. if (_cs >= 0) digitalWrite(_cs, HIGH);
  154. if (_dc >= 0) digitalWrite(_dc, HIGH);
  155. _pSPIx->endTransaction();
  156. }
  157. void GxEPD2_EPD::_writeData(uint8_t d)
  158. {
  159. _pSPIx->beginTransaction(_spi_settings);
  160. if (_cs >= 0) digitalWrite(_cs, LOW);
  161. _pSPIx->transfer(d);
  162. if (_cs >= 0) digitalWrite(_cs, HIGH);
  163. _pSPIx->endTransaction();
  164. }
  165. void GxEPD2_EPD::_writeData(const uint8_t* data, uint16_t n)
  166. {
  167. _pSPIx->beginTransaction(_spi_settings);
  168. if (_cs >= 0) digitalWrite(_cs, LOW);
  169. for (uint16_t i = 0; i < n; i++)
  170. {
  171. _pSPIx->transfer(*data++);
  172. }
  173. if (_cs >= 0) digitalWrite(_cs, HIGH);
  174. _pSPIx->endTransaction();
  175. }
  176. void GxEPD2_EPD::_writeDataPGM(const uint8_t* data, uint16_t n, int16_t fill_with_zeroes)
  177. {
  178. _pSPIx->beginTransaction(_spi_settings);
  179. if (_cs >= 0) digitalWrite(_cs, LOW);
  180. for (uint16_t i = 0; i < n; i++)
  181. {
  182. _pSPIx->transfer(pgm_read_byte(&*data++));
  183. }
  184. while (fill_with_zeroes > 0)
  185. {
  186. _pSPIx->transfer(0x00);
  187. fill_with_zeroes--;
  188. }
  189. if (_cs >= 0) digitalWrite(_cs, HIGH);
  190. _pSPIx->endTransaction();
  191. }
  192. void GxEPD2_EPD::_writeDataPGM_sCS(const uint8_t* data, uint16_t n, int16_t fill_with_zeroes)
  193. {
  194. _pSPIx->beginTransaction(_spi_settings);
  195. for (uint8_t i = 0; i < n; i++)
  196. {
  197. if (_cs >= 0) digitalWrite(_cs, LOW);
  198. _pSPIx->transfer(pgm_read_byte(&*data++));
  199. if (_cs >= 0) digitalWrite(_cs, HIGH);
  200. }
  201. while (fill_with_zeroes > 0)
  202. {
  203. if (_cs >= 0) digitalWrite(_cs, LOW);
  204. _pSPIx->transfer(0x00);
  205. fill_with_zeroes--;
  206. if (_cs >= 0) digitalWrite(_cs, HIGH);
  207. }
  208. _pSPIx->endTransaction();
  209. }
  210. void GxEPD2_EPD::_writeCommandData(const uint8_t* pCommandData, uint8_t datalen)
  211. {
  212. _pSPIx->beginTransaction(_spi_settings);
  213. if (_dc >= 0) digitalWrite(_dc, LOW);
  214. if (_cs >= 0) digitalWrite(_cs, LOW);
  215. _pSPIx->transfer(*pCommandData++);
  216. if (_dc >= 0) digitalWrite(_dc, HIGH);
  217. for (uint8_t i = 0; i < datalen - 1; i++) // sub the command
  218. {
  219. _pSPIx->transfer(*pCommandData++);
  220. }
  221. if (_cs >= 0) digitalWrite(_cs, HIGH);
  222. _pSPIx->endTransaction();
  223. }
  224. void GxEPD2_EPD::_writeCommandDataPGM(const uint8_t* pCommandData, uint8_t datalen)
  225. {
  226. _pSPIx->beginTransaction(_spi_settings);
  227. if (_dc >= 0) digitalWrite(_dc, LOW);
  228. if (_cs >= 0) digitalWrite(_cs, LOW);
  229. _pSPIx->transfer(pgm_read_byte(&*pCommandData++));
  230. if (_dc >= 0) digitalWrite(_dc, HIGH);
  231. for (uint8_t i = 0; i < datalen - 1; i++) // sub the command
  232. {
  233. _pSPIx->transfer(pgm_read_byte(&*pCommandData++));
  234. }
  235. if (_cs >= 0) digitalWrite(_cs, HIGH);
  236. _pSPIx->endTransaction();
  237. }
  238. void GxEPD2_EPD::_startTransfer()
  239. {
  240. _pSPIx->beginTransaction(_spi_settings);
  241. if (_cs >= 0) digitalWrite(_cs, LOW);
  242. }
  243. void GxEPD2_EPD::_transfer(uint8_t value)
  244. {
  245. _pSPIx->transfer(value);
  246. }
  247. void GxEPD2_EPD::_endTransfer()
  248. {
  249. if (_cs >= 0) digitalWrite(_cs, HIGH);
  250. _pSPIx->endTransaction();
  251. }