Chris Arndt / Mbed OS STM32F103_USBMIDI_Switchbox

Dependencies:   PinDetect USBDevice_STM32F103 mbed-STM32F103C8T6

Revision:
3:8b8cb5392fa0
Child:
6:2f804d29cbb0
diff -r 4ef26e58fa0d -r 8b8cb5392fa0 midiswitch.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/midiswitch.cpp	Fri Aug 04 03:15:52 2017 +0200
@@ -0,0 +1,25 @@
+#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.attach_asserted(this, &SwitchHandler::handle_pressed);
+    btn.attach_deasserted(this, &SwitchHandler::handle_released);
+    btn.setSampleFrequency();
+#ifndef NDEBUG
+    serial = NULL;
+#endif
+};
+
+void SwitchHandler::handle_pressed(void) {
+#ifndef NDEBUG
+    serial->printf("Switch pressed: control=%d channel=%d value=%d\r\n", control, channel, on_value);
+#endif
+    queue->call(write_cb, MIDIMessage::ControlChange((int) control, (int) on_value, (int) (channel - 1)));
+};
+
+void SwitchHandler::handle_released(void) {
+#ifndef NDEBUG
+    serial->printf("Switch released: control=%d channel=%d value=%d\r\n", control, channel, off_value);
+#endif
+    queue->call(write_cb, MIDIMessage::ControlChange((int) control, (int) off_value, (int) (channel - 1)));
+};