Skip to content

Commit 485f80b

Browse files
Merge pull request #20 from talbotmcinnis/SwitchWithCover
Switch with cover
2 parents 695f429 + 04ad77d commit 485f80b

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

examples/OneOfEverything/OneOfEverything.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ DcsBios::Switch3Pos switch3PosExample("MSG_0", 1, 2);
2424
// A multiple position switch, often a rotary switch
2525
const byte multiPosPins[4] = {1,2,3,4};
2626
DcsBios::SwitchMultiPos switchMulitPosExample("MSG_0", multiPosPins, 4);
27+
// A switch that has a cover in DCS that must be opened before the switch itself can be activated
28+
DcsBios::SwitchWithCover2Pos pltLaunchbarAbort("PLT_LAUNCHBAR_ABORT", "PLT_LAUNCHBAR_ABORT_COVER", 6);
2729

2830
// Analogs
2931
///////////

src/internal/Switches.h

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,137 @@ namespace DcsBios {
6262
}
6363
};
6464
typedef Switch2PosT<> Switch2Pos;
65+
66+
template <unsigned long pollIntervalMs = POLL_EVERY_TIME, unsigned long coverDelayMs = 200>
67+
class SwitchWithCover2PosT : PollingInput, public ResettableInput
68+
{
69+
private:
70+
const char* switchMsg_;
71+
const char* coverMsg_;
72+
char pin_;
73+
char lastState_;
74+
char switchState_;
75+
bool reverse_;
76+
unsigned long debounceDelay_;
77+
unsigned long lastDebounceTime = 0;
78+
79+
enum switchCoverStateEnum{
80+
stOFF_CLOSED = 0,
81+
stOFF_OPEN = 1,
82+
stON_OPEN = 2
83+
};
84+
85+
switchCoverStateEnum switchCoverState_;
86+
unsigned long lastSwitchStateTime;
87+
88+
void resetState()
89+
{
90+
lastState_ = (lastState_==0)?-1:0;
91+
92+
if( switchState_ && !reverse_ )
93+
switchCoverState_ = stOFF_CLOSED;
94+
else
95+
switchCoverState_ = stON_OPEN;
96+
lastSwitchStateTime = millis();
97+
}
98+
99+
void pollInput() {
100+
char state = digitalRead(pin_);
101+
if (reverse_) state = !state;
102+
if (state != lastState_) {
103+
lastDebounceTime = millis();
104+
}
105+
106+
if (state != switchState_ &&
107+
(millis() - lastDebounceTime) > debounceDelay_)
108+
{
109+
// Switch has debounced and changed state
110+
if( millis() - lastSwitchStateTime > coverDelayMs )
111+
{
112+
// Switch/cover delay has been satisfied.
113+
if( state )
114+
{
115+
// Closing/turning off
116+
switch(switchCoverState_)
117+
{
118+
case stON_OPEN:
119+
if (tryToSendDcsBiosMessage(switchMsg_, "0"))
120+
{
121+
switchCoverState_ = stOFF_OPEN;
122+
lastSwitchStateTime = millis();
123+
}
124+
break;
125+
126+
case stOFF_OPEN:
127+
if (tryToSendDcsBiosMessage(coverMsg_, "0"))
128+
{
129+
switchCoverState_ = stOFF_CLOSED;
130+
lastSwitchStateTime = millis();
131+
switchState_ = state;
132+
}
133+
break;
134+
135+
case stOFF_CLOSED:
136+
// Converged on steady state. Good.
137+
break;
138+
}
139+
}
140+
else
141+
{
142+
// Opening/turning on
143+
switch(switchCoverState_)
144+
{
145+
case stOFF_CLOSED:
146+
if (tryToSendDcsBiosMessage(coverMsg_, "1"))
147+
{
148+
switchCoverState_ = stOFF_OPEN;
149+
lastSwitchStateTime = millis();
150+
}
151+
break;
152+
153+
case stOFF_OPEN:
154+
if (tryToSendDcsBiosMessage(switchMsg_, "1"))
155+
{
156+
switchCoverState_ = stON_OPEN;
157+
lastSwitchStateTime = millis();
158+
switchState_ = state;
159+
}
160+
break;
161+
162+
case stON_OPEN:
163+
// Converged on steady state. Good.
164+
break;
165+
166+
}
167+
}
168+
}
169+
}
170+
171+
lastState_ = state;
172+
}
173+
174+
public:
175+
SwitchWithCover2PosT(const char* switchMessage, const char* coverMessage, char pin, bool reverse = false, unsigned long debounceDelay = 50) :
176+
PollingInput(pollIntervalMs)
177+
{
178+
switchMsg_ = switchMessage;
179+
coverMsg_ = coverMessage;
180+
pin_ = pin;
181+
pinMode(pin_, INPUT_PULLUP);
182+
switchState_ = digitalRead(pin_);
183+
lastState_ = switchState_;
184+
reverse_ = reverse;
185+
debounceDelay_ = debounceDelay;
186+
187+
resetState();
188+
}
189+
190+
void resetThisState()
191+
{
192+
this->resetState();
193+
}
194+
};
195+
typedef SwitchWithCover2PosT<> SwitchWithCover2Pos;
65196

66197
template <unsigned long pollIntervalMs = POLL_EVERY_TIME>
67198
class Switch3PosT : PollingInput, public ResettableInput

0 commit comments

Comments
 (0)