Simple USB-MIDI foot controller

Dependencies:   PinDetect USBDevice_STM32F103 mbed-STM32F103C8T6

Committer:
SpotlightKid
Date:
Fri Aug 04 03:29:27 2017 +0200
Revision:
5:1208f33a9a7c
Parent:
4:91f901ade703
Child:
6:2f804d29cbb0
Use standard queue size and thread priority; initialize USB first

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 3:8b8cb5392fa0 15 {PB_4, 1, 1, 127, 0},
SpotlightKid 3:8b8cb5392fa0 16 {PB_5, 1, 1, 64, 0},
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 USBMIDI * midi;
SpotlightKid 3:8b8cb5392fa0 23 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 24 Serial *serial;
SpotlightKid 3:8b8cb5392fa0 25 #endif
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 3:8b8cb5392fa0 31 midi->write(msg);
SpotlightKid 3:8b8cb5392fa0 32 }
SpotlightKid 0:2f530d7169a6 33
SpotlightKid 0:2f530d7169a6 34 int main() {
SpotlightKid 0:2f530d7169a6 35 // Configure system clock (72MHz HSE clock, 48MHz USB clock)
SpotlightKid 0:2f530d7169a6 36 confSysClock();
SpotlightKid 0:2f530d7169a6 37
SpotlightKid 3:8b8cb5392fa0 38 #ifndef NDEBUG
SpotlightKid 0:2f530d7169a6 39 serial = new Serial(PA_9, PA_10);
SpotlightKid 3:8b8cb5392fa0 40 serial->printf("Creating event queue...\r\n");
SpotlightKid 3:8b8cb5392fa0 41 #endif
SpotlightKid 5:1208f33a9a7c 42 EventQueue queue;
SpotlightKid 5:1208f33a9a7c 43
SpotlightKid 5:1208f33a9a7c 44 #ifndef NDEBUG
SpotlightKid 5:1208f33a9a7c 45 serial->printf("Creating USBMIDI device...\r\n");
SpotlightKid 5:1208f33a9a7c 46 #endif
SpotlightKid 5:1208f33a9a7c 47 midi = new USBMIDI(0x1f00, 0x2012, 0x0001);
SpotlightKid 3:8b8cb5392fa0 48
SpotlightKid 3:8b8cb5392fa0 49 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 50 serial->printf("Initializing LED...\r\n");
SpotlightKid 3:8b8cb5392fa0 51 #endif
SpotlightKid 3:8b8cb5392fa0 52 DigitalOut led1(LED1);
SpotlightKid 3:8b8cb5392fa0 53 led1 = 1;
SpotlightKid 3:8b8cb5392fa0 54
SpotlightKid 3:8b8cb5392fa0 55 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 56 serial->printf("Initializing event queue thread...\r\n");
SpotlightKid 3:8b8cb5392fa0 57 #endif
SpotlightKid 3:8b8cb5392fa0 58 // create a thread that'll run the event queue's dispatch function
SpotlightKid 5:1208f33a9a7c 59 Thread usbThread;
SpotlightKid 3:8b8cb5392fa0 60 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 61 serial->printf("Starting event queue thread...\r\n");
SpotlightKid 3:8b8cb5392fa0 62 #endif
SpotlightKid 3:8b8cb5392fa0 63 usbThread.start(callback(&queue, &EventQueue::dispatch_forever));
SpotlightKid 3:8b8cb5392fa0 64
SpotlightKid 3:8b8cb5392fa0 65 for (int sw=0; sw < NUM_SWITCHES; sw++) {
SpotlightKid 3:8b8cb5392fa0 66 handlers[sw] = new SwitchHandler(&queue, &write_midi_msg, &switches[sw]);
SpotlightKid 3:8b8cb5392fa0 67 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 68 serial->printf("Initializing switch handler %d...\r\n", sw + 1);
SpotlightKid 3:8b8cb5392fa0 69 handlers[sw]->setSerial(serial);
SpotlightKid 3:8b8cb5392fa0 70 #endif
SpotlightKid 3:8b8cb5392fa0 71 }
SpotlightKid 3:8b8cb5392fa0 72
SpotlightKid 3:8b8cb5392fa0 73 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 74 serial->printf("Entering main loop...\r\n");
SpotlightKid 3:8b8cb5392fa0 75 #endif
SpotlightKid 0:2f530d7169a6 76 while (true) {
SpotlightKid 3:8b8cb5392fa0 77 wait(0.5f);
SpotlightKid 3:8b8cb5392fa0 78 led1 = !led1;
SpotlightKid 3:8b8cb5392fa0 79 }
SpotlightKid 0:2f530d7169a6 80 }