-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotenc.c
188 lines (164 loc) · 3.87 KB
/
rotenc.c
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* rotenc.c
* This file is part of RotaryEncoder.
*
* RotaryEncoder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* your option) any later version.
*
* RotaryEncoder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RotaryEncoder. If not, see <http://www.gnu.org/licenses/>.
*
*
* Created: Oct 10, 2015
* Author: Thomas Heßling <[email protected]>
*
*/
#include <util/atomic.h>
#include "rotenc.h"
// a local static variable
static rotenc_state re;
/*
* Initialize the ports etc.
*/
void
rotenc_init()
{
// configure port direction
ROTENC_DDR &= ~((1<<ROTENC_PIN_A)|(1<<ROTENC_PIN_B));
// activate internal pull ups
ROTENC_PORT |= (1<<ROTENC_PIN_A)|(1<<ROTENC_PIN_B);
re.position = 0;
re.state = ROTENC_STATE_WAIT;
re.debounce_count = 0;
}
/*
* This is the magic part where we check the pins and act according to the
* current state we're in.
*/
void
rotenc_update()
{
uint8_t pinA, pinB;
// invert signals, they are active low
pinA = !(ROTENC_PIN & (1<<ROTENC_PIN_A));
pinB = !(ROTENC_PIN & (1<<ROTENC_PIN_B));
// if both are FALSE, return to waiting state
if (! pinA && ! pinB)
{
re.state = ROTENC_STATE_WAIT;
re.debounce_count = 0;
}
// initial state, we are waiting for something to happen
if (re.state == ROTENC_STATE_WAIT)
{
// only one pin can be positive here, otherwise something
// weird happened (bounce, noise, etc.)
if (pinA && ! pinB)
re.state = ROTENC_STATE_WAIT_ASTAB;
//re.state = ROTENC_STATE_WAIT_B;
else if (! pinA && pinB)
re.state = ROTENC_STATE_WAIT_BSTAB;
//re.state = ROTENC_STATE_WAIT_A;
}
// we are waiting for pin A to stabilize, just count up until
// the limit is reached and check the status of pin A again
if (re.state == ROTENC_STATE_WAIT_ASTAB)
{
if (re.debounce_count < ROTENC_DEBOUNCE)
re.debounce_count++;
else
{
// there is only valid state here (pin A low/true and B high/false)
if (pinA && ! pinB)
re.state = ROTENC_STATE_WAIT_B;
else
re.state = ROTENC_STATE_WAIT;
}
}
// waiting for stabilization of pin B is equivalent
if (re.state == ROTENC_STATE_WAIT_BSTAB)
{
if (re.debounce_count < ROTENC_DEBOUNCE)
re.debounce_count++;
else
{
// there is only valid state here (pin A low/true and B high/false)
if (! pinA && pinB)
re.state = ROTENC_STATE_WAIT_A;
else
re.state = ROTENC_STATE_WAIT;
}
}
// if we got this far and both pins are true/low we got a completed step, either
// CW or CCW depending on the wait state
if (re.state == ROTENC_STATE_WAIT_A && pinA && pinB)
{
re.state = ROTENC_STATE_ROT_CCW;
rotenc_dec_position();
}
if (re.state == ROTENC_STATE_WAIT_B && pinA && pinB)
{
re.state = ROTENC_STATE_ROT_CW;
rotenc_inc_position();
}
}
/*
* manually increase the current position by one
*/
void
rotenc_inc_position()
{
// only increase up to max value possible
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
if (re.position < 0xffff)
re.position++;
}
}
/*
* manually decrease the current position by one
*/
void
rotenc_dec_position()
{
// only decrease down to 0
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
if (re.position > 0)
re.position--;
}
}
/*
* query the state (you don't really need this...)
*/
uint8_t
rotenc_get_state()
{
return re.state;
}
/*
* query the current position
*/
uint16_t
rotenc_get_position()
{
return re.position;
}
/*
* set the position to an arbitrary value
*/
void
rotenc_set_position(uint16_t pos)
{
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
re.position = pos;
}
}