diff --git a/src/uClock.cpp b/src/uClock.cpp index b73f9ed..62640df 100755 --- a/src/uClock.cpp +++ b/src/uClock.cpp @@ -76,7 +76,7 @@ static inline uint32_t phase_mult(uint32_t val) return (val * PHASE_FACTOR) >> 8; } -static inline uint16_t clock_diff(uint32_t old_clock, uint32_t new_clock) +static inline uint32_t clock_diff(uint32_t old_clock, uint32_t new_clock) { if (new_clock >= old_clock) { return new_clock - old_clock; @@ -87,7 +87,6 @@ static inline uint16_t clock_diff(uint32_t old_clock, uint32_t new_clock) uClockClass::uClockClass() { - pll_x = 220; tempo = 120; start_timer = 0; last_interval = 0; @@ -248,7 +247,6 @@ void uClockClass::clockMe() void uClockClass::resetCounters() { - counter = 0; external_clock = 0; internal_tick = 0; external_tick = 0; @@ -287,6 +285,9 @@ void uClockClass::handleExternalClock() case STARTED: uint32_t u_timer = micros(); + if(external_tick == 0) { + external_clock = u_timer; + } last_interval = clock_diff(external_clock, u_timer); external_clock = u_timer; @@ -316,7 +317,7 @@ void uClockClass::handleExternalClock() if (external_tick == 1) { interval = last_interval; } else { - interval = (((uint32_t)interval * (uint32_t)pll_x) + (uint32_t)(256 - pll_x) * (uint32_t)last_interval) >> 8; + interval = (((uint32_t)interval * (uint32_t)PLL_X) + (uint32_t)(256 - PLL_X) * (uint32_t)last_interval) >> 8; } break; } @@ -325,6 +326,7 @@ void uClockClass::handleExternalClock() void uClockClass::handleTimerInt() { if (mode == EXTERNAL_CLOCK) { + // sync tick position with external tick clock if ((internal_tick < external_tick) || (internal_tick > (external_tick + 1))) { internal_tick = external_tick; div32th_counter = indiv32th_counter; @@ -332,7 +334,7 @@ void uClockClass::handleTimerInt() mod6_counter = inmod6_counter; } - counter = interval; + uint32_t counter = interval; uint32_t u_timer = micros(); sync_interval = clock_diff(external_clock, u_timer); @@ -433,7 +435,6 @@ uint32_t uClockClass::getPlayTime() umodular::clock::uClockClass uClock; -volatile uint16_t _clock = 0; volatile uint32_t _timer = 0; // @@ -450,7 +451,6 @@ ISR(TIMER1_COMPA_vect) _timer = millis(); if (uClock.state == uClock.STARTED) { - _clock++; uClock.handleTimerInt(); } } diff --git a/src/uClock.h b/src/uClock.h index d69b455..7f1a093 100755 --- a/src/uClock.h +++ b/src/uClock.h @@ -39,6 +39,11 @@ namespace umodular { namespace clock { #define PHASE_FACTOR 16 +#define PLL_X 220 + +// for smooth slave tempo calculate display you should raise this value +// in between 64 to 128. +// note: this doesn't impact on sync time, only display time getTempo() #define EXT_INTERVAL_BUFFER_SIZE 24 #define SECS_PER_MIN (60UL) @@ -63,37 +68,30 @@ class uClockClass { void (*onClockStartCallback)(); void (*onClockStopCallback)(); - volatile uint32_t interval; - volatile uint32_t external_clock; - uint32_t tick_us_interval; - float tick_hertz_interval; - + // internal clock control volatile uint32_t internal_tick; - volatile uint32_t external_tick; - - // tick external and internal control - volatile uint32_t indiv32th_counter; - volatile uint32_t indiv16th_counter; - volatile uint8_t inmod6_counter; - - volatile uint32_t div96th_counter; volatile uint32_t div32th_counter; volatile uint32_t div16th_counter; volatile uint8_t mod6_counter; + // external clock control + volatile uint32_t external_clock; + volatile uint32_t external_tick; + volatile uint32_t indiv32th_counter; + volatile uint32_t indiv16th_counter; + volatile uint8_t inmod6_counter; + volatile uint32_t interval; + volatile uint32_t last_interval; + uint32_t sync_interval; - uint32_t counter; - uint16_t pll_x; + uint32_t tick_us_interval; + float tick_hertz_interval; - uint32_t last_tick; float tempo; uint32_t start_timer; uint8_t mode; - uint16_t last_interval; - uint16_t sync_interval; - - uint16_t ext_interval_buffer[EXT_INTERVAL_BUFFER_SIZE]; + volatile uint32_t ext_interval_buffer[EXT_INTERVAL_BUFFER_SIZE]; uint16_t ext_interval_idx; public: