Skip to content

Commit

Permalink
AM_DBG macro's
Browse files Browse the repository at this point in the history
  • Loading branch information
lathoub committed Jul 25, 2022
1 parent 799efca commit 8289f5b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 98 deletions.
2 changes: 2 additions & 0 deletions src/AppleMIDI.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "AppleMIDI_Debug.h"

// https://developer.apple.com/library/archive/documentation/Audio/Conceptual/MIDINetworkDriverProtocol/MIDI/MIDI.html

#include <MIDI.h>
Expand Down
27 changes: 11 additions & 16 deletions src/AppleMIDI_Debug.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
#pragma once
#ifdef DEBUG

#ifdef SerialMon
namespace {
static void DBG_SETUP(unsigned long baud) {
SerialMon.begin(baud);
while (!SerialMon);
}

template <typename T>
static void DBG_PLAIN(T last) {
SerialMon.println(last);
static void AM_DBG_PLAIN(T last) {
DEBUG.println(last);
}

template <typename T, typename... Args>
static void DBG_PLAIN(T head, Args... tail) {
SerialMon.print(head);
SerialMon.print(' ');
static void AM_DBG_PLAIN(T head, Args... tail) {
DEBUG.print(head);
DEBUG.print(' ');
DBG_PLAIN(tail...);
}

template <typename... Args>
static void DBG(Args... args) {
static void AM_DBG(Args... args) {
DBG_PLAIN(args...);
}
} // namespace

#else
#define DBG_SETUP(...)
#define DBG_PLAIN(...)
#define DBG(...)
#define AM_DBG_PLAIN(...)
#define AM_DBG(...)

#endif
33 changes: 17 additions & 16 deletions src/rtpMIDI_Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,24 @@ class rtpMIDIParser
protected:
void debugPrintBuffer(RtpBuffer_t &buffer)
{
Serial.print("bufferSize: ");
Serial.println(buffer.size());
#ifdef DEBUG
AM_DBG("bufferSize:", buffer.size());
for (int i = 0; i < buffer.size(); i++)
{
Serial.print(" ");
Serial.print(i);
Serial.print(i < 10 ? " " : " ");
SerialMon.print(" ");
SerialMon.print(i);
SerialMon.print(i < 10 ? " " : " ");
}
Serial.println("");
AM_DBG("");
for (int i = 0; i < buffer.size(); i++)
{
Serial.print("0x");
Serial.print(buffer[i] < 16 ? "0" : "");
Serial.print(buffer[i], HEX);
Serial.print(" ");
SerialMon.print("0x");
SerialMon.print(buffer[i] < 16 ? "0" : "");
SerialMon.print(buffer[i], HEX);
SerialMon.print(" ");
}
Serial.println("");
AM_DBG("");
#endif
}

public:
Expand All @@ -64,8 +65,8 @@ class rtpMIDIParser
//
parserReturn parse(RtpBuffer_t &buffer)
{
Serial.println("\n--------------------------------------------------------------");
Serial.println(__func__);
AM_DBG("\n--------------------------------------------------------------");
AM_DBG(__func__);
debugPrintBuffer(buffer);

conversionBuffer cb;
Expand Down Expand Up @@ -93,7 +94,7 @@ class rtpMIDIParser

if (_rtpHeadersComplete == false)
{
Serial.println("Parsing header");
AM_DBG("Parsing header");

auto minimumLen = sizeof(Rtp_t);
if (buffer.size() < minimumLen)
Expand Down Expand Up @@ -200,14 +201,14 @@ class rtpMIDIParser

if (rtpMidi_Flags & RTP_MIDI_CS_FLAG_J)
{
Serial.println("decoding Journal section");
AM_DBG("decoding Journal section");

auto retVal = decodeJournalSection(buffer);
switch (retVal) {
case parserReturn::Processed:
break;
case parserReturn::NotEnoughData:
Serial.println("not enough joournal section");
AM_DBG("not enough joournal section");
return parserReturn::NotEnoughData;
case parserReturn::UnexpectedJournalData:
_rtpHeadersComplete = false;
Expand Down
48 changes: 21 additions & 27 deletions src/rtpMIDI_Parser_CommandSection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

parserReturn decodeMIDICommandSection(RtpBuffer_t &buffer)
{
Serial.println(__func__);
AM_DBG(__func__);
debugPrintBuffer(buffer);

// https://www.ietf.org/rfc/rfc4695.html#section-3.2
Expand Down Expand Up @@ -44,15 +44,12 @@ parserReturn decodeMIDICommandSection(RtpBuffer_t &buffer)
/* Multiple MIDI-commands might follow - the exact number can only be discovered by really decoding the commands! */
while (midiCommandLength)
{
Serial.print("midiCommandLength: ");
Serial.print(midiCommandLength);
Serial.print(" cmdCount: ");
Serial.println(cmdCount);
AM_DBG("midiCommandLength:", midiCommandLength, "cmdCount", cmdCount);

/* for the first command we only have a delta-time if Z-Flag is set */
if ((cmdCount) || (rtpMidi_Flags & RTP_MIDI_CS_FLAG_Z))
{
Serial.println("decoding time");
AM_DBG("decoding time");

size_t consumed = 0;
auto retVal = decodeTime(buffer, consumed);
Expand All @@ -65,7 +62,7 @@ parserReturn decodeMIDICommandSection(RtpBuffer_t &buffer)

if (midiCommandLength > 0)
{
Serial.println("decoding MIDIcommand section");
AM_DBG("decoding MIDIcommand section");

cmdCount++;

Expand All @@ -87,7 +84,7 @@ parserReturn decodeMIDICommandSection(RtpBuffer_t &buffer)

parserReturn decodeTime(RtpBuffer_t &buffer, size_t &consumed)
{
Serial.println(__func__);
AM_DBG(__func__);
debugPrintBuffer(buffer);

uint32_t deltatime = 0;
Expand All @@ -111,7 +108,7 @@ parserReturn decodeTime(RtpBuffer_t &buffer, size_t &consumed)

parserReturn decodeMidi(RtpBuffer_t &buffer, uint8_t &runningstatus, size_t &consumed)
{
Serial.println(__func__);
AM_DBG(__func__);
debugPrintBuffer(buffer);

if (buffer.size() < 1)
Expand Down Expand Up @@ -169,37 +166,37 @@ parserReturn decodeMidi(RtpBuffer_t &buffer, uint8_t &runningstatus, size_t &con
switch (octet & 0xf0)
{
case MIDI_NAMESPACE::MidiType::NoteOff:
Serial.println("noteOff");
AM_DBG("noteOff");
consumed += 2;
break;
case MIDI_NAMESPACE::MidiType::NoteOn:
Serial.println("noteOn");
AM_DBG("noteOn");
consumed += 2;
break;
case MIDI_NAMESPACE::MidiType::AfterTouchPoly:
Serial.println("AfterTouchPoly");
AM_DBG("AfterTouchPoly");
consumed += 2;
break;
case MIDI_NAMESPACE::MidiType::ControlChange:
Serial.println("ControlChange");
AM_DBG("ControlChange");
consumed += 2;
break;
case MIDI_NAMESPACE::MidiType::ProgramChange:
Serial.println("ProgramChange");
AM_DBG("ProgramChange");
consumed += 1;
break;
case MIDI_NAMESPACE::MidiType::AfterTouchChannel:
Serial.println("AfterTouchChannel");
AM_DBG("AfterTouchChannel");
consumed += 1;
break;
case MIDI_NAMESPACE::MidiType::PitchBend:
Serial.println("PitchBend");
AM_DBG("PitchBend");
consumed += 2;
break;
}

if (buffer.size() < consumed) {
Serial.println("parserReturn::NotEnoughData");
AM_DBG("parserReturn::NotEnoughData");
return parserReturn::NotEnoughData;
}

Expand Down Expand Up @@ -231,15 +228,12 @@ parserReturn decodeMidi(RtpBuffer_t &buffer, uint8_t &runningstatus, size_t &con
break;
}

Serial.print("consumed: ");
Serial.println(consumed);
Serial.print("buffer.size(): ");
Serial.println(buffer.size());
AM_DBG("consumed:", consumed, "buffer.size():", buffer.size());

if (buffer.size() < consumed)
return parserReturn::NotEnoughData;

Serial.println("expose");
AM_DBG("expose");
session->StartReceivedMidi();
for (size_t j = 0; j < consumed; j++)
session->ReceivedMidi(buffer[j]);
Expand All @@ -250,10 +244,10 @@ parserReturn decodeMidi(RtpBuffer_t &buffer, uint8_t &runningstatus, size_t &con

parserReturn decodeMidiSysEx(RtpBuffer_t &buffer, size_t &consumed)
{
Serial.println(__func__);
AM_DBG(__func__);
debugPrintBuffer(buffer);

Serial.println("Start SysEx");
AM_DBG("Start SysEx");

// consumed = 1; // beginning SysEx Token is not counted (as it could remain)
size_t i = 1; // 0 = start of SysEx, so we can start with 1
Expand All @@ -269,17 +263,17 @@ parserReturn decodeMidiSysEx(RtpBuffer_t &buffer, size_t &consumed)

if (octet == MIDI_NAMESPACE::MidiType::SystemExclusiveEnd) // Complete message
{
Serial.println("\nend SysEx");
AM_DBG("\nend SysEx");
return parserReturn::Processed;
}
else if (octet == MIDI_NAMESPACE::MidiType::SystemExclusiveStart) // Start
{
Serial.println("\nrestart SysEx ???");
AM_DBG("\nrestart SysEx ???");
return parserReturn::Processed;
}
}

Serial.println("\n SysEx buffer not properly ended");
AM_DBG("\n SysEx buffer not properly ended");

// begin of the SysEx is found, not the end.
// so transmit what we have, add a stop-token at the end,
Expand Down
Loading

0 comments on commit 8289f5b

Please sign in to comment.