GxEPD2_RotaryUsingBusyCallback.ino 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. //
  12. // GxEPD2_RotaryUsingBusyCallback.ino: an example showing use of busyCallback to handle time-critical services
  13. // GxEPD2 uses busy wait on the BUSY line from EPDs by design,
  14. // to handle all combinations of paged display and differential refresh in a user friendly way.
  15. // busyCallback can be used to overcome this restriction caused by busy wait.
  16. // note: this feature is only available if the BUSY line is used, not if the default delay is used instead.
  17. // Supporting Arduino Forum Topics (closed, read only):
  18. // Good Display ePaper for Arduino: https://forum.arduino.cc/t/good-display-epaper-for-arduino/419657
  19. // Waveshare e-paper displays with SPI: https://forum.arduino.cc/t/waveshare-e-paper-displays-with-spi/467865
  20. //
  21. // Add new topics in https://forum.arduino.cc/c/using-arduino/displays/23 for new questions and issues
  22. // see GxEPD2_wiring_examples.h for wiring suggestions and examples
  23. // base class GxEPD2_GFX can be used to pass references or pointers to the display instance as parameter, uses ~1.2k more code
  24. // enable or disable GxEPD2_GFX base class
  25. #define ENABLE_GxEPD2_GFX 0
  26. // uncomment next line to use class GFX of library GFX_Root instead of Adafruit_GFX
  27. //#include <GFX.h>
  28. // Note: if you use this with ENABLE_GxEPD2_GFX 1:
  29. // uncomment it in GxEPD2_GFX.h too, or add #include <GFX.h> before any #include <GxEPD2_GFX.h>
  30. #include <GxEPD2_BW.h>
  31. #include <GxEPD2_3C.h>
  32. #include <GxEPD2_7C.h>
  33. #include <Fonts/FreeMonoBold9pt7b.h>
  34. // select the display class and display driver class in the following file (new style):
  35. #include "GxEPD2_display_selection_new_style.h"
  36. // include the BasicEncoder adapted for GxEPD2_RotaryUsingBusyCallback.ino example use, without interrupts on any processor
  37. #include "BasicEncoder.h"
  38. // define the pins to be used by the BasicEncoder
  39. const int8_t pinA = 5; // D1 on my Wemos D1 mini
  40. const int8_t pinB = 16; // D0 on my Wemos D1 mini
  41. //const int8_t pinA = 2; // on UNO
  42. //const int8_t pinB = 3; // on UNO
  43. BasicEncoder encoder(pinA, pinB);
  44. // busyCallback function called during waiting for BUSY to end, to service encoder
  45. void busyCallback(const void* p)
  46. {
  47. encoder.service();
  48. }
  49. // for handling alternative SPI pins (ESP32, RP2040) see example GxEPD2_Example.ino
  50. void setup()
  51. {
  52. Serial.begin(115200);
  53. Serial.println();
  54. Serial.println("setup");
  55. delay(100);
  56. display.init(); // disable diagnostics to avoid delay and catch most rotary pulses
  57. //display.init(115200); // default 10ms reset pulse, e.g. for bare panels with DESPI-C02
  58. //display.init(115200, true, 2, false); // USE THIS for Waveshare boards with "clever" reset circuit, 2ms reset pulse
  59. display.epd2.setBusyCallback(busyCallback); // register callback to be called during BUSY active time
  60. // first update should be full refresh
  61. helloWorld();
  62. delay(1000);
  63. }
  64. void loop()
  65. {
  66. encoder.service();
  67. int encoder_change = encoder.get_change();
  68. if (encoder_change)
  69. {
  70. showEncoderValue(encoder.get_count());
  71. }
  72. }
  73. const char HelloWorld[] = "Hello World!";
  74. void helloWorld()
  75. {
  76. //Serial.println("helloWorld");
  77. display.setRotation(1);
  78. display.setFont(&FreeMonoBold9pt7b);
  79. if (display.epd2.WIDTH < 104) display.setFont(0);
  80. display.setTextColor(GxEPD_BLACK);
  81. int16_t tbx, tby; uint16_t tbw, tbh;
  82. display.getTextBounds(HelloWorld, 0, 0, &tbx, &tby, &tbw, &tbh);
  83. // center bounding box by transposition of origin:
  84. uint16_t x = ((display.width() - tbw) / 2) - tbx;
  85. uint16_t y = ((display.height() - tbh) / 2) - tby;
  86. display.setFullWindow();
  87. display.firstPage();
  88. do
  89. {
  90. display.fillScreen(GxEPD_WHITE);
  91. display.setCursor(x, y);
  92. display.print(HelloWorld);
  93. }
  94. while (display.nextPage());
  95. //Serial.println("helloWorld done");
  96. }
  97. const char EncoderValue[] = "Encoder = ";
  98. #if (defined(ESP8266) || defined(ESP32)) && false
  99. #include <StreamString.h>
  100. #define PrintString StreamString
  101. #else
  102. class PrintString : public Print, public String
  103. {
  104. public:
  105. PrintString(const char s[]) : String(s){};
  106. size_t write(uint8_t data) override
  107. {
  108. return concat(char(data));
  109. };
  110. };
  111. #endif
  112. void showEncoderValue(int value)
  113. {
  114. //Serial.println("showEncoderValue");
  115. display.setRotation(1);
  116. display.setFont(&FreeMonoBold9pt7b);
  117. display.setTextColor(display.epd2.hasColor ? GxEPD_RED : GxEPD_BLACK);
  118. PrintString valueString(EncoderValue);
  119. valueString.print(value);
  120. static uint16_t ww = 0;
  121. static uint16_t wh = 0;
  122. int16_t tbx, tby; uint16_t tbw, tbh;
  123. display.getTextBounds(valueString, 0, 0, &tbx, &tby, &tbw, &tbh);
  124. if (tbw > ww) ww = tbw;
  125. if (tbh > wh) wh = tbh;
  126. uint16_t x = ((display.width() - tbw) / 2) - tbx;
  127. uint16_t y = ((display.height() * 3 / 4) - tbh / 2) - tby; // y is base line!
  128. uint16_t wx = (display.width() - ww) / 2;
  129. uint16_t wy = ((display.height() * 3 / 4) - wh / 2);
  130. display.setPartialWindow(wx, wy, ww, wh);
  131. display.firstPage();
  132. do
  133. {
  134. display.fillScreen(GxEPD_WHITE);
  135. display.setCursor(x, y);
  136. display.print(valueString);
  137. }
  138. while (display.nextPage());
  139. //Serial.println("showEncoderValue done");
  140. }