From 18abece8f8a8af17a3b5e80dc1baf61457409600 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Fri, 2 Feb 2018 12:12:18 +0100 Subject: 2 February 2018 Documentation: - new API documentation - datasheets for new components - update BOM - update documentation data Hardware: - update schematic for MIDI connector Software: - new MIDI API - update prject target to PIC18F45K22 --- src/midi.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/midi.c (limited to 'src/midi.c') diff --git a/src/midi.c b/src/midi.c new file mode 100644 index 0000000..e82c921 --- /dev/null +++ b/src/midi.c @@ -0,0 +1,108 @@ +#include "midi.h" + +#include +#include +#include +#include + + +#ifdef MIDI_DYNAMIC_MEMORY_ALLOC +midi_message_t *midi_alloc_message(size_t data_size) +{ + return (midi_message_t *) malloc(sizeof(midi_message_t) + data_size); +} + +void midi_free_message(midi_message_t *pkt) +{ +#ifndef MIDI_UNSAFE + if (pkt == NULL) { + return; + } +#endif + + free(pkt); + pkt = NULL; +} + +size_t midi_message_size(midi_message_t *pkt) +{ + if (pkt == NULL) { + return 0; + } + + switch (pkt->status) { + case NOTE_ON: return sizeof(midi_message_t) + 2; + case NOTE_OFF: return sizeof(midi_message_t) + 1; + default: return sizeof(midi_message_t); + } +} +#endif + + +int midi_set_status(midi_message_t *pkt, midi_status_t status) +{ + if (pkt == NULL) { + return -1; + } + + pkt->status = status & 0x0F; + + return 0; +} + +int midi_set_channel(midi_message_t *pkt, unsigned channel) +{ + if (pkt == NULL) { + return -1; + } + + pkt->channel = channel & 0x0F; + + return 0; +} + +int midi_note_on(midi_message_t *pkt, unsigned channel, midi_note_t note, uint8_t velocity) +{ + if (pkt == NULL) { + return -1; + } + + if (pkt->data == NULL) { + return -2; + } + + midi_set_status(pkt, NOTE_ON); + midi_set_channel(pkt, channel); + + pkt->data[0] = note; + pkt->data[1] = velocity; + +#ifndef MIDI_DYNAMIC_MEMORY_ALLOC + pkt->data_size = 2; +#endif + + return 0; +} + +int midi_note_off(midi_message_t *pkt, unsigned channel, midi_note_t note, uint8_t velocity) +{ + if (pkt == NULL) { + return -1; + } + + if (pkt->data == NULL) { + return -2; + } + + midi_set_status(pkt, NOTE_OFF); + midi_set_channel(pkt, channel); + + pkt->data[0] = note; + pkt->data[1] = velocity; + +#ifndef MIDI_DYNAMIC_MEMORY_ALLOC + pkt->data_size = 2; +#endif + + return 0; +} -- cgit v1.2.1