Simple USB-MIDI foot controller
Dependencies: PinDetect USBDevice_STM32F103 mbed-STM32F103C8T6
main.cpp@13:f9eb1f79cd0a, 2018-01-09 (annotated)
- Committer:
- SpotlightKid
- Date:
- Tue Jan 09 21:59:10 2018 +0100
- Revision:
- 13:f9eb1f79cd0a
- Parent:
- 12:d9ccbfd2cc8c
Remove STM32F103C8T6 library and reduced stack sizes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SpotlightKid | 0:2f530d7169a6 | 1 | #include "mbed.h" |
SpotlightKid |
3:8b8cb5392fa0 | 2 | #include "mbed_events.h" |
SpotlightKid | 0:2f530d7169a6 | 3 | #include "USBMIDI.h" |
SpotlightKid |
3:8b8cb5392fa0 | 4 | #include "midiswitch.h" |
SpotlightKid | 0:2f530d7169a6 | 5 | |
SpotlightKid | 0:2f530d7169a6 | 6 | |
SpotlightKid |
3:8b8cb5392fa0 | 7 | /* ******************** Configuration ***************************** */ |
SpotlightKid |
3:8b8cb5392fa0 | 8 | |
SpotlightKid |
3:8b8cb5392fa0 | 9 | #define NUM_SWITCHES 2 |
SpotlightKid | 0:2f530d7169a6 | 10 | |
SpotlightKid |
3:8b8cb5392fa0 | 11 | /* array of SwitchConfig structs */ |
SpotlightKid |
3:8b8cb5392fa0 | 12 | SwitchConfig switches[NUM_SWITCHES] = { |
SpotlightKid |
11:ff22140b5119 | 13 | // pin, type, channel, data1, on_value, off_value |
SpotlightKid |
11:ff22140b5119 | 14 | {PB_4, 0xB, 0, 1, 127, 0}, |
SpotlightKid |
11:ff22140b5119 | 15 | {PB_5, 0xC, 0, 0, 1, -1}, |
SpotlightKid |
3:8b8cb5392fa0 | 16 | }; |
SpotlightKid |
3:8b8cb5392fa0 | 17 | |
SpotlightKid |
3:8b8cb5392fa0 | 18 | |
SpotlightKid |
3:8b8cb5392fa0 | 19 | /* ******************** End Configuration ************************* */ |
SpotlightKid | 0:2f530d7169a6 | 20 | |
SpotlightKid |
3:8b8cb5392fa0 | 21 | #ifndef NDEBUG |
SpotlightKid |
3:8b8cb5392fa0 | 22 | Serial *serial; |
SpotlightKid |
3:8b8cb5392fa0 | 23 | #endif |
SpotlightKid |
7:553836a26221 | 24 | USBMIDI * midi; |
SpotlightKid |
3:8b8cb5392fa0 | 25 | /* array of pointers to SwitchHandler instances */ |
SpotlightKid |
3:8b8cb5392fa0 | 26 | SwitchHandler * handlers[NUM_SWITCHES]; |
SpotlightKid |
3:8b8cb5392fa0 | 27 | |
SpotlightKid |
3:8b8cb5392fa0 | 28 | |
SpotlightKid |
3:8b8cb5392fa0 | 29 | void write_midi_msg(MIDIMessage msg) { |
SpotlightKid |
6:2f804d29cbb0 | 30 | #ifndef NDEBUG |
SpotlightKid |
6:2f804d29cbb0 | 31 | serial->printf("Sending MIDI message controller=%d channel=%d value=%d\r\n", |
SpotlightKid |
6:2f804d29cbb0 | 32 | msg.controller(), msg.channel(), msg.value()); |
SpotlightKid |
6:2f804d29cbb0 | 33 | #endif |
SpotlightKid |
3:8b8cb5392fa0 | 34 | midi->write(msg); |
SpotlightKid |
3:8b8cb5392fa0 | 35 | } |
SpotlightKid | 0:2f530d7169a6 | 36 | |
SpotlightKid |
9:d5fa853818dd | 37 | void handle_sysex(MIDIMessage msg) { |
SpotlightKid |
9:d5fa853818dd | 38 | uint8_t sw; |
SpotlightKid |
10:f9a48eedfd59 | 39 | #ifndef NDEBUG |
SpotlightKid |
11:ff22140b5119 | 40 | serial->printf("MIDI recv: header=0x%02X status=0x%02X data1=0x%02X data2=%02d length=%d\r\n", |
SpotlightKid |
11:ff22140b5119 | 41 | msg.data[0], msg.data[1], msg.data[2], msg.data[3], msg.length); |
SpotlightKid |
10:f9a48eedfd59 | 42 | #endif |
SpotlightKid |
9:d5fa853818dd | 43 | |
SpotlightKid |
11:ff22140b5119 | 44 | if (msg.data[1] == 0xF0 && msg.data[2] == 0x7D && msg.data[11] == 0xF7) { |
SpotlightKid |
9:d5fa853818dd | 45 | sw = msg.data[3]; |
SpotlightKid |
9:d5fa853818dd | 46 | if (sw < NUM_SWITCHES) { |
SpotlightKid |
12:d9ccbfd2cc8c | 47 | switches[sw].type = msg.data[4] & 0xF; |
SpotlightKid |
12:d9ccbfd2cc8c | 48 | switches[sw].channel = msg.data[5] & 0xF; |
SpotlightKid |
12:d9ccbfd2cc8c | 49 | switches[sw].data1 = msg.data[6] & 0x7F; |
SpotlightKid |
11:ff22140b5119 | 50 | if (msg.data[7] > 0) |
SpotlightKid |
12:d9ccbfd2cc8c | 51 | switches[sw].on_value = msg.data[8] & 0x7F; |
SpotlightKid |
11:ff22140b5119 | 52 | else |
SpotlightKid |
11:ff22140b5119 | 53 | switches[sw].on_value = -1; |
SpotlightKid |
11:ff22140b5119 | 54 | if (msg.data[9] > 0) |
SpotlightKid |
12:d9ccbfd2cc8c | 55 | switches[sw].off_value = msg.data[10] & 0x7F; |
SpotlightKid |
11:ff22140b5119 | 56 | else |
SpotlightKid |
11:ff22140b5119 | 57 | switches[sw].off_value = -1; |
SpotlightKid |
9:d5fa853818dd | 58 | handlers[sw]->setConfig(switches[sw]); |
SpotlightKid |
9:d5fa853818dd | 59 | #ifndef NDEBUG |
SpotlightKid |
11:ff22140b5119 | 60 | serial->printf("Changed configuration for switch %d.\r\n", sw + 1); |
SpotlightKid |
11:ff22140b5119 | 61 | serial->printf("type: 0x%02X\r\n", switches[sw].type); |
SpotlightKid |
11:ff22140b5119 | 62 | serial->printf("channel: %02d\r\n", switches[sw].channel); |
SpotlightKid |
11:ff22140b5119 | 63 | serial->printf("data1: %02d\r\n", switches[sw].data1); |
SpotlightKid |
11:ff22140b5119 | 64 | serial->printf("on_value: %02d\r\n", switches[sw].on_value); |
SpotlightKid |
11:ff22140b5119 | 65 | serial->printf("off_value: %02d\r\n", switches[sw].off_value); |
SpotlightKid |
9:d5fa853818dd | 66 | #endif |
SpotlightKid |
9:d5fa853818dd | 67 | } |
SpotlightKid |
9:d5fa853818dd | 68 | } |
SpotlightKid |
9:d5fa853818dd | 69 | } |
SpotlightKid |
9:d5fa853818dd | 70 | |
SpotlightKid | 0:2f530d7169a6 | 71 | int main() { |
SpotlightKid |
3:8b8cb5392fa0 | 72 | #ifndef NDEBUG |
SpotlightKid | 0:2f530d7169a6 | 73 | serial = new Serial(PA_9, PA_10); |
SpotlightKid |
3:8b8cb5392fa0 | 74 | serial->printf("Creating event queue...\r\n"); |
SpotlightKid |
3:8b8cb5392fa0 | 75 | #endif |
SpotlightKid |
5:1208f33a9a7c | 76 | EventQueue queue; |
SpotlightKid |
5:1208f33a9a7c | 77 | |
SpotlightKid |
5:1208f33a9a7c | 78 | #ifndef NDEBUG |
SpotlightKid |
5:1208f33a9a7c | 79 | serial->printf("Creating USBMIDI device...\r\n"); |
SpotlightKid |
5:1208f33a9a7c | 80 | #endif |
SpotlightKid |
5:1208f33a9a7c | 81 | midi = new USBMIDI(0x1f00, 0x2012, 0x0001); |
SpotlightKid |
3:8b8cb5392fa0 | 82 | |
SpotlightKid |
3:8b8cb5392fa0 | 83 | #ifndef NDEBUG |
SpotlightKid |
3:8b8cb5392fa0 | 84 | serial->printf("Initializing LED...\r\n"); |
SpotlightKid |
3:8b8cb5392fa0 | 85 | #endif |
SpotlightKid |
3:8b8cb5392fa0 | 86 | DigitalOut led1(LED1); |
SpotlightKid |
3:8b8cb5392fa0 | 87 | led1 = 1; |
SpotlightKid |
3:8b8cb5392fa0 | 88 | |
SpotlightKid |
3:8b8cb5392fa0 | 89 | #ifndef NDEBUG |
SpotlightKid |
3:8b8cb5392fa0 | 90 | serial->printf("Initializing event queue thread...\r\n"); |
SpotlightKid |
3:8b8cb5392fa0 | 91 | #endif |
SpotlightKid |
3:8b8cb5392fa0 | 92 | // create a thread that'll run the event queue's dispatch function |
SpotlightKid |
5:1208f33a9a7c | 93 | Thread usbThread; |
SpotlightKid |
3:8b8cb5392fa0 | 94 | #ifndef NDEBUG |
SpotlightKid |
3:8b8cb5392fa0 | 95 | serial->printf("Starting event queue thread...\r\n"); |
SpotlightKid |
3:8b8cb5392fa0 | 96 | #endif |
SpotlightKid |
3:8b8cb5392fa0 | 97 | usbThread.start(callback(&queue, &EventQueue::dispatch_forever)); |
SpotlightKid |
3:8b8cb5392fa0 | 98 | |
SpotlightKid |
3:8b8cb5392fa0 | 99 | for (int sw=0; sw < NUM_SWITCHES; sw++) { |
SpotlightKid |
3:8b8cb5392fa0 | 100 | #ifndef NDEBUG |
SpotlightKid |
3:8b8cb5392fa0 | 101 | serial->printf("Initializing switch handler %d...\r\n", sw + 1); |
SpotlightKid |
3:8b8cb5392fa0 | 102 | #endif |
SpotlightKid |
8:75c5ec68765e | 103 | handlers[sw] = new SwitchHandler(&queue, &write_midi_msg, switches[sw]); |
SpotlightKid |
3:8b8cb5392fa0 | 104 | } |
SpotlightKid |
3:8b8cb5392fa0 | 105 | |
SpotlightKid |
9:d5fa853818dd | 106 | midi->attach(&handle_sysex); |
SpotlightKid |
9:d5fa853818dd | 107 | |
SpotlightKid |
3:8b8cb5392fa0 | 108 | #ifndef NDEBUG |
SpotlightKid |
3:8b8cb5392fa0 | 109 | serial->printf("Entering main loop...\r\n"); |
SpotlightKid |
3:8b8cb5392fa0 | 110 | #endif |
SpotlightKid | 0:2f530d7169a6 | 111 | while (true) { |
SpotlightKid |
3:8b8cb5392fa0 | 112 | wait(0.5f); |
SpotlightKid |
3:8b8cb5392fa0 | 113 | led1 = !led1; |
SpotlightKid |
3:8b8cb5392fa0 | 114 | } |
SpotlightKid | 0:2f530d7169a6 | 115 | } |