
USBMIDI example
main.cpp@0:128aa8026f32, 2011-11-09 (annotated)
- Committer:
- samux
- Date:
- Wed Nov 09 10:06:48 2011 +0000
- Revision:
- 0:128aa8026f32
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 0:128aa8026f32 | 1 | // Hello World example for the USBMIDI library |
samux | 0:128aa8026f32 | 2 | |
samux | 0:128aa8026f32 | 3 | #include "mbed.h" |
samux | 0:128aa8026f32 | 4 | #include "USBMIDI.h" |
samux | 0:128aa8026f32 | 5 | |
samux | 0:128aa8026f32 | 6 | //USBMIDI object |
samux | 0:128aa8026f32 | 7 | USBMIDI midi; |
samux | 0:128aa8026f32 | 8 | |
samux | 0:128aa8026f32 | 9 | |
samux | 0:128aa8026f32 | 10 | // Leds which will be switch on or off according to a MIDImessage |
samux | 0:128aa8026f32 | 11 | BusOut leds(LED1, LED2, LED3, LED4); |
samux | 0:128aa8026f32 | 12 | |
samux | 0:128aa8026f32 | 13 | BusInOut buttons(p22, p23, p24, p25); |
samux | 0:128aa8026f32 | 14 | |
samux | 0:128aa8026f32 | 15 | void show_message(MIDIMessage msg) { |
samux | 0:128aa8026f32 | 16 | switch (msg.type()) { |
samux | 0:128aa8026f32 | 17 | case MIDIMessage::NoteOnType: |
samux | 0:128aa8026f32 | 18 | switch (msg.key()) { |
samux | 0:128aa8026f32 | 19 | case 48: |
samux | 0:128aa8026f32 | 20 | leds = (1 << 0); |
samux | 0:128aa8026f32 | 21 | break; |
samux | 0:128aa8026f32 | 22 | case 49: |
samux | 0:128aa8026f32 | 23 | leds = (1 << 1); |
samux | 0:128aa8026f32 | 24 | break; |
samux | 0:128aa8026f32 | 25 | case 50: |
samux | 0:128aa8026f32 | 26 | leds = (1 << 2); |
samux | 0:128aa8026f32 | 27 | break; |
samux | 0:128aa8026f32 | 28 | case 51: |
samux | 0:128aa8026f32 | 29 | leds = (1 << 3); |
samux | 0:128aa8026f32 | 30 | break; |
samux | 0:128aa8026f32 | 31 | } |
samux | 0:128aa8026f32 | 32 | break; |
samux | 0:128aa8026f32 | 33 | case MIDIMessage::NoteOffType: |
samux | 0:128aa8026f32 | 34 | default: |
samux | 0:128aa8026f32 | 35 | leds = 0; |
samux | 0:128aa8026f32 | 36 | } |
samux | 0:128aa8026f32 | 37 | } |
samux | 0:128aa8026f32 | 38 | |
samux | 0:128aa8026f32 | 39 | int main() { |
samux | 0:128aa8026f32 | 40 | uint8_t bus = 0; |
samux | 0:128aa8026f32 | 41 | uint8_t p_bus = 0; |
samux | 0:128aa8026f32 | 42 | |
samux | 0:128aa8026f32 | 43 | // call back for messages received |
samux | 0:128aa8026f32 | 44 | midi.attach(show_message); |
samux | 0:128aa8026f32 | 45 | |
samux | 0:128aa8026f32 | 46 | while (1) { |
samux | 0:128aa8026f32 | 47 | |
samux | 0:128aa8026f32 | 48 | //if buttons state changes, send a MIDI message |
samux | 0:128aa8026f32 | 49 | bus = buttons.read(); |
samux | 0:128aa8026f32 | 50 | for (int i = 0; i < 4; i++) { |
samux | 0:128aa8026f32 | 51 | if ( (bus & (1 << i)) != (p_bus & (1 << i))) { |
samux | 0:128aa8026f32 | 52 | if (bus & (1 << i)) { |
samux | 0:128aa8026f32 | 53 | midi.write(MIDIMessage::NoteOn(48 + i)); |
samux | 0:128aa8026f32 | 54 | } else if ( !(bus & (1 << i)) ) { |
samux | 0:128aa8026f32 | 55 | midi.write(MIDIMessage::NoteOff(48 + i)); |
samux | 0:128aa8026f32 | 56 | } |
samux | 0:128aa8026f32 | 57 | |
samux | 0:128aa8026f32 | 58 | } |
samux | 0:128aa8026f32 | 59 | } |
samux | 0:128aa8026f32 | 60 | wait(0.001); |
samux | 0:128aa8026f32 | 61 | p_bus = bus; |
samux | 0:128aa8026f32 | 62 | } |
samux | 0:128aa8026f32 | 63 | } |