Skip to content

Commit

Permalink
Merge pull request #6 from talbotmcinnis/ToggleButton
Browse files Browse the repository at this point in the history
Toggle button
  • Loading branch information
talbotmcinnis committed Jan 17, 2021
2 parents 4a926a0 + 3d0514c commit 6b06adc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ sentence=Connect input and output devices to the DCS: World flight simulator usi
paragraph=DCS-BIOS is a piece of software that can extract data from DCS: World and sends them to an Arduino. It also accepts commands over the serial port. This library talks to DCS-BIOS and allows you to connect any component your Arduino can communicate with to your virtual cockpit.
url=https://github.com/DCSFlightpanels/dcs-bios
category=Other
version=0.3.1
version=0.3.3
3 changes: 3 additions & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v0.3.3
- Added ToggleSwitch to allow a single (presumably momentary) button to send alternating values for a single command on rising edges (pushes) of said button.

## v0.3.2
- Added new feature for SwitchMultiPos, allowing a "default" state to be specified for controls that can have a default state. For example, A-10 Emergency Trim without a center detent
```c++
Expand Down
46 changes: 46 additions & 0 deletions src/internal/Buttons.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,52 @@ namespace DcsBios {
};
typedef ActionButtonT<> ActionButton;

template <unsigned long pollIntervalMs = POLL_EVERY_TIME>
class ToggleButtonT : PollingInput {
private:
const char* msg_;
const char* arg_A;
const char* arg_B;
char pin_;
char lastState_;
bool phase_;

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

void pollInput() {
char state = digitalRead(pin_);
if (state != lastState_) {
if (lastState_ == HIGH && state == LOW) {
// Rising edge
while(!tryToSendDcsBiosMessage(msg_, phase_?arg_A:arg_B));
phase_ = !phase_;
}
lastState_ = state;
}
}
public:
ToggleButtonT(const char* msg, const char* argA, const char* argB, char pin) :
PollingInput(pollIntervalMs)
{
msg_ = msg;
arg_A = argA;
arg_B = argB;
pin_ = pin;
phase_ = false;
pinMode(pin_, INPUT_PULLUP);
lastState_ = digitalRead(pin_);
}

void SetControl( const char* msg )
{
msg_ = msg;
}
};
typedef ToggleButtonT<> ToggleButton;

//New Matrix-Compatible Button class
//This class expects char pointer to a storage variable. It will read the state of that variable instead of a physical pin.
//This class is used as a TOGGLE button.
Expand Down

0 comments on commit 6b06adc

Please sign in to comment.