-
Notifications
You must be signed in to change notification settings - Fork 3
/
Encoder.h
157 lines (117 loc) · 3.67 KB
/
Encoder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#ifndef Encoder_H
#define Encoder_H
class Debouncer
{
private:
byte _keepValue;
byte _bounce;
public:
Debouncer():_keepValue(0), _bounce(0) {}
bool isDebounced( byte value, byte debounceCount);
};
#define DEBOUNCE_COUNT 3 // This is the number of consecutive reads that must be the same before accepting a pin change
#define INTERRUPT_PERIOD 25 // This is cpu_clock_frequency / prescaler / desired_interrupt_frequency
// for example, set to 133 for 0.5ms (2kHz) on a 16MHz processor: (16000000L / 64 / 2000 -> 133
// for example, set to 50 for 0.2ms (5kHz) on a 16MHz processor: (16000000L / 64 / 5000 -> 50
// for example, set to 25 for 0.1ms (10kHz) on a 16MHz processor: (16000000L / 64 / 10000 -> 25
class Encoder
{
public:
Encoder( byte pinA, byte pinB, byte pinP ) :
_pinA( pinA ), _pinB( pinB ), _pinP( pinP ),
_encoderValueVL(0), _encoderTickVL(0), _encoderTick2VL(0),
_encoderButtonVL(false),
_encoderValue(0), _encoderTick(0) {}
public:
int delta();
int deltaTick();
int deltaTick2();
bool button() const { return _encoderButtonVL; }
private:
void init();
void compute();
private:
volatile unsigned int _encoderValueVL = 0;
volatile unsigned int _encoderTickVL = 0;
volatile unsigned int _encoderTick2VL = 0;
volatile bool _encoderButtonVL = false;
volatile byte _encoderStVL;
unsigned int _encoderValue = 0;
unsigned int _encoderTick = 0;
unsigned int _encoderTick2 = 0;
byte _pinA, _pinB, _pinP;
Debouncer stDebouncer, pDebouncer;
public:
friend class EncoderInterruptClass;
};
class EncoderInterruptClass
{
public:
EncoderInterruptClass() : _numEncoders(0) {}
public:
void begin( Encoder **encoderRefs, int numEncoders);
inline void begin( Encoder *encoder) { begin( &encoder, 1 ); }
private:
int _numEncoders;
Encoder **_encoderArray;
void computeAll();
public:
friend void computeEncoder();
};
extern EncoderInterruptClass EncoderInterrupt;
/* USAGE FOR ONE ENCODER:
// define the input pins
#define pinA 19
#define pinB 20
#define pinP 21
// create an encoder object initialized with their pins
Encoder encoder( pinA, pinB, pinP );
// setup code
void setup()
{
// start the encoder time interrupts
EncoderInterrupt.begin( &encoder );
}
// loop code
void loop()
{
// read the debounced value of the encoder button
bool pb = encoder.button();
// get the encoder variation since our last check, it can be positive or negative, or zero if the encoder didn't move
// only call this once per loop cicle, or at any time you want to know any incremental change
int delta = encoder.delta();
// add the delta value to the variable we are controlling
myEncoderControlledVariable += delta;
// do stuff with the updated value
// that's it
}
*/
/* USAGE FOR SEVERAL ENCODERS:
// create a number of encoder objects initialized with their pins
Encoder encoder1( 19, 20, 21 );
Encoder encoder2( 4, 5, 6 );
// put their references in an array
Encoder *encoderArray[] = { &encoder1, &encoder2 };
// setup code
void setup()
{
// start the encoder time interrupts
EncoderInterrupt.begin( encoderArray, 2 );
}
// loop code
void loop()
{
// read the debounced value of the encoder buttons
bool pb1 = encoder1.button();
bool pb2 = encoder2.button();
// get the encoders deltas (can be positive or negative)
int deltaValue1 = encoder1.delta();
int deltaValue2 = encoder2.delta();
// add the deltas value to the variables you are controlling
myEncoderControlledVariable1 += delta1;
myEncoderControlledVariable2 += delta2;
// do stuff with the updated values
// that's it
}
*/
#endif