Skip to content

Commit df1d425

Browse files
committed
Add invert option
1 parent 71984b9 commit df1d425

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/PPMEncoder.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@
33
PPMEncoder ppmEncoder;
44

55
void PPMEncoder::begin(uint8_t pin) {
6-
begin(pin, PPM_DEFAULT_CHANNELS);
6+
begin(pin, PPM_DEFAULT_CHANNELS, false);
77
}
88

99
void PPMEncoder::begin(uint8_t pin, uint8_t ch) {
10+
begin(pin, ch, false);
11+
}
12+
13+
void PPMEncoder::begin(uint8_t pin, uint8_t ch, boolean inverted) {
1014
cli();
1115

16+
// Store on/off-State in variable to avoid another if in timing-critical interrupt
17+
onState = (inverted) ? HIGH : LOW;
18+
offState = (inverted) ? LOW : HIGH;
19+
1220
pinMode(pin, OUTPUT);
13-
digitalWrite(pin, LOW);
21+
digitalWrite(pin, offState);
1422

1523
state = true;
1624
elapsedUs = 0;
@@ -45,11 +53,11 @@ void PPMEncoder::interrupt() {
4553
TCNT1 = 0;
4654

4755
if (state) {
48-
digitalWrite(outputPin, HIGH);
56+
digitalWrite(outputPin, onState);
4957
OCR1A = PPM_PULSE_LENGTH_uS * 2;
5058

5159
} else {
52-
digitalWrite(outputPin, LOW);
60+
digitalWrite(outputPin, offState);
5361

5462
if (currentChannel >= numChannels) {
5563
currentChannel = 0;

src/PPMEncoder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ class PPMEncoder {
2222
uint8_t currentChannel;
2323
byte outputPin;
2424
boolean state;
25+
26+
uint8_t onState;
27+
uint8_t offState;
28+
2529

2630
public:
2731
static const uint16_t MIN = 1000;
@@ -32,6 +36,7 @@ class PPMEncoder {
3236

3337
void begin(uint8_t pin);
3438
void begin(uint8_t pin, uint8_t ch);
39+
void begin(uint8_t pin, uint8_t ch, boolean inverted);
3540

3641
void interrupt();
3742
};

0 commit comments

Comments
 (0)