diff --git a/clock.h b/clock.h index 5402828..211f2fa 100644 --- a/clock.h +++ b/clock.h @@ -13,7 +13,7 @@ #define CLOCK_H #include -#include +#include "uClock.h" #include "peripherials.h" @@ -54,7 +54,7 @@ class Clock { // MIDI events. uClock.setOnClockStart(sendMIDIStart); uClock.setOnClockStop(sendMIDIStop); - // uClock.setOnSync24(sendMIDIClock); + uClock.setOnSync24(sendMIDIClock); // uClock.setOnSync48(sendPulseOut); uClock.start(); diff --git a/uClock/uClock.cpp b/uClock.cpp similarity index 95% rename from uClock/uClock.cpp rename to uClock.cpp index 9991004..1f53084 100755 --- a/uClock/uClock.cpp +++ b/uClock.cpp @@ -26,7 +26,7 @@ * DEALINGS IN THE SOFTWARE. */ #include "uClock.h" -#include "platforms/avr.h" +#include "uClock/platforms/avr.h" // // Platform specific timer setup/control @@ -72,6 +72,7 @@ uClockClass::uClockClass() resetCounters(); onOutputPPQNCallback = nullptr; + onSync24Callback = nullptr; onClockStartCallback = nullptr; onClockStopCallback = nullptr; // initialize reference data @@ -96,6 +97,7 @@ uint32_t uClockClass::bpmToMicroSeconds(float bpm) void uClockClass::calculateReferencedata() { mod_clock_ref = output_ppqn / input_ppqn; + mod_sync24_ref = output_ppqn / PPQN_24; } void uClockClass::setOutputPPQN(PPQNResolution resolution) @@ -191,13 +193,7 @@ float uClockClass::getTempo() } // for software timer implementation(fallback for no board support) -void uClockClass::run() -{ -#if !defined(UCLOCK_PLATFORM_FOUND) - // call software timer implementation of software - softwareTimerHandler(micros()); -#endif -} +void uClockClass::run() {} float inline uClockClass::freqToBpm(uint32_t freq) { @@ -243,6 +239,10 @@ void uClockClass::resetCounters() tick = 0; int_clock_tick = 0; mod_clock_counter = 0; + + mod_sync24_counter = 0; + sync24_tick = 0; + ext_clock_tick = 0; ext_clock_us = 0; ext_interval_idx = 0; @@ -327,6 +327,17 @@ void uClockClass::handleTimerInt() ++int_clock_tick; } ++mod_clock_counter; + + // Sync24 callback + if (onSync24Callback) { + if (mod_sync24_counter == mod_sync24_ref) + mod_sync24_counter = 0; + if (mod_sync24_counter == 0) { + onSync24Callback(sync24_tick); + ++sync24_tick; + } + ++mod_sync24_counter; + } // main PPQNCallback if (onOutputPPQNCallback) { diff --git a/uClock/uClock.h b/uClock.h similarity index 95% rename from uClock/uClock.h rename to uClock.h index 6f9a453..d8670b0 100755 --- a/uClock/uClock.h +++ b/uClock.h @@ -80,6 +80,10 @@ class uClockClass { onOutputPPQNCallback = callback; } + void setOnSync24(void (*callback)(uint32_t tick)) { + onSync24Callback = callback; + } + void setOnClockStart(void (*callback)()) { onClockStartCallback = callback; } @@ -132,6 +136,7 @@ class uClockClass { void calculateReferencedata(); void (*onOutputPPQNCallback)(uint32_t tick); + void (*onSync24Callback)(uint32_t tick); void (*onClockStartCallback)(); void (*onClockStopCallback)(); @@ -144,6 +149,10 @@ class uClockClass { uint8_t mod_clock_counter; uint16_t mod_clock_ref; + uint8_t mod_sync24_counter; + uint16_t mod_sync24_ref; + uint32_t sync24_tick; + // external clock control volatile uint32_t ext_clock_us; volatile uint32_t ext_clock_tick;