Skip to content

Commit

Permalink
Move midi message text into MidiEvent (#621)
Browse files Browse the repository at this point in the history
There was a comment about moving the midi message type
string mapping. I needed this function in my application
so decided it was a good idea to move it to a shared location.

Signed-off-by: Greg Burns <burns.greg@gmail.com>
  • Loading branch information
GregBurns committed Sep 20, 2024
1 parent f7dd38f commit de51e43
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 48 deletions.
25 changes: 1 addition & 24 deletions examples/MIDI_UART_Input/MIDI_UART_Input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,6 @@
/** This prevents us from having to type "daisy::" in front of a lot of things. */
using namespace daisy;

/** Fills string with string representation of MidiEvent::Type
* str needs to be at least 16 bytes long to store the data
* TODO: Move this into MIDI lib or something
*/
void GetMidiTypeAsString(MidiEvent& msg, char* str)
{
switch(msg.type)
{
case NoteOff: strcpy(str, "NoteOff"); break;
case NoteOn: strcpy(str, "NoteOn"); break;
case PolyphonicKeyPressure: strcpy(str, "PolyKeyPres."); break;
case ControlChange: strcpy(str, "CC"); break;
case ProgramChange: strcpy(str, "Prog. Change"); break;
case ChannelPressure: strcpy(str, "Chn. Pressure"); break;
case PitchBend: strcpy(str, "PitchBend"); break;
case SystemCommon: strcpy(str, "Sys. Common"); break;
case SystemRealTime: strcpy(str, "Sys. Realtime"); break;
case ChannelMode: strcpy(str, "Chn. Mode"); break;
default: strcpy(str, "Unknown"); break;
}
}

/** Global Hardware access */
DaisySeed hw;
MidiUartHandler midi;
Expand Down Expand Up @@ -98,8 +76,7 @@ int main(void)
{
auto msg = event_log.PopFront();
char outstr[128];
char type_str[16];
GetMidiTypeAsString(msg, type_str);
const char* type_str = MidiEvent::GetTypeAsString(msg);
sprintf(outstr,
"time:\t%ld\ttype: %s\tChannel: %d\tData MSB: "
"%d\tData LSB: %d\n",
Expand Down
25 changes: 1 addition & 24 deletions examples/MIDI_USBH_Input/MIDI_USBH_Input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,6 @@
/** This prevents us from having to type "daisy::" in front of a lot of things. */
using namespace daisy;

/** Fills string with string representation of MidiEvent::Type
* str needs to be at least 16 bytes long to store the data
* TODO: Move this into MIDI lib or something
*/
void GetMidiTypeAsString(MidiEvent& msg, char* str)
{
switch(msg.type)
{
case NoteOff: strcpy(str, "NoteOff"); break;
case NoteOn: strcpy(str, "NoteOn"); break;
case PolyphonicKeyPressure: strcpy(str, "PolyKeyPres."); break;
case ControlChange: strcpy(str, "CC"); break;
case ProgramChange: strcpy(str, "Prog. Change"); break;
case ChannelPressure: strcpy(str, "Chn. Pressure"); break;
case PitchBend: strcpy(str, "PitchBend"); break;
case SystemCommon: strcpy(str, "Sys. Common"); break;
case SystemRealTime: strcpy(str, "Sys. Realtime"); break;
case ChannelMode: strcpy(str, "Chn. Mode"); break;
default: strcpy(str, "Unknown"); break;
}
}

/** Global Hardware access */
DaisySeed hw;
MidiUsbHandler midi;
Expand Down Expand Up @@ -150,8 +128,7 @@ int main(void)
{
auto msg = event_log.PopFront();
char outstr[128];
char type_str[16];
GetMidiTypeAsString(msg, type_str);
const char* type_str = MidiEvent::GetTypeAsString(msg);
sprintf(outstr,
"time:\t%ld\ttype: %s\tChannel: %d\tData MSB: "
"%d\tData LSB: %d\n",
Expand Down
18 changes: 18 additions & 0 deletions src/hid/MidiEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,24 @@ struct MidiEvent
m.channel = channel;
return m;
}

static const char* GetTypeAsString(MidiEvent& msg)
{
switch(msg.type)
{
case NoteOff: return "NoteOff";
case NoteOn: return "NoteOn";
case PolyphonicKeyPressure: return "PolyKeyPres.";
case ControlChange: return "CC";
case ProgramChange: return "Prog. Change";
case ChannelPressure: return "Chn. Pressure";
case PitchBend: return "PitchBend";
case SystemCommon: return "Sys. Common";
case SystemRealTime: return "Sys. Realtime";
case ChannelMode: return "Chn. Mode";
default: return "Unknown";
}
}
};

/** @} */ // End midi_events
Expand Down

0 comments on commit de51e43

Please sign in to comment.