Simple USB-MIDI foot controller

Dependencies:   PinDetect USBDevice_STM32F103 mbed-STM32F103C8T6

Revision:
3:8b8cb5392fa0
Parent:
1:61415f07477d
Child:
4:91f901ade703
--- a/main.cpp	Mon Jul 31 05:10:40 2017 +0000
+++ b/main.cpp	Fri Aug 04 03:15:52 2017 +0200
@@ -1,74 +1,94 @@
 #include "stm32f103c8t6.h"
 #include "mbed.h"
+#include "mbed_events.h"
 #include "USBMIDI.h"
-#include "PinDetect.h"
-
-#define SWITCH1_PIN PB_5
-#define LED_DURATION 20000
+#include "midiswitch.h"
 
-USBMIDI *midi;
-Serial *serial;
-DigitalOut *led1;
-int led_ontime;
-Timer *timer;
 
-class SwitchHandler {
-public:
-    explicit SwitchHandler(uint8_t ctrl, uint8_t ch = 1, uint8_t on = 0, uint8_t off = 127) :
-        control(ctrl), channel(ch), on_value(on), off_value(off), b_pressed(false), b_released(false) {
-    };
-    
-    void handle_pressed(void) {
-        b_pressed = true;
-    }
+/* ******************** Configuration ***************************** */
+
+#define NUM_SWITCHES 2
 
-    void handle_released(void) {
-        b_released = true;
-    }
+/* 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 ************************* */
 
-    void handle_events() {
-        if (b_pressed) {
-            led1->write(0);
-            led_ontime = timer->read_us();
-            serial->printf("Switch pressed: controller=%d channel=%d value=%d\r\n", control, channel, on_value);
-            midi->write(MIDIMessage::ControlChange((int) control, (int) on_value, (int) (channel - 1)));
-            b_pressed = false;
-        } else if (b_released) {
-            led1->write(0);
-            led_ontime = timer->read_us();
-            serial->printf("Switch released: control=%d channel=%d value=%d\r\n", control, channel, off_value);
-            midi->write(MIDIMessage::ControlChange((int) control, (int) off_value, (int) (channel - 1)));
-            b_released = false;
-        }
-    }
-private:
-    uint8_t control, channel, on_value, off_value;
-    bool b_pressed, b_released;
-};
+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();
 
-    timer = new Timer;
-    led1 = new DigitalOut(LED1);
-    led1->write(1);
+#ifndef NDEBUG
     serial = new Serial(PA_9, PA_10);
-    midi = new USBMIDI(0x1f00, 0x2012, 0x0001);
-    SwitchHandler handler1(1, 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;
 
-    PinDetect btn(SWITCH1_PIN);
-    btn.attach_asserted(&handler1, &SwitchHandler::handle_pressed);
-    btn.attach_deasserted(&handler1, &SwitchHandler::handle_released);
-    btn.setSampleFrequency();
-    timer->start();
+#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) {
-        handler1.handle_events();
-        wait_ms(10);
-        if (timer->read_us() - led_ontime >= LED_DURATION) {
-            led_ontime = 0;
-            led1->write(1);
-        }   
-   }
+        wait(0.5f);
+        led1 = !led1;
+    }
 }