Skip to content

Commit

Permalink
Checkpointing speedup attempt as pre #26.
Browse files Browse the repository at this point in the history
  • Loading branch information
bengtmartensson committed Jul 2, 2019
1 parent b7f717f commit 060886f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 20 deletions.
3 changes: 3 additions & 0 deletions src/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ inline void pinMode(uint8_t pin, int mode) {
<< ")" << std::endl;
};

inline uint8_t digitalPinToBitMask(uint8_t pin) { return pin; }
inline uint8_t* digitalPinToPort(uint8_t pin UNUSED) { return NULL; }

inline void delayMicroseconds(unsigned int t) { usleep(t); };

inline void delay(unsigned long t) {
Expand Down
12 changes: 6 additions & 6 deletions src/IrSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ void IrSender::delayUSecs(microseconds_t T) {
};
}

IrSender::IrSender() {
outputPin = invalidPin;
IrSender::IrSender() : outputPin(invalidPin) {
}

IrSender::IrSender(pin_t pin) {
outputPin = pin;
IrSender::IrSender(pin_t pin) : outputPin(pin), bitMask(digitalPinToBitMask(pin)), outputRegister(digitalPinToPort(pin)) {
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
mute();
}

IrSender::~IrSender() {
mute();
}

void IrSender::mute() {
digitalWrite(outputPin, LOW);
noInterrupts();
digitalWriteLow();
interrupts();
}

void IrSender::sendIrSignal(const IrSignal& irSignal, unsigned int noSends) {
Expand Down
23 changes: 21 additions & 2 deletions src/IrSender.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,30 @@ this program. If not, see http://www.gnu.org/licenses/.
class IrSender {
private:
pin_t outputPin;
uint8_t bitMask;
volatile uint8_t* outputRegister;

protected:
pin_t getOutputPin() const {
return outputPin;
// pin_t getOutputPin() const {
// return outputPin;
// }

void digitalWriteLow() const {
#ifdef ARDUINO
*outputRegister &= ~bitMask;
#else
digitalWrite(outputPin, LOW);
#endif
}

void digitalWriteHigh() const {
#ifdef ARDUINO
*outputRegister |= bitMask;
#else
digitalWrite(outputPin, HIGH);
#endif
}

void delayUSecs(microseconds_t T);
IrSender();
IrSender(pin_t pin);
Expand Down
11 changes: 8 additions & 3 deletions src/IrSenderNonMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ IrSenderNonMod::IrSenderNonMod(pin_t pin) : IrSender(pin) {
}

void IrSenderNonMod::sendNonModulated(const IrSequence &irSequence) {
for (unsigned int i = 0; i < irSequence.getLength(); i++) {
digitalWrite(getOutputPin(), (i & 1) ? LOW : HIGH);
for (unsigned int i = 0; i < irSequence.getLength(); i += 2) {
noInterrupts();
digitalWriteHigh();
interrupts();
delayUSecs(irSequence.getDurations()[i]);
noInterrupts();
digitalWriteLow();
interrupts();
delayUSecs(irSequence.getDurations()[i+1]);
}
digitalWrite(getOutputPin(), LOW);
}
22 changes: 13 additions & 9 deletions src/IrSenderPwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,20 @@ IrSenderPwm::IrSenderPwm() : IrSender(SEND_PIN) {

void IrSenderPwm::send(const IrSequence& irSequence, frequency_t frequency) {
enable(frequency/1000);
for (unsigned int i = 0; i < irSequence.getLength(); i++) {
digitalWrite(getOutputPin(), (i & 1) ? LOW : HIGH);
if (i & 1) {
TIMER_DISABLE_PWM;
} else {
TIMER_ENABLE_PWM;
}
unsigned long targetTime = micros();
for (unsigned int i = 0; i < irSequence.getLength(); i += 2) {
noInterrupts();
digitalWriteHigh();
TIMER_ENABLE_PWM;
interrupts();
delayUSecs(irSequence.getDurations()[i]);
noInterrupts();
TIMER_DISABLE_PWM;
digitalWriteLow();
interrupts();
targetTime += irSequence.getDurations()[i] + irSequence.getDurations()[i+1];
delayUSecs((microseconds_t) (targetTime - micros()));
}
digitalWrite(getOutputPin(), LOW);
}

IrSenderPwm *IrSenderPwm::newInstance() {
Expand All @@ -59,6 +63,6 @@ IrSenderPwm *IrSenderPwm::getInstance(bool create) {
void IrSenderPwm::enable(unsigned char khz UNUSED) {
TIMER_DISABLE_INTR;
pinMode(SEND_PIN, OUTPUT);
digitalWrite(SEND_PIN, LOW);
digitalWriteLow();
TIMER_CONFIG_KHZ(khz);
}

0 comments on commit 060886f

Please sign in to comment.