Simple USB-MIDI foot controller

Dependencies:   PinDetect USBDevice_STM32F103 mbed-STM32F103C8T6

Committer:
SpotlightKid
Date:
Fri Aug 04 03:20:54 2017 +0200
Revision:
4:91f901ade703
Parent:
3:8b8cb5392fa0
Child:
5:1208f33a9a7c
Remove led blinking on startup

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 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 52 serial->printf("Initializing event queue thread...\r\n");
SpotlightKid 3:8b8cb5392fa0 53 #endif
SpotlightKid 3:8b8cb5392fa0 54 // create a thread that'll run the event queue's dispatch function
SpotlightKid 3:8b8cb5392fa0 55 Thread usbThread(osPriorityLow);
SpotlightKid 3:8b8cb5392fa0 56 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 57 serial->printf("Starting event queue thread...\r\n");
SpotlightKid 3:8b8cb5392fa0 58 #endif
SpotlightKid 3:8b8cb5392fa0 59 usbThread.start(callback(&queue, &EventQueue::dispatch_forever));
SpotlightKid 3:8b8cb5392fa0 60
SpotlightKid 3:8b8cb5392fa0 61 for (int sw=0; sw < NUM_SWITCHES; sw++) {
SpotlightKid 3:8b8cb5392fa0 62 handlers[sw] = new SwitchHandler(&queue, &write_midi_msg, &switches[sw]);
SpotlightKid 3:8b8cb5392fa0 63 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 64 serial->printf("Initializing switch handler %d...\r\n", sw + 1);
SpotlightKid 3:8b8cb5392fa0 65 handlers[sw]->setSerial(serial);
SpotlightKid 3:8b8cb5392fa0 66 #endif
SpotlightKid 3:8b8cb5392fa0 67 }
SpotlightKid 3:8b8cb5392fa0 68
SpotlightKid 3:8b8cb5392fa0 69 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 70 serial->printf("Creating USBMIDI device...\r\n");
SpotlightKid 3:8b8cb5392fa0 71 #endif
SpotlightKid 3:8b8cb5392fa0 72 midi = new USBMIDI(0x1f00, 0x2012, 0x0001);
SpotlightKid 3:8b8cb5392fa0 73
SpotlightKid 3:8b8cb5392fa0 74 #ifndef NDEBUG
SpotlightKid 3:8b8cb5392fa0 75 serial->printf("Entering main loop...\r\n");
SpotlightKid 3:8b8cb5392fa0 76 #endif
SpotlightKid 0:2f530d7169a6 77 while (true) {
SpotlightKid 3:8b8cb5392fa0 78 wait(0.5f);
SpotlightKid 3:8b8cb5392fa0 79 led1 = !led1;
SpotlightKid 3:8b8cb5392fa0 80 }
SpotlightKid 0:2f530d7169a6 81 }