added drift option for external sync

pull/7/head
midilab 4 years ago
parent e58980ad3c
commit 3d474d6f92
  1. 18
      src/uClock.cpp
  2. 11
      src/uClock.h

@ -3,7 +3,7 @@
* Project BPM clock generator for Arduino * Project BPM clock generator for Arduino
* @brief A Library to implement BPM clock tick calls using hardware timer1 interruption. Tested on ATmega168/328, ATmega16u4/32u4 and ATmega2560. * @brief A Library to implement BPM clock tick calls using hardware timer1 interruption. Tested on ATmega168/328, ATmega16u4/32u4 and ATmega2560.
* Derived work from mididuino MidiClock class. (c) 2008 - 2011 - Manuel Odendahl - wesen@ruinwesen.com * Derived work from mididuino MidiClock class. (c) 2008 - 2011 - Manuel Odendahl - wesen@ruinwesen.com
* @version 0.9.0 * @version 0.9.3
* @author Romulo Silva * @author Romulo Silva
* @date 08/21/2020 * @date 08/21/2020
* @license MIT - (c) 2020 - Romulo Silva - contact@midilab.co * @license MIT - (c) 2020 - Romulo Silva - contact@midilab.co
@ -101,7 +101,8 @@ uClockClass::uClockClass()
// 11 is good for native 31250bps midi interface // 11 is good for native 31250bps midi interface
// 4 is good for usb-to-midi hid on leonardo // 4 is good for usb-to-midi hid on leonardo
// 1 is good on teensy lc usb midi // 1 is good on teensy lc usb midi
drift = 11; internal_drift = 11;
external_drift = 11;
pll_x = 220; pll_x = 220;
start_timer = 0; start_timer = 0;
last_interval = 0; last_interval = 0;
@ -170,15 +171,15 @@ void uClockClass::setTempo(uint16_t bpm)
return; return;
} }
if (bpm > 300 || bpm == 0) { if (bpm > 300 || bpm < 10) {
return; return;
} }
tempo = bpm; tempo = bpm;
ATOMIC( ATOMIC(
//interval = 62500 / (tempo * 24 / 60) - drift; //interval = 62500 / (tempo * 24 / 60) - internal_drift;
interval = (uint16_t)(156250 / tempo) - drift; interval = (uint16_t)(156250 / tempo) - internal_drift;
) )
} }
@ -189,15 +190,16 @@ uint16_t uClockClass::getTempo()
ATOMIC( ATOMIC(
external_interval = interval; external_interval = interval;
) )
tempo = (156250 / external_interval); tempo = (156250 / (external_interval + external_drift));
} }
return tempo; return tempo;
} }
void uClockClass::setDrift(uint8_t value) void uClockClass::setDrift(uint8_t internal, uint8_t external)
{ {
ATOMIC( ATOMIC(
drift = value; internal_drift = internal;
external_drift = external == 255 ? internal : external;
) )
} }

@ -3,7 +3,7 @@
* Project BPM clock generator for Arduino * Project BPM clock generator for Arduino
* @brief A Library to implement BPM clock tick calls using hardware timer1 interruption. Tested on ATmega168/328, ATmega16u4/32u4 and ATmega2560. * @brief A Library to implement BPM clock tick calls using hardware timer1 interruption. Tested on ATmega168/328, ATmega16u4/32u4 and ATmega2560.
* Derived work from mididuino MidiClock class. (c) 2008 - 2011 - Manuel Odendahl - wesen@ruinwesen.com * Derived work from mididuino MidiClock class. (c) 2008 - 2011 - Manuel Odendahl - wesen@ruinwesen.com
* @version 0.9.0 * @version 0.9.3
* @author Romulo Silva * @author Romulo Silva
* @date 08/21/2020 * @date 08/21/2020
* @license MIT - (c) 2020 - Romulo Silva - contact@midilab.co * @license MIT - (c) 2020 - Romulo Silva - contact@midilab.co
@ -67,6 +67,7 @@ class uClockClass {
volatile uint16_t interval; volatile uint16_t interval;
volatile uint16_t last_clock; volatile uint16_t last_clock;
uint8_t ppqn;
uint32_t div96th_counter; uint32_t div96th_counter;
uint32_t div32th_counter; uint32_t div32th_counter;
uint32_t div16th_counter; uint32_t div16th_counter;
@ -74,7 +75,8 @@ class uClockClass {
uint16_t counter; uint16_t counter;
uint16_t pll_x; uint16_t pll_x;
uint8_t drift; uint8_t internal_drift;
uint8_t external_drift;
uint16_t tempo; uint16_t tempo;
uint32_t start_timer; uint32_t start_timer;
uint8_t mode; uint8_t mode;
@ -84,6 +86,9 @@ class uClockClass {
public: public:
uint8_t INTERNAL_CLOCK = 0;
uint8_t EXTERNAL_CLOCK = 1;
uint8_t state; uint8_t state;
uClockClass(); uClockClass();
@ -119,7 +124,7 @@ class uClockClass {
void pause(); void pause();
void setTempo(uint16_t bpm); void setTempo(uint16_t bpm);
uint16_t getTempo(); uint16_t getTempo();
void setDrift(uint8_t value); void setDrift(uint8_t internal, uint8_t external = 255);
// external timming control // external timming control
void setMode(uint8_t tempo_mode); void setMode(uint8_t tempo_mode);

Loading…
Cancel
Save