Skip to content

Commit 6b06adc

Browse files
Merge pull request #6 from talbotmcinnis/ToggleButton
Toggle button
2 parents 4a926a0 + 3d0514c commit 6b06adc

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ sentence=Connect input and output devices to the DCS: World flight simulator usi
55
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.
66
url=https://github.com/DCSFlightpanels/dcs-bios
77
category=Other
8-
version=0.3.1
8+
version=0.3.3

releasenotes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v0.3.3
2+
- Added ToggleSwitch to allow a single (presumably momentary) button to send alternating values for a single command on rising edges (pushes) of said button.
3+
14
## v0.3.2
25
- 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
36
```c++

src/internal/Buttons.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,52 @@ namespace DcsBios {
4444
};
4545
typedef ActionButtonT<> ActionButton;
4646

47+
template <unsigned long pollIntervalMs = POLL_EVERY_TIME>
48+
class ToggleButtonT : PollingInput {
49+
private:
50+
const char* msg_;
51+
const char* arg_A;
52+
const char* arg_B;
53+
char pin_;
54+
char lastState_;
55+
bool phase_;
56+
57+
void resetState()
58+
{
59+
lastState_ = (lastState_==0)?-1:0;
60+
}
61+
62+
void pollInput() {
63+
char state = digitalRead(pin_);
64+
if (state != lastState_) {
65+
if (lastState_ == HIGH && state == LOW) {
66+
// Rising edge
67+
while(!tryToSendDcsBiosMessage(msg_, phase_?arg_A:arg_B));
68+
phase_ = !phase_;
69+
}
70+
lastState_ = state;
71+
}
72+
}
73+
public:
74+
ToggleButtonT(const char* msg, const char* argA, const char* argB, char pin) :
75+
PollingInput(pollIntervalMs)
76+
{
77+
msg_ = msg;
78+
arg_A = argA;
79+
arg_B = argB;
80+
pin_ = pin;
81+
phase_ = false;
82+
pinMode(pin_, INPUT_PULLUP);
83+
lastState_ = digitalRead(pin_);
84+
}
85+
86+
void SetControl( const char* msg )
87+
{
88+
msg_ = msg;
89+
}
90+
};
91+
typedef ToggleButtonT<> ToggleButton;
92+
4793
//New Matrix-Compatible Button class
4894
//This class expects char pointer to a storage variable. It will read the state of that variable instead of a physical pin.
4995
//This class is used as a TOGGLE button.

0 commit comments

Comments
 (0)