Simple USB-MIDI foot controller

Dependencies:   PinDetect USBDevice_STM32F103 mbed-STM32F103C8T6

main.cpp

Committer:
SpotlightKid
Date:
2017-08-04
Revision:
7:553836a26221
Parent:
6:2f804d29cbb0
Child:
8:75c5ec68765e

File content as of revision 7:553836a26221:

#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, 1, 1, 127, 0},
    {PB_5, 1, 64, 64, 0},
};


/* ******************** 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);
}

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]);
    }

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