Skip to content

Commit

Permalink
Respect tone()'s minimum frequency (issue #14)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Sep 2, 2023
1 parent 1a4421d commit c411ba1
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
ContinuousStepper: changelog
============================

HEAD
-----

* Respect `tone()`'s minimum frequency (issue #14)

3.0.1
-----

Expand Down
6 changes: 6 additions & 0 deletions src/ContinuousStepper/Tickers/AnalogWriteFrequency.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class AwfOscillator {
active_ = false;
}

unsigned int minFrequency() const {
// The analogWriteFrequency function has a lower limit of a few Hz.
// https://www.pjrc.com/teensy/td_pulse.html#frequency
return 5;
}

private:
bool active_ = false;
uint8_t pin_ = nullPin;
Expand Down
4 changes: 4 additions & 0 deletions src/ContinuousStepper/Tickers/KhoiH_PWM.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class KhoihOscillator {
pwm_->setPWM(pin_, 500, 0);
}

unsigned int minFrequency() const {
return 1;
}

private:
uint8_t pin_ = nullPin;
KhoiH_PWM *pwm_ = nullptr;
Expand Down
5 changes: 3 additions & 2 deletions src/ContinuousStepper/Tickers/OscillatorTicker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ class OscillatorTicker : StepperTicker {
}

void setPeriod(unsigned long period) {
if (period) {
oscillator_.start(1e6 / period);
auto frequency = period ? 1e6 / period : 0;
if (frequency >= oscillator_.minFrequency()) {
oscillator_.start(frequency);
} else {
oscillator_.stop();
}
Expand Down
14 changes: 14 additions & 0 deletions src/ContinuousStepper/Tickers/Tone.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ class ToneOscillator {
noTone(pin_);
}

unsigned int minFrequency() const {
#if defined(ARDUINO_ARCH_AVR)
// It is not possible to generate tones lower than 31Hz
// https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/
return 31;
#elif defined(ARDUINO_ARCH_NRF52)
// For the nRF52, the allowed range for parameter `frequency` is `[20..25000]`.
// https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/1.5.0/cores/nRF5/Tone.h#L54
return 20;
#else
return 1;
#endif
}

private:
uint8_t pin_ = nullPin;
};
Expand Down

0 comments on commit c411ba1

Please sign in to comment.