forked from philipellisis/arduino-virtual-pinball-board
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlunger.cpp
187 lines (161 loc) · 6.51 KB
/
Plunger.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
#include "Plunger.h"
#include <Arduino.h>
#include "HID-Project.h"
#include "Globals.h"
Plunger::Plunger() {
pinMode(23, INPUT_PULLUP); //plunger
//if (DEBUG) {Serial.print(F("DEBUG,plunger: pins initialized\r\n"));}
}
void Plunger::init() {
resetPlunger();
//if (DEBUG) {Serial.print(F("DEBUG,plunger: initialized Gamepad1\r\n"));}
}
void Plunger::resetPlunger() {
plungerScaleFactor = (float)(config.plungerMid - config.plungerMin) / (float)(config.plungerMax - config.plungerMid);
}
void Plunger::plungerRead() {
if (config.enablePlunger == false) {
Gamepad1.zAxis(0);
return;
}
long sensorValue = 0;
int newReading;
//remove any sensor value that does not agree with the prior value
int goodReadings = 0;
for (int i = 0; i < 5; i++) {
newReading = analogRead(23);
if (newReading < truePriorValue + 10 && newReading > truePriorValue - 10) {
goodReadings++;
sensorValue += newReading;
}
}
if (goodReadings > 0) {
sensorValue = sensorValue / (goodReadings);
} else {
sensorValue = newReading;
}
if (config.restingStateCounter < config.restingStateMax || config.disablePlungerWhenNotInUse == 0) {
for (int i = 0; i < config.plungerAverageRead; i++) {
sensorValue += analogRead(23);
}
sensorValue = sensorValue / (config.plungerAverageRead + 1);
}
truePriorValue = sensorValue;
// this checks that the plunger is sitting stationary. If so, it will enable the accelerometer. It also checks if there is nothing connected. to ensure the accelerometer still works even if the plunger is disconnected
if ((sensorValue < config.plungerMid + 50 && sensorValue > config.plungerMid - 50) || sensorValue > 990) {
// plunger is in resting state when counter is > 200. Is moving or almost done moving when counter is > 0 and <= 200
if (config.restingStateCounter < config.restingStateMax) {
config.restingStateCounter++;
}
} else {
//plunger is actively moving when counter is = 0
config.restingStateCounter = 0;
}
// Serial.print("sensornorm: ");
// Serial.print(sensorValue);
// Serial.print("\r\n");
if( (config.plungerButtonPush == 1 || config.plungerButtonPush == 3) && buttonState == 0 && sensorValue >= config.plungerMax - 20) {
buttonState = buttons.sendButtonPush(config.plungerLaunchButton, 1);
config.lastButtonState[config.plungerLaunchButton] = buttonState;
} else if ((config.plungerButtonPush == 1 || config.plungerButtonPush == 3) && buttonState == 1 && sensorValue < config.plungerMax - 20 ) {
buttonState = buttons.sendButtonPush(config.plungerLaunchButton, 0);
config.lastButtonState[config.plungerLaunchButton] = buttonState;
}
if( config.plungerButtonPush >= 2 && buttonState2 == 0 && sensorValue <= config.plungerMin + 10) {
buttonState2 = buttons.sendButtonPush(config.plungerLaunchButton, 1);
config.lastButtonState[config.plungerLaunchButton] = buttonState2;
} else if (config.plungerButtonPush >= 2 && buttonState2 == 1 && sensorValue > config.plungerMin + 10 ) {
buttonState2 = buttons.sendButtonPush(config.plungerLaunchButton, 0);
config.lastButtonState[config.plungerLaunchButton] = buttonState2;
}
if (sensorValue <= config.plungerMid) {
adjustedValue = static_cast<int8_t>((1 - (float)(sensorValue - config.plungerMin) / (config.plungerMid - config.plungerMin)) * -128);
if (adjustedValue > 100) {
adjustedValue = -127;
}
} else {
adjustedValue = static_cast<int8_t>((float)(sensorValue - config.plungerMid) / (config.plungerMax - config.plungerMid) * 128);
if (adjustedValue < -100) {
adjustedValue = 127;
}
}
// Serial.print("not resting: ");
// Serial.print(adjustedValue);
// Serial.print("\r\n");
// if (plungerMinSendCount == 62) {
// plungerData[incrementor] = minValue;
// if (incrementor < 62) {
// incrementor++;
// } else {
// incrementor = 0;
// }
// }
signed char currentDelayedValue = getDelayedPlungerValue(adjustedValue);
//if (DEBUG) {Serial.print(F("DEBUG,plunger: scale factor ")); Serial.print(plungerScaleFactor); Serial.print(F("DEBUG,plunger: value ")); Serial.print(adjustedValue); Serial.print("\r\n");}
if (priorValue != currentDelayedValue && config.restingStateCounter < config.restingStateMax ) {
if (adjustedValue > 0 && plungerReleased == false) {
currentPlungerMax = currentDelayedValue;
}
// Serial.print(adjustedValue);
// Serial.print(F("\r\n"));
config.updateUSB = true;
Gamepad1.zAxis(currentDelayedValue);
// Serial.print("plunger in motion: ");
// Serial.print(currentDelayedValue);
// Serial.print(F("\r\n"));
priorValue = currentDelayedValue;
//is not moving
} else if (priorValue != currentDelayedValue && config.restingStateCounter == config.restingStateMax ) {
currentPlungerMax = 0;
plungerReleased = false;
if (config.disablePlungerWhenNotInUse == 1) {
Gamepad1.zAxis(0);
// Serial.print("plunger not in motion, sending 0: ");
// Serial.print(adjustedValue);
// Serial.print(F("\r\n"));
priorValue = currentDelayedValue;
} else {
// Serial.print("plunger not in motion: ");
// Serial.print(currentDelayedValue);
// Serial.print(F("\r\n"));
Gamepad1.zAxis(currentDelayedValue);
config.updateUSB = true;
priorValue = currentDelayedValue;
}
}
plungerData[plungerDataCounter] = adjustedValue;
if (plungerDataCounter < 49) {
plungerDataCounter++;
} else {
plungerDataCounter = 0;
}
}
signed char Plunger::getDelayedPlungerValue(signed char sensorValue) {
if (config.enablePlungerQuickRelease == 0) {
return sensorValue;
}
if (config.restingStateCounter == config.restingStateMax && plungerReleased == true) {
plungerReleased = false;
config.updateUSB = true;
return 0;
}
if ((sensorValue < 0 && config.restingStateCounter < config.restingStateMax && currentPlungerMax > 0 && truePriorValue > 50) || plungerReleased == true) {
if (plungerReleased == false) {
config.lastButtonState[config.plungerLaunchButton] = buttons.sendButtonPush(config.plungerLaunchButton, 1);
} else {
config.lastButtonState[config.plungerLaunchButton] = buttons.sendButtonPush(config.plungerLaunchButton, 0);
}
plungerReleased = true;
return 0;
}
if (plungerDataCounter == 49) {
return plungerData[0];
} else {
return plungerData[plungerDataCounter + 1];
}
}
void Plunger::sendPlungerState() {
Serial.print(F("P,"));
Serial.print(analogRead(23));
Serial.print(F("\r\n"));
}