Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/PPMEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
PPMEncoder ppmEncoder;

void PPMEncoder::begin(uint8_t pin) {
begin(pin, PPM_DEFAULT_CHANNELS, false);
begin(pin, PPM_DEFAULT_CHANNELS, false, true);
}

void PPMEncoder::begin(uint8_t pin, uint8_t ch) {
begin(pin, ch, false);
begin(pin, ch, false, true);
}

void PPMEncoder::begin(uint8_t pin, uint8_t ch, boolean inverted) {
begin(pin, ch, inverted, true);
}

void PPMEncoder::begin(uint8_t pin, uint8_t ch, boolean inverted, boolean is16MHz) {
cli();

// Store on/off-State in variable to avoid another if in timing-critical interrupt
Expand All @@ -27,6 +31,7 @@ void PPMEncoder::begin(uint8_t pin, uint8_t ch, boolean inverted) {

numChannels = ch;
outputPin = pin;
clockMultiplier = is16MHz ? 2 : 1;

for (uint8_t ch = 0; ch < numChannels; ch++) {
setChannelPercent(ch, 0);
Expand Down Expand Up @@ -72,18 +77,18 @@ void PPMEncoder::interrupt() {

if (state) {
digitalWrite(outputPin, onState);
OCR1A = PPM_PULSE_LENGTH_uS * 2;
OCR1A = PPM_PULSE_LENGTH_uS * clockMultiplier;

} else {
digitalWrite(outputPin, offState);

if (currentChannel >= numChannels) {
currentChannel = 0;
elapsedUs = elapsedUs + PPM_PULSE_LENGTH_uS;
OCR1A = (PPM_FRAME_LENGTH_uS - elapsedUs) * 2;
OCR1A = (PPM_FRAME_LENGTH_uS - elapsedUs) * clockMultiplier;
elapsedUs = 0;
} else {
OCR1A = (channels[currentChannel] - PPM_PULSE_LENGTH_uS) * 2;
OCR1A = (channels[currentChannel] - PPM_PULSE_LENGTH_uS) * clockMultiplier;
elapsedUs = elapsedUs + channels[currentChannel];

currentChannel++;
Expand Down
2 changes: 2 additions & 0 deletions src/PPMEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class PPMEncoder {
byte outputPin;
boolean state;
boolean enabled;
uint8_t clockMultiplier;

uint8_t onState;
uint8_t offState;
Expand All @@ -38,6 +39,7 @@ class PPMEncoder {
void begin(uint8_t pin);
void begin(uint8_t pin, uint8_t ch);
void begin(uint8_t pin, uint8_t ch, boolean inverted);
void begin(uint8_t pin, uint8_t ch, boolean inverted, boolean is16MHz);

void disable();
void enable();
Expand Down