Simple USB-MIDI foot controller

Dependencies:   PinDetect USBDevice_STM32F103 mbed-STM32F103C8T6

Committer:
SpotlightKid
Date:
Fri Aug 04 03:15:52 2017 +0200
Revision:
3:8b8cb5392fa0
Parent:
1:61415f07477d
Child:
4:91f901ade703
New event queue based version

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 3:8b8cb5392fa0 42 // create an event queue
SpotlightKid 3:8b8cb5392fa0 43 EventQueue queue(8*EVENTS_EVENT_SIZE);
SpotlightKid 3:8b8cb5392fa0 44
SpotlightKid 3:8b8cb5392fa0 45 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 46 serial->printf("Initializing LED...\r\n");
SpotlightKid 3:8b8cb5392fa0 47 #endif
SpotlightKid 3:8b8cb5392fa0 48 DigitalOut led1(LED1);
SpotlightKid 3:8b8cb5392fa0 49 led1 = 1;
SpotlightKid 3:8b8cb5392fa0 50
SpotlightKid 3:8b8cb5392fa0 51 for (int i=0; i<5; i++) {
SpotlightKid 3:8b8cb5392fa0 52 led1 = !led1;
SpotlightKid 3:8b8cb5392fa0 53 wait(0.5f);
SpotlightKid 3:8b8cb5392fa0 54 }
SpotlightKid 3:8b8cb5392fa0 55 wait(2.0f);
SpotlightKid 3:8b8cb5392fa0 56 led1 = 1;
SpotlightKid 0:2f530d7169a6 57
SpotlightKid 3:8b8cb5392fa0 58 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 59 serial->printf("Initializing event queue thread...\r\n");
SpotlightKid 3:8b8cb5392fa0 60 #endif
SpotlightKid 3:8b8cb5392fa0 61 // create a thread that'll run the event queue's dispatch function
SpotlightKid 3:8b8cb5392fa0 62 Thread usbThread(osPriorityLow);
SpotlightKid 3:8b8cb5392fa0 63 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 64 serial->printf("Starting event queue thread...\r\n");
SpotlightKid 3:8b8cb5392fa0 65 #endif
SpotlightKid 3:8b8cb5392fa0 66 usbThread.start(callback(&queue, &EventQueue::dispatch_forever));
SpotlightKid 3:8b8cb5392fa0 67
SpotlightKid 3:8b8cb5392fa0 68 for (int i=0; i<5; i++) {
SpotlightKid 3:8b8cb5392fa0 69 led1 = !led1;
SpotlightKid 3:8b8cb5392fa0 70 wait(0.5f);
SpotlightKid 3:8b8cb5392fa0 71 }
SpotlightKid 3:8b8cb5392fa0 72 led1 = 1;
SpotlightKid 0:2f530d7169a6 73
SpotlightKid 3:8b8cb5392fa0 74 for (int sw=0; sw < NUM_SWITCHES; sw++) {
SpotlightKid 3:8b8cb5392fa0 75 handlers[sw] = new SwitchHandler(&queue, &write_midi_msg, &switches[sw]);
SpotlightKid 3:8b8cb5392fa0 76 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 77 serial->printf("Initializing switch handler %d...\r\n", sw + 1);
SpotlightKid 3:8b8cb5392fa0 78 handlers[sw]->setSerial(serial);
SpotlightKid 3:8b8cb5392fa0 79 #endif
SpotlightKid 3:8b8cb5392fa0 80 }
SpotlightKid 3:8b8cb5392fa0 81
SpotlightKid 3:8b8cb5392fa0 82 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 83 serial->printf("Creating USBMIDI device...\r\n");
SpotlightKid 3:8b8cb5392fa0 84 #endif
SpotlightKid 3:8b8cb5392fa0 85 midi = new USBMIDI(0x1f00, 0x2012, 0x0001);
SpotlightKid 3:8b8cb5392fa0 86
SpotlightKid 3:8b8cb5392fa0 87 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 88 serial->printf("Entering main loop...\r\n");
SpotlightKid 3:8b8cb5392fa0 89 #endif
SpotlightKid 0:2f530d7169a6 90 while (true) {
SpotlightKid 3:8b8cb5392fa0 91 wait(0.5f);
SpotlightKid 3:8b8cb5392fa0 92 led1 = !led1;
SpotlightKid 3:8b8cb5392fa0 93 }
SpotlightKid 0:2f530d7169a6 94 }