From 51b03d55b615bc429ba986a6754e8aaf372f41d3 Mon Sep 17 00:00:00 2001 From: midilab Date: Fri, 5 Jan 2024 11:23:33 -0300 Subject: [PATCH] fix: callbacks trigger on tick 0 not skiped anymore --- README.md | 32 ++++++++++++++++++-------------- src/platforms/esp32.h | 2 +- src/uClock.cpp | 26 +++++++++++++------------- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index cdcf873..e9bd542 100755 --- a/README.md +++ b/README.md @@ -15,25 +15,29 @@ The uClock library API operates through attached callback functions mechanism: 2. **onStepCallback(uint32_t step)** good way to code old style step sequencer based on 16th note schema(not dependent on PPQN resolution) 3. **onSync24Callback(uint32_t tick)** good way to code a clock machine, or keep your gears synced with your device 4. **onSync48Callback(uint32_t tick)** there are some 48ppqn based sync devices out there -5. **onClockStartCallback()** there are some 48ppqn based sync devices out there -6. **onClockStopCallback()** there are some 48ppqn based sync devices out there +5. **onClockStartCallback()** on uClock Start event +6. **onClockStopCallback()** on uClock Stop event ```c++ -// avaliable resolutions -// [ uClock.PPQN_24, uClock.PPQN_48, uClock.PPQN_96, uClock.PPQN_384, uClock.PPQN_480, uClock.PPQN_960 ] -// not mandatory to call, the default is 96PPQN if not set -uClock.setPPQN(uClock.PPQN_96); +#include -// you need to use at least one! -uClock.setOnPPQN(onPPQNCallback); -uClock.setOnStep(onStepCallback); -uClock.setOnSync24(onSync24Callback); -uClock.setOnSync48(onSync48Callback); +void setup() { + // avaliable resolutions + // [ uClock.PPQN_24, uClock.PPQN_48, uClock.PPQN_96, uClock.PPQN_384, uClock.PPQN_480, uClock.PPQN_960 ] + // not mandatory to call, the default is 96PPQN if not set + uClock.setPPQN(uClock.PPQN_96); + + // you need to use at least one! + uClock.setOnPPQN(onPPQNCallback); + uClock.setOnStep(onStepCallback); + uClock.setOnSync24(onSync24Callback); + uClock.setOnSync48(onSync48Callback); -uClock.setOnClockStart(onClockStartCallback); -uClock.setOnClockStop(onClockStopCallback); + uClock.setOnClockStart(onClockStartCallback); + uClock.setOnClockStop(onClockStopCallback); -uClock.init(); + uClock.init(); +} ``` Resolutions: set youw own resolution for your clock needs diff --git a/src/platforms/esp32.h b/src/platforms/esp32.h index 99153e3..1344923 100644 --- a/src/platforms/esp32.h +++ b/src/platforms/esp32.h @@ -9,7 +9,7 @@ hw_timer_t * _uclockTimer = NULL; //#define ATOMIC(X) portENTER_CRITICAL_ISR(&_uclockTimerMux); X; portEXIT_CRITICAL_ISR(&_uclockTimerMux); // FreeRTOS main clock task size in bytes -#define CLOCK_STACK_SIZE 5012 // adjust for your needs, a sequencer with heavy serial handling should be large in size +#define CLOCK_STACK_SIZE 5*1024 // adjust for your needs, a sequencer with heavy serial handling should be large in size TaskHandle_t taskHandle; // mutex to protect the shared resource SemaphoreHandle_t _mutex; diff --git a/src/uClock.cpp b/src/uClock.cpp index c34dc74..63da5a0 100755 --- a/src/uClock.cpp +++ b/src/uClock.cpp @@ -367,7 +367,7 @@ void uClockClass::handleTimerInt() { // External sync is handled here... test if clock check on each tick instead when // mod24_counter kicks in will help or worst slave timing sync quality - if (mod24_counter == mod24_ref) { + if (mod24_counter == 0) { if (mode == EXTERNAL_CLOCK) { // sync tick position with external tick clock if ((int_clock_tick < ext_clock_tick) || (int_clock_tick > (ext_clock_tick + 1))) { @@ -399,18 +399,18 @@ void uClockClass::handleTimerInt() if (onSync24Callback) { onSync24Callback(int_clock_tick); } - // reset counter - mod24_counter = 0; + // reset counter reference + mod24_counter = mod24_ref; // internal clock tick me! sync24 tick too ++int_clock_tick; } // sync signals first please... if (onSync48Callback) { - if (mod48_counter == mod48_ref) { + if (mod48_counter == 0) { onSync48Callback(sync48_tick); - // reset counter - mod48_counter = 0; + // reset counter reference + mod48_counter = mod48_ref; // sync48 tick me! ++sync48_tick; } @@ -424,10 +424,10 @@ void uClockClass::handleTimerInt() if (onStepCallback) { // we can add a time signature here for call setup based on mod_step_ref // basic will be 16ths, but let the option to handle unusual sequences - if (mod_step_counter == mod_step_ref) { + if (mod_step_counter == 0) { onStepCallback(step_counter); - // reset counter - mod_step_counter = 0; + // reset counter reference + mod_step_counter = mod_step_ref; // going forward to the next step call ++step_counter; } @@ -444,10 +444,10 @@ void uClockClass::handleTimerInt() // tick me! ++tick; - // increment mod counters - ++mod24_counter; - ++mod48_counter; - ++mod_step_counter; + // decrement mod counters + --mod24_counter; + --mod48_counter; + --mod_step_counter; } // elapsed time support