Control Surface  1.1.1
MIDI Control Surface library for Arduino
SerialMIDI_Parser.cpp
Go to the documentation of this file.
1 #include "SerialMIDI_Parser.hpp"
2 
4 
6  DEBUGFN(hex << NAMEDVALUE(midiByte) << dec);
7 #if !IGNORE_SYSEX
8  // If the previous byte was a SysExStart
9  // I have to handle a start in the next time step, because a start can also
10  // end the previous message. When that happens, I have to return
11  // SYSEX_MESSAGE without resetting the buffer.
12  // Then, after handling the message by the user, I do have to reset the
13  // buffer.
15  startSysEx();
17  }
18 #endif
19  if (isStatus(midiByte)) {
20  // If it's a status byte (first byte)
21  if (midiByte >= 0xF8) {
22  // If it's a Real-Time message
23  return static_cast<MIDI_read_t>(midiByte);
24  } else {
25  // Normal header
26  uint8_t previousHeader = midimsg.header;
27  midimsg.header = midiByte;
28  thirdByte = false;
29 #if !IGNORE_SYSEX
30  if (midimsg.header == TuneRequest) {
31  ; // Tune request (not implemented)
32  }
33  if (previousHeader == SysExStart) {
34  // If we're currently receiving a SysEx message
35  addSysExByte(SysExEnd); // Try to add SysExEnd byte to buffer
36  // Even if the buffer is full, end the message anyway
37  endSysEx();
38  return SYSEX_MESSAGE;
39  }
40 #else
41  (void)previousHeader;
42 #endif // IGNORE_SYSEX
43  }
44  } else {
45  // If it's a data byte
46  if (midimsg.header == 0) {
47  DEBUGFN("Warning: No header");
48  ; // Ignore
49  } else if (thirdByte) {
50  // Third byte of three (data 2)
51  midimsg.data2 = midiByte;
52  thirdByte = false;
53  return CHANNEL_MESSAGE;
54  } else {
55  // Second byte (data 1) or SysEx data
56  if (midimsg.header < 0xC0 || midimsg.header == 0xE0) {
57  // Note, Aftertouch, CC or Pitch Bend
58  midimsg.data1 = midiByte;
59  thirdByte = true;
60  } else if (midimsg.header < 0xE0) {
61  // Program Change or Channel Pressure
62  midimsg.data1 = midiByte;
63  return CHANNEL_MESSAGE;
64  }
65 #if !IGNORE_SYSEX
66  else if (midimsg.header == SysExStart) {
67  // SysEx data byte
68  addSysExByte(midiByte);
69  }
70 #endif // IGNORE_SYSEX
71  else {
72  DEBUGFN("Data byte ignored");
73  }
74  }
75  }
76  return NO_MESSAGE;
77 }
78 
SerialMIDI_Parser::startSysEx
void startSysEx()
Definition: SerialMIDI_Parser.hpp:23
SerialMIDI_Parser::addSysExByte
bool addSysExByte(uint8_t data)
Definition: SerialMIDI_Parser.hpp:22
SerialMIDI_Parser::parse
MIDI_read_t parse(uint8_t midibyte)
Definition: SerialMIDI_Parser.cpp:5
NAMEDVALUE
#define NAMEDVALUE(x)
Macro for printing an expression as a string, followed by its value.
Definition: Debug.hpp:69
MIDI_Parser::isStatus
static bool isStatus(uint8_t data)
Check if the given byte is a MIDI header byte.
Definition: MIDI_Parser.cpp:11
SysExBuffer::isReceiving
bool isReceiving() const
Check if the buffer is receiving a SysEx message.
Definition: SysExBuffer.cpp:31
MIDI_read_t
MIDI_read_t
Definition: MIDI_Parser.hpp:29
CHANNEL_MESSAGE
Definition: MIDI_Parser.hpp:31
SerialMIDI_Parser::sysexbuffer
SysExBuffer sysexbuffer
Definition: SerialMIDI_Parser.hpp:20
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
ChannelMessage::header
uint8_t header
Definition: MIDI_Parser.hpp:48
ChannelMessage::data2
uint8_t data2
Definition: MIDI_Parser.hpp:50
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
SerialMIDI_Parser::endSysEx
void endSysEx()
Definition: SerialMIDI_Parser.hpp:24
TuneRequest
const uint8_t TuneRequest
Definition: MIDI_Parser.hpp:27
SysExEnd
const uint8_t SysExEnd
Definition: MIDI_Parser.hpp:25
NO_MESSAGE
Definition: MIDI_Parser.hpp:30
MIDI_Parser::midimsg
ChannelMessage midimsg
Definition: MIDI_Parser.hpp:102
ChannelMessage::data1
uint8_t data1
Definition: MIDI_Parser.hpp:49
SYSEX_MESSAGE
Definition: MIDI_Parser.hpp:32
hex
Print & hex(Print &printer)
Definition: PrintStream.cpp:62
DEBUGFN
#define DEBUGFN(x)
Print an expression and its function (function name and line number) to the debug output if debugging...
Definition: Debug.hpp:93
dec
Print & dec(Print &printer)
Definition: PrintStream.cpp:77
SerialMIDI_Parser.hpp
SysExStart
const uint8_t SysExStart
Definition: MIDI_Parser.hpp:24
SerialMIDI_Parser::thirdByte
bool thirdByte
Definition: SerialMIDI_Parser.hpp:28