Control Surface  1.2.0
MIDI Control Surface library for Arduino
MIDI_Interface.cpp
Go to the documentation of this file.
1 #include "MIDI_Interface.hpp"
2 
4 
6  if (getDefault() == this)
7  DefaultMIDI_Interface = nullptr;
8 }
9 
11 
13 
15 #ifndef ARDUINO
16  return DefaultMIDI_Interface == nullptr
17  ? dynamic_cast<MIDI_Interface *>(updatables.getLast())
19 #else
20  return DefaultMIDI_Interface == nullptr
21  ? static_cast<MIDI_Interface *>(updatables.getLast())
23 #endif
24 }
25 
26 // -------------------------------- SENDING --------------------------------- //
27 
31 
32 // -------------------------------- READING --------------------------------- //
33 
35  if (event == MIDIReadEvent::NO_MESSAGE) // If previous event was handled
36  event = read(); // Read the next incoming message
37  while (event != MIDIReadEvent::NO_MESSAGE) { // As long as there are
38  // incoming messages
39  if (dispatchMIDIEvent(event)) // If event was handled successfully
40  event = read(); // Read the next incoming message
41  else // If pipe is locked
42  break; // Try sending again next time
43  }
44  // TODO: maximum number of iterations? Timeout?
45 }
46 
48  switch (event) {
49  case MIDIReadEvent::NO_MESSAGE: return true;
50  case MIDIReadEvent::CHANNEL_MESSAGE: return onChannelMessage();
51  case MIDIReadEvent::SYSEX_MESSAGE: return onSysExMessage();
53  default: return true;
54  }
55 }
56 
58  // Always send write to pipe, don't check if it's in exclusive mode or not
59  auto message = getRealTimeMessage();
60  sourceMIDItoPipe(message);
61  if (callbacks)
63  return true;
64 }
65 
67  auto message = getChannelMessage();
68  if (!canWrite(message.CN))
69  return false;
70  sourceMIDItoPipe(message);
71  if (callbacks)
73  // TODO: we have the message already, should we just pass it to the
74  // callback as a parameter? (idem for SysEx and RT)
75  return true;
76 }
77 
79  auto message = getSysExMessage();
80  if (!canWrite(message.CN))
81  return false;
82  sourceMIDItoPipe(message);
83  if (callbacks)
84  callbacks->onSysExMessage(*this);
85  return true;
86 }
87 
MIDI_Interface::DefaultMIDI_Interface
static MIDI_Interface * DefaultMIDI_Interface
Definition: MIDI_Interface.hpp:226
MIDI_Interface::getDefault
static MIDI_Interface * getDefault()
Return the default MIDI interface.
Definition: MIDI_Interface.cpp:14
SysExMessage
Definition: MIDI_MessageTypes.hpp:138
Parsing_MIDI_Interface::getSysExMessage
SysExMessage getSysExMessage() const
Return the received system exclusive message.
Definition: MIDI_Interface.hpp:261
Parsing_MIDI_Interface::onChannelMessage
bool onChannelMessage()
Definition: MIDI_Interface.cpp:66
MIDI_Callbacks::onRealTimeMessage
virtual void onRealTimeMessage(Parsing_MIDI_Interface &midi)
Callback for incoming MIDI Real-Time Messages.
Definition: MIDI_Interface.hpp:303
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
MIDI_Interface::sinkMIDIfromPipe
void sinkMIDIfromPipe(ChannelMessage) override
Accept an incoming MIDI Channel message.
Definition: MIDI_Interface.cpp:28
MIDI_Callbacks::onSysExMessage
virtual void onSysExMessage(Parsing_MIDI_Interface &midi)
Callback for incoming MIDI System Exclusive Messages.
Definition: MIDI_Interface.hpp:301
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
DoublyLinkedList::getLast
Node * getLast() const
Get a pointer to the last node.
Definition: LinkedList.hpp:306
Parsing_MIDI_Interface::update
void update() override
Read the MIDI interface and call the callback if a message is received.
Definition: MIDI_Interface.cpp:34
MIDI_Sender< MIDI_Interface >::send
void send(MIDIMessageType m, Channel c, uint8_t d1, uint8_t d2, Cable cable=CABLE_1)
Send a 3-byte MIDI packet.
Definition: MIDI_Interface.ipp:7
MIDIReadEvent::NO_MESSAGE
@ NO_MESSAGE
No new incoming methods.
Parsing_MIDI_Interface::event
MIDIReadEvent event
Definition: MIDI_Interface.hpp:288
MIDI_Callbacks::onChannelMessage
virtual void onChannelMessage(Parsing_MIDI_Interface &midi)
Callback for incoming MIDI Channel Messages (notes, control change, pitch bend, etc....
Definition: MIDI_Interface.hpp:299
MIDI_Source::sourceMIDItoPipe
void sourceMIDItoPipe(ChannelMessage)
Send a MIDI Channel Message.
Definition: MIDI_Pipes.cpp:132
RealTimeMessage
Definition: MIDI_MessageTypes.hpp:173
Parsing_MIDI_Interface::callbacks
MIDI_Callbacks * callbacks
Definition: MIDI_Interface.hpp:287
Parsing_MIDI_Interface::onRealTimeMessage
bool onRealTimeMessage()
Definition: MIDI_Interface.cpp:57
Parsing_MIDI_Interface::dispatchMIDIEvent
bool dispatchMIDIEvent(MIDIReadEvent event)
Definition: MIDI_Interface.cpp:47
MIDI_Source::canWrite
bool canWrite(cn_t cn) const
Check if this source can write to the sinks it connects to.
Definition: MIDI_Pipes.cpp:128
Parsing_MIDI_Interface::onSysExMessage
bool onSysExMessage()
Definition: MIDI_Interface.cpp:78
MIDI_Interface::setAsDefault
void setAsDefault()
Set this MIDI interface as the default interface.
Definition: MIDI_Interface.cpp:12
ChannelMessage
Definition: MIDI_MessageTypes.hpp:72
MIDIReadEvent
MIDIReadEvent
Result of the MIDI interface read methods.
Definition: MIDI_Parser.hpp:15
MIDI_Interface
An abstract class for MIDI interfaces.
Definition: MIDI_Interface.hpp:134
Parsing_MIDI_Interface::read
virtual MIDIReadEvent read()=0
Try reading and parsing a single incoming MIDI message.
MIDI_Interface.hpp
MIDI_Interface::~MIDI_Interface
virtual ~MIDI_Interface()
Destructor.
Definition: MIDI_Interface.cpp:5
Parsing_MIDI_Interface::getChannelMessage
ChannelMessage getChannelMessage() const
Return the received channel message.
Definition: MIDI_Interface.hpp:251
AH::UpdatableCRTP< Updatable< MIDI_Interface, false >, false >::updatables
static DoublyLinkedList< Updatable< MIDI_Interface, false > > updatables
Definition: Updatable.hpp:173
Parsing_MIDI_Interface::getRealTimeMessage
RealTimeMessage getRealTimeMessage() const
Return the received real-time message.
Definition: MIDI_Interface.hpp:256