Skip to content

Commit dfb8f28

Browse files
Merge pull request #32 from DCSFlightpanels/FixSwitch2PosReset
Fix switch2 pos reset
2 parents e6dfe9d + c247c6c commit dfb8f28

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

releasenotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## vNext
22
- Added input_min and input_max optional parameters to PotentiometerEWMA to allow a user to calibrate their analog inputs if their physical control does not utilize the full range available to the controller.
3+
- Fix Switch2Pos state reset logic, and improve it's internal debounce logic for a few edge cases.
34

45
## v0.3.7
56
- Integrate 2 and 3 position Matrix switches, thanks to Dehuman for the starting point!

src/internal/Switches.h

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ namespace DcsBios {
1111
private:
1212
const char* msg_;
1313
char pin_;
14+
char debounceSteadyState_;
1415
char lastState_;
15-
char switchState_;
1616
bool reverse_;
1717
unsigned long debounceDelay_;
1818
unsigned long lastDebounceTime = 0;
@@ -21,22 +21,25 @@ namespace DcsBios {
2121
{
2222
lastState_ = (lastState_==0)?-1:0;
2323
}
24+
2425
void pollInput() {
2526
char state = digitalRead(pin_);
2627
if (reverse_) state = !state;
27-
if (state != lastState_) {
28-
lastDebounceTime = millis();
28+
29+
unsigned long now = millis();
30+
31+
if (state != debounceSteadyState_) {
32+
lastDebounceTime = now;
33+
debounceSteadyState_ = state;
2934
}
3035

31-
if ((millis() - lastDebounceTime) > debounceDelay_) {
32-
if (state != switchState_) {
36+
if ((now - lastDebounceTime) >= debounceDelay_) {
37+
if (debounceSteadyState_ != lastState_) {
3338
if (tryToSendDcsBiosMessage(msg_, state == HIGH ? "0" : "1")) {
34-
switchState_ = state;
39+
lastState_ = debounceSteadyState_;
3540
}
3641
}
37-
}
38-
39-
lastState_ = state;
42+
}
4043
}
4144
public:
4245
Switch2PosT(const char* msg, char pin, bool reverse = false, unsigned long debounceDelay = 50) :
@@ -45,10 +48,11 @@ namespace DcsBios {
4548
msg_ = msg;
4649
pin_ = pin;
4750
pinMode(pin_, INPUT_PULLUP);
48-
switchState_ = digitalRead(pin_);
49-
lastState_ = switchState_;
50-
reverse_ = reverse;
5151
debounceDelay_ = debounceDelay;
52+
reverse_ = reverse;
53+
54+
lastState_ = digitalRead(pin_);
55+
if (reverse_) lastState_ = !lastState_;
5256
}
5357

5458
void SetControl( const char* msg )
@@ -202,7 +206,7 @@ namespace DcsBios {
202206
char pinA_;
203207
char pinB_;
204208
char lastState_;
205-
char steadyState_;
209+
char debounceSteadyState_;
206210
unsigned long debounceDelay_;
207211
unsigned long lastDebounceTime = 0;
208212

@@ -214,36 +218,35 @@ namespace DcsBios {
214218
void resetState()
215219
{
216220
lastState_ = (lastState_==0)?-1:0;
217-
steadyState_ = lastState_;
218221
}
219222
void pollInput() {
220223
char state = readState();
221-
if (state != lastState_) {
222-
lastDebounceTime = millis();
224+
unsigned long now = millis();
225+
if (state != debounceSteadyState_) {
226+
lastDebounceTime = now;
227+
debounceSteadyState_ = state;
223228
}
224229

225-
if ((millis() - lastDebounceTime) > debounceDelay_)
230+
if ((now - lastDebounceTime) >= debounceDelay_)
226231
{
227-
if (state != steadyState_) {
232+
if (state != lastState_) {
228233
if (state == 0)
229234
{
230235
if (tryToSendDcsBiosMessage(msg_, "0"))
231-
steadyState_ = state;
236+
lastState_ = state;
232237
}
233238
else if (state == 1)
234239
{
235240
if (tryToSendDcsBiosMessage(msg_, "1"))
236-
steadyState_ = state;
241+
lastState_ = state;
237242
}
238243
else if (state == 2)
239244
{
240245
if(tryToSendDcsBiosMessage(msg_, "2"))
241-
steadyState_ = state;
246+
lastState_ = state;
242247
}
243248
}
244249
}
245-
246-
lastState_ = state;
247250
}
248251
public:
249252
Switch3PosT(const char* msg, char pinA, char pinB, unsigned long debounceDelay = 50) :
@@ -255,7 +258,7 @@ namespace DcsBios {
255258
pinMode(pinA_, INPUT_PULLUP);
256259
pinMode(pinB_, INPUT_PULLUP);
257260
lastState_ = readState();
258-
steadyState_ = lastState_;
261+
debounceSteadyState_ = lastState_;
259262
debounceDelay_ = debounceDelay;
260263
}
261264

0 commit comments

Comments
 (0)