mirror of https://github.com/midilab/uClock
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.1 KiB
84 lines
2.1 KiB
/* Uart MIDI Sync Box
|
|
*
|
|
* This example demonstrates how to change the Uart MIDI
|
|
* device name on ESP32 family.
|
|
*
|
|
* This example code is in the public domain.
|
|
*
|
|
* ...
|
|
*
|
|
*/
|
|
#include <uClock.h>
|
|
|
|
// MIDI clock, start and stop byte definitions - based on MIDI 1.0 Standards.
|
|
#define MIDI_CLOCK 0xF8
|
|
#define MIDI_START 0xFA
|
|
#define MIDI_STOP 0xFC
|
|
|
|
// the blue led
|
|
#define LED_BUILTIN 2
|
|
|
|
volatile bool _midi_clk_income = false;
|
|
|
|
uint8_t bpm_blink_timer = 1;
|
|
void handle_bpm_led(uint32_t tick)
|
|
{
|
|
// BPM led indicator
|
|
if ( !(tick % (96)) || (tick == 1) ) { // first compass step will flash longer
|
|
bpm_blink_timer = 8;
|
|
digitalWrite(LED_BUILTIN, HIGH);
|
|
} else if ( !(tick % (24)) ) { // each quarter led on
|
|
bpm_blink_timer = 1;
|
|
digitalWrite(LED_BUILTIN, HIGH);
|
|
} else if ( !(tick % bpm_blink_timer) ) { // get led off
|
|
digitalWrite(LED_BUILTIN, LOW);
|
|
}
|
|
}
|
|
|
|
// Internal clock handlers
|
|
void ClockOut96PPQN(uint32_t tick) {
|
|
// Send MIDI_CLOCK to external gears
|
|
//Serial.write(MIDI_CLOCK);
|
|
_midi_clk_income = true;
|
|
handle_bpm_led(tick);
|
|
}
|
|
|
|
void onClockStart() {
|
|
Serial.write(MIDI_START);
|
|
}
|
|
|
|
void onClockStop() {
|
|
Serial.write(MIDI_STOP);
|
|
}
|
|
|
|
void setup() {
|
|
// Initialize serial communication at 31250 bits per second, the default MIDI serial speed communication:
|
|
Serial.begin(31250);
|
|
|
|
// 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.setClock96PPQNOutput(ClockOut96PPQN);
|
|
// Set the callback function for MIDI Start and Stop messages.
|
|
uClock.setOnClockStartOutput(onClockStart);
|
|
uClock.setOnClockStopOutput(onClockStop);
|
|
// Set the clock BPM to 126 BPM
|
|
uClock.setTempo(126);
|
|
// Starts the clock, tick-tac-tick-tac...
|
|
uClock.start();
|
|
}
|
|
|
|
// Do it whatever to interface with Clock.stop(), Clock.start(), Clock.setTempo() and integrate your environment...
|
|
void loop() {
|
|
// watch for income signal from uClock to fire the clock over midi
|
|
if (_midi_clk_income) {
|
|
Serial.write(MIDI_CLOCK);
|
|
noInterrupts();
|
|
_midi_clk_income = false;
|
|
interrupts();
|
|
}
|
|
}
|
|
|