-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.cpp
More file actions
285 lines (241 loc) · 7.98 KB
/
input.cpp
File metadata and controls
285 lines (241 loc) · 7.98 KB
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
277
278
279
280
281
282
283
284
285
/*
* TT Control, advanced sinusoidal control of multi-phase turntable motors
* Created by Ashley Cox at The Blind Man’s Workshop
* https://theblindmansworkshop.com
* No part of this code may be used or reproduced for commercial purposes without written permission and contractual agreement
* All external libraries and frameworks are the property of their respective authors and governed by their respective licenses
*/
#include "input.h"
#include "hal.h"
#include "globals.h"
#include "settings.h"
InputManager* _inputInstance = nullptr;
InputManager::InputManager() {
_encDelta = 0;
_lastEncTime = 0;
_encAccel = 0;
_pitchLastClk = HIGH;
_pitchDelta = 0;
_lastPitchTime = 0;
_pitchAccel = 0;
_btnPressed = false;
_btnPressTime = 0;
_waitingForDoubleClick = false;
_doubleClickTimer = 0;
_clickCount = 0;
_encoderPosition = 0;
_lastEncoderPosition = 0;
_inputInstance = this;
_speedBtnState = HIGH;
_speedBtnTime = 0;
_startStopBtnState = HIGH;
_startStopBtnTime = 0;
_standbyBtnState = HIGH;
_standbyBtnTime = 0;
_pendingEvent = EVT_NONE;
_injectedDelta = 0;
}
void InputManager::begin() {
hal.setPinMode(PIN_ENC_MAIN_CLK, INPUT_PULLUP);
hal.setPinMode(PIN_ENC_MAIN_DT, INPUT_PULLUP);
hal.setPinMode(PIN_ENC_MAIN_SW, INPUT_PULLUP);
// Attach Interrupt
attachInterrupt(digitalPinToInterrupt(PIN_ENC_MAIN_CLK), isrEncoder, CHANGE);
if (SPEED_BUTTON_ENABLE) hal.setPinMode(PIN_BTN_SPEED, INPUT_PULLUP);
if (START_STOP_BUTTON_ENABLE) hal.setPinMode(PIN_BTN_START_STOP, INPUT_PULLUP);
if (STANDBY_BUTTON_ENABLE) hal.setPinMode(PIN_BTN_STANDBY, INPUT_PULLUP);
// Read initial state (only for pitch encoder now, main encoder handled by ISR)
// _encLastClk = hal.digitalRead(PIN_ENC_MAIN_CLK); // No longer needed for main encoder
#if PITCH_CONTROL_ENABLE
hal.setPinMode(PIN_ENC_PITCH_CLK, INPUT_PULLUP);
hal.setPinMode(PIN_ENC_PITCH_DT, INPUT_PULLUP);
hal.setPinMode(PIN_ENC_PITCH_SW, INPUT_PULLUP); // Optional switch
_pitchLastClk = hal.digitalRead(PIN_ENC_PITCH_CLK);
#endif
}
void InputManager::isrEncoder() {
if (!_inputInstance) return;
// Simple Quadrature Decode
// Read CLK and DT
// We interrupt on CLK change
// If DT != CLK, it's one direction, else other
int clk = digitalRead(PIN_ENC_MAIN_CLK);
int dt = digitalRead(PIN_ENC_MAIN_DT);
if (clk != dt) {
_inputInstance->_encoderPosition++;
} else {
_inputInstance->_encoderPosition--;
}
}
void InputManager::update() {
uint32_t now = hal.getMillis();
// --- Encoder Reading from ISR ---
// Atomic read not strictly necessary for long on 32-bit, but good practice
noInterrupts();
long pos = _encoderPosition;
interrupts();
long delta = pos - _lastEncoderPosition;
_lastEncoderPosition = pos;
// Handle Input Injection (for testing/serial control)
if (_injectedDelta != 0) {
delta += _injectedDelta;
_injectedDelta = 0;
}
if (settings.get().reverseEncoder) {
delta = -delta;
}
// --- Acceleration Logic ---
// If rotation is fast, increase the delta multiplier
if (delta != 0) {
if (now - _lastEncTime < 50) { // Fast rotation threshold
_encAccel++;
if (_encAccel > 5) delta *= 5;
else if (_encAccel > 2) delta *= 2;
} else {
_encAccel = 0;
}
_lastEncTime = now;
_encDelta += delta; // Accumulate for value editing
// Map to Navigation Events
// We generate events for navigation but keep raw delta for smooth value editing
if (delta > 0) _pendingEvent = EVT_NAV_UP;
else if (delta < 0) _pendingEvent = EVT_NAV_DOWN;
}
// --- Pitch Encoder Reading ---
#if PITCH_CONTROL_ENABLE
int pDelta = readPitchEncoder();
if (pDelta != 0) {
// Optional acceleration for pitch too?
// Pitch usually needs fine control, so maybe less aggressive accel
if (now - _lastPitchTime < 30) {
_pitchAccel++;
if (_pitchAccel > 5) pDelta *= 2; // Only 2x for pitch
} else {
_pitchAccel = 0;
}
_lastPitchTime = now;
_pitchDelta += pDelta;
}
#endif
// --- Button Handling ---
bool btnState = (hal.digitalRead(PIN_ENC_MAIN_SW) == LOW);
// Debounce Logic
static uint32_t lastBtnChange = 0;
static bool lastBtnState = false;
if (btnState != lastBtnState) {
lastBtnChange = now;
lastBtnState = btnState;
}
if (now - lastBtnChange > 20) { // Stable state
if (btnState && !_btnPressed) {
// Press Start
_btnPressed = true;
_btnPressTime = now;
} else if (!btnState && _btnPressed) {
// Press Release
_btnPressed = false;
uint32_t duration = now - _btnPressTime;
if (duration > 5000) {
_pendingEvent = EVT_EXIT; // Very Long Press (>5s)
} else if (duration > 3000) {
_pendingEvent = EVT_BACK; // Long Press (>3s)
} else {
// Short Press - Check for double click
if (_waitingForDoubleClick) {
_clickCount++;
} else {
_waitingForDoubleClick = true;
_doubleClickTimer = now;
_clickCount = 1;
}
}
}
}
// Double Click Timeout
if (_waitingForDoubleClick && (now - _doubleClickTimer > 400)) {
_waitingForDoubleClick = false;
if (_clickCount == 2) {
_pendingEvent = EVT_DOUBLE_CLICK;
} else {
_pendingEvent = EVT_SELECT; // Single Click confirmed
}
}
}
InputEvent InputManager::getEvent() {
InputEvent e = _pendingEvent;
_pendingEvent = EVT_NONE; // Consume event
return e;
}
int InputManager::getEncoderDelta() {
int d = _encDelta;
_encDelta = 0; // Consume delta
return d;
}
int InputManager::getPitchDelta() {
int d = _pitchDelta;
_pitchDelta = 0;
return d;
}
void InputManager::injectDelta(int delta) {
_injectedDelta += delta;
}
void InputManager::injectButton(bool pressed) {
if (pressed) _pendingEvent = EVT_SELECT;
}
// --- Global Button Implementations ---
bool InputManager::isSpeedButtonPressed() {
#ifdef SPEED_BUTTON_ENABLE
if (!SPEED_BUTTON_ENABLE) return false;
bool reading = hal.digitalRead(PIN_BTN_SPEED);
if (reading == LOW && _speedBtnState == HIGH && (millis() - _speedBtnTime > 200)) {
_speedBtnTime = millis();
_speedBtnState = LOW;
return true;
}
if (reading == HIGH) _speedBtnState = HIGH;
#endif
return false;
}
bool InputManager::isStartStopPressed() {
#ifdef START_STOP_BUTTON_ENABLE
if (!START_STOP_BUTTON_ENABLE) return false;
bool reading = hal.digitalRead(PIN_BTN_START_STOP);
if (reading == LOW && _startStopBtnState == HIGH && (millis() - _startStopBtnTime > 200)) {
_startStopBtnTime = millis();
_startStopBtnState = LOW;
return true;
}
if (reading == HIGH) _startStopBtnState = HIGH;
#endif
return false;
}
bool InputManager::isStandbyPressed() {
#ifdef STANDBY_BUTTON_ENABLE
if (!STANDBY_BUTTON_ENABLE) return false;
bool reading = hal.digitalRead(PIN_BTN_STANDBY);
if (reading == LOW && _standbyBtnState == HIGH && (millis() - _standbyBtnTime > 200)) {
_standbyBtnTime = millis();
_standbyBtnState = LOW;
return true;
}
if (reading == HIGH) _standbyBtnState = HIGH;
#endif
return false;
}
int InputManager::readPitchEncoder() {
#if PITCH_CONTROL_ENABLE
int clk = hal.digitalRead(PIN_ENC_PITCH_CLK);
int delta = 0;
if (clk != _pitchLastClk) {
if (hal.digitalRead(PIN_ENC_PITCH_DT) != clk) {
delta = 1;
} else {
delta = -1;
}
}
_pitchLastClk = clk;
return delta;
#else
return 0;
#endif
}