Skip to content

Commit

Permalink
SysCommon input support
Browse files Browse the repository at this point in the history
Make Channel and Cable printable
  • Loading branch information
tttapa committed Jun 26, 2021
1 parent c75e43f commit cf32e7e
Show file tree
Hide file tree
Showing 19 changed files with 439 additions and 23 deletions.
13 changes: 12 additions & 1 deletion examples/Other/MIDI-Input-Callback/MIDI-Input-Callback.ino
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ bool sysExMessageCallback(SysExMessage se) {
// return false.
}

bool sysCommonMessageCallback(SysCommonMessage sc) {
Serial << F("System Common message: ") << hex //
<< sc.header << ' ' << sc.data1 << ' ' << sc.data2 //
<< dec << F(" on cable ") << sc.cable.getOneBased() << endl;
return true; // Return true to indicate that handling is done,
// and Control_Surface shouldn't handle it anymore.
// If you want Control_Surface to handle it as well,
// return false.
}

bool realTimeMessageCallback(RealTimeMessage rt) {
Serial << F("Real-time message: ") //
<< hex << rt.message << dec //
Expand All @@ -63,8 +73,9 @@ void setup() {
Control_Surface.begin();
Control_Surface.setMIDIInputCallbacks(channelMessageCallback, //
sysExMessageCallback, //
sysCommonMessageCallback, //
realTimeMessageCallback); //
// If you don't need all three callbacks, you can pass `nullptr` instead of a
// If you don't need all four callbacks, you can pass `nullptr` instead of a
// function pointer
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ bool sysExMessageCallback(SysExMessage se) {
// return false.
}

bool sysCommonMessageCallback(SysCommonMessage sc) {
Serial << F("System Common message: ") << hex << sc.getMessageType();
if (sc.getNumberOfDataBytes() >= 1)
Serial << sc.getData1();
if (sc.getNumberOfDataBytes() >= 2)
Serial << sc.getData2();
Serial << dec << F(" on cable ") << sc.cable.getOneBased() << endl;
return false; // Return true to indicate that handling is done,
// and Control_Surface shouldn't handle it anymore.
// If you want Control_Surface to handle it as well,
// return false.
}

bool realTimeMessageCallback(RealTimeMessage rt) {
Serial << F("Real-Time: ") << hex << rt.message << dec << F(" on cable ")
<< rt.cable.getOneBased() << endl;
Expand All @@ -54,6 +67,7 @@ void setup() {
Control_Surface.begin();
Control_Surface.setMIDIInputCallbacks(channelMessageCallback, //
sysExMessageCallback, //
sysCommonMessageCallback, //
realTimeMessageCallback); //
}

Expand Down
15 changes: 15 additions & 0 deletions src/Control_Surface/Control_Surface_Class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ void Control_Surface_::sendChannelMessageImpl(ChannelMessage msg) {
void Control_Surface_::sendSysExImpl(SysExMessage msg) {
this->sourceMIDItoPipe(msg);
}
void Control_Surface_::sendSysCommonImpl(SysCommonMessage msg) {
this->sourceMIDItoPipe(msg);
}
void Control_Surface_::sendRealTimeImpl(RealTimeMessage msg) {
this->sourceMIDItoPipe(msg);
}
Expand Down Expand Up @@ -191,6 +194,18 @@ void Control_Surface_::sinkMIDIfromPipe(SysExMessage msg) {
MIDIInputElementSysEx::updateAllWith(msg);
}

void Control_Surface_::sinkMIDIfromPipe(SysCommonMessage msg) {
#ifdef DEBUG_MIDI_PACKETS
DEBUG_OUT << ">>> " << hex << msg.getMessageType() << ' ' << msg.getData1()
<< ' ' << msg.getData2() << " (" << msg.cable << ')' << dec
<< endl;
#endif
// If the SysEx Message callback exists, call it to see if we have to
// continue handling it.
if (sysCommonMessageCallback && sysCommonMessageCallback(msg))
return;
}

void Control_Surface_::sinkMIDIfromPipe(RealTimeMessage rtMessage) {
#ifdef DEBUG_MIDI_PACKETS
DEBUG(">>> " << hex << rtMessage.message << " ("
Expand Down
13 changes: 11 additions & 2 deletions src/Control_Surface/Control_Surface_Class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Control_Surface_ : public MIDI_Sender<Control_Surface_>,
/// Copying is not allowed
Control_Surface_ &operator=(Control_Surface_ const &) = delete;

/// Return the static Control_Surface_ instance (Control_Surface_ is a
/// Return the static Control_Surface_ instance (Control_Surface_ is a
/// singleton.)
static Control_Surface_ &getInstance();

Expand Down Expand Up @@ -74,17 +74,19 @@ class Control_Surface_ : public MIDI_Sender<Control_Surface_>,
/// Low-level function for sending a MIDI channel voice message.
void sendChannelMessageImpl(ChannelMessage);
/// Low-level function for sending a MIDI system common message.
void sendSysCommonImpl(SysCommonMessage) { /* TODO */ }
void sendSysCommonImpl(SysCommonMessage);
/// Low-level function for sending a system exclusive MIDI message.
void sendSysExImpl(SysExMessage);
/// Low-level function for sending a MIDI real-time message.
void sendRealTimeImpl(RealTimeMessage);
/// Low-level function for sending any buffered outgoing MIDI messages.
/// @todo Implement this in MIDI_Pipe
void sendNowImpl() { /* TODO */ }

private:
void sinkMIDIfromPipe(ChannelMessage msg) override;
void sinkMIDIfromPipe(SysExMessage msg) override;
void sinkMIDIfromPipe(SysCommonMessage msg) override;
void sinkMIDIfromPipe(RealTimeMessage msg) override;

private:
Expand All @@ -105,6 +107,10 @@ class Control_Surface_ : public MIDI_Sender<Control_Surface_>,
/// done in the user-provided callback, false if `Control_Surface`
/// should handle the message.
using SysExMessageCallback = bool (*)(SysExMessage);
/// Callback function type for System Common messages. Return true if
/// handling is done in the user-provided callback, false if
/// `Control_Surface` should handle the message.
using SysCommonMessageCallback = bool (*)(SysCommonMessage);
/// Callback function type for Real-Time messages. Return true if handling
/// is done in the user-provided callback, false if `Control_Surface`
/// should handle the message.
Expand All @@ -114,9 +120,11 @@ class Control_Surface_ : public MIDI_Sender<Control_Surface_>,
void
setMIDIInputCallbacks(ChannelMessageCallback channelMessageCallback,
SysExMessageCallback sysExMessageCallback,
SysCommonMessageCallback sysCommonMessageCallback,
RealTimeMessageCallback realTimeMessageCallback) {
this->channelMessageCallback = channelMessageCallback;
this->sysExMessageCallback = sysExMessageCallback;
this->sysCommonMessageCallback = sysCommonMessageCallback;
this->realTimeMessageCallback = realTimeMessageCallback;
}

Expand All @@ -125,6 +133,7 @@ class Control_Surface_ : public MIDI_Sender<Control_Surface_>,
private:
ChannelMessageCallback channelMessageCallback = nullptr;
SysExMessageCallback sysExMessageCallback = nullptr;
SysCommonMessageCallback sysCommonMessageCallback = nullptr;
RealTimeMessageCallback realTimeMessageCallback = nullptr;
MIDI_Pipe inpipe, outpipe;
};
Expand Down
10 changes: 10 additions & 0 deletions src/Def/Cable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "Cable.hpp"
#include <AH/PrintStream/PrintStream.hpp>

BEGIN_CS_NAMESPACE

Print &operator<<(Print &os, Cable c) {
return os << F("Cable ") << c.getOneBased();
}

END_CS_NAMESPACE
4 changes: 3 additions & 1 deletion src/Def/Cable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#pragma once

#include <AH/Arduino-Wrapper.h> // Print
#include <Settings/NamespaceSettings.hpp>
#include <stdint.h> // uint8_t

BEGIN_CS_NAMESPACE

Expand Down Expand Up @@ -132,4 +132,6 @@ constexpr Cable CABLE_14 = Cable::createCable(14);
constexpr Cable CABLE_15 = Cable::createCable(15);
constexpr Cable CABLE_16 = Cable::createCable(16);

Print &operator<<(Print &, Cable);

END_CS_NAMESPACE
11 changes: 9 additions & 2 deletions src/Def/Channel.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
#ifdef TEST_COMPILE_ALL_HEADERS_SEPARATELY
#include "Channel.hpp"
#endif
#include <AH/PrintStream/PrintStream.hpp>

BEGIN_CS_NAMESPACE

Print &operator<<(Print &os, Channel c) {
return os << F("Channel ") << c.getOneBased();
}

END_CS_NAMESPACE
4 changes: 3 additions & 1 deletion src/Def/Channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#pragma once

#include <AH/Arduino-Wrapper.h> // Print
#include <Settings/NamespaceSettings.hpp>
#include <stdint.h> // uint8_t

BEGIN_CS_NAMESPACE

Expand Down Expand Up @@ -132,4 +132,6 @@ constexpr Channel CHANNEL_14 = Channel::createChannel(14);
constexpr Channel CHANNEL_15 = Channel::createChannel(15);
constexpr Channel CHANNEL_16 = Channel::createChannel(16);

Print &operator<<(Print &, Channel);

END_CS_NAMESPACE
4 changes: 4 additions & 0 deletions src/MIDI_Interfaces/BluetoothMIDI_Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ void BluetoothMIDI_Interface::sendSysExImpl(SysExMessage msg) {
cv.notify_one();
}

void BluetoothMIDI_Interface::sendSysCommonImpl(SysCommonMessage) {
// TODO
}

// -------------------------------------------------------------------------- //

void BluetoothMIDI_Interface::parse(const uint8_t *const data,
Expand Down
3 changes: 1 addition & 2 deletions src/MIDI_Interfaces/BluetoothMIDI_Interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ class BluetoothMIDI_Interface : public MIDI_Interface {
protected:
// MIDI send implementations
void sendChannelMessageImpl(ChannelMessage) override;
void sendSysCommonImpl(SysCommonMessage) override { /* TODO */
}
void sendSysCommonImpl(SysCommonMessage) override;
void sendSysExImpl(SysExMessage) override;
void sendRealTimeImpl(RealTimeMessage) override;
void sendNowImpl() override { flush(); }
Expand Down
Loading

0 comments on commit cf32e7e

Please sign in to comment.