VizBlocks
RotaryEncoder.hpp
Go to the documentation of this file.
1 #ifndef ROTARYENCODER_h
2 #define ROTARYENCODER_h
3 
9 {
10 
11  // Private members:
12 
13  int _pinA;
14  int _pinB;
15  int _id;
16 
17  volatile int _state[2];
18  volatile int _position;
19 
20  volatile bool _inputFlag = false;
21  bool _changeFlag = false;
22 
23  unsigned long _previousTimer;
24  int _interval = 200;
25 
26  void (*_cb)(RotaryEncoder*, uint8_t, int); // Callback function
27 
28  // {newPin2, newPin1, oldPin2, oldPin1}
29  int movements[5][4][4] = {
30  { // No movement
31  {0, 0, 0, 0},
32  {0, 1, 0, 1},
33  {1, 0, 1, 0},
34  {1, 1, 1, 1}
35  },
36  { // +1
37  {0, 0, 0, 1},
38  {0, 1, 1, 1},
39  {1, 0, 0, 0},
40  {1, 1, 1, 0}
41  },
42  { // -1
43  {0, 0, 1, 0},
44  {0, 1, 0, 0},
45  {1, 0, 1, 1},
46  {1, 1, 0, 1}
47  },
48  { // +2
49  {0, 0, 1, 1},
50  {1, 1, 0, 0}
51  },
52  { // -2
53  {0, 1, 1, 0},
54  {1, 0, 0, 1}
55  },
56  };
57 
58  // Private Functions:
62  void _setState(int a, int b);
66  void _incrementPosition(int delta);
70  int _findChange(int state1[2], volatile int state2[2]);
74  boolean _compareArrays(int a[4], int b[4]);
75 
76 public:
77 
78  // Public members:
79 
80  static const uint8_t kEventStableUpdate = 0;
81  static const uint8_t kEventUnstableUpdate = 1;
82 
83  //Public Functions:
84 
85  RotaryEncoder(int pinA, int pinB, int id = 99) : _pinA(pinA), _pinB(pinB), _id(id)
86  {
87  pinMode(_pinA, INPUT_PULLUP);
88  pinMode(_pinB, INPUT_PULLUP);
89 
90  _previousTimer = millis();
91  _setState(digitalRead(_pinA), digitalRead(_pinB));
92  setPosition(0);
93  }
97  void initInterrupts(void (*function)());
101  void setEventHandler(void (*function)(RotaryEncoder*, uint8_t, int));
109  int getId();
113  void setPosition(int value);
117  void check();
118 
119  void ICACHE_RAM_ATTR tick();
120 
121 };
122 
123 #endif
RotaryEncoder::tick
void ICACHE_RAM_ATTR tick()
RotaryEncoder::kEventUnstableUpdate
static const uint8_t kEventUnstableUpdate
Definition: RotaryEncoder.hpp:81
RotaryEncoder::setEventHandler
void setEventHandler(void(*function)(RotaryEncoder *, uint8_t, int))
What does this do?
RotaryEncoder::kEventStableUpdate
static const uint8_t kEventStableUpdate
Definition: RotaryEncoder.hpp:80
RotaryEncoder::initInterrupts
void initInterrupts(void(*function)())
What does this do?
RotaryEncoder
Definition: RotaryEncoder.hpp:9
RotaryEncoder::setPosition
void setPosition(int value)
What does this do?
RotaryEncoder::RotaryEncoder
RotaryEncoder(int pinA, int pinB, int id=99)
Definition: RotaryEncoder.hpp:85
RotaryEncoder::check
void check()
What does this do?
RotaryEncoder::getId
int getId()
What does this do?
RotaryEncoder::getPostition
int getPostition()
What does this do?