mbeduino MIDI Shield test I ported SparkFun MIDI Shield test code into meduino. Regarding mbeduino, refer to http://mbed.org/users/okini3939/notebook/mbeduino/
main.cpp@0:665c7d36f533, 2010-10-11 (annotated)
- Committer:
- xshige
- Date:
- Mon Oct 11 10:53:42 2010 +0000
- Revision:
- 0:665c7d36f533
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xshige | 0:665c7d36f533 | 1 | // |
xshige | 0:665c7d36f533 | 2 | // mbeduino MIDI Shield test |
xshige | 0:665c7d36f533 | 3 | // |
xshige | 0:665c7d36f533 | 4 | // original: SparkFun MIDI Sheild test code |
xshige | 0:665c7d36f533 | 5 | // |
xshige | 0:665c7d36f533 | 6 | // 2010/10/11 ported by: xshige |
xshige | 0:665c7d36f533 | 7 | // I keep the original(Arduino) programming style as much as possible |
xshige | 0:665c7d36f533 | 8 | |
xshige | 0:665c7d36f533 | 9 | #include "mbed.h" |
xshige | 0:665c7d36f533 | 10 | #include "ArduinoShield.h" |
xshige | 0:665c7d36f533 | 11 | |
xshige | 0:665c7d36f533 | 12 | // ---- --Arduino program start ------------ |
xshige | 0:665c7d36f533 | 13 | |
xshige | 0:665c7d36f533 | 14 | // SparkFun MIDI Sheild and MIDI Breakout test code |
xshige | 0:665c7d36f533 | 15 | // Defines bare-bones routines for sending and receiving MIDI data |
xshige | 0:665c7d36f533 | 16 | // Written 02/16/10 |
xshige | 0:665c7d36f533 | 17 | |
xshige | 0:665c7d36f533 | 18 | |
xshige | 0:665c7d36f533 | 19 | // defines for MIDI Shield components only |
xshige | 0:665c7d36f533 | 20 | #if 1 |
xshige | 0:665c7d36f533 | 21 | #define KNOB1 ARD_A0 |
xshige | 0:665c7d36f533 | 22 | #define KNOB2 ARD_A1 |
xshige | 0:665c7d36f533 | 23 | |
xshige | 0:665c7d36f533 | 24 | #define BUTTON1 ARD_D2 |
xshige | 0:665c7d36f533 | 25 | #define BUTTON2 ARD_D3 |
xshige | 0:665c7d36f533 | 26 | #define BUTTON3 ARD_D4 |
xshige | 0:665c7d36f533 | 27 | |
xshige | 0:665c7d36f533 | 28 | #define STAT1 ARD_D7 |
xshige | 0:665c7d36f533 | 29 | #define STAT2 ARD_D6 |
xshige | 0:665c7d36f533 | 30 | #endif |
xshige | 0:665c7d36f533 | 31 | #if 0 |
xshige | 0:665c7d36f533 | 32 | #define KNOB1 0 |
xshige | 0:665c7d36f533 | 33 | #define KNOB2 1 |
xshige | 0:665c7d36f533 | 34 | |
xshige | 0:665c7d36f533 | 35 | #define BUTTON1 2 |
xshige | 0:665c7d36f533 | 36 | #define BUTTON2 3 |
xshige | 0:665c7d36f533 | 37 | #define BUTTON3 4 |
xshige | 0:665c7d36f533 | 38 | |
xshige | 0:665c7d36f533 | 39 | #define STAT1 7 |
xshige | 0:665c7d36f533 | 40 | #define STAT2 6 |
xshige | 0:665c7d36f533 | 41 | #endif |
xshige | 0:665c7d36f533 | 42 | |
xshige | 0:665c7d36f533 | 43 | #define OFF 1 |
xshige | 0:665c7d36f533 | 44 | #define ON 2 |
xshige | 0:665c7d36f533 | 45 | #define WAIT 3 |
xshige | 0:665c7d36f533 | 46 | |
xshige | 0:665c7d36f533 | 47 | byte incomingByte; |
xshige | 0:665c7d36f533 | 48 | byte note; |
xshige | 0:665c7d36f533 | 49 | byte velocity; |
xshige | 0:665c7d36f533 | 50 | int pot; |
xshige | 0:665c7d36f533 | 51 | |
xshige | 0:665c7d36f533 | 52 | byte byte1; |
xshige | 0:665c7d36f533 | 53 | byte byte2; |
xshige | 0:665c7d36f533 | 54 | byte byte3; |
xshige | 0:665c7d36f533 | 55 | |
xshige | 0:665c7d36f533 | 56 | int action=2; //1 =note off ; 2=note on ; 3= wait |
xshige | 0:665c7d36f533 | 57 | |
xshige | 0:665c7d36f533 | 58 | #if 1 |
xshige | 0:665c7d36f533 | 59 | // prototype |
xshige | 0:665c7d36f533 | 60 | char button(int button_num); |
xshige | 0:665c7d36f533 | 61 | // button_num should be int not char!. |
xshige | 0:665c7d36f533 | 62 | void Midi_Send(byte cmd, byte data1, byte data2); |
xshige | 0:665c7d36f533 | 63 | |
xshige | 0:665c7d36f533 | 64 | // port allocation |
xshige | 0:665c7d36f533 | 65 | DigitalOut stat1(STAT1); |
xshige | 0:665c7d36f533 | 66 | DigitalOut stat2(STAT2); |
xshige | 0:665c7d36f533 | 67 | |
xshige | 0:665c7d36f533 | 68 | AnalogIn knob1(KNOB1); |
xshige | 0:665c7d36f533 | 69 | AnalogIn knob2(KNOB2); |
xshige | 0:665c7d36f533 | 70 | |
xshige | 0:665c7d36f533 | 71 | DigitalInOut button1(BUTTON1); |
xshige | 0:665c7d36f533 | 72 | DigitalInOut button2(BUTTON2); |
xshige | 0:665c7d36f533 | 73 | DigitalInOut button3(BUTTON3); |
xshige | 0:665c7d36f533 | 74 | |
xshige | 0:665c7d36f533 | 75 | Serial midi(ARD_TX, ARD_RX); |
xshige | 0:665c7d36f533 | 76 | #endif |
xshige | 0:665c7d36f533 | 77 | |
xshige | 0:665c7d36f533 | 78 | void setup() { |
xshige | 0:665c7d36f533 | 79 | #if 1 |
xshige | 0:665c7d36f533 | 80 | button1.mode(PullUp); |
xshige | 0:665c7d36f533 | 81 | button2.mode(PullUp); |
xshige | 0:665c7d36f533 | 82 | button3.mode(PullUp); |
xshige | 0:665c7d36f533 | 83 | #endif |
xshige | 0:665c7d36f533 | 84 | #if 0 |
xshige | 0:665c7d36f533 | 85 | pinMode(STAT1,OUTPUT); |
xshige | 0:665c7d36f533 | 86 | pinMode(STAT2,OUTPUT); |
xshige | 0:665c7d36f533 | 87 | |
xshige | 0:665c7d36f533 | 88 | pinMode(BUTTON1,INPUT); |
xshige | 0:665c7d36f533 | 89 | pinMode(BUTTON2,INPUT); |
xshige | 0:665c7d36f533 | 90 | pinMode(BUTTON3,INPUT); |
xshige | 0:665c7d36f533 | 91 | |
xshige | 0:665c7d36f533 | 92 | digitalWrite(BUTTON1,HIGH); |
xshige | 0:665c7d36f533 | 93 | digitalWrite(BUTTON2,HIGH); |
xshige | 0:665c7d36f533 | 94 | digitalWrite(BUTTON3,HIGH); |
xshige | 0:665c7d36f533 | 95 | #endif |
xshige | 0:665c7d36f533 | 96 | |
xshige | 0:665c7d36f533 | 97 | for(int i = 0;i < 10;i++) // flash MIDI Sheild LED's on startup |
xshige | 0:665c7d36f533 | 98 | { |
xshige | 0:665c7d36f533 | 99 | #if 1 |
xshige | 0:665c7d36f533 | 100 | stat1=HIGH; |
xshige | 0:665c7d36f533 | 101 | stat2=LOW; |
xshige | 0:665c7d36f533 | 102 | #endif |
xshige | 0:665c7d36f533 | 103 | #if 0 |
xshige | 0:665c7d36f533 | 104 | digitalWrite(STAT1,HIGH); |
xshige | 0:665c7d36f533 | 105 | digitalWrite(STAT2,LOW); |
xshige | 0:665c7d36f533 | 106 | #endif |
xshige | 0:665c7d36f533 | 107 | delay(30); |
xshige | 0:665c7d36f533 | 108 | #if 1 |
xshige | 0:665c7d36f533 | 109 | stat1=LOW; |
xshige | 0:665c7d36f533 | 110 | stat2=HIGH; |
xshige | 0:665c7d36f533 | 111 | #endif |
xshige | 0:665c7d36f533 | 112 | #if 0 |
xshige | 0:665c7d36f533 | 113 | digitalWrite(STAT1,LOW); |
xshige | 0:665c7d36f533 | 114 | digitalWrite(STAT2,HIGH); |
xshige | 0:665c7d36f533 | 115 | #endif |
xshige | 0:665c7d36f533 | 116 | delay(30); |
xshige | 0:665c7d36f533 | 117 | } |
xshige | 0:665c7d36f533 | 118 | #if 1 |
xshige | 0:665c7d36f533 | 119 | stat1=HIGH; |
xshige | 0:665c7d36f533 | 120 | stat2=HIGH; |
xshige | 0:665c7d36f533 | 121 | #endif |
xshige | 0:665c7d36f533 | 122 | #if 0 |
xshige | 0:665c7d36f533 | 123 | digitalWrite(STAT1,HIGH); |
xshige | 0:665c7d36f533 | 124 | digitalWrite(STAT2,HIGH); |
xshige | 0:665c7d36f533 | 125 | #endif |
xshige | 0:665c7d36f533 | 126 | |
xshige | 0:665c7d36f533 | 127 | //start serial with midi baudrate 31250 |
xshige | 0:665c7d36f533 | 128 | #if 1 |
xshige | 0:665c7d36f533 | 129 | midi.baud(31250); |
xshige | 0:665c7d36f533 | 130 | #endif |
xshige | 0:665c7d36f533 | 131 | #if 0 |
xshige | 0:665c7d36f533 | 132 | Serial.begin(31250); |
xshige | 0:665c7d36f533 | 133 | #endif |
xshige | 0:665c7d36f533 | 134 | } |
xshige | 0:665c7d36f533 | 135 | |
xshige | 0:665c7d36f533 | 136 | void loop () { |
xshige | 0:665c7d36f533 | 137 | |
xshige | 0:665c7d36f533 | 138 | //*************** MIDI OUT ***************// |
xshige | 0:665c7d36f533 | 139 | #if 1 |
xshige | 0:665c7d36f533 | 140 | pot = knob1.read_u16(); |
xshige | 0:665c7d36f533 | 141 | //printf("pot:%d ",pot);// debug |
xshige | 0:665c7d36f533 | 142 | #endif |
xshige | 0:665c7d36f533 | 143 | #if 0 |
xshige | 0:665c7d36f533 | 144 | pot = analogRead(0); |
xshige | 0:665c7d36f533 | 145 | #endif |
xshige | 0:665c7d36f533 | 146 | #if 1 |
xshige | 0:665c7d36f533 | 147 | note = pot/511; // convert value to value 0-127 |
xshige | 0:665c7d36f533 | 148 | #endif |
xshige | 0:665c7d36f533 | 149 | #if 0 |
xshige | 0:665c7d36f533 | 150 | note = pot/8; // convert value to value 0-127 |
xshige | 0:665c7d36f533 | 151 | #endif |
xshige | 0:665c7d36f533 | 152 | if(button(BUTTON1) || button(BUTTON2) || button(BUTTON3)) |
xshige | 0:665c7d36f533 | 153 | { |
xshige | 0:665c7d36f533 | 154 | Midi_Send(0x90,note,0x45); |
xshige | 0:665c7d36f533 | 155 | while(button(BUTTON1) || button(BUTTON2) || button(BUTTON3)); |
xshige | 0:665c7d36f533 | 156 | } |
xshige | 0:665c7d36f533 | 157 | |
xshige | 0:665c7d36f533 | 158 | //*************** MIDI LOOPBACK ******************// |
xshige | 0:665c7d36f533 | 159 | #if 1 |
xshige | 0:665c7d36f533 | 160 | if(midi.readable() > 0) |
xshige | 0:665c7d36f533 | 161 | #endif |
xshige | 0:665c7d36f533 | 162 | #if 0 |
xshige | 0:665c7d36f533 | 163 | if(Serial.available() > 0) |
xshige | 0:665c7d36f533 | 164 | #endif |
xshige | 0:665c7d36f533 | 165 | { |
xshige | 0:665c7d36f533 | 166 | #if 1 |
xshige | 0:665c7d36f533 | 167 | byte1 = midi.getc(); |
xshige | 0:665c7d36f533 | 168 | byte2 = midi.getc(); |
xshige | 0:665c7d36f533 | 169 | byte3 = midi.getc(); |
xshige | 0:665c7d36f533 | 170 | #endif |
xshige | 0:665c7d36f533 | 171 | #if 0 |
xshige | 0:665c7d36f533 | 172 | byte1 = Serial.read(); |
xshige | 0:665c7d36f533 | 173 | byte2 = Serial.read(); |
xshige | 0:665c7d36f533 | 174 | byte3 = Serial.read(); |
xshige | 0:665c7d36f533 | 175 | #endif |
xshige | 0:665c7d36f533 | 176 | Midi_Send(byte1, byte2, byte3); |
xshige | 0:665c7d36f533 | 177 | } |
xshige | 0:665c7d36f533 | 178 | |
xshige | 0:665c7d36f533 | 179 | //*************** MIDI IN ***************// |
xshige | 0:665c7d36f533 | 180 | #if 1 |
xshige | 0:665c7d36f533 | 181 | if (midi.readable() > 0) { |
xshige | 0:665c7d36f533 | 182 | // read the incoming byte: |
xshige | 0:665c7d36f533 | 183 | incomingByte = midi.getc(); |
xshige | 0:665c7d36f533 | 184 | printf("Incoming MIDI data: %02X\r\n",incomingByte);//debug |
xshige | 0:665c7d36f533 | 185 | #endif |
xshige | 0:665c7d36f533 | 186 | #if 0 |
xshige | 0:665c7d36f533 | 187 | if (Serial.available() > 0) { |
xshige | 0:665c7d36f533 | 188 | // read the incoming byte: |
xshige | 0:665c7d36f533 | 189 | incomingByte = Serial.read(); |
xshige | 0:665c7d36f533 | 190 | #endif |
xshige | 0:665c7d36f533 | 191 | // wait for as status-byte, channel 1, note on or off |
xshige | 0:665c7d36f533 | 192 | if (incomingByte== 144) // Note on |
xshige | 0:665c7d36f533 | 193 | { |
xshige | 0:665c7d36f533 | 194 | action = OFF; |
xshige | 0:665c7d36f533 | 195 | } |
xshige | 0:665c7d36f533 | 196 | else if (incomingByte== 128) // Note off |
xshige | 0:665c7d36f533 | 197 | { |
xshige | 0:665c7d36f533 | 198 | action = ON; |
xshige | 0:665c7d36f533 | 199 | } |
xshige | 0:665c7d36f533 | 200 | else if (note==0 && action != WAIT) // note on, wait for note value |
xshige | 0:665c7d36f533 | 201 | { |
xshige | 0:665c7d36f533 | 202 | note=incomingByte; |
xshige | 0:665c7d36f533 | 203 | } |
xshige | 0:665c7d36f533 | 204 | else if (note!=0 && action != WAIT) // velocity |
xshige | 0:665c7d36f533 | 205 | { |
xshige | 0:665c7d36f533 | 206 | velocity=incomingByte; |
xshige | 0:665c7d36f533 | 207 | if(action == ON){ |
xshige | 0:665c7d36f533 | 208 | Midi_Send(0x90,note,velocity); |
xshige | 0:665c7d36f533 | 209 | } |
xshige | 0:665c7d36f533 | 210 | if(action == OFF){ |
xshige | 0:665c7d36f533 | 211 | Midi_Send(0x80,note,velocity); |
xshige | 0:665c7d36f533 | 212 | } |
xshige | 0:665c7d36f533 | 213 | note=0; |
xshige | 0:665c7d36f533 | 214 | velocity=0; |
xshige | 0:665c7d36f533 | 215 | action=WAIT; |
xshige | 0:665c7d36f533 | 216 | } |
xshige | 0:665c7d36f533 | 217 | else{ |
xshige | 0:665c7d36f533 | 218 | } |
xshige | 0:665c7d36f533 | 219 | } |
xshige | 0:665c7d36f533 | 220 | |
xshige | 0:665c7d36f533 | 221 | } |
xshige | 0:665c7d36f533 | 222 | |
xshige | 0:665c7d36f533 | 223 | void Midi_Send(byte cmd, byte data1, byte data2) { |
xshige | 0:665c7d36f533 | 224 | #if 1 |
xshige | 0:665c7d36f533 | 225 | printf("Outgoing MIDI data: %02X:%02X:%02X\r\n",cmd,data1,data2); // debug |
xshige | 0:665c7d36f533 | 226 | midi.putc(cmd); |
xshige | 0:665c7d36f533 | 227 | midi.putc(data1); |
xshige | 0:665c7d36f533 | 228 | midi.putc(data2); |
xshige | 0:665c7d36f533 | 229 | #endif |
xshige | 0:665c7d36f533 | 230 | #if 0 |
xshige | 0:665c7d36f533 | 231 | Serial.print(cmd, BYTE); |
xshige | 0:665c7d36f533 | 232 | Serial.print(data1, BYTE); |
xshige | 0:665c7d36f533 | 233 | Serial.print(data2, BYTE); |
xshige | 0:665c7d36f533 | 234 | #endif |
xshige | 0:665c7d36f533 | 235 | } |
xshige | 0:665c7d36f533 | 236 | |
xshige | 0:665c7d36f533 | 237 | void blink(){ |
xshige | 0:665c7d36f533 | 238 | #if 1 |
xshige | 0:665c7d36f533 | 239 | stat1=HIGH; |
xshige | 0:665c7d36f533 | 240 | #endif |
xshige | 0:665c7d36f533 | 241 | #if 0 |
xshige | 0:665c7d36f533 | 242 | digitalWrite(STAT1, HIGH); |
xshige | 0:665c7d36f533 | 243 | #endif |
xshige | 0:665c7d36f533 | 244 | delay(100); |
xshige | 0:665c7d36f533 | 245 | #if 1 |
xshige | 0:665c7d36f533 | 246 | stat1=LOW; |
xshige | 0:665c7d36f533 | 247 | #endif |
xshige | 0:665c7d36f533 | 248 | #if 0 |
xshige | 0:665c7d36f533 | 249 | digitalWrite(STAT1, LOW); |
xshige | 0:665c7d36f533 | 250 | #endif |
xshige | 0:665c7d36f533 | 251 | delay(100); |
xshige | 0:665c7d36f533 | 252 | } |
xshige | 0:665c7d36f533 | 253 | #if 1 |
xshige | 0:665c7d36f533 | 254 | char button(int button_num) |
xshige | 0:665c7d36f533 | 255 | #endif |
xshige | 0:665c7d36f533 | 256 | #if 0 |
xshige | 0:665c7d36f533 | 257 | char button(char button_num) |
xshige | 0:665c7d36f533 | 258 | #endif |
xshige | 0:665c7d36f533 | 259 | { |
xshige | 0:665c7d36f533 | 260 | #if 1 |
xshige | 0:665c7d36f533 | 261 | if (BUTTON1==button_num) return (!button1.read()); |
xshige | 0:665c7d36f533 | 262 | if (BUTTON2==button_num) return (!button2.read()); |
xshige | 0:665c7d36f533 | 263 | if (BUTTON3==button_num) return (!button3.read()); |
xshige | 0:665c7d36f533 | 264 | return 0; // never happen |
xshige | 0:665c7d36f533 | 265 | #endif |
xshige | 0:665c7d36f533 | 266 | #if 0 |
xshige | 0:665c7d36f533 | 267 | return (!(digitalRead(button_num))); |
xshige | 0:665c7d36f533 | 268 | #endif |
xshige | 0:665c7d36f533 | 269 | } |
xshige | 0:665c7d36f533 | 270 | |
xshige | 0:665c7d36f533 | 271 | |
xshige | 0:665c7d36f533 | 272 | // -------- arduino hidden main --------- |
xshige | 0:665c7d36f533 | 273 | |
xshige | 0:665c7d36f533 | 274 | void init(void) { |
xshige | 0:665c7d36f533 | 275 | // make debug port Fast |
xshige | 0:665c7d36f533 | 276 | Serial pc(USBTX, USBRX); |
xshige | 0:665c7d36f533 | 277 | // pc.baud(9600); |
xshige | 0:665c7d36f533 | 278 | pc.baud(115200); |
xshige | 0:665c7d36f533 | 279 | // pc.baud(230400); |
xshige | 0:665c7d36f533 | 280 | printf("mbeduino MIDI Shield Test\r\n"); |
xshige | 0:665c7d36f533 | 281 | printf("Rotate Knob#1 to change note.\r\n" |
xshige | 0:665c7d36f533 | 282 | "Push any button to send MIDI note data.\r\n"); |
xshige | 0:665c7d36f533 | 283 | } |
xshige | 0:665c7d36f533 | 284 | |
xshige | 0:665c7d36f533 | 285 | int main(void) |
xshige | 0:665c7d36f533 | 286 | { |
xshige | 0:665c7d36f533 | 287 | init(); |
xshige | 0:665c7d36f533 | 288 | |
xshige | 0:665c7d36f533 | 289 | setup(); |
xshige | 0:665c7d36f533 | 290 | |
xshige | 0:665c7d36f533 | 291 | for (;;) |
xshige | 0:665c7d36f533 | 292 | loop(); |
xshige | 0:665c7d36f533 | 293 | |
xshige | 0:665c7d36f533 | 294 | return 0; |
xshige | 0:665c7d36f533 | 295 | } |