-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.h
executable file
·278 lines (218 loc) · 7.26 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
//#include "U8glib.h"
#include "global.h"
//#include "lcdstrings.h"
#include "dollyController.h"
*/
void rotaryEncoderSetup() {
pinMode(encoderPin1, INPUT);
pinMode(encoderPin2, INPUT);
digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
digitalWrite(encoderPin2, HIGH); //turn pullup resistor on
//call updateEncoder() when any high/low changed seen
//on interrupt 0 (atmega328:pin 2 / atmega644p: pin 10-->PD2),
//or interrupt 1 (atmega328:pin 3 / atmega644p: pin 11-->PD3)
attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);
}
/***********************************************************************************/
void updateEncoder() {
/*
Debounce.
Intentamos controlar el tiempo que ha pasado desde el ultimo disparo del encoder
En caso de que sea menor que un umbral, lo identificamos como ruido y no hacemos
nada.
*/
if (millis()-lastDetent < 2) return;
int MSB = digitalRead(encoderPin1); //MSB = most significant bit
int LSB = digitalRead(encoderPin2); //LSB = least significant bit
int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
/*
El valor de RTmenuItem ha podido cambiar durante el procesamiento de alguna función,
por lo que antes de actualizarlo, lo inicializamos al valor real menuItem
*/
if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) {
encoderValue ++;
if (encoderValue %4 == 0 ) {
//RTmenuItem=menuItem;
if (config.encoderSpin) RTmenuItem++; else RTmenuItem--;
}
}
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) {
encoderValue --;
if (encoderValue % 4 == 0 ) {
//RTmenuItem=menuItem;
if (!config.encoderSpin) RTmenuItem++; else RTmenuItem--;
}
}
lastEncoded = encoded; //store this value for next time
lastDetent=millis();
}
void resetEncoder (void) {
encoderValue=0;
RTmenuItem=0;
menuItem=0;
}
void disableEncoder (void) {
detachInterrupt(0);
detachInterrupt(1);
}
void enableEncoder (void) {
attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);
}
void processEncoderRotation(void) {
// Desacoplamos la variable que se utiliza en ISR
lastMenuItem=menuItem;
menuItem=RTmenuItem;
if (lastMenuItem<menuItem) {
processEncoderPlusClick();
//lastMenuItem=menuItem;
}
if (lastMenuItem>menuItem) {
processEncoderMinusClick();
//lastMenuItem=menuItem;
}
}
void processEncoderPlusClick(){
makeSelectionCiclyc();
switch (STATE) {
case SCN03S02: //Seleccion intervalo modo TimeLapse
if (currentTLInterval == TLMaxDuration) // Maximo
currentTLInterval = 1;
else
currentTLInterval++;
break;
case SCN04S02: // Seleccion intervalo modo TimeLapsePro
if (currentTLInterval == TLMaxDuration) // Maximo
currentTLInterval =1;
else
currentTLInterval++;
break;
case SCN04S03:
//timelapseDuration++;
//calculateTimeLapseParameters();
break;
case SCN06S04: // Modo seleccion de la duracion. Video Pro
selectedDuration++;
if (selectedDuration > maxDuration)
selectedDuration=maxDuration; // Definimos la duración minima por defecto
break;
// Valores máximos de la duracion en modo ASTROFOCUS
case SCN07S04:
selectedDurationSecondsUnits++;
if (selectedDurationSecondsUnits >9) selectedDurationSecondsUnits=0;
break;
case SCN07S05:
selectedDurationSecondsTens++;
if (selectedDurationSecondsTens >5) selectedDurationSecondsTens=0;
break;
case SCN07S06:
selectedDurationMinutesUnits++;
if (selectedDurationMinutesUnits >9) selectedDurationMinutesUnits=0;
break;
case SCN07S07:
selectedDurationMinutesTens++;
if (selectedDurationMinutesTens >5) selectedDurationMinutesTens=0;
break;
case SCN07S08:
selectedDurationHours++;
if (selectedDurationHours > maxDurationHoursAFMode) selectedDurationHours=0;
break;
// Modo Follow Focus, seleccion de la duración
case SCN09S04: // decenas de segundo
selectedDurationSecondsTens++;
if (selectedDurationSecondsTens>5) selectedDurationSecondsTens=0;
break;
case SCN09S05: // unidades de segundo
selectedDurationSecondsUnits++;
if (selectedDurationSecondsUnits>5) selectedDurationSecondsUnits=0;
break;
case SCN09S06: // centesimas de segundo
selectedDurationSecondsTenths++;
if (selectedDurationSecondsTenths>5) selectedDurationSecondsTenths=0;
break;
// Configuracion longitud rail
case SCN11S02:
selectedRailLenghtHundreds ++;
if (selectedRailLenghtHundreds > 9) selectedRailLenghtHundreds = 9;
break;
case SCN11S03:
selectedRailLenghtTenths ++;
if (selectedRailLenghtTenths > 9) selectedRailLenghtTenths = 9;
break;
case SCN11S04:
selectedRailLenghtUnits ++;
if (selectedRailLenghtUnits > 9) selectedRailLenghtUnits = 9;
break;
}
//calculateTimeLapseParameters();
}
void processEncoderMinusClick() {
makeSelectionCiclyc();
switch (STATE) {
case SCN03S02: //Seleccion intervalo modo TimeLapse
if (currentTLInterval == 1) // Maximo
currentTLInterval = TLMaxDuration;
else
currentTLInterval--;
break;
case SCN04S02:
if (currentTLInterval == 1)
currentTLInterval = TLMaxDuration; //maximo
else
currentTLInterval--;
break;
case SCN04S03:
//timelapseDuration--;
//calculateTimeLapseParameters();
break;
case SCN06S04: // Modo seleccion de la duracion. Video Pro
selectedDuration--;
if (selectedDuration < minDuration)
selectedDuration=minDuration; // Definimos la duración minima por defecto
break;
// Valores minimos de la duracion en modo ASTROFOCUS
case SCN07S04:
if (selectedDurationSecondsUnits !=0) selectedDurationSecondsUnits--;
break;
case SCN07S05:
if (selectedDurationSecondsTens !=0) selectedDurationSecondsTens--;
break;
case SCN07S06:
if (selectedDurationMinutesUnits !=0) selectedDurationMinutesUnits--;
break;
case SCN07S07:
if (selectedDurationMinutesTens !=0) selectedDurationMinutesTens--;
break;
case SCN07S08:
if (selectedDurationHours !=0) selectedDurationHours--;
break;
// Modo Follow Focus, seleccion de la duración
case SCN09S04: // decenas de segundo
if (selectedDurationSecondsTens !=0) selectedDurationSecondsTens--;
break;
case SCN09S05: // unidades de segundo
if (selectedDurationSecondsUnits !=0) selectedDurationSecondsUnits--;
break;
case SCN09S06: // centesimas de segundo
if (selectedDurationSecondsTenths !=0) selectedDurationSecondsTenths--;
break;
// Configuracion longitud rail
case SCN11S02:
if (selectedRailLenghtHundreds != 0) selectedRailLenghtHundreds --;
break;
case SCN11S03:
if (selectedRailLenghtTenths != 0) selectedRailLenghtTenths --;
break;
case SCN11S04:
if (selectedRailLenghtUnits != 0) selectedRailLenghtUnits --;
break;
}
}