EncButton.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #pragma once
  2. #include <Arduino.h>
  3. #include "VirtEncButton.h"
  4. #include "io.h"
  5. // ===================== CLASS =====================
  6. class EncButton : public VirtEncButton {
  7. public:
  8. // настроить пины (энк, энк, кнопка, pinmode энк, pinmode кнопка)
  9. EncButton(uint8_t encA = 0, uint8_t encB = 0, uint8_t btn = 0, uint8_t modeEnc = INPUT, uint8_t modeBtn = INPUT_PULLUP, uint8_t btnLevel = LOW) {
  10. init(encA, encB, btn, modeEnc, modeBtn, btnLevel);
  11. }
  12. // настроить пины (энк, энк, кнопка, pinmode энк, pinmode кнопка)
  13. void init(uint8_t encA = 0, uint8_t encB = 0, uint8_t btn = 0, uint8_t modeEnc = INPUT, uint8_t modeBtn = INPUT_PULLUP, uint8_t btnLevel = LOW) {
  14. e0 = encA;
  15. e1 = encB;
  16. b = btn;
  17. EB_mode(e0, modeEnc);
  18. EB_mode(e1, modeEnc);
  19. EB_mode(b, modeBtn);
  20. setBtnLevel(btnLevel);
  21. initEnc(readEnc());
  22. }
  23. // ====================== TICK ======================
  24. // функция обработки для вызова в прерывании энкодера
  25. int8_t tickISR() {
  26. return VirtEncButton::tickISR(readEnc());
  27. }
  28. // функция обработки, вызывать в loop
  29. bool tick() {
  30. if (ef.read(EB_EISR)) return VirtEncButton::tick(EB_read(b));
  31. else return VirtEncButton::tick(readEnc(), EB_read(b));
  32. }
  33. // функция обработки без сброса событий
  34. bool tickRaw() {
  35. if (ef.read(EB_EISR)) return VirtEncButton::tickRaw(EB_read(b));
  36. else return VirtEncButton::tickRaw(readEnc(), EB_read(b));
  37. }
  38. // ====================== READ ======================
  39. // прочитать значение кнопки
  40. bool readBtn() {
  41. return EB_read(b) ^ bf.read(EB_INV);
  42. }
  43. // прочитать значение энкодера
  44. int8_t readEnc() {
  45. return EB_read(e0) | (EB_read(e1) << 1);
  46. }
  47. // ===================== PRIVATE =====================
  48. private:
  49. uint8_t e0, e1, b;
  50. };
  51. // ===================== T CLASS =====================
  52. template <uint8_t ENCA, uint8_t ENCB, uint8_t BTN>
  53. class EncButtonT : public VirtEncButton {
  54. public:
  55. // настроить пины (энк, энк, кнопка, pinmode энк, pinmode кнопка)
  56. EncButtonT(uint8_t modeEnc = INPUT, uint8_t modeBtn = INPUT_PULLUP, uint8_t btnLevel = LOW) {
  57. init(modeEnc, modeBtn, btnLevel);
  58. }
  59. // настроить пины (pinmode энк, pinmode кнопка)
  60. void init(uint8_t modeEnc = INPUT, uint8_t modeBtn = INPUT_PULLUP, uint8_t btnLevel = LOW) {
  61. EB_mode(ENCA, modeEnc);
  62. EB_mode(ENCB, modeEnc);
  63. EB_mode(BTN, modeBtn);
  64. setBtnLevel(btnLevel);
  65. initEnc(readEnc());
  66. }
  67. // ====================== TICK ======================
  68. // функция обработки для вызова в прерывании энкодера
  69. int8_t tickISR() {
  70. return VirtEncButton::tickISR(readEnc());
  71. }
  72. // функция обработки, вызывать в loop
  73. bool tick() {
  74. if (ef.read(EB_EISR)) return VirtEncButton::tick(EB_read(BTN));
  75. else return VirtEncButton::tick(readEnc(), EB_read(BTN));
  76. }
  77. // функция обработки без сброса событий
  78. bool tickRaw() {
  79. if (ef.read(EB_EISR)) return VirtEncButton::tickRaw(EB_read(BTN));
  80. else return VirtEncButton::tickRaw(readEnc(), EB_read(BTN));
  81. }
  82. // ====================== READ ======================
  83. // прочитать значение кнопки
  84. bool readBtn() {
  85. return EB_read(BTN) ^ bf.read(EB_INV);
  86. }
  87. // прочитать значение энкодера
  88. int8_t readEnc() {
  89. return EB_read(ENCA) | (EB_read(ENCB) << 1);
  90. }
  91. // ===================== PRIVATE =====================
  92. private:
  93. };