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.
90 lines
2.3 KiB
90 lines
2.3 KiB
7 months ago
|
/*
|
||
|
* USB/Uart MIDI Sync Box
|
||
10 months ago
|
*
|
||
|
* This example code is in the public domain.
|
||
|
*
|
||
|
*/
|
||
7 months ago
|
|
||
10 months ago
|
#include <Adafruit_TinyUSB.h>
|
||
|
#include <MIDI.h>
|
||
|
|
||
|
#include <uClock.h>
|
||
|
|
||
7 months ago
|
// Instantiate the MIDI interfaces
|
||
|
Adafruit_USBD_MIDI usb_midi;
|
||
|
MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, MIDI_USB);
|
||
|
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);
|
||
10 months ago
|
|
||
7 months ago
|
// Do your rpi 2040 has a ws2812 RGB LED? set the pin!
|
||
|
// otherwise keep it commented for normal LED_BUILTIN led blinking
|
||
|
#define WS2812_BUILTIN_LED 16
|
||
10 months ago
|
|
||
|
uint8_t bpm_blink_timer = 1;
|
||
|
void handle_bpm_led(uint32_t tick)
|
||
|
{
|
||
|
// BPM led indicator
|
||
7 months ago
|
if ( !(tick % (96)) || (tick == 1) ) { // first of 4 quarter pulse will flash longer
|
||
10 months ago
|
bpm_blink_timer = 8;
|
||
7 months ago
|
ledOn();
|
||
10 months ago
|
} else if ( !(tick % (24)) ) { // each quarter led on
|
||
|
bpm_blink_timer = 1;
|
||
7 months ago
|
ledOn();
|
||
10 months ago
|
} else if ( !(tick % bpm_blink_timer) ) { // get led off
|
||
7 months ago
|
ledOff();
|
||
10 months ago
|
}
|
||
|
}
|
||
|
|
||
|
// Internal clock handlers
|
||
|
void onSync24Callback(uint32_t tick) {
|
||
|
// Send MIDI_CLOCK to external gears
|
||
7 months ago
|
MIDI.sendRealTime(midi::Clock);
|
||
10 months ago
|
MIDI_USB.sendRealTime(midi::Clock);
|
||
7 months ago
|
// blink tempo
|
||
10 months ago
|
handle_bpm_led(tick);
|
||
|
}
|
||
|
|
||
|
void onClockStart() {
|
||
7 months ago
|
MIDI.sendRealTime(midi::Start);
|
||
10 months ago
|
MIDI_USB.sendRealTime(midi::Start);
|
||
|
}
|
||
|
|
||
|
void onClockStop() {
|
||
7 months ago
|
MIDI.sendRealTime(midi::Stop);
|
||
10 months ago
|
MIDI_USB.sendRealTime(midi::Stop);
|
||
|
}
|
||
|
|
||
|
void setup() {
|
||
|
#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
|
||
|
// Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040
|
||
|
TinyUSB_Device_Init(0);
|
||
|
#endif
|
||
|
|
||
7 months ago
|
// Initialize USB midi stack
|
||
10 months ago
|
MIDI_USB.begin(MIDI_CHANNEL_OMNI);
|
||
7 months ago
|
// Initialize UART midi stack
|
||
|
MIDI.begin(MIDI_CHANNEL_OMNI);
|
||
10 months ago
|
|
||
7 months ago
|
// Initialize builtin led for clock timer blinking
|
||
|
initBlinkLed();
|
||
10 months ago
|
|
||
|
// 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.setOnClockStop(onClockStop);
|
||
|
// Set the clock BPM to 126 BPM
|
||
7 months ago
|
uClock.setTempo(126);
|
||
10 months ago
|
// 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() {
|
||
7 months ago
|
// handle midi input?
|
||
|
MIDI.read();
|
||
10 months ago
|
MIDI_USB.read();
|
||
|
}
|