// Teensy UART MIDI Input
// MIDI input variables
byte channel;
byte pitch;
byte velocity;
byte ccnumber;
byte ccvalue;
byte ccvalue2;
byte bendLSB;
byte bendMSB;
byte rstat;
byte dataIn;
int flag_previous = 0;
// Initialise hardware
HardwareSerial Uart = HardwareSerial();
void setup() {
Uart.begin(31250);
}
void loop() {
if(Uart.available() > 0) {
dataIn = Uart.read();
if(dataIn < 0x80 && flag_previous == 0) {
doMidiIn(rstat);
}
doMidiIn(dataIn);
}
}
void doNote(byte channel, byte pitch, byte velocity) {
}
void doNoteOff(byte channel, byte pitch, byte velocity) {
}
void doCC(byte channel, byte ccnumber, byte ccvalue) {
}
void doBend(byte channel, unsigned int bend_usb) {
}
void doMidiIn(byte data) {
// running status set
if((data >= 0x80) && (data < 0xf0) && (flag_previous == 0)) {
rstat = data;
}
// deal with note on
if((data >= 0x90) && (data < 0xa0) && (flag_previous == 0)) {
channel = data & B00001111;
flag_previous = 1;
}
else if((data < 0x80) && (flag_previous == 1)) {
pitch = data;
flag_previous = 2;
}
else if((data < 0x80) && (flag_previous == 2)) {
velocity = data;
doNote(channel, pitch, velocity);
flag_previous = 0;
}
// done with note on
// deal with note off (as discrete status byte)
else if((data >= 0x80) && (data < 0x90) && (flag_previous == 0)) {
channel = data & B00001111;
flag_previous = -1;
}
else if((data < 0x80) && (flag_previous == -1)) {
pitch = data;
flag_previous = -2;
}
else if((data < 0x80) && (flag_previous == -2)) {
velocity = data;
doNoteOff(channel, pitch, velocity);
flag_previous = 0;
}
// done with note off (as discrete status byte)
// deal with cc data
else if((data >= 0xb0) && (data < 0xc0) && (flag_previous == 0)) {
channel = data & B00001111;
flag_previous = 3;
}
else if((data < 0x80) && (flag_previous == 3)) {
ccnumber = data;
flag_previous = 4;
}
else if((data < 0x80) && (flag_previous == 4)) {
ccvalue = data;
doCC(channel, ccnumber, ccvalue);
flag_previous = 0;
}
// done with cc data
// deal with bend data
else if((data >= 0xe0) && (data < 0xf0) && (flag_previous == 0)) {
channel = data & B00001111;
flag_previous = 5;
}
else if((data < 0x80) && (flag_previous == 5)) {
bendLSB = data;
flag_previous = 6;
}
else if((data < 0x80) && (flag_previous == 6)) {
bendMSB = data;
doBend(channel, bendLSB + (bendMSB << 7));
flag_previous = 0;
}
// done with bend data
}
Saturday, May 11, 2013
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment