GxEPD2_EPD.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // Display Library for SPI e-paper panels from Dalian Good Display and boards from Waveshare.
  2. // Requires HW SPI and Adafruit_GFX. Caution: the e-paper panels require 3.3V supply AND data lines!
  3. //
  4. // Display Library based on Demo Example from Good Display: https://www.good-display.com/companyfile/32/
  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. _init_display_done = false;
  29. _reset_duration = 10;
  30. _busy_callback = 0;
  31. _busy_callback_parameter = 0;
  32. _sck = -1;
  33. _mosi = -1;
  34. }
  35. void GxEPD2_EPD::init(uint32_t serial_diag_bitrate)
  36. {
  37. init(serial_diag_bitrate, true, 10, false);
  38. }
  39. void GxEPD2_EPD::init(uint32_t serial_diag_bitrate, bool initial, uint16_t reset_duration, bool pulldown_rst_mode)
  40. {
  41. _initial_write = initial;
  42. _initial_refresh = initial;
  43. _pulldown_rst_mode = pulldown_rst_mode;
  44. _power_is_on = false;
  45. _using_partial_mode = false;
  46. _hibernating = false;
  47. _init_display_done = false;
  48. _reset_duration = reset_duration;
  49. if (serial_diag_bitrate > 0)
  50. {
  51. Serial.begin(serial_diag_bitrate);
  52. _diag_enabled = true;
  53. }
  54. if (_cs >= 0)
  55. {
  56. digitalWrite(_cs, HIGH); // preset (less glitch for any analyzer)
  57. pinMode(_cs, OUTPUT);
  58. digitalWrite(_cs, HIGH); // set (needed e.g. for RP2040)
  59. }
  60. _reset();
  61. _pSPIx->begin(); // may steal _rst pin (Waveshare Pico-ePaper-2.9)
  62. if (_rst >= 0)
  63. {
  64. digitalWrite(_rst, HIGH); // preset (less glitch for any analyzer)
  65. pinMode(_rst, OUTPUT);
  66. digitalWrite(_rst, HIGH); // set (needed e.g. for RP2040)
  67. }
  68. if (_cs >= 0)
  69. {
  70. digitalWrite(_cs, HIGH); // preset (less glitch for any analyzer)
  71. pinMode(_cs, OUTPUT);
  72. digitalWrite(_cs, HIGH); // set (needed e.g. for RP2040)
  73. }
  74. if (_dc >= 0)
  75. {
  76. digitalWrite(_dc, HIGH); // preset (less glitch for any analyzer)
  77. pinMode(_dc, OUTPUT);
  78. digitalWrite(_dc, HIGH); // set (needed e.g. for RP2040)
  79. }
  80. if (_busy >= 0)
  81. {
  82. pinMode(_busy, INPUT);
  83. }
  84. }
  85. void GxEPD2_EPD::end()
  86. {
  87. _pSPIx->end();
  88. if (_cs >= 0) pinMode(_cs, INPUT);
  89. if (_dc >= 0) pinMode(_dc, INPUT);
  90. if (_rst >= 0) pinMode(_rst, INPUT);
  91. }
  92. void GxEPD2_EPD::setBusyCallback(void (*busyCallback)(const void*), const void* busy_callback_parameter)
  93. {
  94. _busy_callback = busyCallback;
  95. _busy_callback_parameter = busy_callback_parameter;
  96. }
  97. void GxEPD2_EPD::selectSPI(SPIClass& spi, SPISettings spi_settings)
  98. {
  99. _pSPIx = &spi;
  100. _spi_settings = spi_settings;
  101. }
  102. void GxEPD2_EPD::_reset()
  103. {
  104. if (_rst >= 0)
  105. {
  106. if (_pulldown_rst_mode)
  107. {
  108. digitalWrite(_rst, LOW);
  109. pinMode(_rst, OUTPUT);
  110. digitalWrite(_rst, LOW);
  111. delay(_reset_duration);
  112. pinMode(_rst, INPUT_PULLUP);
  113. delay(_reset_duration > 10 ? _reset_duration : 10);
  114. }
  115. else
  116. {
  117. digitalWrite(_rst, HIGH); // NEEDED for Waveshare "clever" reset circuit, power controller before reset pulse, preset (less glitch for any analyzer)
  118. pinMode(_rst, OUTPUT);
  119. digitalWrite(_rst, HIGH); // NEEDED for Waveshare "clever" reset circuit, power controller before reset pulse, set (needed e.g. for RP2040)
  120. delay(10); // NEEDED for Waveshare "clever" reset circuit, at least delay(2);
  121. digitalWrite(_rst, LOW);
  122. delay(_reset_duration);
  123. digitalWrite(_rst, HIGH);
  124. delay(_reset_duration > 10 ? _reset_duration : 10);
  125. }
  126. _hibernating = false;
  127. }
  128. }
  129. void GxEPD2_EPD::_waitWhileBusy(const char* comment, uint16_t busy_time)
  130. {
  131. if (_busy >= 0)
  132. {
  133. delay(1); // add some margin to become active
  134. unsigned long start = micros();
  135. while (1)
  136. {
  137. if (digitalRead(_busy) != _busy_level) break;
  138. if (_busy_callback) _busy_callback(_busy_callback_parameter);
  139. else delay(1);
  140. if (digitalRead(_busy) != _busy_level) break;
  141. if (micros() - start > _busy_timeout)
  142. {
  143. Serial.println("Busy Timeout!");
  144. break;
  145. }
  146. #if defined(ESP8266) || defined(ESP32)
  147. yield(); // avoid wdt
  148. #endif
  149. }
  150. if (comment)
  151. {
  152. #if !defined(DISABLE_DIAGNOSTIC_OUTPUT)
  153. if (_diag_enabled)
  154. {
  155. unsigned long elapsed = micros() - start;
  156. Serial.print(comment);
  157. Serial.print(" : ");
  158. Serial.println(elapsed);
  159. }
  160. #endif
  161. }
  162. (void) start;
  163. }
  164. else delay(busy_time);
  165. }
  166. void GxEPD2_EPD::_writeCommand(uint8_t c)
  167. {
  168. _pSPIx->beginTransaction(_spi_settings);
  169. if (_dc >= 0) digitalWrite(_dc, LOW);
  170. if (_cs >= 0) digitalWrite(_cs, LOW);
  171. _pSPIx->transfer(c);
  172. if (_cs >= 0) digitalWrite(_cs, HIGH);
  173. if (_dc >= 0) digitalWrite(_dc, HIGH);
  174. _pSPIx->endTransaction();
  175. }
  176. void GxEPD2_EPD::_writeData(uint8_t d)
  177. {
  178. _pSPIx->beginTransaction(_spi_settings);
  179. if (_cs >= 0) digitalWrite(_cs, LOW);
  180. _pSPIx->transfer(d);
  181. if (_cs >= 0) digitalWrite(_cs, HIGH);
  182. _pSPIx->endTransaction();
  183. }
  184. void GxEPD2_EPD::_writeData(const uint8_t* data, uint16_t n)
  185. {
  186. _pSPIx->beginTransaction(_spi_settings);
  187. if (_cs >= 0) digitalWrite(_cs, LOW);
  188. for (uint16_t i = 0; i < n; i++)
  189. {
  190. _pSPIx->transfer(*data++);
  191. }
  192. if (_cs >= 0) digitalWrite(_cs, HIGH);
  193. _pSPIx->endTransaction();
  194. }
  195. void GxEPD2_EPD::_writeDataPGM(const uint8_t* data, uint16_t n, int16_t fill_with_zeroes)
  196. {
  197. _pSPIx->beginTransaction(_spi_settings);
  198. if (_cs >= 0) digitalWrite(_cs, LOW);
  199. for (uint16_t i = 0; i < n; i++)
  200. {
  201. _pSPIx->transfer(pgm_read_byte(&*data++));
  202. }
  203. while (fill_with_zeroes > 0)
  204. {
  205. _pSPIx->transfer(0x00);
  206. fill_with_zeroes--;
  207. }
  208. if (_cs >= 0) digitalWrite(_cs, HIGH);
  209. _pSPIx->endTransaction();
  210. }
  211. void GxEPD2_EPD::_writeDataPGM_sCS(const uint8_t* data, uint16_t n, int16_t fill_with_zeroes)
  212. {
  213. _pSPIx->beginTransaction(_spi_settings);
  214. for (uint8_t i = 0; i < n; i++)
  215. {
  216. if (_cs >= 0) digitalWrite(_cs, LOW);
  217. _pSPIx->transfer(pgm_read_byte(&*data++));
  218. if (_cs >= 0) digitalWrite(_cs, HIGH);
  219. }
  220. while (fill_with_zeroes > 0)
  221. {
  222. if (_cs >= 0) digitalWrite(_cs, LOW);
  223. _pSPIx->transfer(0x00);
  224. fill_with_zeroes--;
  225. if (_cs >= 0) digitalWrite(_cs, HIGH);
  226. }
  227. _pSPIx->endTransaction();
  228. }
  229. void GxEPD2_EPD::_writeCommandData(const uint8_t* pCommandData, uint8_t datalen)
  230. {
  231. _pSPIx->beginTransaction(_spi_settings);
  232. if (_dc >= 0) digitalWrite(_dc, LOW);
  233. if (_cs >= 0) digitalWrite(_cs, LOW);
  234. _pSPIx->transfer(*pCommandData++);
  235. if (_dc >= 0) digitalWrite(_dc, HIGH);
  236. for (uint8_t i = 0; i < datalen - 1; i++) // sub the command
  237. {
  238. _pSPIx->transfer(*pCommandData++);
  239. }
  240. if (_cs >= 0) digitalWrite(_cs, HIGH);
  241. _pSPIx->endTransaction();
  242. }
  243. void GxEPD2_EPD::_writeCommandDataPGM(const uint8_t* pCommandData, uint8_t datalen)
  244. {
  245. _pSPIx->beginTransaction(_spi_settings);
  246. if (_dc >= 0) digitalWrite(_dc, LOW);
  247. if (_cs >= 0) digitalWrite(_cs, LOW);
  248. _pSPIx->transfer(pgm_read_byte(&*pCommandData++));
  249. if (_dc >= 0) digitalWrite(_dc, HIGH);
  250. for (uint8_t i = 0; i < datalen - 1; i++) // sub the command
  251. {
  252. _pSPIx->transfer(pgm_read_byte(&*pCommandData++));
  253. }
  254. if (_cs >= 0) digitalWrite(_cs, HIGH);
  255. _pSPIx->endTransaction();
  256. }
  257. void GxEPD2_EPD::_startTransfer()
  258. {
  259. _pSPIx->beginTransaction(_spi_settings);
  260. if (_cs >= 0) digitalWrite(_cs, LOW);
  261. }
  262. void GxEPD2_EPD::_transfer(uint8_t value)
  263. {
  264. _pSPIx->transfer(value);
  265. }
  266. void GxEPD2_EPD::_endTransfer()
  267. {
  268. if (_cs >= 0) digitalWrite(_cs, HIGH);
  269. _pSPIx->endTransaction();
  270. }
  271. void GxEPD2_EPD::enableRead(int16_t sck, int16_t mosi)
  272. {
  273. _sck = sck;
  274. _mosi = mosi;
  275. }
  276. uint8_t GxEPD2_EPD::_readData()
  277. {
  278. uint8_t data = 0;
  279. if ((_sck >= 0) && (_mosi >= 0))
  280. {
  281. _pSPIx->end();
  282. digitalWrite(_sck, HIGH);
  283. pinMode(_sck, OUTPUT);
  284. digitalWrite(_sck, HIGH);
  285. pinMode(_mosi, INPUT);
  286. if (_cs >= 0) digitalWrite(_cs, LOW);
  287. for (int i = 0; i < 8; i++)
  288. {
  289. data <<= 1;
  290. digitalWrite(_sck, LOW);
  291. data |= digitalRead(_mosi);
  292. digitalWrite(_sck, HIGH);
  293. }
  294. if (_cs >= 0) digitalWrite(_cs, HIGH);
  295. _pSPIx->begin();
  296. }
  297. return data;
  298. }
  299. void GxEPD2_EPD::_readData(uint8_t* data, uint16_t n)
  300. {
  301. if ((_sck >= 0) && (_mosi >= 0))
  302. {
  303. _pSPIx->end();
  304. digitalWrite(_sck, LOW);
  305. pinMode(_sck, OUTPUT);
  306. digitalWrite(_sck, LOW);
  307. pinMode(_mosi, INPUT);
  308. if (_cs >= 0) digitalWrite(_cs, LOW);
  309. for (uint8_t i = 0; i < n; i++)
  310. {
  311. *data = 0;
  312. for (int i = 0; i < 8; i++)
  313. {
  314. *data <<= 1;
  315. digitalWrite(_sck, HIGH);
  316. *data |= digitalRead(_mosi);
  317. digitalWrite(_sck, LOW);
  318. }
  319. data++;
  320. }
  321. if (_cs >= 0) digitalWrite(_cs, HIGH);
  322. _pSPIx->begin();
  323. }
  324. }