Skip to content

Commit

Permalink
Merge pull request #20 from talbotmcinnis/SwitchWithCover
Browse files Browse the repository at this point in the history
Switch with cover
  • Loading branch information
talbotmcinnis authored Nov 15, 2021
2 parents 695f429 + 04ad77d commit 485f80b
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/OneOfEverything/OneOfEverything.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ DcsBios::Switch3Pos switch3PosExample("MSG_0", 1, 2);
// A multiple position switch, often a rotary switch
const byte multiPosPins[4] = {1,2,3,4};
DcsBios::SwitchMultiPos switchMulitPosExample("MSG_0", multiPosPins, 4);
// A switch that has a cover in DCS that must be opened before the switch itself can be activated
DcsBios::SwitchWithCover2Pos pltLaunchbarAbort("PLT_LAUNCHBAR_ABORT", "PLT_LAUNCHBAR_ABORT_COVER", 6);

// Analogs
///////////
Expand Down
131 changes: 131 additions & 0 deletions src/internal/Switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,137 @@ namespace DcsBios {
}
};
typedef Switch2PosT<> Switch2Pos;

template <unsigned long pollIntervalMs = POLL_EVERY_TIME, unsigned long coverDelayMs = 200>
class SwitchWithCover2PosT : PollingInput, public ResettableInput
{
private:
const char* switchMsg_;
const char* coverMsg_;
char pin_;
char lastState_;
char switchState_;
bool reverse_;
unsigned long debounceDelay_;
unsigned long lastDebounceTime = 0;

enum switchCoverStateEnum{
stOFF_CLOSED = 0,
stOFF_OPEN = 1,
stON_OPEN = 2
};

switchCoverStateEnum switchCoverState_;
unsigned long lastSwitchStateTime;

void resetState()
{
lastState_ = (lastState_==0)?-1:0;

if( switchState_ && !reverse_ )
switchCoverState_ = stOFF_CLOSED;
else
switchCoverState_ = stON_OPEN;
lastSwitchStateTime = millis();
}

void pollInput() {
char state = digitalRead(pin_);
if (reverse_) state = !state;
if (state != lastState_) {
lastDebounceTime = millis();
}

if (state != switchState_ &&
(millis() - lastDebounceTime) > debounceDelay_)
{
// Switch has debounced and changed state
if( millis() - lastSwitchStateTime > coverDelayMs )
{
// Switch/cover delay has been satisfied.
if( state )
{
// Closing/turning off
switch(switchCoverState_)
{
case stON_OPEN:
if (tryToSendDcsBiosMessage(switchMsg_, "0"))
{
switchCoverState_ = stOFF_OPEN;
lastSwitchStateTime = millis();
}
break;

case stOFF_OPEN:
if (tryToSendDcsBiosMessage(coverMsg_, "0"))
{
switchCoverState_ = stOFF_CLOSED;
lastSwitchStateTime = millis();
switchState_ = state;
}
break;

case stOFF_CLOSED:
// Converged on steady state. Good.
break;
}
}
else
{
// Opening/turning on
switch(switchCoverState_)
{
case stOFF_CLOSED:
if (tryToSendDcsBiosMessage(coverMsg_, "1"))
{
switchCoverState_ = stOFF_OPEN;
lastSwitchStateTime = millis();
}
break;

case stOFF_OPEN:
if (tryToSendDcsBiosMessage(switchMsg_, "1"))
{
switchCoverState_ = stON_OPEN;
lastSwitchStateTime = millis();
switchState_ = state;
}
break;

case stON_OPEN:
// Converged on steady state. Good.
break;

}
}
}
}

lastState_ = state;
}

public:
SwitchWithCover2PosT(const char* switchMessage, const char* coverMessage, char pin, bool reverse = false, unsigned long debounceDelay = 50) :
PollingInput(pollIntervalMs)
{
switchMsg_ = switchMessage;
coverMsg_ = coverMessage;
pin_ = pin;
pinMode(pin_, INPUT_PULLUP);
switchState_ = digitalRead(pin_);
lastState_ = switchState_;
reverse_ = reverse;
debounceDelay_ = debounceDelay;

resetState();
}

void resetThisState()
{
this->resetState();
}
};
typedef SwitchWithCover2PosT<> SwitchWithCover2Pos;

template <unsigned long pollIntervalMs = POLL_EVERY_TIME>
class Switch3PosT : PollingInput, public ResettableInput
Expand Down

0 comments on commit 485f80b

Please sign in to comment.