GxEPD2_EPD.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifndef _GxEPD2_EPD_H_
  12. #define _GxEPD2_EPD_H_
  13. #include <Arduino.h>
  14. #include <SPI.h>
  15. #include <GxEPD2.h>
  16. #pragma GCC diagnostic ignored "-Wunused-parameter"
  17. //#pragma GCC diagnostic ignored "-Wsign-compare"
  18. class GxEPD2_EPD
  19. {
  20. public:
  21. // attributes
  22. const uint16_t WIDTH;
  23. const uint16_t HEIGHT;
  24. const GxEPD2::Panel panel;
  25. const bool hasColor;
  26. const bool hasPartialUpdate;
  27. const bool hasFastPartialUpdate;
  28. // constructor
  29. GxEPD2_EPD(int16_t cs, int16_t dc, int16_t rst, int16_t busy, int16_t busy_level, uint32_t busy_timeout,
  30. uint16_t w, uint16_t h, GxEPD2::Panel p, bool c, bool pu, bool fpu);
  31. virtual void init(uint32_t serial_diag_bitrate = 0); // serial_diag_bitrate = 0 : disabled
  32. virtual void init(uint32_t serial_diag_bitrate, bool initial, uint16_t reset_duration = 10, bool pulldown_rst_mode = false);
  33. // Support for Bitmaps (Sprites) to Controller Buffer and to Screen
  34. virtual void clearScreen(uint8_t value) = 0; // init controller memory and screen (default white)
  35. virtual void writeScreenBuffer(uint8_t value) = 0; // init controller memory (default white)
  36. // write to controller memory, without screen refresh; x and w should be multiple of 8
  37. virtual void writeImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false) = 0;
  38. virtual void writeImageForFullRefresh(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false)
  39. {
  40. // writeImage is independent from refresh mode for most controllers, exception e.g. SSD1681
  41. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  42. }
  43. virtual void writeImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  44. int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false) = 0;
  45. // virtual void writeImage(const uint8_t* black, const uint8_t* color, int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false) = 0;
  46. // virtual void writeImagePart(const uint8_t* black, const uint8_t* color, int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  47. // int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false) = 0;
  48. // write sprite of native data to controller memory, without screen refresh; x and w should be multiple of 8
  49. // virtual void writeNative(const uint8_t* data1, const uint8_t* data2, int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false) = 0;
  50. // for differential update: set current and previous buffers equal (for fast partial update to work correctly)
  51. virtual void writeScreenBufferAgain(uint8_t value = 0xFF) // init controller memory (default white)
  52. {
  53. // most controllers with differential update do switch buffers on refresh, can use:
  54. writeScreenBuffer(value);
  55. }
  56. virtual void writeImageAgain(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false)
  57. {
  58. // most controllers with differential update do switch buffers on refresh, can use:
  59. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  60. }
  61. virtual void writeImagePartAgain(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  62. int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false)
  63. {
  64. // most controllers with differential update do switch buffers on refresh, can use:
  65. writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  66. }
  67. // write to controller memory, with screen refresh; x and w should be multiple of 8
  68. // virtual void drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false) = 0;
  69. // virtual void drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  70. // int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false) = 0;
  71. // virtual void drawImage(const uint8_t* black, const uint8_t* color, int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false) = 0;
  72. // virtual void drawImagePart(const uint8_t* black, const uint8_t* color, int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  73. // int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false) = 0;
  74. // write sprite of native data to controller memory, with screen refresh; x and w should be multiple of 8
  75. // virtual void drawNative(const uint8_t* data1, const uint8_t* data2, int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false) = 0;
  76. virtual void refresh(bool partial_update_mode = false) = 0; // screen refresh from controller memory to full screen
  77. virtual void refresh(int16_t x, int16_t y, int16_t w, int16_t h) = 0; // screen refresh from controller memory, partial screen
  78. virtual void powerOff() = 0; // turns off generation of panel driving voltages, avoids screen fading over time
  79. virtual void hibernate() = 0; // turns powerOff() and sets controller to deep sleep for minimum power use, ONLY if wakeable by RST (rst >= 0)
  80. virtual void setPaged() {}; // for GxEPD2_154c paged workaround
  81. // register a callback function to be called during _waitWhileBusy continuously.
  82. void setBusyCallback(void (*busyCallback)(const void*), const void* busy_callback_parameter = 0);
  83. static inline uint16_t gx_uint16_min(uint16_t a, uint16_t b)
  84. {
  85. return (a < b ? a : b);
  86. };
  87. static inline uint16_t gx_uint16_max(uint16_t a, uint16_t b)
  88. {
  89. return (a > b ? a : b);
  90. };
  91. void selectSPI(SPIClass& spi, SPISettings spi_settings);
  92. protected:
  93. void _reset();
  94. void _waitWhileBusy(const char* comment = 0, uint16_t busy_time = 5000);
  95. void _writeCommand(uint8_t c);
  96. void _writeData(uint8_t d);
  97. void _writeData(const uint8_t* data, uint16_t n);
  98. void _writeDataPGM(const uint8_t* data, uint16_t n, int16_t fill_with_zeroes = 0);
  99. void _writeDataPGM_sCS(const uint8_t* data, uint16_t n, int16_t fill_with_zeroes = 0);
  100. void _writeCommandData(const uint8_t* pCommandData, uint8_t datalen);
  101. void _writeCommandDataPGM(const uint8_t* pCommandData, uint8_t datalen);
  102. void _startTransfer();
  103. void _transfer(uint8_t value);
  104. void _endTransfer();
  105. protected:
  106. int16_t _cs, _dc, _rst, _busy, _busy_level;
  107. uint32_t _busy_timeout;
  108. bool _diag_enabled, _pulldown_rst_mode;
  109. SPIClass* _pSPIx;
  110. SPISettings _spi_settings;
  111. bool _initial_write, _initial_refresh;
  112. bool _power_is_on, _using_partial_mode, _hibernating;
  113. uint16_t _reset_duration;
  114. void (*_busy_callback)(const void*);
  115. const void* _busy_callback_parameter;
  116. };
  117. #endif