GxEPD2_EPD.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #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. virtual void init(int16_t sck, int16_t mosi, uint32_t serial_diag_bitrate, bool initial, uint16_t reset_duration = 20, bool pulldown_rst_mode = false);
  34. virtual void end(); // release SPI and control pins
  35. // Support for Bitmaps (Sprites) to Controller Buffer and to Screen
  36. virtual void clearScreen(uint8_t value) = 0; // init controller memory and screen (default white)
  37. virtual void writeScreenBuffer(uint8_t value) = 0; // init controller memory (default white)
  38. // write to controller memory, without screen refresh; x and w should be multiple of 8
  39. 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;
  40. 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)
  41. {
  42. // writeImage is independent from refresh mode for most controllers, exception e.g. SSD1681
  43. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  44. }
  45. virtual void writeImageToPrevious(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) {};
  46. virtual void writeImagePart(const uint8_t bitmap[], 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. // 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;
  49. // 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,
  50. // int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false) = 0;
  51. // virtual void writeImagePartToPrevious(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  52. // int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false) {};
  53. // write sprite of native data to controller memory, without screen refresh; x and w should be multiple of 8
  54. // 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;
  55. // for differential update: set current and previous buffers equal (for fast partial update to work correctly)
  56. virtual void writeScreenBufferAgain(uint8_t value = 0xFF) // init controller memory (default white)
  57. {
  58. // most controllers with differential update do switch buffers on refresh, can use:
  59. writeScreenBuffer(value);
  60. }
  61. 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)
  62. {
  63. // most controllers with differential update do switch buffers on refresh, can use:
  64. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  65. }
  66. virtual void writeImagePartAgain(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  67. int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false)
  68. {
  69. // most controllers with differential update do switch buffers on refresh, can use:
  70. writeImagePart(bitmap, x_part, y_part, w_bitmap, h_bitmap, x, y, w, h, invert, mirror_y, pgm);
  71. }
  72. // write to controller memory, with screen refresh; x and w should be multiple of 8
  73. // 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;
  74. // virtual void drawImagePart(const uint8_t bitmap[], int16_t x_part, int16_t y_part, int16_t w_bitmap, int16_t h_bitmap,
  75. // 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 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;
  77. // 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,
  78. // int16_t x, int16_t y, int16_t w, int16_t h, bool invert = false, bool mirror_y = false, bool pgm = false) = 0;
  79. // write sprite of native data to controller memory, with screen refresh; x and w should be multiple of 8
  80. // 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;
  81. // a demo bitmap can use yet another bitmap format, e.g. 7-color bitmap from Good Display for GDEY073D46
  82. virtual void writeDemoBitmap(const uint8_t* data1, const uint8_t* data2, int16_t x, int16_t y, int16_t w, int16_t h, int16_t mode = 0, bool mirror_y = false, bool pgm = false) {};
  83. virtual void drawDemoBitmap(const uint8_t* data1, const uint8_t* data2, int16_t x, int16_t y, int16_t w, int16_t h, int16_t mode = 0, bool mirror_y = false, bool pgm = false) {};
  84. virtual void refresh(bool partial_update_mode = false) = 0; // screen refresh from controller memory to full screen
  85. virtual void refresh(int16_t x, int16_t y, int16_t w, int16_t h) = 0; // screen refresh from controller memory, partial screen
  86. virtual void powerOff() = 0; // turns off generation of panel driving voltages, avoids screen fading over time
  87. virtual void hibernate() = 0; // turns powerOff() and sets controller to deep sleep for minimum power use, ONLY if wakeable by RST (rst >= 0)
  88. virtual void setPaged() {}; // for GxEPD2_154c paged workaround
  89. virtual void selectFastFullUpdate(bool) {}; // for some panels that support this
  90. virtual void drawNativeColors() {}; // for test (7-color native mapping)
  91. // register a callback function to be called during _waitWhileBusy continuously.
  92. void setBusyCallback(void (*busyCallback)(const void*), const void* busy_callback_parameter = 0);
  93. static inline uint16_t gx_uint16_min(uint16_t a, uint16_t b)
  94. {
  95. return (a < b ? a : b);
  96. };
  97. static inline uint16_t gx_uint16_max(uint16_t a, uint16_t b)
  98. {
  99. return (a > b ? a : b);
  100. };
  101. void selectSPI(SPIClass& spi, SPISettings spi_settings);
  102. protected:
  103. void _reset();
  104. void _waitWhileBusy(const char* comment = 0, uint16_t busy_time = 5000);
  105. void _writeCommand(uint8_t c);
  106. void _writeData(uint8_t d);
  107. void _writeData(const uint8_t* data, uint16_t n);
  108. void _writeDataPGM(const uint8_t* data, uint16_t n, int16_t fill_with_zeroes = 0);
  109. void _writeDataPGM_sCS(const uint8_t* data, uint16_t n, int16_t fill_with_zeroes = 0);
  110. void _writeCommandData(const uint8_t* pCommandData, uint8_t datalen);
  111. void _writeCommandDataPGM(const uint8_t* pCommandData, uint8_t datalen);
  112. void _startTransfer();
  113. void _transfer(uint8_t value);
  114. void _endTransfer();
  115. // additions for read from OTP
  116. uint8_t _readData();
  117. void _readData(uint8_t* data, uint16_t n);
  118. protected:
  119. int16_t _cs, _dc, _rst, _busy, _busy_level;
  120. int16_t _sck, _mosi;
  121. uint32_t _busy_timeout;
  122. bool _diag_enabled, _pulldown_rst_mode;
  123. SPIClass* _pSPIx;
  124. SPISettings _spi_settings;
  125. bool _initial_write, _initial_refresh;
  126. bool _power_is_on, _using_partial_mode, _hibernating;
  127. bool _init_display_done;
  128. uint16_t _reset_duration;
  129. void (*_busy_callback)(const void*);
  130. const void* _busy_callback_parameter;
  131. #include <GxEPD2_EPD_friends.h>
  132. };
  133. #endif