![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Framed Laser Harp Implementation with MBed, that allows interface with MIDI synthesizers software.
Fork of LaserHarpProj by
main.cpp@2:21f0ae3bef8a, 2014-12-09 (annotated)
- Committer:
- ywang627
- Date:
- Tue Dec 09 00:03:45 2014 +0000
- Revision:
- 2:21f0ae3bef8a
- Parent:
- 1:2b551cb862fe
Code for Laser Harp Implementation with MBed.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 0:4b55d56b6b61 | 1 | // Hello World example for the USBMIDI library |
simon | 0:4b55d56b6b61 | 2 | |
simon | 0:4b55d56b6b61 | 3 | #include "mbed.h" |
simon | 0:4b55d56b6b61 | 4 | #include "USBMIDI.h" |
ywang627 | 1:2b551cb862fe | 5 | #include <string> |
ywang627 | 1:2b551cb862fe | 6 | #include <list> |
simon | 0:4b55d56b6b61 | 7 | |
ywang627 | 1:2b551cb862fe | 8 | #include <mpr121.h> |
ywang627 | 1:2b551cb862fe | 9 | AnalogIn laser1 (p20); |
ywang627 | 1:2b551cb862fe | 10 | AnalogIn laser2(p19); |
ywang627 | 1:2b551cb862fe | 11 | AnalogIn laser3(p18); |
ywang627 | 1:2b551cb862fe | 12 | AnalogIn laser4(p17); |
ywang627 | 1:2b551cb862fe | 13 | AnalogIn laser5(p16); |
ywang627 | 1:2b551cb862fe | 14 | DigitalOut led1(LED1); |
ywang627 | 1:2b551cb862fe | 15 | DigitalOut led2(LED2); |
ywang627 | 1:2b551cb862fe | 16 | DigitalOut led3(LED3); |
ywang627 | 1:2b551cb862fe | 17 | DigitalOut led4(LED4); |
ywang627 | 1:2b551cb862fe | 18 | |
ywang627 | 1:2b551cb862fe | 19 | char str1[100]; |
ywang627 | 1:2b551cb862fe | 20 | char str2[100]; |
ywang627 | 1:2b551cb862fe | 21 | char str3[100]; |
ywang627 | 1:2b551cb862fe | 22 | char str4[100]; |
ywang627 | 1:2b551cb862fe | 23 | char str5[100]; |
ywang627 | 1:2b551cb862fe | 24 | Serial pc(USBTX,USBRX); |
ywang627 | 1:2b551cb862fe | 25 | float val1 = 1; |
ywang627 | 1:2b551cb862fe | 26 | float val2 = 1; |
ywang627 | 1:2b551cb862fe | 27 | float val3 = 1; |
ywang627 | 1:2b551cb862fe | 28 | float val4 = 1; |
ywang627 | 1:2b551cb862fe | 29 | float val5 = 1; |
ywang627 | 1:2b551cb862fe | 30 | int flag1 = 0; |
ywang627 | 1:2b551cb862fe | 31 | int flag2 = 0; |
ywang627 | 1:2b551cb862fe | 32 | int flag3 = 0; |
ywang627 | 1:2b551cb862fe | 33 | int flag4 = 0; |
ywang627 | 1:2b551cb862fe | 34 | int flag5 = 0; |
ywang627 | 1:2b551cb862fe | 35 | |
ywang627 | 1:2b551cb862fe | 36 | int interupt = 0; |
ywang627 | 1:2b551cb862fe | 37 | void show_message(MIDIMessage msg) |
ywang627 | 1:2b551cb862fe | 38 | { |
simon | 0:4b55d56b6b61 | 39 | switch (msg.type()) { |
simon | 0:4b55d56b6b61 | 40 | case MIDIMessage::NoteOnType: |
simon | 0:4b55d56b6b61 | 41 | printf("NoteOn key:%d, velocity: %d, channel: %d\n", msg.key(), msg.velocity(), msg.channel()); |
simon | 0:4b55d56b6b61 | 42 | break; |
simon | 0:4b55d56b6b61 | 43 | case MIDIMessage::NoteOffType: |
simon | 0:4b55d56b6b61 | 44 | printf("NoteOff key:%d, velocity: %d, channel: %d\n", msg.key(), msg.velocity(), msg.channel()); |
simon | 0:4b55d56b6b61 | 45 | break; |
ywang627 | 1:2b551cb862fe | 46 | case MIDIMessage::ControlChangeType: |
simon | 0:4b55d56b6b61 | 47 | printf("ControlChange controller: %d, data: %d\n", msg.controller(), msg.value()); |
simon | 0:4b55d56b6b61 | 48 | break; |
simon | 0:4b55d56b6b61 | 49 | case MIDIMessage::PitchWheelType: |
simon | 0:4b55d56b6b61 | 50 | printf("PitchWheel channel: %d, pitch: %d\n", msg.channel(), msg.pitch()); |
simon | 0:4b55d56b6b61 | 51 | break; |
simon | 0:4b55d56b6b61 | 52 | default: |
simon | 0:4b55d56b6b61 | 53 | printf("Another message\n"); |
ywang627 | 1:2b551cb862fe | 54 | } |
ywang627 | 1:2b551cb862fe | 55 | } |
ywang627 | 1:2b551cb862fe | 56 | |
ywang627 | 1:2b551cb862fe | 57 | InterruptIn interrupt(p26); |
ywang627 | 1:2b551cb862fe | 58 | // Setup the i2c bus on pins 28 and 27 |
ywang627 | 1:2b551cb862fe | 59 | I2C i2c(p9, p10); |
ywang627 | 1:2b551cb862fe | 60 | |
ywang627 | 1:2b551cb862fe | 61 | // Setup the Mpr121: |
ywang627 | 1:2b551cb862fe | 62 | // constructor(i2c object, i2c address of the mpr121) |
ywang627 | 1:2b551cb862fe | 63 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
ywang627 | 1:2b551cb862fe | 64 | |
ywang627 | 2:21f0ae3bef8a | 65 | int fs; |
ywang627 | 2:21f0ae3bef8a | 66 | int octave; |
ywang627 | 2:21f0ae3bef8a | 67 | void fallInterrupt() { |
ywang627 | 2:21f0ae3bef8a | 68 | int fs_code=0; |
ywang627 | 2:21f0ae3bef8a | 69 | int oct_code = 0; |
ywang627 | 1:2b551cb862fe | 70 | int i=0; |
ywang627 | 1:2b551cb862fe | 71 | int value=mpr121.read(0x00); |
ywang627 | 1:2b551cb862fe | 72 | value +=mpr121.read(0x01)<<8; |
ywang627 | 2:21f0ae3bef8a | 73 | // LED demo mod by J. Hamblen |
ywang627 | 2:21f0ae3bef8a | 74 | //pc.printf("MPR value: %x \r\n", value); |
ywang627 | 1:2b551cb862fe | 75 | i=0; |
ywang627 | 2:21f0ae3bef8a | 76 | // puts key number out to LEDs for demo |
ywang627 | 2:21f0ae3bef8a | 77 | for (i=0; i<4; i++) { |
ywang627 | 2:21f0ae3bef8a | 78 | if (((value>>i)&0x01)==1) fs_code=i+1; |
ywang627 | 2:21f0ae3bef8a | 79 | } |
ywang627 | 2:21f0ae3bef8a | 80 | switch (fs_code) |
ywang627 | 2:21f0ae3bef8a | 81 | { |
ywang627 | 2:21f0ae3bef8a | 82 | case 1: |
ywang627 | 2:21f0ae3bef8a | 83 | fs = 1; |
ywang627 | 2:21f0ae3bef8a | 84 | led1 = 1; |
ywang627 | 2:21f0ae3bef8a | 85 | led2 = 0; |
ywang627 | 2:21f0ae3bef8a | 86 | break; |
ywang627 | 2:21f0ae3bef8a | 87 | case 2: |
ywang627 | 2:21f0ae3bef8a | 88 | fs = 0; |
ywang627 | 2:21f0ae3bef8a | 89 | led1 = 0; |
ywang627 | 2:21f0ae3bef8a | 90 | led2 = 0; |
ywang627 | 2:21f0ae3bef8a | 91 | break; |
ywang627 | 2:21f0ae3bef8a | 92 | case 3: |
ywang627 | 2:21f0ae3bef8a | 93 | fs = -1; |
ywang627 | 2:21f0ae3bef8a | 94 | led1 = 0; |
ywang627 | 2:21f0ae3bef8a | 95 | led2 = 1; |
ywang627 | 2:21f0ae3bef8a | 96 | break; |
ywang627 | 2:21f0ae3bef8a | 97 | case 4: |
ywang627 | 2:21f0ae3bef8a | 98 | fs = 0; |
ywang627 | 2:21f0ae3bef8a | 99 | octave = 0; |
ywang627 | 2:21f0ae3bef8a | 100 | led1 = 0; |
ywang627 | 2:21f0ae3bef8a | 101 | led2 = 0; |
ywang627 | 2:21f0ae3bef8a | 102 | led3 = 0; |
ywang627 | 2:21f0ae3bef8a | 103 | led4 = 0; |
ywang627 | 2:21f0ae3bef8a | 104 | break; |
ywang627 | 1:2b551cb862fe | 105 | } |
ywang627 | 2:21f0ae3bef8a | 106 | |
ywang627 | 2:21f0ae3bef8a | 107 | for (i=4; i<7; i++) { |
ywang627 | 2:21f0ae3bef8a | 108 | if (((value>>i)&0x01)==1) oct_code=i; |
ywang627 | 2:21f0ae3bef8a | 109 | } |
ywang627 | 2:21f0ae3bef8a | 110 | switch (oct_code) |
ywang627 | 2:21f0ae3bef8a | 111 | { |
ywang627 | 2:21f0ae3bef8a | 112 | case 4: |
ywang627 | 2:21f0ae3bef8a | 113 | octave = 12; |
ywang627 | 2:21f0ae3bef8a | 114 | led3= 1; |
ywang627 | 2:21f0ae3bef8a | 115 | led4 = 0; |
ywang627 | 2:21f0ae3bef8a | 116 | break; |
ywang627 | 2:21f0ae3bef8a | 117 | case 5: |
ywang627 | 2:21f0ae3bef8a | 118 | octave = 0; |
ywang627 | 2:21f0ae3bef8a | 119 | led3 = 0; |
ywang627 | 2:21f0ae3bef8a | 120 | led4 = 0; |
ywang627 | 2:21f0ae3bef8a | 121 | break; |
ywang627 | 2:21f0ae3bef8a | 122 | case 6: |
ywang627 | 2:21f0ae3bef8a | 123 | octave = -12; |
ywang627 | 2:21f0ae3bef8a | 124 | led3 = 0; |
ywang627 | 2:21f0ae3bef8a | 125 | led4 = 1; |
ywang627 | 2:21f0ae3bef8a | 126 | break; |
ywang627 | 2:21f0ae3bef8a | 127 | } |
ywang627 | 2:21f0ae3bef8a | 128 | |
simon | 0:4b55d56b6b61 | 129 | } |
simon | 0:4b55d56b6b61 | 130 | |
simon | 0:4b55d56b6b61 | 131 | USBMIDI midi; |
simon | 0:4b55d56b6b61 | 132 | |
ywang627 | 1:2b551cb862fe | 133 | float lightScale(float val, float light) |
ywang627 | 1:2b551cb862fe | 134 | { |
ywang627 | 1:2b551cb862fe | 135 | val = light; |
ywang627 | 1:2b551cb862fe | 136 | return val; |
ywang627 | 1:2b551cb862fe | 137 | } |
ywang627 | 1:2b551cb862fe | 138 | |
ywang627 | 1:2b551cb862fe | 139 | |
ywang627 | 1:2b551cb862fe | 140 | int main() |
ywang627 | 1:2b551cb862fe | 141 | { |
ywang627 | 1:2b551cb862fe | 142 | pc.baud(19200); |
ywang627 | 1:2b551cb862fe | 143 | midi.attach(show_message); // call back for messages receive |
ywang627 | 1:2b551cb862fe | 144 | |
ywang627 | 1:2b551cb862fe | 145 | while (1) { |
ywang627 | 1:2b551cb862fe | 146 | fallInterrupt(); |
ywang627 | 1:2b551cb862fe | 147 | float light1 = laser1; |
ywang627 | 1:2b551cb862fe | 148 | float light2 = laser2; |
ywang627 | 1:2b551cb862fe | 149 | float light3 = laser3; |
ywang627 | 1:2b551cb862fe | 150 | float light4 = laser4; |
ywang627 | 1:2b551cb862fe | 151 | float light5 = laser5; |
ywang627 | 1:2b551cb862fe | 152 | float pval1 = val1; |
ywang627 | 1:2b551cb862fe | 153 | float pval2 = val2; |
ywang627 | 1:2b551cb862fe | 154 | float pval3 = val3; |
ywang627 | 1:2b551cb862fe | 155 | float pval4 = val4; |
ywang627 | 1:2b551cb862fe | 156 | float pval5 = val5; |
ywang627 | 1:2b551cb862fe | 157 | val1 = lightScale(val1, light1); |
ywang627 | 1:2b551cb862fe | 158 | val2 = lightScale(val2, light2); |
ywang627 | 1:2b551cb862fe | 159 | val3 = lightScale(val3, light3); |
ywang627 | 1:2b551cb862fe | 160 | val4 = lightScale(val4, light4); |
ywang627 | 1:2b551cb862fe | 161 | val5 = lightScale(val5, light5); |
ywang627 | 1:2b551cb862fe | 162 | sprintf (str1, "light value 1 = %04f\n", light1); |
ywang627 | 1:2b551cb862fe | 163 | sprintf (str2, "light value 2= %04f\n", light2); |
ywang627 | 1:2b551cb862fe | 164 | sprintf (str3, "light value 3= %04f\n", light3); |
ywang627 | 1:2b551cb862fe | 165 | sprintf (str4, "light value 4= %04f\n", light4); |
ywang627 | 1:2b551cb862fe | 166 | sprintf (str5, "light value 5= %04f\n", light5); |
ywang627 | 1:2b551cb862fe | 167 | //wait(1); |
ywang627 | 1:2b551cb862fe | 168 | pc.printf(str1); |
ywang627 | 1:2b551cb862fe | 169 | pc.printf(str2); |
ywang627 | 1:2b551cb862fe | 170 | pc.printf(str3); |
ywang627 | 1:2b551cb862fe | 171 | pc.printf(str4); |
ywang627 | 1:2b551cb862fe | 172 | pc.printf(str5); |
ywang627 | 1:2b551cb862fe | 173 | |
ywang627 | 1:2b551cb862fe | 174 | if (val1 <0.7 && pval1 > 0.7) { |
ywang627 | 1:2b551cb862fe | 175 | flag1 = 1; |
ywang627 | 1:2b551cb862fe | 176 | } else if (val1 < 0.7 && flag1 ==1) { |
ywang627 | 2:21f0ae3bef8a | 177 | midi.write(MIDIMessage::NoteOn(60+fs+octave)); |
ywang627 | 1:2b551cb862fe | 178 | flag1 = 0; |
ywang627 | 1:2b551cb862fe | 179 | } else if (val1 >0.7) { |
ywang627 | 2:21f0ae3bef8a | 180 | midi.write(MIDIMessage::NoteOff(60+fs+octave)); |
ywang627 | 1:2b551cb862fe | 181 | flag1 = 0; |
ywang627 | 1:2b551cb862fe | 182 | } |
ywang627 | 1:2b551cb862fe | 183 | if (val2 <0.7 && pval2 > 0.7) { |
ywang627 | 1:2b551cb862fe | 184 | flag2 = 1; |
ywang627 | 1:2b551cb862fe | 185 | } else if (val2 < 0.7 && flag2 ==1) { |
ywang627 | 2:21f0ae3bef8a | 186 | midi.write(MIDIMessage::NoteOn(62+fs+octave)); |
ywang627 | 1:2b551cb862fe | 187 | flag2 = 0; |
ywang627 | 1:2b551cb862fe | 188 | } else if (val2 >0.7) { |
ywang627 | 2:21f0ae3bef8a | 189 | midi.write(MIDIMessage::NoteOff(62+fs+octave)); |
ywang627 | 1:2b551cb862fe | 190 | flag2 = 0; |
ywang627 | 1:2b551cb862fe | 191 | } |
ywang627 | 1:2b551cb862fe | 192 | if (val3 <0.7 && pval3 > 0.7) { |
ywang627 | 1:2b551cb862fe | 193 | flag3 = 1; |
ywang627 | 1:2b551cb862fe | 194 | } else if (val3 < 0.7 && flag3 ==1) { |
ywang627 | 2:21f0ae3bef8a | 195 | midi.write(MIDIMessage::NoteOn(64+fs+octave)); |
ywang627 | 1:2b551cb862fe | 196 | flag3 = 0; |
ywang627 | 1:2b551cb862fe | 197 | } else if (val3 >0.7) { |
ywang627 | 2:21f0ae3bef8a | 198 | midi.write(MIDIMessage::NoteOff(64+fs+octave)); |
ywang627 | 1:2b551cb862fe | 199 | flag3 = 0; |
ywang627 | 1:2b551cb862fe | 200 | } |
ywang627 | 1:2b551cb862fe | 201 | if (val4 <0.7 && pval4 > 0.7) { |
ywang627 | 1:2b551cb862fe | 202 | flag4 = 1; |
ywang627 | 1:2b551cb862fe | 203 | } else if (val4 < 0.7 && flag4 ==1) { |
ywang627 | 2:21f0ae3bef8a | 204 | midi.write(MIDIMessage::NoteOn(65+fs+octave)); |
ywang627 | 1:2b551cb862fe | 205 | flag4 = 0; |
ywang627 | 1:2b551cb862fe | 206 | } else if (val4 >0.7) { |
ywang627 | 2:21f0ae3bef8a | 207 | midi.write(MIDIMessage::NoteOff(65+fs+octave)); |
ywang627 | 1:2b551cb862fe | 208 | flag4 = 0; |
ywang627 | 1:2b551cb862fe | 209 | } |
ywang627 | 1:2b551cb862fe | 210 | if (val5 <0.7 && pval5 > 0.7) { |
ywang627 | 1:2b551cb862fe | 211 | flag5 = 1; |
ywang627 | 1:2b551cb862fe | 212 | } else if (val5 < 0.7 && flag5 ==1) { |
ywang627 | 2:21f0ae3bef8a | 213 | midi.write(MIDIMessage::NoteOn(67+fs+octave)); |
ywang627 | 1:2b551cb862fe | 214 | flag5 = 0; |
ywang627 | 1:2b551cb862fe | 215 | } else if (val5 >0.7) { |
ywang627 | 2:21f0ae3bef8a | 216 | midi.write(MIDIMessage::NoteOff(67+fs+octave)); |
ywang627 | 1:2b551cb862fe | 217 | flag5 = 0; |
simon | 0:4b55d56b6b61 | 218 | } |
simon | 0:4b55d56b6b61 | 219 | } |
ywang627 | 1:2b551cb862fe | 220 | } |