Chris Arndt / Mbed OS STM32F103_USBMIDI_Switchbox

Dependencies:   PinDetect USBDevice_STM32F103 mbed-STM32F103C8T6

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers midiswitch.cpp Source File

midiswitch.cpp

00001 #include "mbed.h"
00002 #include "midiswitch.h"
00003 
00004 SwitchHandler::SwitchHandler(EventQueue * queue, MIDI_CB cb, SwitchConfig sw) :
00005     queue(queue), write_cb(cb), btn(sw.pin), cfg(sw) {
00006     btn.mode(PullUp);
00007     btn.setAssertValue(0);
00008     btn.attach_asserted(this, &SwitchHandler::handle_pressed);
00009     btn.attach_deasserted(this, &SwitchHandler::handle_released);
00010     btn.setSampleFrequency();
00011 };
00012 
00013 void SwitchHandler::handle_pressed(void) {
00014     if (cfg.on_value >= 0) {
00015         if (make_message(true)) {
00016             queue->call(write_cb, msg);
00017         }
00018     }
00019 };
00020 
00021 void SwitchHandler::handle_released(void) {
00022     if (cfg.off_value >= 0) {
00023         if (make_message(false)) {
00024             queue->call(write_cb, msg);
00025         }
00026     }
00027 };
00028 
00029 bool SwitchHandler::make_message(bool onoff) {
00030     uint8_t value = (onoff ? cfg.on_value : cfg.off_value) & 0x7F;
00031     msg.data[0] = CABLE_NUM | cfg.type;
00032     msg.data[1] = (cfg.type << 4) | (cfg.channel & 0xF);
00033     msg.data[2] = 0x00;
00034     msg.data[3] = 0x00;
00035 
00036     switch(cfg.type) {
00037         case 2:  // START
00038         case 3:  // CONTINUE
00039         case 4:  // STOP
00040             msg.data[0] = 0xF;
00041             msg.data[1] = 0xF8 + cfg.type;
00042             break;
00043         case 0xC:  // Program change
00044         case 0xD:  // Channel Aftertouch
00045             msg.data[2] = value;
00046             break;
00047         case 0x8:  // Note off
00048         case 0x9:  // Note on
00049             msg.data[3] = value;
00050             msg.data[2] = cfg.data1 & 0x7F;
00051             break;
00052         case 0xA:  // Poly pressure
00053         case 0xB:  // Control change
00054         case 0xE:  // PitchBend
00055             msg.data[2] = cfg.data1 & 0x7F;
00056             msg.data[3] = value;
00057             break;
00058         default:
00059             return false;
00060     }
00061     return true;
00062 };