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