GxEPD2_RotaryUsingBusyCallback.ino 5.1 KB

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