mbeduino MIDI Shield test I ported SparkFun MIDI Shield test code into meduino. Regarding mbeduino, refer to http://mbed.org/users/okini3939/notebook/mbeduino/

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //
00002 // mbeduino MIDI Shield test
00003 //
00004 // original: SparkFun MIDI Sheild test code
00005 //
00006 // 2010/10/11 ported by: xshige
00007 // I keep the original(Arduino) programming style as much as possible
00008 
00009 #include "mbed.h"
00010 #include "ArduinoShield.h"
00011 
00012 // ---- --Arduino program start ------------
00013 
00014 // SparkFun MIDI Sheild and MIDI Breakout test code
00015 // Defines bare-bones routines for sending and receiving MIDI data
00016 // Written 02/16/10
00017 
00018 
00019 // defines for MIDI Shield components only
00020 #if 1
00021 #define KNOB1  ARD_A0
00022 #define KNOB2  ARD_A1
00023 
00024 #define BUTTON1  ARD_D2
00025 #define BUTTON2  ARD_D3
00026 #define BUTTON3  ARD_D4
00027 
00028 #define STAT1  ARD_D7
00029 #define STAT2  ARD_D6
00030 #endif
00031 #if 0
00032 #define KNOB1  0
00033 #define KNOB2  1
00034 
00035 #define BUTTON1  2
00036 #define BUTTON2  3
00037 #define BUTTON3  4
00038 
00039 #define STAT1  7
00040 #define STAT2  6
00041 #endif
00042 
00043 #define OFF 1
00044 #define ON 2
00045 #define WAIT 3
00046 
00047 byte incomingByte;
00048 byte note;
00049 byte velocity;
00050 int pot;
00051 
00052 byte byte1;
00053 byte byte2;
00054 byte byte3;
00055 
00056 int action=2; //1 =note off ; 2=note on ; 3= wait
00057 
00058 #if 1
00059 // prototype
00060 char button(int button_num);
00061 // button_num should be int not char!.
00062 void Midi_Send(byte cmd, byte data1, byte data2);
00063 
00064 // port allocation
00065 DigitalOut stat1(STAT1);
00066 DigitalOut stat2(STAT2);
00067     
00068 AnalogIn knob1(KNOB1);
00069 AnalogIn knob2(KNOB2);
00070 
00071 DigitalInOut button1(BUTTON1);
00072 DigitalInOut button2(BUTTON2);
00073 DigitalInOut button3(BUTTON3);
00074 
00075 Serial midi(ARD_TX, ARD_RX);
00076 #endif
00077 
00078 void setup() {
00079 #if 1
00080   button1.mode(PullUp);
00081   button2.mode(PullUp);
00082   button3.mode(PullUp);
00083 #endif
00084 #if 0
00085   pinMode(STAT1,OUTPUT);   
00086   pinMode(STAT2,OUTPUT);
00087 
00088   pinMode(BUTTON1,INPUT);
00089   pinMode(BUTTON2,INPUT);
00090   pinMode(BUTTON3,INPUT);
00091 
00092   digitalWrite(BUTTON1,HIGH);
00093   digitalWrite(BUTTON2,HIGH);
00094   digitalWrite(BUTTON3,HIGH);
00095 #endif
00096 
00097   for(int i = 0;i < 10;i++) // flash MIDI Sheild LED's on startup
00098   {
00099 #if 1
00100     stat1=HIGH;  
00101     stat2=LOW;
00102 #endif
00103 #if 0
00104     digitalWrite(STAT1,HIGH);  
00105     digitalWrite(STAT2,LOW);
00106 #endif
00107     delay(30);
00108 #if 1
00109    stat1=LOW;  
00110    stat2=HIGH;
00111 #endif
00112 #if 0
00113     digitalWrite(STAT1,LOW);  
00114     digitalWrite(STAT2,HIGH);
00115 #endif
00116     delay(30);
00117   }
00118 #if 1
00119   stat1=HIGH;   
00120   stat2=HIGH;
00121 #endif  
00122 #if 0
00123   digitalWrite(STAT1,HIGH);   
00124   digitalWrite(STAT2,HIGH);
00125 #endif
00126 
00127   //start serial with midi baudrate 31250
00128 #if 1
00129   midi.baud(31250);
00130 #endif
00131 #if 0
00132   Serial.begin(31250);     
00133 #endif
00134 }
00135 
00136 void loop () {
00137 
00138   //*************** MIDI OUT ***************//
00139 #if 1
00140   pot = knob1.read_u16();
00141 //printf("pot:%d ",pot);// debug
00142 #endif
00143 #if 0
00144   pot = analogRead(0);
00145 #endif
00146 #if 1
00147   note = pot/511;  // convert value to value 0-127
00148 #endif
00149 #if 0
00150   note = pot/8;  // convert value to value 0-127
00151 #endif
00152   if(button(BUTTON1) || button(BUTTON2) || button(BUTTON3))
00153   {  
00154     Midi_Send(0x90,note,0x45);
00155     while(button(BUTTON1) || button(BUTTON2) || button(BUTTON3));
00156   }
00157 
00158   //*************** MIDI LOOPBACK ******************//
00159 #if 1
00160   if(midi.readable() > 0)
00161 #endif
00162 #if 0
00163   if(Serial.available() > 0)
00164 #endif
00165   {
00166 #if 1
00167     byte1 = midi.getc();
00168     byte2 = midi.getc();
00169     byte3 = midi.getc();
00170 #endif
00171 #if 0
00172     byte1 = Serial.read();
00173     byte2 = Serial.read();
00174     byte3 = Serial.read();
00175 #endif
00176     Midi_Send(byte1, byte2, byte3);
00177   }
00178 
00179   //*************** MIDI IN ***************//
00180 #if 1
00181   if (midi.readable() > 0) {
00182     // read the incoming byte:
00183     incomingByte = midi.getc();
00184 printf("Incoming MIDI data: %02X\r\n",incomingByte);//debug
00185 #endif
00186 #if 0
00187   if (Serial.available() > 0) {
00188     // read the incoming byte:
00189     incomingByte = Serial.read();
00190 #endif
00191     // wait for as status-byte, channel 1, note on or off
00192     if (incomingByte== 144) // Note on
00193     { 
00194       action = OFF;
00195     }
00196     else if (incomingByte== 128) // Note off
00197     { 
00198       action = ON;
00199     }
00200     else if (note==0 && action != WAIT) // note on, wait for note value
00201     { 
00202       note=incomingByte;
00203     }
00204     else if (note!=0 && action != WAIT)  // velocity
00205     { 
00206       velocity=incomingByte;
00207       if(action == ON){ 
00208         Midi_Send(0x90,note,velocity); 
00209       }
00210       if(action == OFF){ 
00211         Midi_Send(0x80,note,velocity); 
00212       }
00213       note=0;
00214       velocity=0;
00215       action=WAIT;
00216     }
00217     else{
00218     }
00219   }
00220 
00221 }
00222 
00223 void Midi_Send(byte cmd, byte data1, byte data2) {
00224 #if 1
00225 printf("Outgoing MIDI data: %02X:%02X:%02X\r\n",cmd,data1,data2); // debug
00226   midi.putc(cmd);
00227   midi.putc(data1);
00228   midi.putc(data2);
00229 #endif
00230 #if 0
00231   Serial.print(cmd, BYTE);
00232   Serial.print(data1, BYTE);
00233   Serial.print(data2, BYTE);
00234 #endif
00235 }
00236 
00237 void blink(){
00238 #if 1
00239   stat1=HIGH;
00240 #endif
00241 #if 0
00242   digitalWrite(STAT1, HIGH);
00243 #endif
00244   delay(100);
00245 #if 1
00246   stat1=LOW;
00247 #endif
00248 #if 0
00249   digitalWrite(STAT1, LOW);
00250 #endif
00251   delay(100);
00252 }
00253 #if 1
00254 char button(int button_num)
00255 #endif
00256 #if 0
00257 char button(char button_num)
00258 #endif
00259 {
00260 #if 1
00261     if (BUTTON1==button_num) return (!button1.read());
00262     if (BUTTON2==button_num) return (!button2.read());
00263     if (BUTTON3==button_num) return (!button3.read());
00264     return 0; // never happen
00265 #endif
00266 #if 0
00267   return (!(digitalRead(button_num)));
00268 #endif
00269 }
00270 
00271 
00272 // -------- arduino hidden main ---------
00273 
00274 void init(void) {
00275  // make debug port Fast
00276    Serial pc(USBTX, USBRX);
00277 //    pc.baud(9600);
00278     pc.baud(115200);
00279 //  pc.baud(230400);
00280     printf("mbeduino MIDI Shield Test\r\n");
00281     printf("Rotate Knob#1 to change note.\r\n"
00282             "Push any button to send MIDI note data.\r\n");
00283 }
00284 
00285 int main(void)
00286 {
00287     init();
00288  
00289     setup();
00290  
00291     for (;;)
00292         loop();
00293  
00294     return 0;
00295 }