Skip to content

Commit 594c41d

Browse files
Merge pull request #15 from talbotmcinnis/RotarySwitch
Rotary switch
2 parents d5afc0b + 6503d2b commit 594c41d

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

examples/OneOfEverything/OneOfEverything.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ DcsBios::Potentiometer potentiometerExample("MSG_0", 1);
4747
// An inverted version of a linear axis control
4848
DcsBios::Potentiometer invertedPotentiometerExample("MSG_0", 1, true);
4949

50+
// Rotary encoder being used to control something that is an N-pos switch in DCS. 3 represents the number of positions of the switch in DCS.
51+
DcsBios::RotarySwitch rotarySwitchExample("MSG_0", 1, 2, 3);
52+
5053
// Outputs
5154
///////////
5255
// A single LED

releasenotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
- Replaced InvertedPotentiometer class with a reverse bool constructor option for the existing potentiometer.
33
- Added Synchronization example to layout different strategies for physical control <=> DCS synchronization on startup, periodic or on aircraft entry.
44
- Created new synchronization option via ResettableInput/resetThisState()
5+
- Added RotarySwitch for cockpits with a rotary encoder in place of N-Position switches
56

67
## v0.3.5
78
- Switched all examples to DCSBIOS_DEFAULTSERIAL for compatibility.

src/DcsBios.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ do not come with their own build system, we are just putting everything into the
125125
#include "internal/Dimmer.h"
126126
#include "internal/BcdWheels.h"
127127
#include "internal/AnalogMultiPos.h"
128+
#include "internal/RotarySwitch.h"
128129

129130
namespace DcsBios {
130131
template<unsigned int first, unsigned int second>

src/internal/RotarySwitch.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#ifndef __DCSBIOS_ROTARYSWITCH_H
2+
#define __DCSBIOS_ROTARYSWITCH_H
3+
4+
#include "Arduino.h"
5+
#include "PollingInput.h"
6+
7+
namespace DcsBios {
8+
template <unsigned long pollIntervalMs = POLL_EVERY_TIME, StepsPerDetent stepsPerDetent = ONE_STEP_PER_DETENT>
9+
class RotarySwitchT : PollingInput, public ResettableInput {
10+
private:
11+
const char* msg_;
12+
char pinA_;
13+
char pinB_;
14+
signed char switchValue_;
15+
signed char maxSwichValue_;
16+
char lastState_;
17+
signed char delta_;
18+
char readState() {
19+
return (digitalRead(pinA_) << 1) | digitalRead(pinB_);
20+
}
21+
void resetState()
22+
{
23+
lastState_ = (lastState_==0)?-1:0;
24+
}
25+
void pollInput() {
26+
char state = readState();
27+
switch(lastState_) {
28+
case 0:
29+
if (state == 2) delta_--;
30+
if (state == 1) delta_++;
31+
break;
32+
case 1:
33+
if (state == 0) delta_--;
34+
if (state == 3) delta_++;
35+
break;
36+
case 2:
37+
if (state == 3) delta_--;
38+
if (state == 0) delta_++;
39+
break;
40+
case 3:
41+
if (state == 1) delta_--;
42+
if (state == 2) delta_++;
43+
break;
44+
}
45+
lastState_ = state;
46+
47+
if (delta_ >= stepsPerDetent) {
48+
switchValue_ = min(switchValue_+1, maxSwichValue_);
49+
char buf[7];
50+
utoa(switchValue_, buf, 10);
51+
if (tryToSendDcsBiosMessage(msg_, buf))
52+
delta_ -= stepsPerDetent;
53+
}
54+
if (delta_ <= -stepsPerDetent) {
55+
switchValue_ = max(switchValue_-1, 0);
56+
char buf[7];
57+
utoa(switchValue_, buf, 10);
58+
if (tryToSendDcsBiosMessage(msg_, buf))
59+
delta_ += stepsPerDetent;
60+
}
61+
}
62+
public:
63+
RotarySwitchT(const char* msg, char pinA, char pinB, signed char maxSwichValue) :
64+
PollingInput(pollIntervalMs) {
65+
msg_ = msg;
66+
pinA_ = pinA;
67+
pinB_ = pinB;
68+
pinMode(pinA_, INPUT_PULLUP);
69+
pinMode(pinB_, INPUT_PULLUP);
70+
delta_ = 0;
71+
switchValue_ = 0;
72+
maxSwichValue_ = maxSwichValue;
73+
lastState_ = readState();
74+
}
75+
76+
void SetControl( const char* msg )
77+
{
78+
msg_ = msg;
79+
}
80+
81+
void resetThisState()
82+
{
83+
this->resetState();
84+
}
85+
};
86+
typedef RotarySwitchT<> RotarySwitch;
87+
}
88+
89+
#endif

0 commit comments

Comments
 (0)