2016-03-21 15:35:40 +01:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
* USB-MIDI to Legacy Serial MIDI converter
|
|
|
|
* Copyright (C) 2012-2016 Yuuichi Akagawa
|
|
|
|
*
|
|
|
|
* Idea from LPK25 USB-MIDI to Serial MIDI converter
|
|
|
|
* by Collin Cunningham - makezine.com, narbotic.com
|
|
|
|
*
|
|
|
|
* This is sample program. Do not expect perfect behavior.
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <usbh_midi.h>
|
|
|
|
#include <usbhub.h>
|
|
|
|
|
|
|
|
// Satisfy the IDE, which needs to see the include statment in the ino too.
|
|
|
|
#ifdef dobogusinclude
|
|
|
|
#include <spi4teensy3.h>
|
|
|
|
#include <SPI.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USBCON
|
|
|
|
#define _MIDI_SERIAL_PORT Serial1
|
|
|
|
#else
|
|
|
|
#define _MIDI_SERIAL_PORT Serial
|
|
|
|
#endif
|
|
|
|
//////////////////////////
|
|
|
|
// MIDI Pin assign
|
|
|
|
// 2 : GND
|
|
|
|
// 4 : +5V(Vcc) with 220ohm
|
|
|
|
// 5 : TX
|
|
|
|
//////////////////////////
|
|
|
|
|
|
|
|
USB Usb;
|
|
|
|
USBH_MIDI Midi(&Usb);
|
|
|
|
|
|
|
|
void MIDI_poll();
|
|
|
|
void doDelay(unsigned long t1, unsigned long t2, unsigned long delayTime);
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
_MIDI_SERIAL_PORT.begin(31250);
|
|
|
|
|
|
|
|
if (Usb.Init() == -1) {
|
2016-04-18 18:04:08 +02:00
|
|
|
while (1); //halt
|
2016-03-21 15:35:40 +01:00
|
|
|
}//if (Usb.Init() == -1...
|
|
|
|
delay( 200 );
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
unsigned long t1;
|
|
|
|
|
|
|
|
Usb.Task();
|
|
|
|
t1 = micros();
|
2016-04-18 18:04:08 +02:00
|
|
|
if ( Usb.getUsbTaskState() == USB_STATE_RUNNING )
|
2016-03-21 15:35:40 +01:00
|
|
|
{
|
|
|
|
MIDI_poll();
|
|
|
|
}
|
|
|
|
//delay(1ms)
|
|
|
|
doDelay(t1, micros(), 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Poll USB MIDI Controler and send to serial MIDI
|
|
|
|
void MIDI_poll()
|
|
|
|
{
|
2016-04-26 16:44:07 +02:00
|
|
|
uint8_t outBuf[ 3 ];
|
2016-04-18 18:04:08 +02:00
|
|
|
uint8_t size;
|
2016-03-21 15:35:40 +01:00
|
|
|
|
2016-04-18 18:04:08 +02:00
|
|
|
do {
|
|
|
|
if ( (size = Midi.RecvData(outBuf)) > 0 ) {
|
|
|
|
//MIDI Output
|
|
|
|
_MIDI_SERIAL_PORT.write(outBuf, size);
|
|
|
|
}
|
|
|
|
} while (size > 0);
|
2016-03-21 15:35:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delay time (max 16383 us)
|
|
|
|
void doDelay(unsigned long t1, unsigned long t2, unsigned long delayTime)
|
|
|
|
{
|
2016-04-18 18:04:08 +02:00
|
|
|
unsigned long t3;
|
2016-03-21 15:35:40 +01:00
|
|
|
|
2016-04-18 18:04:08 +02:00
|
|
|
if ( t1 > t2 ) {
|
2016-04-24 02:02:33 +02:00
|
|
|
t3 = (0xFFFFFFFF - t1 + t2);
|
2016-04-18 18:04:08 +02:00
|
|
|
} else {
|
|
|
|
t3 = t2 - t1;
|
|
|
|
}
|
2016-03-21 15:35:40 +01:00
|
|
|
|
2016-04-18 18:04:08 +02:00
|
|
|
if ( t3 < delayTime ) {
|
|
|
|
delayMicroseconds(delayTime - t3);
|
|
|
|
}
|
2016-03-21 15:35:40 +01:00
|
|
|
}
|