GxEPD2x_MixedTest.ino 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Display Library example 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. // experimental example GxEPD2x_MixedTest.ino: proof of concept for differential refresh on capable 3-color panels
  12. // see also GxEPD2x_FastBlackWhiteOnColor.ino: differential refresh on capable 3-color panels
  13. // see GxEPD2_wiring_examples.h for wiring suggestions and examples
  14. // if you use a different wiring, you need to adapt the constructor parameters!
  15. // uncomment next line to use class GFX of library GFX_Root instead of Adafruit_GFX
  16. //#include <GFX.h>
  17. #include <GxEPD2_BW.h>
  18. #include <GxEPD2_3C.h>
  19. #include <Fonts/FreeMonoBold9pt7b.h>
  20. // e.g. for Wemos D1 mini:
  21. #if defined (ESP8266)
  22. GxEPD2_3C<GxEPD2_213_Z19c, GxEPD2_213_Z19c::HEIGHT> display(GxEPD2_213_Z19c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEW0213Z19
  23. //GxEPD2_3C<GxEPD2_290_Z13c, GxEPD2_290_Z13c::HEIGHT> display(GxEPD2_290_Z13c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEH029Z13
  24. #endif
  25. #if defined(ESP32)
  26. //GxEPD2_3C<GxEPD2_213_Z19c, GxEPD2_213_Z19c::HEIGHT> display(GxEPD2_213_Z19c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEW0213Z19
  27. //GxEPD2_3C<GxEPD2_290_Z13c, GxEPD2_290_Z13c::HEIGHT> display(GxEPD2_290_Z13c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEH029Z13
  28. #endif
  29. const uint16_t window_x = 8;
  30. const uint16_t window_y = 10;
  31. const uint16_t window_width = 80;
  32. const uint16_t window_height = 24;
  33. // we need a canvas with correct explicit assignment operator
  34. class MyCanvas1 : public GFXcanvas1
  35. {
  36. private:
  37. uint32_t bytes;
  38. public:
  39. MyCanvas1(uint16_t w, uint16_t h) : GFXcanvas1(w, h), bytes(((w + 7) / 8) * h) {};
  40. MyCanvas1& operator=(const MyCanvas1& from)
  41. {
  42. if (bytes == from.bytes)
  43. {
  44. memcpy(getBuffer(), from.getBuffer(), bytes);
  45. } // else would need realloc, but buffer is private
  46. return *this;
  47. };
  48. };
  49. //GFXcanvas1 current_bw_window(window_width, window_height);
  50. //GFXcanvas1 previous_bw_window(window_width, window_height);
  51. MyCanvas1 current_bw_window(window_width, window_height);
  52. MyCanvas1 previous_bw_window(window_width, window_height);
  53. void setup()
  54. {
  55. Serial.begin(115200);
  56. Serial.println();
  57. display.init(115200);
  58. helloWorld();
  59. if (current_bw_window.getBuffer() && previous_bw_window.getBuffer())
  60. {
  61. current_bw_window.fillScreen(GxEPD_WHITE);
  62. previous_bw_window.fillScreen(GxEPD_WHITE);
  63. // only needed if window is not yet white
  64. //display.drawImage(current_bw_window.getBuffer(), previous_bw_window.getBuffer(), window_x, window_y, window_width, window_height);
  65. for (uint16_t i = 0; i < 100; i++)
  66. {
  67. showValue(i);
  68. }
  69. }
  70. else
  71. {
  72. Serial.println("GxEPD2x_MixedTest: canvas buffer allocation failed!");
  73. Serial.print("current_bw_window 0x"); Serial.println(uint32_t(current_bw_window.getBuffer()), HEX);
  74. Serial.print("previous_bw_window 0x"); Serial.println(uint32_t(previous_bw_window.getBuffer()), HEX);
  75. }
  76. display.hibernate();
  77. }
  78. void showValue(uint16_t value)
  79. {
  80. current_bw_window.setFont(&FreeMonoBold9pt7b);
  81. current_bw_window.setTextColor(GxEPD_BLACK);
  82. current_bw_window.fillScreen(GxEPD_WHITE);
  83. current_bw_window.setCursor(20, FreeMonoBold9pt7b.yAdvance);
  84. current_bw_window.print(value);
  85. display.writeImage(previous_bw_window.getBuffer(), current_bw_window.getBuffer(), window_x, window_y, window_width, window_height);
  86. display.epd2.refresh_bw(window_x, window_y, window_width, window_height);
  87. // this needs a canvas with correct explicit assignment operator
  88. previous_bw_window = current_bw_window;
  89. //Serial.print("current_bw_window 0x"); Serial.println(uint32_t(current_bw_window.getBuffer()), HEX);
  90. //Serial.print("previous_bw_window 0x"); Serial.println(uint32_t(previous_bw_window.getBuffer()), HEX);
  91. delay(1000);
  92. }
  93. const char HelloWorld[] = "Hello World!";
  94. void helloWorld()
  95. {
  96. display.setRotation(1);
  97. display.setFont(&FreeMonoBold9pt7b);
  98. display.setTextColor(GxEPD_RED);
  99. int16_t tbx, tby; uint16_t tbw, tbh;
  100. display.getTextBounds(HelloWorld, 0, 0, &tbx, &tby, &tbw, &tbh);
  101. // center the bounding box by transposition of the origin:
  102. uint16_t x = ((display.width() - tbw) / 2) - tbx;
  103. uint16_t y = ((display.height() - tbh) / 2) - tby;
  104. display.setFullWindow();
  105. display.firstPage();
  106. do
  107. {
  108. display.fillScreen(GxEPD_WHITE);
  109. display.setCursor(x, y);
  110. display.print(HelloWorld);
  111. }
  112. while (display.nextPage());
  113. }
  114. void loop() {};