forked from cgwood/ArdustationMega
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RotaryEncoder.ino
145 lines (127 loc) · 4.1 KB
/
RotaryEncoder.ino
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
// -*- Mode: C++; c-basic-offset: 8; indent-tabs-mode: nil -*-
//-
// Copyright (c) 2010 Michael Smith. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
/// @file RotaryEncoder.pde
/// @brief rotary encoder class
RotaryEncoder::RotaryEncoder(int pinA, int pinB, int gndPin) :
_pinA(pinA),
_pinB(pinB)
{
pinMode(pinA, INPUT);
pinMode(pinB, INPUT);
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
if (-1 != gndPin) {
pinMode(gndPin, OUTPUT);
digitalWrite(gndPin, LOW);
}
}
void
RotaryEncoder::configure(volatile int *variable, int upperBound, int lowerBound, int prescaler, void (* callback)(int value))
{
// save parameters
_variable = variable;
_upperBound = upperBound;
_lowerBound = lowerBound;
_prescaler = prescaler;
_callback = callback;
// initialise
_accumulator = 0;
_variable_old = *_variable;
_prevPins = (digitalRead(_pinA) ? 2 : 0) | (digitalRead(_pinB) ? 1 : 0);
// if we have no variable but we have a callback, use our private variable
if ((NULL == _variable) && (NULL != _callback))
_variable = &_value;
}
uint8_t
RotaryEncoder::haschanged(void)
{
if (_variable_old != *_variable)
{
_variable_old = *_variable;
return 1;//Page::ENCODER;
}
else
{
return 0;
}
}
void
RotaryEncoder::update(void)
{
uint8_t pa1, pa2, pb1, pb2;
uint8_t pins, index;
// rudimentary debounce
do {
pa1 = digitalRead(_pinA);
pb1 = digitalRead(_pinB);
pa2 = digitalRead(_pinA);
pb2 = digitalRead(_pinB);
}
while ((pa1 != pa2) || (pb1 != pb2));
// compute LUT and save new pin values
pins = (pa1 ? 2 : 0) | (pb1 ? 1 : 0);
index = (pins << 2) | _prevPins;
_prevPins = pins;
// Serial.print("pins ");
// Serial.println((int)pins);
// is the transition valid?
if (_validCode & (1 << index)) {
// what is the direction?
if (_directionCode & (1 << index)) {
_accumulator++;
}
else {
_accumulator--;
}
// Serial.print("valid ");
// Serial.print((int)index);
// Serial.print(" pins ");
// Serial.print((int)pins);
// Serial.print(" accumulator ");
// Serial.println(_accumulator);
// Serial.print(" variable ");
// Serial.println(*_variable);
// handle prescaling
if (0 == (_prescaler - _accumulator)) {
if (*_variable < _upperBound)
(*_variable)++;
_accumulator = 0;
}
else if (0 == (-_prescaler - _accumulator)) {
if (*_variable > _lowerBound)
(*_variable)--;
_accumulator = 0;
}
else {
// no change to value, nothing more to do
return;
}
// Serial.print(" variable ");
// Serial.println(*_variable);
// handle callback
if (NULL != _callback)
_callback(*_variable);
}
}