GxEPD2_RotaryUsingBusyCallback.ino 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. BasicEncoder encoder(pinA, pinB);
  40. // busyCallback function called during waiting for BUSY to end, to service encoder
  41. void busyCallback(const void* p)
  42. {
  43. encoder.service();
  44. }
  45. void setup()
  46. {
  47. Serial.begin(115200);
  48. Serial.println();
  49. Serial.println("setup");
  50. delay(100);
  51. display.init(115200); // default 10ms reset pulse, e.g. for bare panels with DESPI-C02
  52. //display.init(115200, true, 2, false); // USE THIS for Waveshare boards with "clever" reset circuit, 2ms reset pulse
  53. display.epd2.setBusyCallback(busyCallback); // register callback to be called during BUSY active time
  54. // first update should be full refresh
  55. helloWorld();
  56. delay(1000);
  57. }
  58. void loop()
  59. {
  60. encoder.service();
  61. int encoder_change = encoder.get_change();
  62. if (encoder_change)
  63. {
  64. showEncoderValue(encoder.get_count());
  65. }
  66. }
  67. const char HelloWorld[] = "Hello World!";
  68. void helloWorld()
  69. {
  70. //Serial.println("helloWorld");
  71. display.setRotation(1);
  72. display.setFont(&FreeMonoBold9pt7b);
  73. if (display.epd2.WIDTH < 104) display.setFont(0);
  74. display.setTextColor(GxEPD_BLACK);
  75. int16_t tbx, tby; uint16_t tbw, tbh;
  76. display.getTextBounds(HelloWorld, 0, 0, &tbx, &tby, &tbw, &tbh);
  77. // center bounding box by transposition of origin:
  78. uint16_t x = ((display.width() - tbw) / 2) - tbx;
  79. uint16_t y = ((display.height() - tbh) / 2) - tby;
  80. display.setFullWindow();
  81. display.firstPage();
  82. do
  83. {
  84. display.fillScreen(GxEPD_WHITE);
  85. display.setCursor(x, y);
  86. display.print(HelloWorld);
  87. }
  88. while (display.nextPage());
  89. //Serial.println("helloWorld done");
  90. }
  91. const char EncoderValue[] = "Encoder = ";
  92. #if defined(ESP8266) || defined(ESP32)
  93. #include <StreamString.h>
  94. #define PrintString StreamString
  95. #else
  96. class PrintString : public Print, public String
  97. {
  98. public:
  99. size_t write(uint8_t data) override
  100. {
  101. return concat(char(data));
  102. };
  103. };
  104. #endif
  105. void showEncoderValue(int value)
  106. {
  107. //Serial.println("showEncoderValue");
  108. display.setRotation(1);
  109. display.setFont(&FreeMonoBold9pt7b);
  110. display.setTextColor(display.epd2.hasColor ? GxEPD_RED : GxEPD_BLACK);
  111. PrintString valueString(EncoderValue);
  112. valueString.print(value);
  113. static uint16_t ww = 0;
  114. static uint16_t wh = 0;
  115. int16_t tbx, tby; uint16_t tbw, tbh;
  116. display.getTextBounds(valueString, 0, 0, &tbx, &tby, &tbw, &tbh);
  117. if (tbw > ww) ww = tbw;
  118. if (tbh > wh) wh = tbh;
  119. uint16_t x = ((display.width() - tbw) / 2) - tbx;
  120. uint16_t y = ((display.height() * 3 / 4) - tbh / 2) - tby; // y is base line!
  121. uint16_t wx = (display.width() - ww) / 2;
  122. uint16_t wy = ((display.height() * 3 / 4) - wh / 2);
  123. display.setPartialWindow(wx, wy, ww, wh);
  124. display.firstPage();
  125. do
  126. {
  127. display.fillScreen(GxEPD_WHITE);
  128. display.setCursor(x, y);
  129. display.print(valueString);
  130. }
  131. while (display.nextPage());
  132. //Serial.println("showEncoderValue done");
  133. }