Chris Arndt / Mbed OS STM32F103_USBMIDI_Switchbox

Dependencies:   PinDetect USBDevice_STM32F103 mbed-STM32F103C8T6

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "mbed_events.h"
00003 #include "USBMIDI.h"
00004 #include "midiswitch.h"
00005 
00006 
00007 /* ******************** Configuration ***************************** */
00008 
00009 #define NUM_SWITCHES 2
00010 
00011 /* array of SwitchConfig structs */
00012 SwitchConfig switches[NUM_SWITCHES] = {
00013     // pin, type, channel, data1, on_value, off_value
00014     {PB_4, 0xB, 0, 1, 127, 0},
00015     {PB_5, 0xC, 0, 0, 1, -1},
00016 };
00017 
00018 
00019 /* ******************** End Configuration ************************* */
00020 
00021 #ifndef NDEBUG
00022 Serial *serial;
00023 #endif
00024 USBMIDI * midi;
00025 /* array of pointers to SwitchHandler instances */
00026 SwitchHandler * handlers[NUM_SWITCHES];
00027 
00028 
00029 void write_midi_msg(MIDIMessage msg) {
00030 #ifndef NDEBUG
00031     serial->printf("Sending MIDI message controller=%d channel=%d value=%d\r\n",
00032                    msg.controller(), msg.channel(), msg.value());
00033 #endif
00034     midi->write(msg);
00035 }
00036 
00037 void handle_sysex(MIDIMessage msg) {
00038     uint8_t sw;
00039 #ifndef NDEBUG
00040     serial->printf("MIDI recv: header=0x%02X status=0x%02X data1=0x%02X data2=%02d length=%d\r\n",
00041                    msg.data[0], msg.data[1], msg.data[2], msg.data[3], msg.length);
00042 #endif
00043 
00044     if (msg.data[1] == 0xF0 && msg.data[2] == 0x7D && msg.data[11] == 0xF7) {
00045         sw = msg.data[3];
00046         if (sw < NUM_SWITCHES) {
00047             switches[sw].type = msg.data[4] & 0xF;
00048             switches[sw].channel = msg.data[5] & 0xF;
00049             switches[sw].data1 = msg.data[6] & 0x7F;
00050             if (msg.data[7] > 0)
00051                 switches[sw].on_value = msg.data[8] & 0x7F;
00052             else
00053                 switches[sw].on_value = -1;
00054             if (msg.data[9] > 0)
00055                 switches[sw].off_value = msg.data[10] & 0x7F;
00056             else
00057                 switches[sw].off_value = -1;
00058             handlers[sw]->setConfig(switches[sw]);
00059 #ifndef NDEBUG
00060             serial->printf("Changed configuration for switch %d.\r\n", sw + 1);
00061             serial->printf("type: 0x%02X\r\n", switches[sw].type);
00062             serial->printf("channel: %02d\r\n", switches[sw].channel);
00063             serial->printf("data1: %02d\r\n", switches[sw].data1);
00064             serial->printf("on_value: %02d\r\n", switches[sw].on_value);
00065             serial->printf("off_value: %02d\r\n", switches[sw].off_value);
00066 #endif
00067         }
00068     }
00069 }
00070 
00071 int main() {
00072 #ifndef NDEBUG
00073     serial = new Serial(PA_9, PA_10);
00074     serial->printf("Creating event queue...\r\n");
00075 #endif
00076     EventQueue queue;
00077 
00078 #ifndef NDEBUG
00079     serial->printf("Creating USBMIDI device...\r\n");
00080 #endif
00081     midi = new USBMIDI(0x1f00, 0x2012, 0x0001);
00082 
00083 #ifndef NDEBUG
00084     serial->printf("Initializing LED...\r\n");
00085 #endif
00086     DigitalOut led1(LED1);
00087     led1 = 1;
00088 
00089 #ifndef NDEBUG
00090     serial->printf("Initializing event queue thread...\r\n");
00091 #endif
00092     // create a thread that'll run the event queue's dispatch function
00093     Thread usbThread;
00094 #ifndef NDEBUG
00095     serial->printf("Starting event queue thread...\r\n");
00096 #endif
00097     usbThread.start(callback(&queue, &EventQueue::dispatch_forever));
00098 
00099     for (int sw=0; sw < NUM_SWITCHES; sw++) {
00100 #ifndef NDEBUG
00101         serial->printf("Initializing switch handler %d...\r\n", sw + 1);
00102 #endif
00103         handlers[sw] = new SwitchHandler(&queue, &write_midi_msg, switches[sw]);
00104     }
00105 
00106     midi->attach(&handle_sysex);
00107 
00108 #ifndef NDEBUG
00109     serial->printf("Entering main loop...\r\n");
00110 #endif
00111     while (true) {
00112         wait(0.5f);
00113         led1 = !led1;
00114     }
00115 }