Simple USB-MIDI foot controller

Dependencies:   PinDetect USBDevice_STM32F103 mbed-STM32F103C8T6

main.cpp

Committer:
SpotlightKid
Date:
2017-08-04
Revision:
9:d5fa853818dd
Parent:
8:75c5ec68765e
Child:
10:f9a48eedfd59

File content as of revision 9:d5fa853818dd:

#include "stm32f103c8t6.h"
#include "mbed.h"
#include "mbed_events.h"
#include "USBMIDI.h"
#include "midiswitch.h"


/* ******************** Configuration ***************************** */

#define NUM_SWITCHES 2

/* array of SwitchConfig structs */
SwitchConfig switches[NUM_SWITCHES] = {
    // pin, channel, control, on_value, off_value
    {PB_4, 0xB0, 0, 1, 127, 0},
    {PB_5, 0xC0, 0, 0, 1, -1},
};


/* ******************** End Configuration ************************* */

#ifndef NDEBUG
Serial *serial;
#endif
USBMIDI * midi;
/* array of pointers to SwitchHandler instances */
SwitchHandler * handlers[NUM_SWITCHES];


void write_midi_msg(MIDIMessage msg) {
#ifndef NDEBUG
    serial->printf("Sending MIDI message controller=%d channel=%d value=%d\r\n",
                   msg.controller(), msg.channel(), msg.value());
#endif
    midi->write(msg);
}

void handle_sysex(MIDIMessage msg) {
    uint8_t sw;

    if (msg.data[1] == 0xF0 && msg.data[2] == 0x7D && msg.data[9] == 0xF7) {
        sw = msg.data[3];
        if (sw < NUM_SWITCHES) {
            switches[sw].type = msg.data[4];
            switches[sw].channel = msg.data[5];
            switches[sw].data1 = msg.data[6];
            switches[sw].on_value = msg.data[7];
            switches[sw].off_value = msg.data[8];
            handlers[sw]->setConfig(switches[sw]);
#ifndef NDEBUG
            serial->printf("Changed configuration for switch %s.\r\n", sw);
#endif
        }
    }
}

int main() {
    // Configure system clock (72MHz HSE clock, 48MHz USB clock)
    confSysClock();

#ifndef NDEBUG
    serial = new Serial(PA_9, PA_10);
    serial->printf("Creating event queue...\r\n");
#endif
    EventQueue queue;

#ifndef NDEBUG
    serial->printf("Creating USBMIDI device...\r\n");
#endif
    midi = new USBMIDI(0x1f00, 0x2012, 0x0001);

#ifndef NDEBUG
    serial->printf("Initializing LED...\r\n");
#endif
    DigitalOut led1(LED1);
    led1 = 1;

#ifndef NDEBUG
    serial->printf("Initializing event queue thread...\r\n");
#endif
    // create a thread that'll run the event queue's dispatch function
    Thread usbThread;
#ifndef NDEBUG
    serial->printf("Starting event queue thread...\r\n");
#endif
    usbThread.start(callback(&queue, &EventQueue::dispatch_forever));

    for (int sw=0; sw < NUM_SWITCHES; sw++) {
#ifndef NDEBUG
        serial->printf("Initializing switch handler %d...\r\n", sw + 1);
#endif
        handlers[sw] = new SwitchHandler(&queue, &write_midi_msg, switches[sw]);
    }

    midi->attach(&handle_sysex);

#ifndef NDEBUG
    serial->printf("Entering main loop...\r\n");
#endif
    while (true) {
        wait(0.5f);
        led1 = !led1;
    }
}