Skip to content

Commit

Permalink
Fix large error in PW when PW close to ISR period.
Browse files Browse the repository at this point in the history
  • Loading branch information
anarkiwi committed Jan 5, 2020
1 parent d2e6551 commit 19e35b4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
37 changes: 14 additions & 23 deletions CRIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ DigitalPin<fixedVarPulseInPin> _fixedVarInPin(INPUT, LOW);
DigitalPin<percussionEnableInPin> _percussionEnableInPin(INPUT, LOW);

#include "CRIO.h"
#include "constants.h"


CRLiquidCrystal lcd(lcd_rs, lcd_rw, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7);

Expand Down Expand Up @@ -89,15 +89,6 @@ inline void CRIO::pulseOff() {
_pulseState = false;
}

void CRIO::oneShotPulse() {
if (_oneShotPulseUs) {
pulseOn();
delayMicroseconds(_oneShotPulseUs);
pulseOff();
_oneShotPulseUs = 0;
}
}

void CRIO::handlePulse() {
++_ticksSinceLastPulse;
if (scheduled) {
Expand All @@ -114,20 +105,12 @@ void CRIO::handlePulse() {
void CRIO::startPulse() {
if (scheduled) {
_ticksSinceLastPulse = 0;
if (_multiShotPulses) {
if (_oneShotPulseUs) {
if (_oneShotPulseUs >= (masterClockPeriodUs - oneShotPulsePadUs)) {
++_multiShotPulses;
} else if (_oneShotPulseUs > oneShotPulsePadUs) {
delayMicroseconds((masterClockPeriodUs - _oneShotPulseUs) - oneShotPulsePadUs);
pulseOn();
}
_oneShotPulseUs = 0;
}
} else {
oneShotPulse();
}
scheduled = false;
if (_oneShotPulseUs) {
delayMicroseconds(masterClockPeriodUs - _oneShotPulseUs);
_oneShotPulseUs = 0;
pulseOn();
}
}
}

Expand All @@ -149,6 +132,14 @@ void CRIO::schedulePulse(cr_fp_t pulseUs) {
} else {
_multiShotPulses = 0;
}
if (_oneShotPulseUs) {
if (_oneShotPulseUs > oneShotRemainderPulsePadUs) {
++_multiShotPulses;
_oneShotPulseUs = 0;
} else if (_oneShotPulseUs < oneShotPulsePadUs) {
_oneShotPulseUs = oneShotPulsePadUs;
}
}
}

inline cr_fp_t CRIOLcd::_scalePot(uint8_t pin) {
Expand Down
5 changes: 3 additions & 2 deletions CRIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
#define CRIO_H

#include "types.h"
#include "constants.h"

const uint8_t lcdWidth = 16;
const uint8_t lcdLines = 2;
const uint8_t analogBits = 12;
const cr_fp_t maxAnalogRead = cr_fp_t(1) / cr_fp_t(4095);
const uint8_t oneShotPulsePadUs = 2;
const uint8_t oneShotPulsePadUs = 1;
const uint8_t oneShotRemainderPulsePadUs = masterClockPeriodUs - oneShotPulsePadUs;
const uint8_t potPins = 3;
const uint8_t potSampleWindow = 5;

Expand Down Expand Up @@ -45,7 +47,6 @@ class CRIO {
uint8_t maxPitch;
cr_fp_t maxChargePct;
private:
void oneShotPulse();
void pulseOff();
void pulseOn();
bool _pulseState;
Expand Down

0 comments on commit 19e35b4

Please sign in to comment.