forked from LucidVR/lucidgloves
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.ino
133 lines (114 loc) · 3.17 KB
/
input.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
// Requires RunningMedian library by Rob Tillaart
#if ENABLE_MEDIAN_FILTER
#include <RunningMedian.h>
RunningMedian rmSamples[5] = {
RunningMedian(MEDIAN_SAMPLES),
RunningMedian(MEDIAN_SAMPLES),
RunningMedian(MEDIAN_SAMPLES),
RunningMedian(MEDIAN_SAMPLES),
RunningMedian(MEDIAN_SAMPLES)
};
#endif
int maxFingers[5] = {0,0,0,0,0};
int minFingers[5] = {ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX, ANALOG_MAX};
void setupInputs(){
pinMode(PIN_JOY_BTN, INPUT_PULLUP);
pinMode(PIN_A_BTN, INPUT_PULLUP);
pinMode(PIN_B_BTN, INPUT_PULLUP);
pinMode(PIN_MENU_BTN, INPUT_PULLUP);
#if !TRIGGER_GESTURE
pinMode(PIN_TRIG_BTN, INPUT_PULLUP);
#endif
#if !GRAB_GESTURE
pinMode(PIN_GRAB_BTN, INPUT_PULLUP);
#endif
#if !PINCH_GESTURE
pinMode(PIN_PNCH_BTN, INPUT_PULLUP);
#endif
#if USING_CALIB_PIN
pinMode(PIN_CALIB, INPUT_PULLUP);
#endif
}
int* getFingerPositions(bool calibrating, bool reset){
int rawFingers[5] = {NO_THUMB?0:analogRead(PIN_THUMB), analogRead(PIN_INDEX), analogRead(PIN_MIDDLE), analogRead(PIN_RING), analogRead(PIN_PINKY)};
//flip pot values if needed
#if FLIP_POTS
for (int i = 0; i <5; i++){
rawFingers[i] = ANALOG_MAX - rawFingers[i];
}
#endif
#if ENABLE_MEDIAN_FILTER
for (int i = 0; i < 5; i++){
rmSamples[i].add( rawFingers[i] );
rawFingers[i] = rmSamples[i].getMedian();
}
#endif
//reset max and mins as needed
if (reset){
for (int i = 0; i <5; i++){
maxFingers[i] = 0;
minFingers[i] = ANALOG_MAX;
}
}
//if during the calibration sequence, make sure to update max and mins
if (calibrating){
for (int i = 0; i <5; i++){
if (rawFingers[i] > maxFingers[i])
#if CLAMP_FLEXION
maxFingers[i] = ( rawFingers[i] <= CLAMP_MAX )? rawFingers[i] : CLAMP_MAX;
#else
maxFingers[i] = rawFingers[i];
#endif
if (rawFingers[i] < minFingers[i])
#if CLAMP_FLEXION
minFingers[i] = ( rawFingers[i] >= CLAMP_MIN )? rawFingers[i] : CLAMP_MIN;
#else
minFingers[i] = rawFingers[i];
#endif
}
}
static int calibrated[5] = {511,511,511,511,511};
for (int i = 0; i<5; i++){
if (minFingers[i] != maxFingers[i]){
calibrated[i] = map( rawFingers[i], minFingers[i], maxFingers[i], 0, ANALOG_MAX );
#if CLAMP_ANALOG_MAP
if (calibrated[i] < 0)
calibrated[i] = 0;
if (calibrated[i] > ANALOG_MAX)
calibrated[i] = ANALOG_MAX;
#endif
}
else {
calibrated[i] = ANALOG_MAX / 2;
}
}
return calibrated;
}
int analogReadDeadzone(byte pin){
int raw = analogRead(pin);
if (abs(ANALOG_MAX/2 - raw) < JOYSTICK_DEADZONE * ANALOG_MAX / 100)
return ANALOG_MAX/2;
else
return raw;
}
int getJoyX(){
#if JOYSTICK_BLANK
return ANALOG_MAX/2;
#elif JOY_FLIP_X
return ANALOG_MAX - analogReadDeadzone(PIN_JOY_X);
#else
return analogReadDeadzone(PIN_JOY_X);
#endif
}
int getJoyY(){
#if JOYSTICK_BLANK
return ANALOG_MAX/2;
#elif JOY_FLIP_Y
return ANALOG_MAX - analogReadDeadzone(PIN_JOY_Y);
#else
return analogReadDeadzone(PIN_JOY_Y);
#endif
}
bool getButton(byte pin){
return digitalRead(pin) != HIGH;
}