-
Notifications
You must be signed in to change notification settings - Fork 1
/
Encoder.cpp
243 lines (205 loc) · 6.6 KB
/
Encoder.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//
// Encoder fuctions
//
#include "RadioControl.h"
//#DEFINE DEBUG_ENC
//////////////////////////////////////////////////////////////////////
// Rotary Enconder //
//////////////////////////////////////////////////////////////////////
Rotary encoder = Rotary(ENCODER_A, ENCODER_B, ENCODER_BTN); // Setup Encoder
//////////////////////////////////////////////////////////////////////
// //
// Rotary Encoder Variables //
// Number of clockwise and counterclockwise ticks //
// Delta between successive measurements //
// //
//////////////////////////////////////////////////////////////////////
volatile int encoder_count = 0; // count of encoder clicks +1 for CW, -1 for CCW (volatile since used in ISR)
int prev_encoder_count = 0; // used to measure changes over time
int encoder_delta = 0; // differrnce between successive checks of encoder count
bool incrementChanged = false;
// when multiplied by tuning increment tells what the frequency change on
// on the active VFO will be
// Encoder button control
extern byte EncButtonState;
extern byte lastEncButtonState;
///////////////////////////////////////////////////////////
// ************* ISR **************** //
// Interrupt service routine, called on encoder movement //
// Only interested in completed clicks //
// +1 for Clocwwise //
// -1 for Counter Clockwise //
// Ignore intermediate values //
///////////////////////////////////////////////////////////
#if _BOARDTYPE != Every
ISR(PCINT2_vect) {
unsigned char result = encoder.process();
if (result == DIR_CW) {
encoder_count++;
} else if (result == DIR_CCW) {
encoder_count--;
}
}
#else
#define PA0_INTERRUPT PORTA.INTFLAGS & PIN0_bm
#define PA0_CLEAR_INTERRUPT_FLAG PORTA.INTFLAGS &= PIN0_bm
#define PF5_INTERRUPT PORTF.INTFLAGS & PIN5_bm
#define PF5_CLEAR_INTERRUPT_FLAG PORTF.INTFLAGS &= PIN5_bm
ISR(PORTA_PORT_vect) {
unsigned char result = encoder.process();
if (PA0_INTERRUPT)
PA0_CLEAR_INTERRUPT_FLAG;
if (result == DIR_CW) {
encoder_count++;
} else if (result == DIR_CCW) {
encoder_count--;
}
}
ISR(PORTF_PORT_vect) {
unsigned char result = encoder.process();
if (PF5_INTERRUPT)
PF5_CLEAR_INTERRUPT_FLAG;
if (result == DIR_CW) {
encoder_count++;
} else if (result == DIR_CCW) {
encoder_count--;
}
}
#endif
//*******************Setup Interrupt Service Routine********************
void setupEncoderISR() {
cli();
// Set up for the rotary encoder interrupts
#if _BOARDTYPE != Every
PCICR |= (1 << PCIE2);
PCMSK2 |= (1 << PCINT18) | (1 << PCINT19);
#endif
#if _BOARDTYPE == Every
PORTA.PIN0CTRL |= PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
PORTF.PIN5CTRL |= PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;
#endif
sei();
}
//********************CheckEncoder*******************************************
// roundedF - Calculates new frequency
// After an increment change rounds up or down to the next even increment
// Otherwise just changes increments or decrements the frequency
//
uint32_t roundedF(uint32_t freq, int32_t delta) {
uint32_t f = freq;
/* if (incrementChanged) {
if (f % increment) {
if (delta > 0 ) {
f = f + increment - (f % increment);
} else {
f = f - (f % increment);
}
}
incrementChanged = false;
} else {
f = f + delta;
}
*/
if (f % increment) {
if (delta > 0 ) {
f = f + increment - (f % increment);
} else {
f = f - (f % increment);
}
} else {
f = f + delta;
}
return f;
}
void AdjustVFO(long delta) {
switch (active_vfo) {
case VFOA:
// vfoAfreq = vfoAfreq + delta;
vfoAfreq = roundedF(vfoAfreq, delta);
setVFO(vfoAfreq);
displayActVFO(vfoAfreq);
break;
case VFOB:
// vfoBfreq = vfoBfreq + delta;
vfoBfreq = roundedF(vfoBfreq, delta);
setVFO(vfoBfreq);
displayActVFO(vfoBfreq);
break;
}
startSettingsTimer();
}
void CheckEncoder() {
int current_count = encoder_count; // grab the current encoder_count
int32_t encoder_delta = 0;
if (current_count != prev_encoder_count) { // if there is any change in the encoder coount
#ifdef DEBUG_ENC
sprintf(debugmsg, "VFOA: %ld", vfoAfreq);
Serial.println(debugmsg);
#endif
//
// Calculate the delta (how many click positive or negaitve)
//
encoder_delta = current_count - prev_encoder_count;
//
// Adjust the currently selected VFO
//
AdjustVFO(encoder_delta * increment);
#ifdef DEBUG
sprintf(debugmsg, "current_count: %d, New VFOA: %ld", current_count, vfoAfreq);
Serial.println(debugmsg);
#endif
prev_encoder_count = current_count; // save the current_count for next time around
}
}
//********************CheckIncrement*******************************************
// Cycle through tuning increment values on button press
// 10, 100, 1K, 10K, 100K, 1M
//********************CheckIncrement*******************************************
void AdvanceIncrement() {
if (increment == 10) {
increment = 100;
}
else if (increment == 100) {
increment = 1000;
}
else if (increment == 1000) {
increment = 10000;
}
else if (increment == 10000) {
increment = 100000;
}
else if (increment == 100000) {
increment = 1000000;
}
else {
increment = 10;
}
displayIncr(increment);
incrementChanged = true;
startSettingsTimer();
}
void CheckIncrement () {
EncButtonState = encoder.buttonState();
//EncButtonState = digitalRead(ENCODER_BTN);
#ifdef DEBUG
sprintf(debugmsg, "Encoder button state: %d", EncButtonState);
Serial.println(debugmsg);
Delay(1000);
#endif
if (EncButtonState != lastEncButtonState) {
#ifdef DEBUG
sprintf(debugmsg, "Encoder button state: %d", EncButtonState);
Serial.println(debugmsg);
#endif
if (EncButtonState == LOW) {
AdvanceIncrement();
}
lastEncButtonState = EncButtonState;
Delay(50);
EncButtonState = encoder.buttonState(); //debounce
//EncButtonState = digitalRead(ENCODER_BTN);
}
}
void setupEncoder() {
setupEncoderISR();
}