You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.0KB

  1. // Copyright 2012 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (olivier@mutable-instruments.net)
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. //
  16. // -----------------------------------------------------------------------------
  17. //
  18. // Square oscillator generated from a timer.
  19. #ifndef EDGES_TIMER_OSCILLATOR_H_
  20. #define EDGES_TIMER_OSCILLATOR_H_
  21. #include "avrlibx/avrlibx.h"
  22. #include "avrlibx/system/timer.h"
  23. namespace edges {
  24. using namespace avrlibx;
  25. enum PulseWidth {
  26. PULSE_WIDTH_50,
  27. PULSE_WIDTH_66,
  28. PULSE_WIDTH_75,
  29. PULSE_WIDTH_87,
  30. PULSE_WIDTH_95,
  31. PULSE_WIDTH_CV_CONTROLLED
  32. };
  33. class TimerOscillator {
  34. public:
  35. TimerOscillator() { }
  36. ~TimerOscillator() { }
  37. template<typename Timer>
  38. void UpdatePitch(int16_t pitch, PulseWidth pulse_width) {
  39. if (pitch < (24 << 7) && prescaler_ != TIMER_PRESCALER_CLK_64) {
  40. prescaler_ = TIMER_PRESCALER_CLK_64;
  41. }
  42. if (pitch > (104 << 7) && prescaler_ != TIMER_PRESCALER_CLK_8) {
  43. prescaler_ = TIMER_PRESCALER_CLK_8;
  44. }
  45. UpdateTimerParameters(pitch, pulse_width);
  46. Timer::set_period(period_);
  47. Timer::set_prescaler(prescaler_);
  48. }
  49. // Create a x16 version (/2 after triangle conversion) for the NES triangle
  50. // expander.
  51. template<typename Timer>
  52. void SubFollow(const TimerOscillator& t) {
  53. uint16_t period = t.period();
  54. period >>= 1;
  55. value_ = period >> 1;
  56. Timer::set_period(period);
  57. Timer::set_prescaler(
  58. t.prescaler() == TIMER_PRESCALER_CLK_64
  59. ? TIMER_PRESCALER_CLK_8
  60. : TIMER_PRESCALER_CLK);
  61. }
  62. template<typename PWM>
  63. void Gate(bool gate) {
  64. PWM::set_value(gate ? value_ : 0);
  65. }
  66. template<typename PWM, typename Timer>
  67. void Init() {
  68. Timer::set_mode(TIMER_MODE_DUAL_PWM_T);
  69. prescaler_ = TIMER_PRESCALER_CLK_8;
  70. PWM::Start();
  71. }
  72. // Value of the pulse width when the mode is set to "cv controlled".
  73. inline void set_cv_pw(uint8_t pw) {
  74. if (pw > 250) {
  75. pw = 250;
  76. }
  77. if (pw < 6) {
  78. pw = 6;
  79. }
  80. cv_pw_ = pw;
  81. }
  82. inline uint16_t period() const { return period_; }
  83. inline uint16_t value() const { return value_; }
  84. inline TimerPrescaler prescaler() const { return prescaler_; }
  85. private:
  86. void UpdateTimerParameters(int16_t pitch, PulseWidth pulse_width);
  87. uint16_t value_;
  88. uint16_t period_;
  89. uint8_t pitch_range_;
  90. uint8_t cv_pw_;
  91. TimerPrescaler prescaler_;
  92. DISALLOW_COPY_AND_ASSIGN(TimerOscillator);
  93. };
  94. } // namespace edges
  95. #endif // EDGES_TIMER_OSCILLATOR_H_