From 3ea0983dd6fb02fff310b52399d17d2b4298dd6e Mon Sep 17 00:00:00 2001 From: midilab Date: Tue, 27 May 2025 16:18:06 -0300 Subject: [PATCH 1/3] added setExtIntervalBuffer(uint8_t buffer_size) for getTempo() average calculus setup. should be called before uClock.init(); --- src/uClock.cpp | 103 ++++++++++++++++++++++++++++--------------------- src/uClock.h | 37 +++++++++--------- 2 files changed, 76 insertions(+), 64 deletions(-) diff --git a/src/uClock.cpp b/src/uClock.cpp index 72e324f..ad152e2 100755 --- a/src/uClock.cpp +++ b/src/uClock.cpp @@ -23,7 +23,7 @@ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. + * DEALINGS IN THE SOFTWARE. */ #include "uClock.h" @@ -90,23 +90,23 @@ // header of this file void uclockInitTimer() { - // begin at 120bpm + // begin at 120bpm initTimer(uClock.bpmToMicroSeconds(120.00)); } -void setTimerTempo(float bpm) +void setTimerTempo(float bpm) { setTimer(uClock.bpmToMicroSeconds(bpm)); } namespace umodular { namespace clock { -static inline uint32_t phase_mult(uint32_t val) +static inline uint32_t phase_mult(uint32_t val) { return (val * PHASE_FACTOR) >> 8; } -static inline uint32_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; @@ -140,21 +140,24 @@ uClockClass::uClockClass() calculateReferencedata(); } -void uClockClass::init() +void uClockClass::init() { + if (ext_interval_buffer == nullptr) + setExtIntervalBuffer(1); + uclockInitTimer(); // first interval calculus setTempo(tempo); } -uint32_t uClockClass::bpmToMicroSeconds(float bpm) +uint32_t uClockClass::bpmToMicroSeconds(float bpm) { return (60000000.0f / (float)output_ppqn / bpm); } void uClockClass::calculateReferencedata() { - mod_clock_ref = output_ppqn / input_ppqn; + mod_clock_ref = output_ppqn / input_ppqn; mod_sync1_ref = output_ppqn / PPQN_1; mod_sync2_ref = output_ppqn / PPQN_2; mod_sync4_ref = output_ppqn / PPQN_4; @@ -185,20 +188,20 @@ void uClockClass::setInputPPQN(PPQNResolution resolution) ) } -void uClockClass::start() +void uClockClass::start() { resetCounters(); start_timer = millis(); - + if (onClockStartCallback) { onClockStartCallback(); - } - + } + if (clock_mode == INTERNAL_CLOCK) { clock_state = STARTED; } else { clock_state = STARTING; - } + } } void uClockClass::stop() @@ -211,7 +214,7 @@ void uClockClass::stop() } } -void uClockClass::pause() +void uClockClass::pause() { if (clock_mode == INTERNAL_CLOCK) { if (clock_state == PAUSED) { @@ -222,12 +225,12 @@ void uClockClass::pause() } } -void uClockClass::setTempo(float bpm) +void uClockClass::setTempo(float bpm) { if (clock_mode == EXTERNAL_CLOCK) { return; } - + if (bpm < MIN_BPM || bpm > MAX_BPM) { return; } @@ -239,19 +242,19 @@ void uClockClass::setTempo(float bpm) setTimerTempo(bpm); } -float uClockClass::getTempo() +float uClockClass::getTempo() { if (clock_mode == EXTERNAL_CLOCK) { uint32_t acc = 0; // wait the buffer to get full - if (ext_interval_buffer[EXT_INTERVAL_BUFFER_SIZE-1] == 0) { + if (ext_interval_buffer[ext_interval_buffer_size-1] == 0) { return tempo; } - for (uint8_t i=0; i < EXT_INTERVAL_BUFFER_SIZE; i++) { + for (uint8_t i=0; i < ext_interval_buffer_size; i++) { acc += ext_interval_buffer[i]; } if (acc != 0) { - return freqToBpm(acc / EXT_INTERVAL_BUFFER_SIZE); + return freqToBpm(acc / ext_interval_buffer_size); } } return tempo; @@ -272,17 +275,17 @@ float inline uClockClass::freqToBpm(uint32_t freq) return (float)((float)(usecs/(float)input_ppqn) * 60.0); } -void uClockClass::setClockMode(ClockMode tempo_mode) +void uClockClass::setClockMode(ClockMode tempo_mode) { clock_mode = tempo_mode; } -uClockClass::ClockMode uClockClass::getClockMode() +uClockClass::ClockMode uClockClass::getClockMode() { return clock_mode; } -void uClockClass::clockMe() +void uClockClass::clockMe() { if (clock_mode == EXTERNAL_CLOCK) { ATOMIC( @@ -291,7 +294,17 @@ void uClockClass::clockMe() } } -void uClockClass::resetCounters() +void uClockClass::setExtIntervalBuffer(uint8_t buffer_size) +{ + if (ext_interval_buffer != nullptr) + return; + + // alloc once and forever policy + ext_interval_buffer_size = buffer_size; + ext_interval_buffer = (uint32_t*) malloc( sizeof(uint32_t) * ext_interval_buffer_size ); +} + +void uClockClass::resetCounters() { tick = 0; int_clock_tick = 0; @@ -316,16 +329,16 @@ void uClockClass::resetCounters() sync24_tick = 0; mod_sync48_counter = 0; sync48_tick = 0; - - for (uint8_t i=0; i < EXT_INTERVAL_BUFFER_SIZE; i++) { + + for (uint8_t i=0; i < ext_interval_buffer_size; i++) { ext_interval_buffer[i] = 0; } } -void uClockClass::tap() +void uClockClass::tap() { // we can make use of mod_sync1_ref for tap - //uint8_t mod_tap_ref = output_ppqn / PPQN_1; + //uint8_t mod_tap_ref = output_ppqn / PPQN_1; // we only set tap if ClockMode is INTERNAL_CLOCK } @@ -381,7 +394,7 @@ bool inline uClockClass::processShuffle() int8_t shff = shuffle.step[step_counter%shuffle.size]; if (shuffle_shoot_ctrl == false && mod_step_counter == 0) - shuffle_shoot_ctrl = true; + shuffle_shoot_ctrl = true; //if (mod_step_counter == mod_step_ref-1) @@ -389,11 +402,11 @@ bool inline uClockClass::processShuffle() mod_shuffle = mod_step_counter - shff; // any late shuffle? we should skip next mod_step_counter == 0 if (last_shff < 0 && mod_step_counter != 1) - return false; + return false; } else if (shff < 0) { mod_shuffle = mod_step_counter - (mod_step_ref + shff); //if (last_shff < 0 && mod_step_counter != 1) - // return false; + // return false; shuffle_shoot_ctrl = true; } @@ -414,7 +427,7 @@ bool inline uClockClass::processShuffle() return false; } -void uClockClass::handleExternalClock() +void uClockClass::handleExternalClock() { switch (clock_state) { case PAUSED: @@ -434,7 +447,7 @@ void uClockClass::handleExternalClock() ext_clock_tick++; // accumulate interval incomming ticks data for getTempo() smooth reads on slave clock_mode - if(++ext_interval_idx >= EXT_INTERVAL_BUFFER_SIZE) { + if(++ext_interval_idx >= ext_interval_buffer_size) { ext_interval_idx = 0; } ext_interval_buffer[ext_interval_idx] = last_interval; @@ -448,7 +461,7 @@ void uClockClass::handleExternalClock() } } -void uClockClass::handleTimerInt() +void uClockClass::handleTimerInt() { // track main input clock counter if (mod_clock_counter == mod_clock_ref) @@ -515,7 +528,7 @@ void uClockClass::handleTimerInt() } ++mod_sync2_counter; } - + // Sync4 callback if (onSync4Callback) { if (mod_sync4_counter == mod_sync4_ref) @@ -583,7 +596,7 @@ void uClockClass::handleTimerInt() if (mod_step_counter == mod_step_ref) mod_step_counter = 0; // processShufle make use of mod_step_counter == 0 logic too - if (processShuffle()) { + if (processShuffle()) { onStepCallback(step_counter); // going forward to the next step call ++step_counter; @@ -605,7 +618,7 @@ uint8_t uClockClass::getNumberOfMinutes(uint32_t time) { if ( time == 0 ) { return time; - } + } return (((_millis - time) / 1000) / SECS_PER_MIN) % SECS_PER_MIN; } @@ -613,7 +626,7 @@ uint8_t uClockClass::getNumberOfHours(uint32_t time) { if ( time == 0 ) { return time; - } + } return (((_millis - time) / 1000) % SECS_PER_DAY) / SECS_PER_HOUR; } @@ -621,7 +634,7 @@ uint8_t uClockClass::getNumberOfDays(uint32_t time) { if ( time == 0 ) { return time; - } + } return ((_millis - time) / 1000) / SECS_PER_DAY; } @@ -629,12 +642,12 @@ uint32_t uClockClass::getNowTimer() { return _millis; } - + uint32_t uClockClass::getPlayTime() { return start_timer; } - + } } // end namespace umodular::clock umodular::clock::uClockClass uClock; @@ -642,13 +655,13 @@ umodular::clock::uClockClass uClock; volatile uint32_t _millis = 0; // -// TIMER HANDLER -// -void uClockHandler() +// TIMER HANDLER +// +void uClockHandler() { // global timer counter _millis = millis(); - + if (uClock.clock_state == uClock.STARTED) { uClock.handleTimerInt(); } diff --git a/src/uClock.h b/src/uClock.h index 97ca6ac..468cf1d 100755 --- a/src/uClock.h +++ b/src/uClock.h @@ -23,7 +23,7 @@ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. + * DEALINGS IN THE SOFTWARE. */ #ifndef __U_CLOCK_H__ @@ -36,7 +36,7 @@ namespace umodular { namespace clock { // Shuffle templates are specific for each PPQN output resolution // min: -(output_ppqn/4)-1 ticks -// max: (output_ppqn/4)-1 ticks +// max: (output_ppqn/4)-1 ticks // adjust the size of you template if more than 16 shuffle step info needed #define MAX_SHUFFLE_TEMPLATE_SIZE 16 typedef struct { @@ -45,12 +45,6 @@ typedef struct { int8_t step[MAX_SHUFFLE_TEMPLATE_SIZE] = {0}; } SHUFFLE_TEMPLATE; -// 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() -// if you dont want to use it, set it to 1 for memory save -#define EXT_INTERVAL_BUFFER_SIZE 128 - #define MIN_BPM 1 #define MAX_BPM 400 @@ -90,7 +84,7 @@ class uClockClass { }; ClockState clock_state; - + uClockClass(); void setOnOutputPPQN(void (*callback)(uint32_t tick)) { @@ -105,19 +99,19 @@ class uClockClass { void setOnSync1(void (*callback)(uint32_t tick)) { onSync1Callback = callback; } - + void setOnSync2(void (*callback)(uint32_t tick)) { onSync2Callback = callback; } - + void setOnSync4(void (*callback)(uint32_t tick)) { onSync4Callback = callback; } - + void setOnSync8(void (*callback)(uint32_t tick)) { onSync8Callback = callback; } - + void setOnSync12(void (*callback)(uint32_t tick)) { onSync12Callback = callback; } @@ -125,7 +119,7 @@ class uClockClass { void setOnSync24(void (*callback)(uint32_t tick)) { onSync24Callback = callback; } - + void setOnSync48(void (*callback)(uint32_t tick)) { onSync48Callback = callback; } @@ -145,7 +139,7 @@ class uClockClass { void handleTimerInt(); void handleExternalClock(); void resetCounters(); - + // external class control void start(); void stop(); @@ -160,6 +154,11 @@ class uClockClass { void setClockMode(ClockMode tempo_mode); ClockMode getClockMode(); void clockMe(); + // for smooth slave tempo calculate display you should raise the + // buffer_size of ext_interval_buffer in between 64 to 128. 254 max size. + // note: this doesn't impact on sync time, only display time getTempo() + // if you dont want to use it, it is default set it to 1 for memory save + void setExtIntervalBuffer(uint8_t buffer_size); // shuffle void setShuffle(bool active); @@ -169,10 +168,10 @@ class uClockClass { void setShuffleTemplate(int8_t * shuff, uint8_t size); // use this to know how many positive or negative ticks to add to current note length int8_t getShuffleLength(); - + // todo! void tap(); - + // elapsed time support uint8_t getNumberOfSeconds(uint32_t time); uint8_t getNumberOfMinutes(uint32_t time); @@ -246,7 +245,8 @@ class uClockClass { uint32_t start_timer; ClockMode clock_mode; - volatile uint32_t ext_interval_buffer[EXT_INTERVAL_BUFFER_SIZE]; + volatile uint32_t * ext_interval_buffer = nullptr; + uint8_t ext_interval_buffer_size; uint16_t ext_interval_idx; // shuffle implementation @@ -265,4 +265,3 @@ extern "C" { } #endif /* __U_CLOCK_H__ */ - From b30a6950a0b14fb87d632130738823182b7fa5de Mon Sep 17 00:00:00 2001 From: Adam Wonak Date: Mon, 2 Jun 2025 20:51:39 -0700 Subject: [PATCH 2/3] Ensure external clock input tempo and bpm are constrained to the defined min and max values. --- src/uClock.cpp | 15 +++++++++------ src/uClock.h | 1 + 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/uClock.cpp b/src/uClock.cpp index ad152e2..7923b9d 100755 --- a/src/uClock.cpp +++ b/src/uClock.cpp @@ -254,7 +254,7 @@ float uClockClass::getTempo() acc += ext_interval_buffer[i]; } if (acc != 0) { - return freqToBpm(acc / ext_interval_buffer_size); + return constrainBpm(freqToBpm(acc / ext_interval_buffer_size)); } } return tempo; @@ -275,6 +275,11 @@ float inline uClockClass::freqToBpm(uint32_t freq) return (float)((float)(usecs/(float)input_ppqn) * 60.0); } +float inline uClockClass::constrainBpm(float bpm) +{ + return (bpm < MIN_BPM) ? MIN_BPM : ( bpm > MAX_BPM ? MAX_BPM : bpm ); +} + void uClockClass::setClockMode(ClockMode tempo_mode) { clock_mode = tempo_mode; @@ -492,12 +497,10 @@ void uClockClass::handleTimerInt() } // update internal clock timer frequency - float bpm = freqToBpm(counter); + float bpm = constrainBpm(freqToBpm(counter)); if (bpm != tempo) { - if (bpm >= MIN_BPM && bpm <= MAX_BPM) { - tempo = bpm; - setTimerTempo(bpm); - } + tempo = bpm; + setTimerTempo(bpm); } } diff --git a/src/uClock.h b/src/uClock.h index 468cf1d..b4fafaf 100755 --- a/src/uClock.h +++ b/src/uClock.h @@ -184,6 +184,7 @@ class uClockClass { private: float inline freqToBpm(uint32_t freq); + float inline constrainBpm(float bpm); void calculateReferencedata(); // shuffle From 94aa9ef67407f40ba288bde7a37990038a270661 Mon Sep 17 00:00:00 2001 From: midilab Date: Wed, 11 Jun 2025 17:52:56 -0300 Subject: [PATCH 3/3] updating README and examples with newer v2.2.1 api --- README.md | 89 ++++++++++++++----- .../AVRUartSlaveMidiClockMonitor.ino | 26 ++++-- .../AcidStepSequencer/AcidStepSequencer.ino | 40 ++++----- .../ESP32UartMasterMidiClock.ino | 26 +++--- .../GenericMasterOrExternalSync.ino | 19 ++-- .../LeonardoUsbSlaveMidiClockMonitor.ino | 38 ++++---- examples/MidiClock/MidiClock.ino | 18 ++-- .../RP2040UsbUartMasterClock.ino | 16 ++-- .../STM32UartMasterMidiClock.ino | 24 ++--- .../TeensyUsbMasterMidiClock.ino | 18 ++-- .../TeensyUsbSlaveMidiClock.ino | 20 +++-- .../TeensyUsbSlaveMidiClockMonitor.ino | 40 +++++---- .../XiaoUsbMasterMidiClock.ino | 22 ++--- 13 files changed, 241 insertions(+), 155 deletions(-) diff --git a/README.md b/README.md index 4ef20f4..b27e88c 100755 --- a/README.md +++ b/README.md @@ -11,15 +11,23 @@ With uClock, you can create professional-grade sequencers, sync boxes, or genera ## Interface The uClock library API operates through an attached callback function mechanism: -1. **setOnOutputPPQN(onPPQNCallback) > onOutputPPQNCallback(uint32_t tick)** Calls are made on each new output pulse based on the selected PPQN resolution (if no PPQN is set, the default is 96 PPQN). -2. **setOnInputPPQN(onPPQNCallback) > onInputPPQNCallback(uint32_t tick)** Set the expected input PPQN (Pulses Per Quarter Note) resolution for external clock sync. -3. **setOnStep(onStepCallback) > onStepCallback(uint32_t step)** A good way to code an old-style step sequencer based on a 16th-note schema, which is not dependent on PPQN (Pulses Per Quarter Note) output config. -4. **setOnSync24(onSync24Callback) > onSync24Callback(uint32_t tick)** A good way to code a clock machine or keep your devices in sync with your system is to use setOnSyncXX(), where XX represents the PPQN (Pulses Per Quarter Note) value you want to use. MIDI specifications typically expect 24 PPQN, but if you're working with other devices that are not MIDI standard, you can choose a different PPQN value. Please refer to the supported PPQNs to select from. You can use one or more setOnSyncXX callbacks for different sync output signatures. -5. **setOnClockStart(onClockStartCallback) > onClockStartCallback()** On the uClock Start event. -6. **setOnClockStop(onClockStopCallback) > onClockStopCallback()** On the uClock Stop event. +1. **setOnOutputPPQN(onPPQNCallback) > onOutputPPQNCallback(uint32_t tick)** Callback are made on each new output pulse based on the selected PPQN resolution (if no PPQN is set, the default is 96 PPQN). +2. **setOnStep(onStepCallback) > onStepCallback(uint32_t step)** A good way to code an old-style step sequencer based on a 16th-note schema, which is not dependent on PPQN (Pulses Per Quarter Note) output config. +3. **setOnSync24(onSync24Callback) > onSync24Callback(uint32_t tick)** A good way to code a clock machine or keep your devices in sync with your system is to use setOnSyncXX(), where XX represents the PPQN (Pulses Per Quarter Note) value you want to use. MIDI specifications typically expect 24 PPQN, but if you're working with other devices that are not MIDI standard, you can choose a different PPQN value. Please refer to the supported PPQNs to select from. You can use one or more setOnSyncXX callbacks for different sync output signatures. +4. **setOnClockStart(onClockStartCallback) > onClockStartCallback()** On the uClock Start event. +5. **setOnClockStop(onClockStopCallback) > onClockStopCallback()** On the uClock Stop event. ### Clock input/output resolutions +You can setup multiple clock resolutions for clock generation and clock sync signals. + +#### Set internal output clock and external input clock resolution + +1. **void setOutputPPQN(PPQNResolution resolution);** sets the main clock output(independent from clock input). +2. **void setInputPPQN(PPQNResolution resolution);** sets the expected external clock resolution for input(independent from clock output). + +#### PPQNResolution Avaliable Resolutions + 1. **PPQN_1** 1 Pulses Per Quarter Note (only input) 2. **PPQN_2** 2 Pulses Per Quarter Note (only input) 3. **PPQN_4** 4 Pulses Per Quarter Note @@ -82,7 +90,7 @@ You will find more complete examples on examples/ folder: bool _external_sync_on = false; // the main uClock PPQN resolution ticking -void onPPQNCallback(uint32_t tick) { +void onOutputPPQNCallback(uint32_t tick) { // tick your sequencers or tickable devices... } @@ -90,11 +98,31 @@ void onStepCallback(uint32_t step) { // triger step data for sequencer device... } +// The callback function called by uClock each Pulse of 1PPQN clock resolution. +void onSync1Callback(uint32_t tick) { + // send sync signal to... +} + +// The callback function called by uClock each Pulse of 2PPQN clock resolution. +void onSync2Callback(uint32_t tick) { + // send sync signal to... +} + +// The callback function called by uClock each Pulse of 4PPQN clock resolution. +void onSync4Callback(uint32_t tick) { + // send sync signal to... +} + // The callback function called by uClock each Pulse of 24PPQN clock resolution. void onSync24Callback(uint32_t tick) { // send sync signal to... } +// The callback function called by uClock each Pulse of 48PPQN clock resolution. +void onSync48Callback(uint32_t tick) { + // send sync signal to... +} + // The callback function called when clock starts by using uClock.start() method. void onClockStartCallback() { // send start signal to... @@ -106,28 +134,41 @@ void onClockStopCallback() { } void setup() { - - // inits the clock library - uClock.init(); - - // avaliable resolutions - // [ uClock.PPQN_24, uClock.PPQN_48, uClock.PPQN_96, uClock.PPQN_384, uClock.PPQN_480, uClock.PPQN_960 ] + // setup clock library + // avaliable output resolutions + // [ uClock.PPQN_4, uClock.PPQN_8, uClock.PPQN_12, 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); + uClock.setOutputPPQN(uClock.PPQN_96); // you need to use at least one! - uClock.setOnOutputPPQN(onPPQNCallback); + uClock.setOnOutputPPQN(onOutputPPQNCallback); uClock.setOnStep(onStepCallback); + // multi sync output signatures avaliable + // normaly used by eurorack modular modules + uClock.setOnSync1(onSync1Callback); + uClock.setOnSync2(onSync2Callback); + uClock.setOnSync4(onSync4Callback); + // midi sync standard uClock.setOnSync24(onSync24Callback); + // some korg machines do 48ppqn + uClock.setOnSync48(onSync48Callback); uClock.setOnClockStart(onClockStartCallback); uClock.setOnClockStop(onClockStopCallback); // set external sync mode? if (_external_sync_on) { - uClock.setMode(uClock.EXTERNAL_CLOCK); + uClock.setClockMode(uClock.EXTERNAL_CLOCK); + // what is the clock of incomming signal to sync with? + // not mandatory to call, the default is 24PPQN if not set + // avaliable input resolutions - should be always InputPPQN <= OutputPPQN + // [ uClock.PPQN_1, uClock.PPQN_2, uClock.PPQN_4, uClock.PPQN_8, uClock.PPQN_12, uClock.PPQN_24, uClock.PPQN_48, uClock.PPQN_96, uClock.PPQN_384, uClock.PPQN_480, uClock.PPQN_960 ] + uClock.setInputPPQN(uClock.PPQN_24); } + // inits the clock library + uClock.init(); + // starts clock uClock.start(); } @@ -184,8 +225,6 @@ void setup() { // Initialize serial communication at 31250 bits per second, the default MIDI serial speed communication: Serial.begin(31250); - // Inits the clock - uClock.init(); // Set the callback function for the clock output to send MIDI Sync message based on 24PPQN uClock.setOnSync24(onSync24Callback); // Set the callback function for MIDI Start and Stop messages. @@ -194,6 +233,9 @@ void setup() { // Set the clock BPM to 126 BPM uClock.setTempo(126); + // Inits the clock + uClock.init(); + // Starts the clock, tick-tac-tick-tac... uClock.start(); @@ -227,8 +269,6 @@ void onClockStop() { } void setup() { - // Inits the clock - uClock.init(); // Set the callback function for the clock output to send MIDI Sync message. based on 24PPQN uClock.setOnSync24(onSync24Callback); // Set the callback function for MIDI Start and Stop messages. @@ -236,6 +276,9 @@ void setup() { uClock.setOnClockStopOutput(onClockStop); // Set the clock BPM to 126 BPM uClock.setTempo(126); + + // Inits the clock + uClock.init(); // Starts the clock, tick-tac-tick-tac... uClock.start(); } @@ -395,9 +438,6 @@ void setup() // the default MIDI serial speed communication at 31250 bits per second Serial.begin(31250); - // Inits the clock - uClock.init(); - // Set the callback function for the clock output to send MIDI Sync message. uClock.setOnOutputPPQN(onPPQNCallback); @@ -428,6 +468,9 @@ void setup() // pins, buttons, leds and pots config //configureYourUserInterface(); + // Inits the clock + uClock.init(); + // start sequencer uClock.start(); } diff --git a/examples/AVRUartSlaveMidiClockMonitor/AVRUartSlaveMidiClockMonitor.ino b/examples/AVRUartSlaveMidiClockMonitor/AVRUartSlaveMidiClockMonitor.ino index 10bf4f2..c7c3cae 100644 --- a/examples/AVRUartSlaveMidiClockMonitor/AVRUartSlaveMidiClockMonitor.ino +++ b/examples/AVRUartSlaveMidiClockMonitor/AVRUartSlaveMidiClockMonitor.ino @@ -1,10 +1,10 @@ /* Uart MIDI Sync Slave Box Monitor - * + * * This example demonstrates how to create a - * MIDI slave clock box with + * MIDI slave clock box with * monitor support using oled display - * - * MIDI in must be provided via an opto-isolator to pin RX/D0 + * + * MIDI in must be provided via an opto-isolator to pin RX/D0 * Tested on an Arduino Uno. * * You need the following libraries to make it work @@ -103,20 +103,28 @@ void setup() { //u8x8 = new U8X8_SH1106_128X64_NONAME_HW_I2C(U8X8_PIN_NONE); u8x8 = new U8X8_SSD1306_128X64_NONAME_HW_I2C(U8X8_PIN_NONE); u8x8->begin(); - u8x8->setFont(u8x8_font_pressstart2p_r); + u8x8->setFont(u8x8_font_pressstart2p_r); u8x8->clear(); u8x8->setFlipMode(true); - u8x8->drawUTF8(0, 0, "uClock"); + u8x8->drawUTF8(0, 0, "uClock"); // // uClock Setup // - uClock.init(); uClock.setOnSync24(onSync24Callback); // For MIDI Sync Start and Stop uClock.setOnClockStart(onClockStart); uClock.setOnClockStop(onClockStop); - uClock.setMode(uClock.EXTERNAL_CLOCK); + uClock.setClockMode(uClock.EXTERNAL_CLOCK); + // for smooth slave tempo calculate display you should raise the + // buffer_size of ext_interval_buffer in between 64 to 128. 254 max size. + // note: this doesn't impact on sync time, only display time getTempo() + // if you dont want to use it, it is default set it to 1 for memory save + uClock.setExtIntervalBuffer(128); + + // inits uClock + uClock.init(); + //uClock.setTempo(136.5); //uClock.start(); } @@ -124,7 +132,7 @@ void setup() { void loop() { while(MIDI.read()) {} // DO NOT ADD MORE PROCESS HERE AT THE COST OF LOSING CLOCK SYNC - // Since arduino make use of Serial RX interruption we need to + // Since arduino make use of Serial RX interruption we need to // read Serial as fast as we can on the loop if (bpm != uClock.getTempo()) { bpm = uClock.getTempo(); diff --git a/examples/AcidStepSequencer/AcidStepSequencer.ino b/examples/AcidStepSequencer/AcidStepSequencer.ino index 474a61e..33efcc4 100644 --- a/examples/AcidStepSequencer/AcidStepSequencer.ino +++ b/examples/AcidStepSequencer/AcidStepSequencer.ino @@ -54,7 +54,7 @@ bool _playing = false; uint16_t _step = 0; void sendMidiMessage(uint8_t command, uint8_t byte1, uint8_t byte2) -{ +{ // send midi message command = command | (uint8_t)MIDI_CHANNEL; Serial.write(command); @@ -63,14 +63,14 @@ void sendMidiMessage(uint8_t command, uint8_t byte1, uint8_t byte2) } // Each call represents exactly one step. -void onStepCallback(uint32_t tick) +void onStepCallback(uint32_t tick) { uint16_t step; uint16_t length = NOTE_LENGTH; - + // get actual step. _step = tick % _step_length; - + // send note on only if this step are not in rest mode if ( _sequencer[_step].rest == false ) { @@ -93,15 +93,15 @@ void onStepCallback(uint32_t tick) _note_stack[i].note = _sequencer[_step].note; _note_stack[i].length = length; // send note on - sendMidiMessage(NOTE_ON, _sequencer[_step].note, _sequencer[_step].accent ? ACCENT_VELOCITY : NOTE_VELOCITY); + sendMidiMessage(NOTE_ON, _sequencer[_step].note, _sequencer[_step].accent ? ACCENT_VELOCITY : NOTE_VELOCITY); return; } } - } + } } // The callback function wich will be called by uClock each Pulse of 96PPQN clock resolution. -void onOutputPPQNCallback(uint32_t tick) +void onOutputPPQNCallback(uint32_t tick) { // handle note on stack for ( uint8_t i = 0; i < NOTE_STACK_SIZE; i++ ) { @@ -111,7 +111,7 @@ void onOutputPPQNCallback(uint32_t tick) sendMidiMessage(NOTE_OFF, _note_stack[i].note, 0); _note_stack[i].length = -1; } - } + } } // user feedback about sequence time events @@ -124,14 +124,14 @@ void onSync24Callback(uint32_t tick) { } // The callback function wich will be called when clock starts by using Clock.start() method. -void onClockStart() +void onClockStart() { Serial.write(MIDI_START); _playing = true; } // The callback function wich will be called when clock stops by using Clock.stop() method. -void onClockStop() +void onClockStop() { Serial.write(MIDI_STOP); // send all note off on sequencer stop @@ -142,21 +142,18 @@ void onClockStop() _playing = false; } -void setup() +void setup() { // Initialize serial communication #ifdef MIDI_MODE // the default MIDI serial speed communication at 31250 bits per second - Serial.begin(31250); + Serial.begin(31250); #endif #ifdef SERIAL_MODE // for usage with a PC with a serial to MIDI bridge Serial.begin(115200); #endif - // Inits the clock - uClock.init(); - // Set the callback function for the clock output to send MIDI Sync message. uClock.setOnOutputPPQN(onOutputPPQNCallback); @@ -164,12 +161,15 @@ void setup() uClock.setOnSync24(onSync24Callback); // Set the callback function for the step sequencer on 16ppqn - uClock.setOnStep(onStepCallback); - + uClock.setOnStep(onStepCallback); + // Set the callback function for MIDI Start and Stop messages. - uClock.setOnClockStart(onClockStart); + uClock.setOnClockStart(onClockStart); uClock.setOnClockStop(onClockStop); - + + // Inits the clock + uClock.init(); + // Set the clock BPM to 126 BPM uClock.setTempo(126); @@ -192,7 +192,7 @@ void setup() } // User interaction goes here -void loop() +void loop() { processInterface(); } diff --git a/examples/ESP32UartMasterMidiClock/ESP32UartMasterMidiClock.ino b/examples/ESP32UartMasterMidiClock/ESP32UartMasterMidiClock.ino index 25da851..134919d 100644 --- a/examples/ESP32UartMasterMidiClock/ESP32UartMasterMidiClock.ino +++ b/examples/ESP32UartMasterMidiClock/ESP32UartMasterMidiClock.ino @@ -1,12 +1,12 @@ /* Uart MIDI Sync Box - * - * This example demonstrates how to change the Uart MIDI - * device name on ESP32 family. - * + * + * This example demonstrates how to change the Uart MIDI + * device name on ESP32 family. + * * This example code is in the public domain. - * + * * ... - * + * */ #include @@ -54,15 +54,17 @@ void setup() { // A led to count bpms pinMode(LED_BUILTIN, OUTPUT); - + // Setup our clock system - // Inits the clock - uClock.init(); // Set the callback function for the clock output to send MIDI Sync message. uClock.setOnSync24(onSync24Callback); // Set the callback function for MIDI Start and Stop messages. - uClock.setOnClockStart(onClockStart); + uClock.setOnClockStart(onClockStart); uClock.setOnClockStop(onClockStop); + + // Inits the clock + uClock.init(); + // Set the clock BPM to 126 BPM uClock.setTempo(126); // Starts the clock, tick-tac-tick-tac... @@ -71,5 +73,5 @@ void setup() { // Do it whatever to interface with Clock.stop(), Clock.start(), Clock.setTempo() and integrate your environment... void loop() { - -} \ No newline at end of file + +} diff --git a/examples/GenericMasterOrExternalSync/GenericMasterOrExternalSync.ino b/examples/GenericMasterOrExternalSync/GenericMasterOrExternalSync.ino index 7cef51b..d8ae8f8 100644 --- a/examples/GenericMasterOrExternalSync/GenericMasterOrExternalSync.ino +++ b/examples/GenericMasterOrExternalSync/GenericMasterOrExternalSync.ino @@ -48,12 +48,9 @@ void onClockStopCallback() { } void setup() { - - // inits the clock library - uClock.init(); - - // avaliable output PPQN resolutions for this example - // [ uClock.PPQN_48, uClock.PPQN_96, uClock.PPQN_384, uClock.PPQN_480, uClock.PPQN_960 ] + // setup clock library + // avaliable output resolutions + // [ uClock.PPQN_4, uClock.PPQN_8, uClock.PPQN_12, 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.setOutputPPQN(uClock.PPQN_96); @@ -76,8 +73,16 @@ void setup() { // set external sync mode? if (_external_sync_on) { uClock.setClockMode(uClock.EXTERNAL_CLOCK); + // what is the clock of incomming signal to sync with? + // not mandatory to call, the default is 24PPQN if not set + // avaliable input resolutions - should be always InputPPQN <= OutputPPQN + // [ uClock.PPQN_1, uClock.PPQN_2, uClock.PPQN_4, uClock.PPQN_8, uClock.PPQN_12, uClock.PPQN_24, uClock.PPQN_48, uClock.PPQN_96, uClock.PPQN_384, uClock.PPQN_480, uClock.PPQN_960 ] + uClock.setInputPPQN(uClock.PPQN_24); } + // inits the clock library + uClock.init(); + // starts clock uClock.start(); } @@ -92,4 +97,4 @@ void loop() { uClock.clockMe(); } } -} \ No newline at end of file +} diff --git a/examples/LeonardoUsbSlaveMidiClockMonitor/LeonardoUsbSlaveMidiClockMonitor.ino b/examples/LeonardoUsbSlaveMidiClockMonitor/LeonardoUsbSlaveMidiClockMonitor.ino index 76b5f63..a7ac66e 100644 --- a/examples/LeonardoUsbSlaveMidiClockMonitor/LeonardoUsbSlaveMidiClockMonitor.ino +++ b/examples/LeonardoUsbSlaveMidiClockMonitor/LeonardoUsbSlaveMidiClockMonitor.ino @@ -1,9 +1,9 @@ /* USB MIDI Sync Slave Box Monitor - * + * * This example demonstrates how to create a - * MIDI hid compilant slave clock box with + * MIDI hid compilant slave clock box with * monitor support using oled displays - * + * * You need the following libraries to make it work * - Midi Library * - USB-MIDI and MIDIUSB @@ -94,20 +94,28 @@ void setup() { //u8x8 = new U8X8_SH1106_128X64_NONAME_HW_I2C(U8X8_PIN_NONE); u8x8 = new U8X8_SSD1306_128X64_NONAME_HW_I2C(U8X8_PIN_NONE); u8x8->begin(); - u8x8->setFont(u8x8_font_pressstart2p_r); + u8x8->setFont(u8x8_font_pressstart2p_r); u8x8->clear(); u8x8->setFlipMode(true); - u8x8->drawUTF8(0, 0, "uClock"); + u8x8->drawUTF8(0, 0, "uClock"); // // uClock Setup // - uClock.init(); uClock.setOnSync24(onSync24Callback); // For MIDI Sync Start and Stop uClock.setOnClockStart(onClockStart); uClock.setOnClockStop(onClockStop); - uClock.setMode(uClock.EXTERNAL_CLOCK); + uClock.setClockMode(uClock.EXTERNAL_CLOCK); + // for smooth slave tempo calculate display you should raise the + // buffer_size of ext_interval_buffer in between 64 to 128. 254 max size. + // note: this doesn't impact on sync time, only display time getTempo() + // if you dont want to use it, it is default set it to 1 for memory save + uClock.setExtIntervalBuffer(128); + + // init uClock + uClock.init(); + //uClock.setTempo(136.5); //uClock.start(); } @@ -115,7 +123,7 @@ void setup() { void loop() { while(MIDI.read()) {} // DO NOT ADD MORE PROCESS HERE AT THE COST OF LOSING CLOCK SYNC - // Since arduino make use of Serial RX interruption we need to + // Since arduino make use of Serial RX interruption we need to // read Serial as fast as we can on the loop if (bpm != uClock.getTempo()) { bpm = uClock.getTempo(); @@ -127,20 +135,20 @@ void loop() { u8x8->drawUTF8(8+4, 7, " "); } } - if (clock_state != uClock.state) { + if (clock_state != uClock.state) { clock_state = uClock.state; if (clock_state >= 1) { - u8x8->drawUTF8(0, 7, "Playing"); - } else { - u8x8->drawUTF8(0, 7, "Stopped"); + u8x8->drawUTF8(0, 7, "Playing"); + } else { + u8x8->drawUTF8(0, 7, "Stopped"); } } if (clock_mode != uClock.getMode()) { clock_mode = uClock.getMode(); if (clock_mode == uClock.EXTERNAL_CLOCK) { - u8x8->drawUTF8(10, 0, "Slave "); - } else { - u8x8->drawUTF8(10, 0, "Master"); + u8x8->drawUTF8(10, 0, "Slave "); + } else { + u8x8->drawUTF8(10, 0, "Master"); } } } diff --git a/examples/MidiClock/MidiClock.ino b/examples/MidiClock/MidiClock.ino index 169e504..29b9f5a 100755 --- a/examples/MidiClock/MidiClock.ino +++ b/examples/MidiClock/MidiClock.ino @@ -7,37 +7,39 @@ #define MIDI_STOP 0xFC // The callback function wich will be called by Clock each Pulse of 24PPQN clock resolution. -void onSync24Callback(uint32_t tick) +void onSync24Callback(uint32_t tick) { // Send MIDI_CLOCK to external gears Serial.write(MIDI_CLOCK); } // The callback function wich will be called when clock starts by using Clock.start() method. -void onClockStart() +void onClockStart() { Serial.write(MIDI_START); } // The callback function wich will be called when clock stops by using Clock.stop() method. -void onClockStop() +void onClockStop() { Serial.write(MIDI_STOP); } -void setup() +void setup() { // Initialize serial communication at 31250 bits per second, the default MIDI serial speed communication: Serial.begin(31250); - // Inits the clock - uClock.init(); // Set the callback function for the clock output to send MIDI Sync message. uClock.setOnSync24(onSync24Callback); // Set the callback function for MIDI Start and Stop messages. - uClock.setOnClockStart(onClockStart); + uClock.setOnClockStart(onClockStart); uClock.setOnClockStop(onClockStop); + + // Inits the clock + uClock.init(); + // Set the clock BPM to 126 BPM uClock.setTempo(126); @@ -47,7 +49,7 @@ void setup() } // Do it whatever to interface with Clock.stop(), Clock.start(), Clock.setTempo() and integrate your environment... -void loop() +void loop() { } diff --git a/examples/RP2040UsbUartMasterClock/RP2040UsbUartMasterClock.ino b/examples/RP2040UsbUartMasterClock/RP2040UsbUartMasterClock.ino index e571da9..734d467 100644 --- a/examples/RP2040UsbUartMasterClock/RP2040UsbUartMasterClock.ino +++ b/examples/RP2040UsbUartMasterClock/RP2040UsbUartMasterClock.ino @@ -1,8 +1,8 @@ -/* - * USB/Uart MIDI Sync Box - * +/* + * USB/Uart MIDI Sync Box + * * This example code is in the public domain. - * + * */ #include @@ -68,13 +68,15 @@ void setup() { initBlinkLed(); // Setup our clock system - // Inits the clock - uClock.init(); // Set the callback function for the clock output to send MIDI Sync message. uClock.setOnSync24(onSync24Callback); // Set the callback function for MIDI Start and Stop messages. - uClock.setOnClockStart(onClockStart); + uClock.setOnClockStart(onClockStart); uClock.setOnClockStop(onClockStop); + + // Inits the clock + uClock.init(); + // Set the clock BPM to 126 BPM uClock.setTempo(126); // Starts the clock, tick-tac-tick-tac.. diff --git a/examples/STM32UartMasterMidiClock/STM32UartMasterMidiClock.ino b/examples/STM32UartMasterMidiClock/STM32UartMasterMidiClock.ino index e3421c6..b91410a 100644 --- a/examples/STM32UartMasterMidiClock/STM32UartMasterMidiClock.ino +++ b/examples/STM32UartMasterMidiClock/STM32UartMasterMidiClock.ino @@ -1,13 +1,13 @@ /* Uart MIDI out - * - * This example demonstrates how to send MIDI data via Uart - * interface on STM32 family. - * + * + * This example demonstrates how to send MIDI data via Uart + * interface on STM32 family. + * * This example code is in the public domain. * * Requires STM32Duino board manager to be installed. - * - * Define HardwareSerial using any available UART/USART. + * + * Define HardwareSerial using any available UART/USART. * Nucleo boards have UART/USART pins that are used by the ST-LINK interface (unless using solder bridging). * * Tested on Nucleo-F401RE and Nucleo-F072RB (PA9=D8 PA10=D2 on the Arduino pins) @@ -62,15 +62,17 @@ void setup() { // An led to display BPM pinMode(LED_BUILTIN, OUTPUT); - + // Setup our clock system - // Inits the clock - uClock.init(); // Set the callback function for the clock output to send MIDI Sync message. uClock.setOnSync24(onSync24Callback); // Set the callback function for MIDI Start and Stop messages. - uClock.setOnClockStart(onClockStart); + uClock.setOnClockStart(onClockStart); uClock.setOnClockStop(onClockStop); + + // Inits the clock + uClock.init(); + // Set the clock BPM to 126 BPM uClock.setTempo(120); // Starts the clock, tick-tac-tick-tac... @@ -79,5 +81,5 @@ void setup() { // Do it whatever to interface with Clock.stop(), Clock.start(), Clock.setTempo() and integrate your environment... void loop() { - + } diff --git a/examples/TeensyUsbMasterMidiClock/TeensyUsbMasterMidiClock.ino b/examples/TeensyUsbMasterMidiClock/TeensyUsbMasterMidiClock.ino index 2c814b4..d9402d8 100644 --- a/examples/TeensyUsbMasterMidiClock/TeensyUsbMasterMidiClock.ino +++ b/examples/TeensyUsbMasterMidiClock/TeensyUsbMasterMidiClock.ino @@ -1,6 +1,6 @@ /* USB MIDI Sync Box - * - * This example demonstrates how to change the USB MIDI + * + * This example demonstrates how to change the USB MIDI * device name on Teensy LC, 3.x and 4.x. When creating more * that one MIDI device, custom names are much easier to * use when selecting each device in MIDI software on @@ -12,9 +12,9 @@ * steps to get your operating system to "forget" the * cached info. (TODO: wanted... can anyone contribute * instructions for these systems) - * + * * You must select MIDI from the "Tools > USB Type" menu - * + * * This example code is in the public domain. */ @@ -53,15 +53,17 @@ void onClockStop() { void setup() { // A led to count bpms pinMode(LED_BUILTIN, OUTPUT); - + // Setup our clock system - // Inits the clock - uClock.init(); // Set the callback function for the clock output to send MIDI Sync message. uClock.setOnSync24(onSync24Callback); // Set the callback function for MIDI Start and Stop messages. - uClock.setOnClockStart(onClockStart); + uClock.setOnClockStart(onClockStart); uClock.setOnClockStop(onClockStop); + + // Inits the clock + uClock.init(); + // Set the clock BPM to 126 BPM uClock.setTempo(126); // Starts the clock, tick-tac-tick-tac... diff --git a/examples/TeensyUsbSlaveMidiClock/TeensyUsbSlaveMidiClock.ino b/examples/TeensyUsbSlaveMidiClock/TeensyUsbSlaveMidiClock.ino index 98c1ee3..a56ed6b 100644 --- a/examples/TeensyUsbSlaveMidiClock/TeensyUsbSlaveMidiClock.ino +++ b/examples/TeensyUsbSlaveMidiClock/TeensyUsbSlaveMidiClock.ino @@ -1,6 +1,6 @@ /* USB MIDI Sync Slave Box - * - * This example demonstrates how to change the USB MIDI + * + * This example demonstrates how to change the USB MIDI * device name on Teensy LC, 3.x and 4.x. When creating more * that one MIDI device, custom names are much easier to * use when selecting each device in MIDI software on @@ -12,9 +12,9 @@ * steps to get your operating system to "forget" the * cached info. (TODO: wanted... can anyone contribute * instructions for these systems) - * + * * You must select MIDI from the "Tools > USB Type" menu - * + * * This example code is in the public domain. */ @@ -69,22 +69,24 @@ void onExternalStop() void setup() { // A led to count bpms pinMode(LED_BUILTIN, OUTPUT); - + // Setup realtime midi event handlers usbMIDI.setHandleClock(onExternalClock); usbMIDI.setHandleStart(onExternalStart); usbMIDI.setHandleStop(onExternalStop); // Setup our clock system - // Inits the clock - uClock.init(); // Set the callback function for the clock output to send MIDI Sync message. uClock.setOnSync24(onSync24Callback); // Set the callback function for MIDI Start and Stop messages. - uClock.setOnClockStart(onClockStart); + uClock.setOnClockStart(onClockStart); uClock.setOnClockStop(onClockStop); + // set to external sync mode - uClock.setMode(1); + uClock.setClockMode(uClock.EXTERNAL_CLOCK); + + // Inits the clock + uClock.init(); } void loop() { diff --git a/examples/TeensyUsbSlaveMidiClockMonitor/TeensyUsbSlaveMidiClockMonitor.ino b/examples/TeensyUsbSlaveMidiClockMonitor/TeensyUsbSlaveMidiClockMonitor.ino index 57b2989..e6dc0c0 100644 --- a/examples/TeensyUsbSlaveMidiClockMonitor/TeensyUsbSlaveMidiClockMonitor.ino +++ b/examples/TeensyUsbSlaveMidiClockMonitor/TeensyUsbSlaveMidiClockMonitor.ino @@ -1,20 +1,20 @@ /* USB MIDI Sync Slave Box Monitor - * + * * This example demonstrates how to create a * MIDI hid compilant slave clock box using - * Teensy LC, 3.x and 4.x with + * Teensy LC, 3.x and 4.x with * monitor support using oled displays * * Making use of a 250 usceconds timer to * handle MIDI input to avoid jitter on clock - * + * * You need the following libraries to make it work * - u8g2 * - uClock * * This example code is in the public domain. */ - + #include // @@ -100,23 +100,31 @@ void setup() { //u8x8 = new U8X8_SH1106_128X64_NONAME_HW_I2C(U8X8_PIN_NONE); u8x8 = new U8X8_SSD1306_128X64_NONAME_HW_I2C(U8X8_PIN_NONE); u8x8->begin(); - u8x8->setFont(u8x8_font_pressstart2p_r); + u8x8->setFont(u8x8_font_pressstart2p_r); u8x8->clear(); u8x8->setFlipMode(true); - u8x8->drawUTF8(0, 0, "uClock"); + u8x8->drawUTF8(0, 0, "uClock"); // // uClock Setup // // Setup our clock system - uClock.init(); uClock.setOnSync24(onSync24Callback); // For MIDI Sync Start and Stop uClock.setOnClockStart(onClockStart); uClock.setOnClockStop(onClockStop); - uClock.setMode(uClock.EXTERNAL_CLOCK); + uClock.setClockMode(uClock.EXTERNAL_CLOCK); + // for smooth slave tempo calculate display you should raise the + // buffer_size of ext_interval_buffer in between 64 to 128. 254 max size. + // note: this doesn't impact on sync time, only display time getTempo() + // if you dont want to use it, it is default set it to 1 for memory save + uClock.setExtIntervalBuffer(128); + + // inits uClock + uClock.init(); + // make use of 250us timer to handle midi input sync - teensyTimer.begin(handleMidiInput, 250); + teensyTimer.begin(handleMidiInput, 250); teensyTimer.priority(80); } @@ -136,20 +144,20 @@ void loop() { u8x8->drawUTF8(8+4, 7, " "); } } - if (clock_state != uClock.state) { + if (clock_state != uClock.state) { clock_state = uClock.state; if (clock_state >= 1) { - u8x8->drawUTF8(0, 7, "Playing"); - } else { - u8x8->drawUTF8(0, 7, "Stopped"); + u8x8->drawUTF8(0, 7, "Playing"); + } else { + u8x8->drawUTF8(0, 7, "Stopped"); } } if (clock_mode != uClock.getMode()) { clock_mode = uClock.getMode(); if (clock_mode == uClock.EXTERNAL_CLOCK) { - u8x8->drawUTF8(10, 0, "Slave "); - } else { - u8x8->drawUTF8(10, 0, "Master"); + u8x8->drawUTF8(10, 0, "Slave "); + } else { + u8x8->drawUTF8(10, 0, "Master"); } } } diff --git a/examples/XiaoUsbMasterMidiClock/XiaoUsbMasterMidiClock.ino b/examples/XiaoUsbMasterMidiClock/XiaoUsbMasterMidiClock.ino index c052f79..33a7827 100644 --- a/examples/XiaoUsbMasterMidiClock/XiaoUsbMasterMidiClock.ino +++ b/examples/XiaoUsbMasterMidiClock/XiaoUsbMasterMidiClock.ino @@ -1,12 +1,12 @@ /* USB MIDI Sync Box - * - * This example demonstrates how to change the USB MIDI - * device name on Seeedstudio XIAO M0. - * + * + * This example demonstrates how to change the USB MIDI + * device name on Seeedstudio XIAO M0. + * * This example code is in the public domain. - * + * * Tested with Adafruit TinyUSB version 0.10.5 - * + * */ #include #include @@ -52,15 +52,17 @@ void setup() { // A led to count bpms pinMode(LED_BUILTIN, OUTPUT); - + // Setup our clock system - // Inits the clock - uClock.init(); // Set the callback function for the clock output to send MIDI Sync message. uClock.setOnSync24(onSync24Callback); // Set the callback function for MIDI Start and Stop messages. - uClock.setOnClockStart(onClockStart); + uClock.setOnClockStart(onClockStart); uClock.setOnClockStop(onClockStop); + + // Inits the clock + uClock.init(); + // Set the clock BPM to 126 BPM uClock.setTempo(126); // Starts the clock, tick-tac-tick-tac...