Simple USB-MIDI foot controller

Dependencies:   PinDetect USBDevice_STM32F103 mbed-STM32F103C8T6

Committer:
SpotlightKid
Date:
Fri Aug 04 06:08:29 2017 +0200
Revision:
10:f9a48eedfd59
Parent:
9:d5fa853818dd
Child:
11:ff22140b5119
MIDI receive debugging

Who changed what in which revision?

UserRevisionLine numberNew 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 9:d5fa853818dd 15 {PB_4, 0xB0, 0, 1, 127, 0},
SpotlightKid 9:d5fa853818dd 16 {PB_5, 0xC0, 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 10:f9a48eedfd59 41 serial->printf("MIDI recv: status=%02X data1=%02X data2=%02X length=%d\r\n",
SpotlightKid 10:f9a48eedfd59 42 msg.data[1], msg.data[2], msg.data[3], msg.length);
SpotlightKid 10:f9a48eedfd59 43 #endif
SpotlightKid 9:d5fa853818dd 44
SpotlightKid 9:d5fa853818dd 45 if (msg.data[1] == 0xF0 && msg.data[2] == 0x7D && msg.data[9] == 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 9:d5fa853818dd 51 switches[sw].on_value = msg.data[7];
SpotlightKid 9:d5fa853818dd 52 switches[sw].off_value = msg.data[8];
SpotlightKid 9:d5fa853818dd 53 handlers[sw]->setConfig(switches[sw]);
SpotlightKid 9:d5fa853818dd 54 #ifndef NDEBUG
SpotlightKid 9:d5fa853818dd 55 serial->printf("Changed configuration for switch %s.\r\n", sw);
SpotlightKid 9:d5fa853818dd 56 #endif
SpotlightKid 9:d5fa853818dd 57 }
SpotlightKid 9:d5fa853818dd 58 }
SpotlightKid 9:d5fa853818dd 59 }
SpotlightKid 9:d5fa853818dd 60
SpotlightKid 0:2f530d7169a6 61 int main() {
SpotlightKid 0:2f530d7169a6 62 // Configure system clock (72MHz HSE clock, 48MHz USB clock)
SpotlightKid 0:2f530d7169a6 63 confSysClock();
SpotlightKid 0:2f530d7169a6 64
SpotlightKid 3:8b8cb5392fa0 65 #ifndef NDEBUG
SpotlightKid 0:2f530d7169a6 66 serial = new Serial(PA_9, PA_10);
SpotlightKid 3:8b8cb5392fa0 67 serial->printf("Creating event queue...\r\n");
SpotlightKid 3:8b8cb5392fa0 68 #endif
SpotlightKid 5:1208f33a9a7c 69 EventQueue queue;
SpotlightKid 5:1208f33a9a7c 70
SpotlightKid 5:1208f33a9a7c 71 #ifndef NDEBUG
SpotlightKid 5:1208f33a9a7c 72 serial->printf("Creating USBMIDI device...\r\n");
SpotlightKid 5:1208f33a9a7c 73 #endif
SpotlightKid 5:1208f33a9a7c 74 midi = new USBMIDI(0x1f00, 0x2012, 0x0001);
SpotlightKid 3:8b8cb5392fa0 75
SpotlightKid 3:8b8cb5392fa0 76 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 77 serial->printf("Initializing LED...\r\n");
SpotlightKid 3:8b8cb5392fa0 78 #endif
SpotlightKid 3:8b8cb5392fa0 79 DigitalOut led1(LED1);
SpotlightKid 3:8b8cb5392fa0 80 led1 = 1;
SpotlightKid 3:8b8cb5392fa0 81
SpotlightKid 3:8b8cb5392fa0 82 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 83 serial->printf("Initializing event queue thread...\r\n");
SpotlightKid 3:8b8cb5392fa0 84 #endif
SpotlightKid 3:8b8cb5392fa0 85 // create a thread that'll run the event queue's dispatch function
SpotlightKid 5:1208f33a9a7c 86 Thread usbThread;
SpotlightKid 3:8b8cb5392fa0 87 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 88 serial->printf("Starting event queue thread...\r\n");
SpotlightKid 3:8b8cb5392fa0 89 #endif
SpotlightKid 3:8b8cb5392fa0 90 usbThread.start(callback(&queue, &EventQueue::dispatch_forever));
SpotlightKid 3:8b8cb5392fa0 91
SpotlightKid 3:8b8cb5392fa0 92 for (int sw=0; sw < NUM_SWITCHES; sw++) {
SpotlightKid 3:8b8cb5392fa0 93 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 94 serial->printf("Initializing switch handler %d...\r\n", sw + 1);
SpotlightKid 3:8b8cb5392fa0 95 #endif
SpotlightKid 8:75c5ec68765e 96 handlers[sw] = new SwitchHandler(&queue, &write_midi_msg, switches[sw]);
SpotlightKid 3:8b8cb5392fa0 97 }
SpotlightKid 3:8b8cb5392fa0 98
SpotlightKid 9:d5fa853818dd 99 midi->attach(&handle_sysex);
SpotlightKid 9:d5fa853818dd 100
SpotlightKid 3:8b8cb5392fa0 101 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 102 serial->printf("Entering main loop...\r\n");
SpotlightKid 3:8b8cb5392fa0 103 #endif
SpotlightKid 0:2f530d7169a6 104 while (true) {
SpotlightKid 3:8b8cb5392fa0 105 wait(0.5f);
SpotlightKid 3:8b8cb5392fa0 106 led1 = !led1;
SpotlightKid 3:8b8cb5392fa0 107 }
SpotlightKid 0:2f530d7169a6 108 }