Simple USB-MIDI foot controller

Dependencies:   PinDetect USBDevice_STM32F103 mbed-STM32F103C8T6

main.cpp

Committer:
SpotlightKid
Date:
2017-08-04
Revision:
3:8b8cb5392fa0
Parent:
1:61415f07477d
Child:
4:91f901ade703

File content as of revision 3:8b8cb5392fa0:

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


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

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


void write_midi_msg(MIDIMessage msg) {
    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
    // create an event queue
    EventQueue queue(8*EVENTS_EVENT_SIZE);

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

    for (int i=0; i<5; i++) {
        led1 = !led1;
        wait(0.5f);
    }
    wait(2.0f);
    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(osPriorityLow);
#ifndef NDEBUG
    serial->printf("Starting event queue thread...\r\n");
#endif
    usbThread.start(callback(&queue, &EventQueue::dispatch_forever));

    for (int i=0; i<5; i++) {
        led1 = !led1;
        wait(0.5f);
    }
    led1 = 1;

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

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

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