Chris Arndt / Mbed OS STM32F103_USBMIDI_Switchbox

Dependencies:   PinDetect USBDevice_STM32F103 mbed-STM32F103C8T6

Files at this revision

API Documentation at this revision

Comitter:
SpotlightKid
Date:
Fri Aug 04 05:22:22 2017 +0200
Parent:
7:553836a26221
Child:
9:d5fa853818dd
Commit message:
Make MIDI message type configurable

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
midiswitch.cpp Show annotated file Show diff for this revision Revisions of this file
midiswitch.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri Aug 04 04:20:37 2017 +0200
+++ b/main.cpp	Fri Aug 04 05:22:22 2017 +0200
@@ -12,8 +12,8 @@
 /* array of SwitchConfig structs */
 SwitchConfig switches[NUM_SWITCHES] = {
     // pin, channel, control, on_value, off_value
-    {PB_4, 1, 1, 127, 0},
-    {PB_5, 1, 64, 64, 0},
+    {PB_4, 0xB0, 1, 1, 127, 0},
+    {PB_5, 0xC0, 1, 0, 1, -1},
 };
 
 
@@ -70,7 +70,7 @@
 #ifndef NDEBUG
         serial->printf("Initializing switch handler %d...\r\n", sw + 1);
 #endif
-        handlers[sw] = new SwitchHandler(&queue, &write_midi_msg, &switches[sw]);
+        handlers[sw] = new SwitchHandler(&queue, &write_midi_msg, switches[sw]);
     }
 
 #ifndef NDEBUG
--- a/midiswitch.cpp	Fri Aug 04 04:20:37 2017 +0200
+++ b/midiswitch.cpp	Fri Aug 04 05:22:22 2017 +0200
@@ -1,9 +1,9 @@
 #include "mbed.h"
 #include "midiswitch.h"
 
-SwitchHandler::SwitchHandler(EventQueue * queue, MIDI_CB cb, SwitchConfig * sw) :
-    queue(queue), write_cb(cb), btn(sw->pin), control(sw->control), channel(sw->channel), on_value(sw->on_value), off_value(sw->off_value) {
-    btn.mode(Pullup);
+SwitchHandler::SwitchHandler(EventQueue * queue, MIDI_CB cb, SwitchConfig sw) :
+    queue(queue), write_cb(cb), btn(sw.pin), cfg(sw) {
+    btn.mode(PullUp);
     btn.setAssertValue(0);
     btn.attach_asserted(this, &SwitchHandler::handle_pressed);
     btn.attach_deasserted(this, &SwitchHandler::handle_released);
@@ -11,9 +11,54 @@
 };
 
 void SwitchHandler::handle_pressed(void) {
-    queue->call(write_cb, MIDIMessage::ControlChange((int) control, (int) on_value, (int) (channel - 1)));
+    if (cfg.on_value >= 0) {
+        if (make_message(true)) {
+            queue->call(write_cb, msg);
+        }
+    }
 };
 
 void SwitchHandler::handle_released(void) {
-    queue->call(write_cb, MIDIMessage::ControlChange((int) control, (int) off_value, (int) (channel - 1)));
+    if (cfg.off_value >= 0) {
+        if (make_message(false)) {
+            queue->call(write_cb, msg);
+        }
+    }
 };
+
+bool SwitchHandler::make_message(bool onoff) {
+    uint8_t value = (onoff ? cfg.on_value : cfg.off_value) & 0x7F;
+    msg.data[0] = CABLE_NUM | cfg.type;
+    msg.data[1] = cfg.type;
+
+    switch(cfg.type) {
+        case 0xFA:  // START
+        case 0xFB:  // CONTINUE
+        case 0xFC:  // STOP
+            msg.data[2] = 0x00;
+            msg.data[3] = 0x00;
+            break;
+        case 0xC0:  // Program change
+        case 0xD0:  // Channel Aftertouch
+            msg.data[1] = msg.data[1] | (cfg.channel & 0x0F);
+            msg.data[2] = value;
+            msg.data[3] = 0x00;
+            break;
+        case 0x80:  // Note off
+        case 0x90:  // Note on
+            msg.data[1] = msg.data[1] | (cfg.channel & 0x0F);
+            msg.data[3] = value;
+            msg.data[2] = cfg.data1;
+            break;
+        case 0xA0:  // Poly pressure
+        case 0xB0:  // Control change
+        case 0xE0:  // PitchBend
+            msg.data[1] = msg.data[1] | (cfg.channel & 0x0F);
+            msg.data[2] = cfg.data1;
+            msg.data[3] = value;
+            break;
+        default:
+            return false;
+    }
+    return true;
+};
--- a/midiswitch.h	Fri Aug 04 04:20:37 2017 +0200
+++ b/midiswitch.h	Fri Aug 04 05:22:22 2017 +0200
@@ -9,24 +9,27 @@
 
 typedef struct SwitchConfig {
     PinName pin;
+    uint8_t type;
     uint8_t channel;
-    uint8_t control;
-    uint8_t on_value;
-    uint8_t off_value;
+    uint8_t data1;
+    int8_t on_value;
+    int8_t off_value;
 } SwitchConfig;
 
 typedef void (*MIDI_CB)(MIDIMessage);
 
 class SwitchHandler {
 public:
-    SwitchHandler(EventQueue * queue, MIDI_CB cb, SwitchConfig * sw);
+    SwitchHandler(EventQueue * queue, MIDI_CB cb, SwitchConfig sw);
     void handle_pressed(void);
     void handle_released(void);
 private:
+    bool make_message(bool onoff);
     EventQueue * queue;
     MIDI_CB write_cb;
     PinDetect btn;
-    uint8_t control, channel, on_value, off_value;
+    SwitchConfig cfg;
+    MIDIMessage msg;
 };
 
 #endif  /* MIDISWITCH_H */