Simple USB-MIDI foot controller
Dependencies: PinDetect USBDevice_STM32F103 mbed-STM32F103C8T6
main.cpp@7:553836a26221, 2017-08-04 (annotated)
- Committer:
- SpotlightKid
- Date:
- Fri Aug 04 04:20:37 2017 +0200
- Revision:
- 7:553836a26221
- Parent:
- 6:2f804d29cbb0
- Child:
- 8:75c5ec68765e
Fix controller number; use internal pin pull-ups
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 |
3:8b8cb5392fa0 | 14 | // pin, channel, control, on_value, off_value |
SpotlightKid |
3:8b8cb5392fa0 | 15 | {PB_4, 1, 1, 127, 0}, |
SpotlightKid |
7:553836a26221 | 16 | {PB_5, 1, 64, 64, 0}, |
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 | 0:2f530d7169a6 | 38 | int main() { |
SpotlightKid | 0:2f530d7169a6 | 39 | // Configure system clock (72MHz HSE clock, 48MHz USB clock) |
SpotlightKid | 0:2f530d7169a6 | 40 | confSysClock(); |
SpotlightKid | 0:2f530d7169a6 | 41 | |
SpotlightKid |
3:8b8cb5392fa0 | 42 | #ifndef NDEBUG |
SpotlightKid | 0:2f530d7169a6 | 43 | serial = new Serial(PA_9, PA_10); |
SpotlightKid |
3:8b8cb5392fa0 | 44 | serial->printf("Creating event queue...\r\n"); |
SpotlightKid |
3:8b8cb5392fa0 | 45 | #endif |
SpotlightKid |
5:1208f33a9a7c | 46 | EventQueue queue; |
SpotlightKid |
5:1208f33a9a7c | 47 | |
SpotlightKid |
5:1208f33a9a7c | 48 | #ifndef NDEBUG |
SpotlightKid |
5:1208f33a9a7c | 49 | serial->printf("Creating USBMIDI device...\r\n"); |
SpotlightKid |
5:1208f33a9a7c | 50 | #endif |
SpotlightKid |
5:1208f33a9a7c | 51 | midi = new USBMIDI(0x1f00, 0x2012, 0x0001); |
SpotlightKid |
3:8b8cb5392fa0 | 52 | |
SpotlightKid |
3:8b8cb5392fa0 | 53 | #ifndef NDEBUG |
SpotlightKid |
3:8b8cb5392fa0 | 54 | serial->printf("Initializing LED...\r\n"); |
SpotlightKid |
3:8b8cb5392fa0 | 55 | #endif |
SpotlightKid |
3:8b8cb5392fa0 | 56 | DigitalOut led1(LED1); |
SpotlightKid |
3:8b8cb5392fa0 | 57 | led1 = 1; |
SpotlightKid |
3:8b8cb5392fa0 | 58 | |
SpotlightKid |
3:8b8cb5392fa0 | 59 | #ifndef NDEBUG |
SpotlightKid |
3:8b8cb5392fa0 | 60 | serial->printf("Initializing event queue thread...\r\n"); |
SpotlightKid |
3:8b8cb5392fa0 | 61 | #endif |
SpotlightKid |
3:8b8cb5392fa0 | 62 | // create a thread that'll run the event queue's dispatch function |
SpotlightKid |
5:1208f33a9a7c | 63 | Thread usbThread; |
SpotlightKid |
3:8b8cb5392fa0 | 64 | #ifndef NDEBUG |
SpotlightKid |
3:8b8cb5392fa0 | 65 | serial->printf("Starting event queue thread...\r\n"); |
SpotlightKid |
3:8b8cb5392fa0 | 66 | #endif |
SpotlightKid |
3:8b8cb5392fa0 | 67 | usbThread.start(callback(&queue, &EventQueue::dispatch_forever)); |
SpotlightKid |
3:8b8cb5392fa0 | 68 | |
SpotlightKid |
3:8b8cb5392fa0 | 69 | for (int sw=0; sw < NUM_SWITCHES; sw++) { |
SpotlightKid |
3:8b8cb5392fa0 | 70 | #ifndef NDEBUG |
SpotlightKid |
3:8b8cb5392fa0 | 71 | serial->printf("Initializing switch handler %d...\r\n", sw + 1); |
SpotlightKid |
3:8b8cb5392fa0 | 72 | #endif |
SpotlightKid |
6:2f804d29cbb0 | 73 | handlers[sw] = new SwitchHandler(&queue, &write_midi_msg, &switches[sw]); |
SpotlightKid |
3:8b8cb5392fa0 | 74 | } |
SpotlightKid |
3:8b8cb5392fa0 | 75 | |
SpotlightKid |
3:8b8cb5392fa0 | 76 | #ifndef NDEBUG |
SpotlightKid |
3:8b8cb5392fa0 | 77 | serial->printf("Entering main loop...\r\n"); |
SpotlightKid |
3:8b8cb5392fa0 | 78 | #endif |
SpotlightKid | 0:2f530d7169a6 | 79 | while (true) { |
SpotlightKid |
3:8b8cb5392fa0 | 80 | wait(0.5f); |
SpotlightKid |
3:8b8cb5392fa0 | 81 | led1 = !led1; |
SpotlightKid |
3:8b8cb5392fa0 | 82 | } |
SpotlightKid | 0:2f530d7169a6 | 83 | } |