-
Notifications
You must be signed in to change notification settings - Fork 0
/
RotaryEncoder.cpp
128 lines (95 loc) · 3.48 KB
/
RotaryEncoder.cpp
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
/**************************************************************************************************
***************************************************************************************************
***************************************************************************************************
RotaryEncoder
ver. 1.1
Updated 8-5-2016
This library is being shared under the Creative Commons Share-Alike liscence agreement.
[url]http://creativecommons.org/licenses/by-sa/3.0/[/url]
If you have questions, or find errors in the code, or wish to make a contribution towards future development, please contact me at matthewssrpat "at" comcast.net
******************************************************************************************************
******************************************************************************************************
******************************************************************************************************/
// MEMBER FUNCTIONS
#include "RotaryEncoder.h"
int* RotaryEncoder::attached_val = NULL;
boolean RotaryEncoder::A_set = false;
boolean RotaryEncoder::B_set = false;
boolean RotaryEncoder::rotating = false;
byte RotaryEncoder::bit_maskA=0;
byte RotaryEncoder::bit_maskB=0;
volatile byte* RotaryEncoder::registerA=0;
volatile byte* RotaryEncoder::registerB=0;
byte RotaryEncoder::PinA = 0;
byte RotaryEncoder::PinB = 0;
RotaryEncoder::RotaryEncoder()
{
// do nothing
}
RotaryEncoder::RotaryEncoder(RotaryEncoder& _object)
{
// do nothing
}
RotaryEncoder::RotaryEncoder(byte _pin1, byte _pin2)
{
setupPins(_pin1, _pin2);
}
void RotaryEncoder::doEncoderA(){
if ( RotaryEncoder::rotating ) delay (2);
if(digitalRead(RotaryEncoder::PinA) != RotaryEncoder::A_set) {
RotaryEncoder::A_set = !RotaryEncoder::A_set;
if ( RotaryEncoder::A_set && !RotaryEncoder::B_set)
*RotaryEncoder::attached_val+=1;
RotaryEncoder::rotating = false;
delay(2);
}
}
void RotaryEncoder::doEncoderB(){
if ( RotaryEncoder::rotating ) delay (2);
if(digitalRead(RotaryEncoder::PinB) != RotaryEncoder::B_set) {
RotaryEncoder::B_set = !RotaryEncoder::B_set;
if (RotaryEncoder::B_set && !RotaryEncoder::A_set)
*RotaryEncoder::attached_val-=1;
RotaryEncoder::rotating = false;
if(*RotaryEncoder::attached_val < 0)
*RotaryEncoder::attached_val = 0;
delay(2);
}
}
void RotaryEncoder::attachFunction()
{
attachInterrupt(0, RotaryEncoder::doEncoderA, CHANGE);
attachInterrupt(1, RotaryEncoder::doEncoderB, CHANGE);
}
void RotaryEncoder::detachFunction()
{
detachInterrupt(0);
detachInterrupt(1);
}
void RotaryEncoder::detachVariable()
{
detachFunction();
attached_val = NULL;
}
void RotaryEncoder::attachVariable(int& _var)
{
attached_val = &(_var);
attachFunction();
}
void RotaryEncoder::setupPins(byte _pin1, byte _pin2)
{
byte portA;
byte portB;
pinMode(_pin1, INPUT_PULLUP);
pinMode(_pin2, INPUT_PULLUP);
digitalWrite(_pin1, HIGH);
digitalWrite(_pin2, HIGH);
portA=digitalPinToPort(_pin1);
portB=digitalPinToPort(_pin2);
RotaryEncoder::PinA = _pin1;
RotaryEncoder::PinB = _pin2;
RotaryEncoder::bit_maskA = digitalPinToBitMask(_pin1);
RotaryEncoder::bit_maskB = digitalPinToBitMask(_pin2);
RotaryEncoder::registerA = portInputRegister(portA);
RotaryEncoder::registerB = portInputRegister(portB);
}