1
0
Fork 0

clang-format changes

This commit is contained in:
skullY 2019-08-30 11:19:03 -07:00 committed by skullydazed
parent 61af76a10d
commit b624f32f94
502 changed files with 32259 additions and 39062 deletions

28
tmk_core/protocol/midi/Config/LUFAConfig.h Executable file → Normal file
View file

@ -42,12 +42,12 @@
#ifndef _LUFA_CONFIG_H_
#define _LUFA_CONFIG_H_
#if (ARCH == ARCH_AVR8)
#if (ARCH == ARCH_AVR8)
/* Non-USB Related Configuration Tokens: */
/* Non-USB Related Configuration Tokens: */
// #define DISABLE_TERMINAL_CODES
/* USB Class Driver Related Tokens: */
/* USB Class Driver Related Tokens: */
// #define HID_HOST_BOOT_PROTOCOL_ONLY
// #define HID_STATETABLE_STACK_DEPTH {Insert Value Here}
// #define HID_USAGE_STACK_DEPTH {Insert Value Here}
@ -56,38 +56,38 @@
// #define HID_MAX_REPORT_IDS {Insert Value Here}
// #define NO_CLASS_DRIVER_AUTOFLUSH
/* General USB Driver Related Tokens: */
/* General USB Driver Related Tokens: */
// #define ORDERED_EP_CONFIG
#define USE_STATIC_OPTIONS (USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)
#define USB_DEVICE_ONLY
# define USE_STATIC_OPTIONS (USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)
# define USB_DEVICE_ONLY
// #define USB_HOST_ONLY
// #define USB_STREAM_TIMEOUT_MS {Insert Value Here}
// #define NO_LIMITED_CONTROLLER_CONNECT
// #define NO_SOF_EVENTS
/* USB Device Mode Driver Related Tokens: */
/* USB Device Mode Driver Related Tokens: */
// #define USE_RAM_DESCRIPTORS
#define USE_FLASH_DESCRIPTORS
# define USE_FLASH_DESCRIPTORS
// #define USE_EEPROM_DESCRIPTORS
// #define NO_INTERNAL_SERIAL
#define FIXED_CONTROL_ENDPOINT_SIZE 8
# define FIXED_CONTROL_ENDPOINT_SIZE 8
// #define DEVICE_STATE_AS_GPIOR {Insert Value Here}
#define FIXED_NUM_CONFIGURATIONS 1
# define FIXED_NUM_CONFIGURATIONS 1
// #define CONTROL_ONLY_DEVICE
// #define INTERRUPT_CONTROL_ENDPOINT
// #define NO_DEVICE_REMOTE_WAKEUP
// #define NO_DEVICE_SELF_POWER
/* USB Host Mode Driver Related Tokens: */
/* USB Host Mode Driver Related Tokens: */
// #define HOST_STATE_AS_GPIOR {Insert Value Here}
// #define USB_HOST_TIMEOUT_MS {Insert Value Here}
// #define HOST_DEVICE_SETTLE_DELAY_MS {Insert Value Here}
// #define NO_AUTO_VBUS_MANAGEMENT
// #define INVERTED_VBUS_ENABLE_LINE
#else
#else
#error Unsupported architecture for this LUFA configuration file.
# error Unsupported architecture for this LUFA configuration file.
#endif
#endif
#endif

93
tmk_core/protocol/midi/bytequeue/bytequeue.c Executable file → Normal file
View file

@ -1,65 +1,62 @@
//this is a single reader [maybe multiple writer?] byte queue
//Copyright 2008 Alex Norman
//writen by Alex Norman
// this is a single reader [maybe multiple writer?] byte queue
// Copyright 2008 Alex Norman
// writen by Alex Norman
//
//This file is part of avr-bytequeue.
// This file is part of avr-bytequeue.
//
//avr-bytequeue is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
// avr-bytequeue is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//avr-bytequeue is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
// avr-bytequeue is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with avr-bytequeue. If not, see <http://www.gnu.org/licenses/>.
// You should have received a copy of the GNU General Public License
// along with avr-bytequeue. If not, see <http://www.gnu.org/licenses/>.
#include "bytequeue.h"
#include "interrupt_setting.h"
void bytequeue_init(byteQueue_t * queue, uint8_t * dataArray, byteQueueIndex_t arrayLen){
queue->length = arrayLen;
queue->data = dataArray;
queue->start = queue->end = 0;
void bytequeue_init(byteQueue_t* queue, uint8_t* dataArray, byteQueueIndex_t arrayLen) {
queue->length = arrayLen;
queue->data = dataArray;
queue->start = queue->end = 0;
}
bool bytequeue_enqueue(byteQueue_t * queue, uint8_t item){
interrupt_setting_t setting = store_and_clear_interrupt();
//full
if(((queue->end + 1) % queue->length) == queue->start){
restore_interrupt_setting(setting);
return false;
} else {
queue->data[queue->end] = item;
queue->end = (queue->end + 1) % queue->length;
restore_interrupt_setting(setting);
return true;
}
bool bytequeue_enqueue(byteQueue_t* queue, uint8_t item) {
interrupt_setting_t setting = store_and_clear_interrupt();
// full
if (((queue->end + 1) % queue->length) == queue->start) {
restore_interrupt_setting(setting);
return false;
} else {
queue->data[queue->end] = item;
queue->end = (queue->end + 1) % queue->length;
restore_interrupt_setting(setting);
return true;
}
}
byteQueueIndex_t bytequeue_length(byteQueue_t * queue){
byteQueueIndex_t len;
interrupt_setting_t setting = store_and_clear_interrupt();
if(queue->end >= queue->start)
len = queue->end - queue->start;
else
len = (queue->length - queue->start) + queue->end;
restore_interrupt_setting(setting);
return len;
byteQueueIndex_t bytequeue_length(byteQueue_t* queue) {
byteQueueIndex_t len;
interrupt_setting_t setting = store_and_clear_interrupt();
if (queue->end >= queue->start)
len = queue->end - queue->start;
else
len = (queue->length - queue->start) + queue->end;
restore_interrupt_setting(setting);
return len;
}
//we don't need to avoid interrupts if there is only one reader
uint8_t bytequeue_get(byteQueue_t * queue, byteQueueIndex_t index){
return queue->data[(queue->start + index) % queue->length];
}
// we don't need to avoid interrupts if there is only one reader
uint8_t bytequeue_get(byteQueue_t* queue, byteQueueIndex_t index) { return queue->data[(queue->start + index) % queue->length]; }
//we just update the start index to remove elements
void bytequeue_remove(byteQueue_t * queue, byteQueueIndex_t numToRemove){
interrupt_setting_t setting = store_and_clear_interrupt();
queue->start = (queue->start + numToRemove) % queue->length;
restore_interrupt_setting(setting);
// we just update the start index to remove elements
void bytequeue_remove(byteQueue_t* queue, byteQueueIndex_t numToRemove) {
interrupt_setting_t setting = store_and_clear_interrupt();
queue->start = (queue->start + numToRemove) % queue->length;
restore_interrupt_setting(setting);
}

59
tmk_core/protocol/midi/bytequeue/bytequeue.h Executable file → Normal file
View file

@ -1,28 +1,28 @@
//this is a single reader [maybe multiple writer?] byte queue
//Copyright 2008 Alex Norman
//writen by Alex Norman
// this is a single reader [maybe multiple writer?] byte queue
// Copyright 2008 Alex Norman
// writen by Alex Norman
//
//This file is part of avr-bytequeue.
// This file is part of avr-bytequeue.
//
//avr-bytequeue is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
// avr-bytequeue is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//avr-bytequeue is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
// avr-bytequeue is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with avr-bytequeue. If not, see <http://www.gnu.org/licenses/>.
// You should have received a copy of the GNU General Public License
// along with avr-bytequeue. If not, see <http://www.gnu.org/licenses/>.
#ifndef BYTEQUEUE_H
#define BYTEQUEUE_H
#ifdef __cplusplus
extern "C" {
#endif
#endif
#include <inttypes.h>
#include <stdbool.h>
@ -30,30 +30,29 @@ extern "C" {
typedef uint8_t byteQueueIndex_t;
typedef struct {
byteQueueIndex_t start;
byteQueueIndex_t end;
byteQueueIndex_t length;
uint8_t * data;
byteQueueIndex_t start;
byteQueueIndex_t end;
byteQueueIndex_t length;
uint8_t* data;
} byteQueue_t;
//you must have a queue, an array of data which the queue will use, and the length of that array
void bytequeue_init(byteQueue_t * queue, uint8_t * dataArray, byteQueueIndex_t arrayLen);
// you must have a queue, an array of data which the queue will use, and the length of that array
void bytequeue_init(byteQueue_t* queue, uint8_t* dataArray, byteQueueIndex_t arrayLen);
//add an item to the queue, returns false if the queue is full
bool bytequeue_enqueue(byteQueue_t * queue, uint8_t item);
// add an item to the queue, returns false if the queue is full
bool bytequeue_enqueue(byteQueue_t* queue, uint8_t item);
//get the length of the queue
byteQueueIndex_t bytequeue_length(byteQueue_t * queue);
// get the length of the queue
byteQueueIndex_t bytequeue_length(byteQueue_t* queue);
//this grabs data at the index given [starting at queue->start]
uint8_t bytequeue_get(byteQueue_t * queue, byteQueueIndex_t index);
// this grabs data at the index given [starting at queue->start]
uint8_t bytequeue_get(byteQueue_t* queue, byteQueueIndex_t index);
//update the index in the queue to reflect data that has been dealt with
void bytequeue_remove(byteQueue_t * queue, byteQueueIndex_t numToRemove);
// update the index in the queue to reflect data that has been dealt with
void bytequeue_remove(byteQueue_t* queue, byteQueueIndex_t numToRemove);
#ifdef __cplusplus
}
#endif
#endif
#endif

54
tmk_core/protocol/midi/bytequeue/interrupt_setting.c Executable file → Normal file
View file

@ -1,49 +1,43 @@
//Copyright 20010 Alex Norman
//writen by Alex Norman
// Copyright 20010 Alex Norman
// writen by Alex Norman
//
//This file is part of avr-bytequeue.
// This file is part of avr-bytequeue.
//
//avr-bytequeue is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
// avr-bytequeue is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//avr-bytequeue is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
// avr-bytequeue is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with avr-bytequeue. If not, see <http://www.gnu.org/licenses/>.
// You should have received a copy of the GNU General Public License
// along with avr-bytequeue. If not, see <http://www.gnu.org/licenses/>.
//AVR specific code
//should be able to port to other systems by simply providing chip specific
//implementations of the typedef and these functions
// AVR specific code
// should be able to port to other systems by simply providing chip specific
// implementations of the typedef and these functions
#include "interrupt_setting.h"
#if defined(__AVR__)
#include <avr/interrupt.h>
# include <avr/interrupt.h>
interrupt_setting_t store_and_clear_interrupt(void) {
uint8_t sreg = SREG;
cli();
return sreg;
uint8_t sreg = SREG;
cli();
return sreg;
}
void restore_interrupt_setting(interrupt_setting_t setting) {
SREG = setting;
}
void restore_interrupt_setting(interrupt_setting_t setting) { SREG = setting; }
#elif defined(__arm__)
#include "ch.h"
# include "ch.h"
interrupt_setting_t store_and_clear_interrupt(void) {
chSysLock();
return 0;
chSysLock();
return 0;
}
void restore_interrupt_setting(interrupt_setting_t setting) {
chSysUnlock();
}
void restore_interrupt_setting(interrupt_setting_t setting) { chSysUnlock(); }
#endif

33
tmk_core/protocol/midi/bytequeue/interrupt_setting.h Executable file → Normal file
View file

@ -1,39 +1,38 @@
//Copyright 20010 Alex Norman
//writen by Alex Norman
// Copyright 20010 Alex Norman
// writen by Alex Norman
//
//This file is part of avr-bytequeue.
// This file is part of avr-bytequeue.
//
//avr-bytequeue is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
// avr-bytequeue is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//avr-bytequeue is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
// avr-bytequeue is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with avr-bytequeue. If not, see <http://www.gnu.org/licenses/>.
// You should have received a copy of the GNU General Public License
// along with avr-bytequeue. If not, see <http://www.gnu.org/licenses/>.
#ifndef INTERRUPT_SETTING_H
#define INTERRUPT_SETTING_H
#ifdef __cplusplus
extern "C" {
#endif
#endif
#include <inttypes.h>
//AVR specific typedef
// AVR specific typedef
typedef uint8_t interrupt_setting_t;
interrupt_setting_t store_and_clear_interrupt(void);
void restore_interrupt_setting(interrupt_setting_t setting);
void restore_interrupt_setting(interrupt_setting_t setting);
#ifdef __cplusplus
}
#endif
#endif
#endif

378
tmk_core/protocol/midi/midi.c Executable file → Normal file
View file

@ -1,277 +1,181 @@
//midi for embedded chips,
//Copyright 2010 Alex Norman
// midi for embedded chips,
// Copyright 2010 Alex Norman
//
//This file is part of avr-midi.
// This file is part of avr-midi.
//
//avr-midi is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
// avr-midi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//avr-midi is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
// avr-midi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with avr-midi. If not, see <http://www.gnu.org/licenses/>.
// You should have received a copy of the GNU General Public License
// along with avr-midi. If not, see <http://www.gnu.org/licenses/>.
#include "midi.h"
#include <string.h> //for memcpy
#include <string.h> //for memcpy
#define MIN(x,y) (((x) < (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#ifndef NULL
#define NULL 0
# define NULL 0
#endif
bool midi_is_statusbyte(uint8_t theByte){
return (bool)(theByte & MIDI_STATUSMASK);
bool midi_is_statusbyte(uint8_t theByte) { return (bool)(theByte & MIDI_STATUSMASK); }
bool midi_is_realtime(uint8_t theByte) { return (theByte >= MIDI_CLOCK); }
midi_packet_length_t midi_packet_length(uint8_t status) {
switch (status & 0xF0) {
case MIDI_CC:
case MIDI_NOTEON:
case MIDI_NOTEOFF:
case MIDI_AFTERTOUCH:
case MIDI_PITCHBEND:
return THREE;
case MIDI_PROGCHANGE:
case MIDI_CHANPRESSURE:
case MIDI_SONGSELECT:
return TWO;
case 0xF0:
switch (status) {
case MIDI_CLOCK:
case MIDI_TICK:
case MIDI_START:
case MIDI_CONTINUE:
case MIDI_STOP:
case MIDI_ACTIVESENSE:
case MIDI_RESET:
case MIDI_TUNEREQUEST:
return ONE;
case MIDI_SONGPOSITION:
return THREE;
case MIDI_TC_QUARTERFRAME:
case MIDI_SONGSELECT:
return TWO;
case SYSEX_END:
case SYSEX_BEGIN:
default:
return UNDEFINED;
}
default:
return UNDEFINED;
}
}
bool midi_is_realtime(uint8_t theByte){
return (theByte >= MIDI_CLOCK);
void midi_send_cc(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t val) {
// CC Status: 0xB0 to 0xBF where the low nibble is the MIDI channel.
// CC Data: Controller Num, Controller Val
device->send_func(device, 3, MIDI_CC | (chan & MIDI_CHANMASK), num & 0x7F, val & 0x7F);
}
midi_packet_length_t midi_packet_length(uint8_t status){
switch(status & 0xF0){
case MIDI_CC:
case MIDI_NOTEON:
case MIDI_NOTEOFF:
case MIDI_AFTERTOUCH:
case MIDI_PITCHBEND:
return THREE;
case MIDI_PROGCHANGE:
case MIDI_CHANPRESSURE:
case MIDI_SONGSELECT:
return TWO;
case 0xF0:
switch(status) {
case MIDI_CLOCK:
case MIDI_TICK:
case MIDI_START:
case MIDI_CONTINUE:
case MIDI_STOP:
case MIDI_ACTIVESENSE:
case MIDI_RESET:
case MIDI_TUNEREQUEST:
return ONE;
case MIDI_SONGPOSITION:
return THREE;
case MIDI_TC_QUARTERFRAME:
case MIDI_SONGSELECT:
return TWO;
case SYSEX_END:
case SYSEX_BEGIN:
default:
return UNDEFINED;
}
default:
return UNDEFINED;
}
void midi_send_noteon(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t vel) {
// Note Data: Note Num, Note Velocity
device->send_func(device, 3, MIDI_NOTEON | (chan & MIDI_CHANMASK), num & 0x7F, vel & 0x7F);
}
void midi_send_cc(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t val){
//CC Status: 0xB0 to 0xBF where the low nibble is the MIDI channel.
//CC Data: Controller Num, Controller Val
device->send_func(device, 3,
MIDI_CC | (chan & MIDI_CHANMASK),
num & 0x7F,
val & 0x7F);
void midi_send_noteoff(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t vel) {
// Note Data: Note Num, Note Velocity
device->send_func(device, 3, MIDI_NOTEOFF | (chan & MIDI_CHANMASK), num & 0x7F, vel & 0x7F);
}
void midi_send_noteon(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t vel){
//Note Data: Note Num, Note Velocity
device->send_func(device, 3,
MIDI_NOTEON | (chan & MIDI_CHANMASK),
num & 0x7F,
vel & 0x7F);
void midi_send_aftertouch(MidiDevice* device, uint8_t chan, uint8_t note_num, uint8_t amt) { device->send_func(device, 3, MIDI_AFTERTOUCH | (chan & MIDI_CHANMASK), note_num & 0x7F, amt & 0x7F); }
// XXX does this work right?
// amt in range -0x2000, 0x1fff
// uAmt should be in range..
// 0x0000 to 0x3FFF
void midi_send_pitchbend(MidiDevice* device, uint8_t chan, int16_t amt) {
uint16_t uAmt;
// check range
if (amt > 0x1fff) {
uAmt = 0x3FFF;
} else if (amt < -0x2000) {
uAmt = 0;
} else {
uAmt = amt + 0x2000;
}
device->send_func(device, 3, MIDI_PITCHBEND | (chan & MIDI_CHANMASK), uAmt & 0x7F, (uAmt >> 7) & 0x7F);
}
void midi_send_noteoff(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t vel){
//Note Data: Note Num, Note Velocity
device->send_func(device, 3,
MIDI_NOTEOFF | (chan & MIDI_CHANMASK),
num & 0x7F,
vel & 0x7F);
void midi_send_programchange(MidiDevice* device, uint8_t chan, uint8_t num) { device->send_func(device, 2, MIDI_PROGCHANGE | (chan & MIDI_CHANMASK), num & 0x7F, 0); }
void midi_send_channelpressure(MidiDevice* device, uint8_t chan, uint8_t amt) { device->send_func(device, 2, MIDI_CHANPRESSURE | (chan & MIDI_CHANMASK), amt & 0x7F, 0); }
void midi_send_clock(MidiDevice* device) { device->send_func(device, 1, MIDI_CLOCK, 0, 0); }
void midi_send_tick(MidiDevice* device) { device->send_func(device, 1, MIDI_TICK, 0, 0); }
void midi_send_start(MidiDevice* device) { device->send_func(device, 1, MIDI_START, 0, 0); }
void midi_send_continue(MidiDevice* device) { device->send_func(device, 1, MIDI_CONTINUE, 0, 0); }
void midi_send_stop(MidiDevice* device) { device->send_func(device, 1, MIDI_STOP, 0, 0); }
void midi_send_activesense(MidiDevice* device) { device->send_func(device, 1, MIDI_ACTIVESENSE, 0, 0); }
void midi_send_reset(MidiDevice* device) { device->send_func(device, 1, MIDI_RESET, 0, 0); }
void midi_send_tcquarterframe(MidiDevice* device, uint8_t time) { device->send_func(device, 2, MIDI_TC_QUARTERFRAME, time & 0x7F, 0); }
// XXX is this right?
void midi_send_songposition(MidiDevice* device, uint16_t pos) { device->send_func(device, 3, MIDI_SONGPOSITION, pos & 0x7F, (pos >> 7) & 0x7F); }
void midi_send_songselect(MidiDevice* device, uint8_t song) { device->send_func(device, 2, MIDI_SONGSELECT, song & 0x7F, 0); }
void midi_send_tunerequest(MidiDevice* device) { device->send_func(device, 1, MIDI_TUNEREQUEST, 0, 0); }
void midi_send_byte(MidiDevice* device, uint8_t b) { device->send_func(device, 1, b, 0, 0); }
void midi_send_data(MidiDevice* device, uint16_t count, uint8_t byte0, uint8_t byte1, uint8_t byte2) {
// ensure that the count passed along is always 3 or lower
if (count > 3) {
// TODO how to do this correctly?
}
device->send_func(device, count, byte0, byte1, byte2);
}
void midi_send_aftertouch(MidiDevice * device, uint8_t chan, uint8_t note_num, uint8_t amt){
device->send_func(device, 3,
MIDI_AFTERTOUCH | (chan & MIDI_CHANMASK),
note_num & 0x7F,
amt & 0x7F);
void midi_send_array(MidiDevice* device, uint16_t count, uint8_t* array) {
uint16_t i;
for (i = 0; i < count; i += 3) {
uint8_t b[3] = {0, 0, 0};
uint16_t to_send = count - i;
to_send = (to_send > 3) ? 3 : to_send;
memcpy(b, array + i, to_send);
midi_send_data(device, to_send, b[0], b[1], b[2]);
}
}
//XXX does this work right?
//amt in range -0x2000, 0x1fff
//uAmt should be in range..
//0x0000 to 0x3FFF
void midi_send_pitchbend(MidiDevice * device, uint8_t chan, int16_t amt){
uint16_t uAmt;
//check range
if(amt > 0x1fff){
uAmt = 0x3FFF;
} else if(amt < -0x2000){
uAmt = 0;
} else {
uAmt = amt + 0x2000;
}
device->send_func(device, 3,
MIDI_PITCHBEND | (chan & MIDI_CHANMASK),
uAmt & 0x7F,
(uAmt >> 7) & 0x7F);
}
void midi_register_cc_callback(MidiDevice* device, midi_three_byte_func_t func) { device->input_cc_callback = func; }
void midi_send_programchange(MidiDevice * device, uint8_t chan, uint8_t num){
device->send_func(device, 2,
MIDI_PROGCHANGE | (chan & MIDI_CHANMASK),
num & 0x7F,
0);
}
void midi_register_noteon_callback(MidiDevice* device, midi_three_byte_func_t func) { device->input_noteon_callback = func; }
void midi_send_channelpressure(MidiDevice * device, uint8_t chan, uint8_t amt){
device->send_func(device, 2,
MIDI_CHANPRESSURE | (chan & MIDI_CHANMASK),
amt & 0x7F,
0);
}
void midi_register_noteoff_callback(MidiDevice* device, midi_three_byte_func_t func) { device->input_noteoff_callback = func; }
void midi_send_clock(MidiDevice * device){
device->send_func(device, 1, MIDI_CLOCK, 0, 0);
}
void midi_register_aftertouch_callback(MidiDevice* device, midi_three_byte_func_t func) { device->input_aftertouch_callback = func; }
void midi_send_tick(MidiDevice * device){
device->send_func(device, 1, MIDI_TICK, 0, 0);
}
void midi_register_pitchbend_callback(MidiDevice* device, midi_three_byte_func_t func) { device->input_pitchbend_callback = func; }
void midi_send_start(MidiDevice * device){
device->send_func(device, 1, MIDI_START, 0, 0);
}
void midi_register_songposition_callback(MidiDevice* device, midi_three_byte_func_t func) { device->input_songposition_callback = func; }
void midi_send_continue(MidiDevice * device){
device->send_func(device, 1, MIDI_CONTINUE, 0, 0);
}
void midi_register_progchange_callback(MidiDevice* device, midi_two_byte_func_t func) { device->input_progchange_callback = func; }
void midi_send_stop(MidiDevice * device){
device->send_func(device, 1, MIDI_STOP, 0, 0);
}
void midi_register_chanpressure_callback(MidiDevice* device, midi_two_byte_func_t func) { device->input_chanpressure_callback = func; }
void midi_send_activesense(MidiDevice * device){
device->send_func(device, 1, MIDI_ACTIVESENSE, 0, 0);
}
void midi_register_songselect_callback(MidiDevice* device, midi_two_byte_func_t func) { device->input_songselect_callback = func; }
void midi_send_reset(MidiDevice * device){
device->send_func(device, 1, MIDI_RESET, 0, 0);
}
void midi_register_tc_quarterframe_callback(MidiDevice* device, midi_two_byte_func_t func) { device->input_tc_quarterframe_callback = func; }
void midi_send_tcquarterframe(MidiDevice * device, uint8_t time){
device->send_func(device, 2,
MIDI_TC_QUARTERFRAME,
time & 0x7F,
0);
}
void midi_register_realtime_callback(MidiDevice* device, midi_one_byte_func_t func) { device->input_realtime_callback = func; }
//XXX is this right?
void midi_send_songposition(MidiDevice * device, uint16_t pos){
device->send_func(device, 3,
MIDI_SONGPOSITION,
pos & 0x7F,
(pos >> 7) & 0x7F);
}
void midi_register_tunerequest_callback(MidiDevice* device, midi_one_byte_func_t func) { device->input_tunerequest_callback = func; }
void midi_send_songselect(MidiDevice * device, uint8_t song){
device->send_func(device, 2,
MIDI_SONGSELECT,
song & 0x7F,
0);
}
void midi_register_sysex_callback(MidiDevice* device, midi_sysex_func_t func) { device->input_sysex_callback = func; }
void midi_send_tunerequest(MidiDevice * device){
device->send_func(device, 1, MIDI_TUNEREQUEST, 0, 0);
}
void midi_send_byte(MidiDevice * device, uint8_t b){
device->send_func(device, 1, b, 0, 0);
}
void midi_send_data(MidiDevice * device, uint16_t count, uint8_t byte0, uint8_t byte1, uint8_t byte2){
//ensure that the count passed along is always 3 or lower
if (count > 3) {
//TODO how to do this correctly?
}
device->send_func(device, count, byte0, byte1, byte2);
}
void midi_send_array(MidiDevice * device, uint16_t count, uint8_t * array) {
uint16_t i;
for (i = 0; i < count; i += 3) {
uint8_t b[3] = { 0, 0, 0 };
uint16_t to_send = count - i;
to_send = (to_send > 3) ? 3 : to_send;
memcpy(b, array + i, to_send);
midi_send_data(device, to_send, b[0], b[1], b[2]);
}
}
void midi_register_cc_callback(MidiDevice * device, midi_three_byte_func_t func){
device->input_cc_callback = func;
}
void midi_register_noteon_callback(MidiDevice * device, midi_three_byte_func_t func){
device->input_noteon_callback = func;
}
void midi_register_noteoff_callback(MidiDevice * device, midi_three_byte_func_t func){
device->input_noteoff_callback = func;
}
void midi_register_aftertouch_callback(MidiDevice * device, midi_three_byte_func_t func){
device->input_aftertouch_callback = func;
}
void midi_register_pitchbend_callback(MidiDevice * device, midi_three_byte_func_t func){
device->input_pitchbend_callback = func;
}
void midi_register_songposition_callback(MidiDevice * device, midi_three_byte_func_t func){
device->input_songposition_callback = func;
}
void midi_register_progchange_callback(MidiDevice * device, midi_two_byte_func_t func) {
device->input_progchange_callback = func;
}
void midi_register_chanpressure_callback(MidiDevice * device, midi_two_byte_func_t func) {
device->input_chanpressure_callback = func;
}
void midi_register_songselect_callback(MidiDevice * device, midi_two_byte_func_t func) {
device->input_songselect_callback = func;
}
void midi_register_tc_quarterframe_callback(MidiDevice * device, midi_two_byte_func_t func) {
device->input_tc_quarterframe_callback = func;
}
void midi_register_realtime_callback(MidiDevice * device, midi_one_byte_func_t func){
device->input_realtime_callback = func;
}
void midi_register_tunerequest_callback(MidiDevice * device, midi_one_byte_func_t func){
device->input_tunerequest_callback = func;
}
void midi_register_sysex_callback(MidiDevice * device, midi_sysex_func_t func) {
device->input_sysex_callback = func;
}
void midi_register_fallthrough_callback(MidiDevice * device, midi_var_byte_func_t func){
device->input_fallthrough_callback = func;
}
void midi_register_catchall_callback(MidiDevice * device, midi_var_byte_func_t func){
device->input_catchall_callback = func;
}
void midi_register_fallthrough_callback(MidiDevice* device, midi_var_byte_func_t func) { device->input_fallthrough_callback = func; }
void midi_register_catchall_callback(MidiDevice* device, midi_var_byte_func_t func) { device->input_catchall_callback = func; }

152
tmk_core/protocol/midi/midi.h Executable file → Normal file
View file

@ -1,20 +1,20 @@
//midi for embedded chips,
//Copyright 2010 Alex Norman
// midi for embedded chips,
// Copyright 2010 Alex Norman
//
//This file is part of avr-midi.
// This file is part of avr-midi.
//
//avr-midi is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
// avr-midi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//avr-midi is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
// avr-midi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with avr-midi. If not, see <http://www.gnu.org/licenses/>.
// You should have received a copy of the GNU General Public License
// along with avr-midi. If not, see <http://www.gnu.org/licenses/>.
/**
* @file
@ -30,7 +30,7 @@
#ifdef __cplusplus
extern "C" {
#endif
#endif
#include "midi_device.h"
#include "midi_function_types.h"
@ -48,8 +48,8 @@ extern "C" {
* You must call this before using the device in question.
*
* @param device the device to initialize
*/
void midi_device_init(MidiDevice * device); // [implementation in midi_device.c]
*/
void midi_device_init(MidiDevice* device); // [implementation in midi_device.c]
/**
* @brief Process input data
@ -58,8 +58,8 @@ void midi_device_init(MidiDevice * device); // [implementation in midi_device.c]
* if you expect to have your input callbacks called.
*
* @param device the device to process
*/
void midi_device_process(MidiDevice * device); // [implementation in midi_device.c]
*/
void midi_device_process(MidiDevice* device); // [implementation in midi_device.c]
/**@}*/
@ -76,8 +76,8 @@ void midi_device_process(MidiDevice * device); // [implementation in midi_device
* @param chan the channel to send on, 0-15
* @param num the cc num
* @param val the value of that cc num
*/
void midi_send_cc(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t val);
*/
void midi_send_cc(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t val);
/**
* @brief Send a note on message via the given device.
@ -86,8 +86,8 @@ void midi_send_cc(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t val);
* @param chan the channel to send on, 0-15
* @param num the note number
* @param vel the note velocity
*/
void midi_send_noteon(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t vel);
*/
void midi_send_noteon(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t vel);
/**
* @brief Send a note off message via the given device.
@ -96,8 +96,8 @@ void midi_send_noteon(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t ve
* @param chan the channel to send on, 0-15
* @param num the note number
* @param vel the note velocity
*/
void midi_send_noteoff(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t vel);
*/
void midi_send_noteoff(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t vel);
/**
* @brief Send an after touch message via the given device.
@ -106,8 +106,8 @@ void midi_send_noteoff(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t v
* @param chan the channel to send on, 0-15
* @param note_num the note number
* @param amt the after touch amount
*/
void midi_send_aftertouch(MidiDevice * device, uint8_t chan, uint8_t note_num, uint8_t amt);
*/
void midi_send_aftertouch(MidiDevice* device, uint8_t chan, uint8_t note_num, uint8_t amt);
/**
* @brief Send a pitch bend message via the given device.
@ -115,8 +115,8 @@ void midi_send_aftertouch(MidiDevice * device, uint8_t chan, uint8_t note_num, u
* @param device the device to use for sending
* @param chan the channel to send on, 0-15
* @param amt the bend amount range: -8192..8191, 0 means no bend
*/
void midi_send_pitchbend(MidiDevice * device, uint8_t chan, int16_t amt); //range -8192, 8191
*/
void midi_send_pitchbend(MidiDevice* device, uint8_t chan, int16_t amt); // range -8192, 8191
/**
* @brief Send a program change message via the given device.
@ -124,8 +124,8 @@ void midi_send_pitchbend(MidiDevice * device, uint8_t chan, int16_t amt); //rang
* @param device the device to use for sending
* @param chan the channel to send on, 0-15
* @param num the program to change to
*/
void midi_send_programchange(MidiDevice * device, uint8_t chan, uint8_t num);
*/
void midi_send_programchange(MidiDevice* device, uint8_t chan, uint8_t num);
/**
* @brief Send a channel pressure message via the given device.
@ -133,58 +133,57 @@ void midi_send_programchange(MidiDevice * device, uint8_t chan, uint8_t num);
* @param device the device to use for sending
* @param chan the channel to send on, 0-15
* @param amt the amount of channel pressure
*/
void midi_send_channelpressure(MidiDevice * device, uint8_t chan, uint8_t amt);
*/
void midi_send_channelpressure(MidiDevice* device, uint8_t chan, uint8_t amt);
/**
* @brief Send a clock message via the given device.
*
* @param device the device to use for sending
*/
void midi_send_clock(MidiDevice * device);
void midi_send_clock(MidiDevice* device);
/**
* @brief Send a tick message via the given device.
*
* @param device the device to use for sending
*/
void midi_send_tick(MidiDevice * device);
void midi_send_tick(MidiDevice* device);
/**
* @brief Send a start message via the given device.
*
* @param device the device to use for sending
*/
void midi_send_start(MidiDevice * device);
void midi_send_start(MidiDevice* device);
/**
* @brief Send a continue message via the given device.
*
* @param device the device to use for sending
*/
void midi_send_continue(MidiDevice * device);
void midi_send_continue(MidiDevice* device);
/**
* @brief Send a stop message via the given device.
*
* @param device the device to use for sending
*/
void midi_send_stop(MidiDevice * device);
void midi_send_stop(MidiDevice* device);
/**
* @brief Send an active sense message via the given device.
*
* @param device the device to use for sending
*/
void midi_send_activesense(MidiDevice * device);
void midi_send_activesense(MidiDevice* device);
/**
* @brief Send a reset message via the given device.
*
* @param device the device to use for sending
*/
void midi_send_reset(MidiDevice * device);
void midi_send_reset(MidiDevice* device);
/**
* @brief Send a tc quarter frame message via the given device.
@ -192,7 +191,7 @@ void midi_send_reset(MidiDevice * device);
* @param device the device to use for sending
* @param time the time of this quarter frame, range 0..16383
*/
void midi_send_tcquarterframe(MidiDevice * device, uint8_t time);
void midi_send_tcquarterframe(MidiDevice* device, uint8_t time);
/**
* @brief Send a song position message via the given device.
@ -200,7 +199,7 @@ void midi_send_tcquarterframe(MidiDevice * device, uint8_t time);
* @param device the device to use for sending
* @param pos the song position
*/
void midi_send_songposition(MidiDevice * device, uint16_t pos);
void midi_send_songposition(MidiDevice* device, uint16_t pos);
/**
* @brief Send a song select message via the given device.
@ -208,14 +207,14 @@ void midi_send_songposition(MidiDevice * device, uint16_t pos);
* @param device the device to use for sending
* @param song the song to select
*/
void midi_send_songselect(MidiDevice * device, uint8_t song);
void midi_send_songselect(MidiDevice* device, uint8_t song);
/**
* @brief Send a tune request message via the given device.
*
* @param device the device to use for sending
*/
void midi_send_tunerequest(MidiDevice * device);
void midi_send_tunerequest(MidiDevice* device);
/**
* @brief Send a byte via the given device.
@ -228,7 +227,7 @@ void midi_send_tunerequest(MidiDevice * device);
* @param device the device to use for sending
* @param b the byte to send
*/
void midi_send_byte(MidiDevice * device, uint8_t b);
void midi_send_byte(MidiDevice* device, uint8_t b);
/**
* @brief Send up to 3 bytes of data
@ -241,7 +240,7 @@ void midi_send_byte(MidiDevice * device, uint8_t b);
* @param byte1 the second byte, ignored if cnt % 4 != 2
* @param byte2 the third byte, ignored if cnt % 4 != 3
*/
void midi_send_data(MidiDevice * device, uint16_t count, uint8_t byte0, uint8_t byte1, uint8_t byte2);
void midi_send_data(MidiDevice* device, uint16_t count, uint8_t byte0, uint8_t byte1, uint8_t byte2);
/**
* @brief Send an array of formatted midi data.
@ -252,14 +251,13 @@ void midi_send_data(MidiDevice * device, uint16_t count, uint8_t byte0, uint8_t
* @param count the count of bytes to send
* @param array the array of bytes
*/
void midi_send_array(MidiDevice * device, uint16_t count, uint8_t * array);
void midi_send_array(MidiDevice* device, uint16_t count, uint8_t* array);
/**@}*/
/**
* @defgroup input_callback_reg Input callback registration functions
*
*
* @brief These are the functions you use to register your input callbacks.
*
* The functions are called when the appropriate midi message is matched on the
@ -268,7 +266,7 @@ void midi_send_array(MidiDevice * device, uint16_t count, uint8_t * array);
* @{
*/
//three byte funcs
// three byte funcs
/**
* @brief Register a control change message (cc) callback.
@ -276,7 +274,7 @@ void midi_send_array(MidiDevice * device, uint16_t count, uint8_t * array);
* @param device the device associate with
* @param func the callback function to register
*/
void midi_register_cc_callback(MidiDevice * device, midi_three_byte_func_t func);
void midi_register_cc_callback(MidiDevice* device, midi_three_byte_func_t func);
/**
* @brief Register a note on callback.
@ -284,7 +282,7 @@ void midi_register_cc_callback(MidiDevice * device, midi_three_byte_func_t func)
* @param device the device associate with
* @param func the callback function to register
*/
void midi_register_noteon_callback(MidiDevice * device, midi_three_byte_func_t func);
void midi_register_noteon_callback(MidiDevice* device, midi_three_byte_func_t func);
/**
* @brief Register a note off callback.
@ -292,7 +290,7 @@ void midi_register_noteon_callback(MidiDevice * device, midi_three_byte_func_t f
* @param device the device associate with
* @param func the callback function to register
*/
void midi_register_noteoff_callback(MidiDevice * device, midi_three_byte_func_t func);
void midi_register_noteoff_callback(MidiDevice* device, midi_three_byte_func_t func);
/**
* @brief Register an after touch callback.
@ -301,7 +299,7 @@ void midi_register_noteoff_callback(MidiDevice * device, midi_three_byte_func_t
* @param func the callback function to register
*/
void midi_register_aftertouch_callback(MidiDevice * device, midi_three_byte_func_t func);
void midi_register_aftertouch_callback(MidiDevice* device, midi_three_byte_func_t func);
/**
* @brief Register a pitch bend callback.
@ -309,7 +307,7 @@ void midi_register_aftertouch_callback(MidiDevice * device, midi_three_byte_func
* @param device the device associate with
* @param func the callback function to register
*/
void midi_register_pitchbend_callback(MidiDevice * device, midi_three_byte_func_t func);
void midi_register_pitchbend_callback(MidiDevice* device, midi_three_byte_func_t func);
/**
* @brief Register a song position callback.
@ -317,9 +315,9 @@ void midi_register_pitchbend_callback(MidiDevice * device, midi_three_byte_func_
* @param device the device associate with
* @param func the callback function to register
*/
void midi_register_songposition_callback(MidiDevice * device, midi_three_byte_func_t func);
void midi_register_songposition_callback(MidiDevice* device, midi_three_byte_func_t func);
//two byte funcs
// two byte funcs
/**
* @brief Register a program change callback.
@ -327,7 +325,7 @@ void midi_register_songposition_callback(MidiDevice * device, midi_three_byte_fu
* @param device the device associate with
* @param func the callback function to register
*/
void midi_register_progchange_callback(MidiDevice * device, midi_two_byte_func_t func);
void midi_register_progchange_callback(MidiDevice* device, midi_two_byte_func_t func);
/**
* @brief Register a channel pressure callback.
@ -335,7 +333,7 @@ void midi_register_progchange_callback(MidiDevice * device, midi_two_byte_func_t
* @param device the device associate with
* @param func the callback function to register
*/
void midi_register_chanpressure_callback(MidiDevice * device, midi_two_byte_func_t func);
void midi_register_chanpressure_callback(MidiDevice* device, midi_two_byte_func_t func);
/**
* @brief Register a song select callback.
@ -343,7 +341,7 @@ void midi_register_chanpressure_callback(MidiDevice * device, midi_two_byte_func
* @param device the device associate with
* @param func the callback function to register
*/
void midi_register_songselect_callback(MidiDevice * device, midi_two_byte_func_t func);
void midi_register_songselect_callback(MidiDevice* device, midi_two_byte_func_t func);
/**
* @brief Register a tc quarter frame callback.
@ -351,9 +349,9 @@ void midi_register_songselect_callback(MidiDevice * device, midi_two_byte_func_t
* @param device the device associate with
* @param func the callback function to register
*/
void midi_register_tc_quarterframe_callback(MidiDevice * device, midi_two_byte_func_t func);
void midi_register_tc_quarterframe_callback(MidiDevice* device, midi_two_byte_func_t func);
//one byte funcs
// one byte funcs
/**
* @brief Register a realtime callback.
@ -363,7 +361,7 @@ void midi_register_tc_quarterframe_callback(MidiDevice * device, midi_two_byte_f
* @param device the device associate with
* @param func the callback function to register
*/
void midi_register_realtime_callback(MidiDevice * device, midi_one_byte_func_t func);
void midi_register_realtime_callback(MidiDevice* device, midi_one_byte_func_t func);
/**
* @brief Register a tune request callback.
@ -371,7 +369,7 @@ void midi_register_realtime_callback(MidiDevice * device, midi_one_byte_func_t f
* @param device the device associate with
* @param func the callback function to register
*/
void midi_register_tunerequest_callback(MidiDevice * device, midi_one_byte_func_t func);
void midi_register_tunerequest_callback(MidiDevice* device, midi_one_byte_func_t func);
/**
* @brief Register a sysex callback.
@ -379,7 +377,7 @@ void midi_register_tunerequest_callback(MidiDevice * device, midi_one_byte_func_
* @param device the device associate with
* @param func the callback function to register
*/
void midi_register_sysex_callback(MidiDevice * device, midi_sysex_func_t func);
void midi_register_sysex_callback(MidiDevice* device, midi_sysex_func_t func);
/**
* @brief Register fall through callback.
@ -391,8 +389,7 @@ void midi_register_sysex_callback(MidiDevice * device, midi_sysex_func_t func);
* @param device the device associate with
* @param func the callback function to register
*/
void midi_register_fallthrough_callback(MidiDevice * device, midi_var_byte_func_t func);
void midi_register_fallthrough_callback(MidiDevice* device, midi_var_byte_func_t func);
/**
* @brief Register a catch all callback.
@ -403,7 +400,7 @@ void midi_register_fallthrough_callback(MidiDevice * device, midi_var_byte_func_
* @param device the device associate with
* @param func the callback function to register
*/
void midi_register_catchall_callback(MidiDevice * device, midi_var_byte_func_t func);
void midi_register_catchall_callback(MidiDevice* device, midi_var_byte_func_t func);
/**@}*/
@ -417,11 +414,7 @@ void midi_register_catchall_callback(MidiDevice * device, midi_var_byte_func_t f
*
* An enumeration of the possible packet length values.
*/
typedef enum {
UNDEFINED = 0,
ONE = 1,
TWO = 2,
THREE = 3} midi_packet_length_t;
typedef enum { UNDEFINED = 0, ONE = 1, TWO = 2, THREE = 3 } midi_packet_length_t;
/**
* @brief Test to see if the byte given is a status byte
@ -456,11 +449,11 @@ midi_packet_length_t midi_packet_length(uint8_t status);
#define SYSEX_BEGIN 0xF0
#define SYSEX_END 0xF7
//if you and this with a byte and you get anything non-zero
//it is a status message
// if you and this with a byte and you get anything non-zero
// it is a status message
#define MIDI_STATUSMASK 0x80
//if you and this with a status message that contains channel info,
//you'll get the channel
// if you and this with a status message that contains channel info,
// you'll get the channel
#define MIDI_CHANMASK 0x0F
#define MIDI_CC 0xB0
@ -471,7 +464,7 @@ midi_packet_length_t midi_packet_length(uint8_t status);
#define MIDI_PROGCHANGE 0xC0
#define MIDI_CHANPRESSURE 0xD0
//midi realtime
// midi realtime
#define MIDI_CLOCK 0xF8
#define MIDI_TICK 0xF9
#define MIDI_START 0xFA
@ -485,14 +478,13 @@ midi_packet_length_t midi_packet_length(uint8_t status);
#define MIDI_SONGSELECT 0xF3
#define MIDI_TUNEREQUEST 0xF6
//This ID is for educational or development use only
// This ID is for educational or development use only
#define SYSEX_EDUMANUFID 0x7D
/**@}*/
#ifdef __cplusplus
}
#endif
#endif
#endif

483
tmk_core/protocol/midi/midi_device.c Executable file → Normal file
View file

@ -1,291 +1,272 @@
//midi for embedded chips,
//Copyright 2010 Alex Norman
// midi for embedded chips,
// Copyright 2010 Alex Norman
//
//This file is part of avr-midi.
// This file is part of avr-midi.
//
//avr-midi is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
// avr-midi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//avr-midi is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
// avr-midi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with avr-midi. If not, see <http://www.gnu.org/licenses/>.
// You should have received a copy of the GNU General Public License
// along with avr-midi. If not, see <http://www.gnu.org/licenses/>.
#include "midi_device.h"
#include "midi.h"
#ifndef NULL
#define NULL 0
# define NULL 0
#endif
//forward declarations, internally used to call the callbacks
void midi_input_callbacks(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2);
void midi_process_byte(MidiDevice * device, uint8_t input);
// forward declarations, internally used to call the callbacks
void midi_input_callbacks(MidiDevice* device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2);
void midi_process_byte(MidiDevice* device, uint8_t input);
void midi_device_init(MidiDevice * device){
device->input_state = IDLE;
device->input_count = 0;
bytequeue_init(&device->input_queue, device->input_queue_data, MIDI_INPUT_QUEUE_LENGTH);
void midi_device_init(MidiDevice* device) {
device->input_state = IDLE;
device->input_count = 0;
bytequeue_init(&device->input_queue, device->input_queue_data, MIDI_INPUT_QUEUE_LENGTH);
//three byte funcs
device->input_cc_callback = NULL;
device->input_noteon_callback = NULL;
device->input_noteoff_callback = NULL;
device->input_aftertouch_callback = NULL;
device->input_pitchbend_callback = NULL;
device->input_songposition_callback = NULL;
// three byte funcs
device->input_cc_callback = NULL;
device->input_noteon_callback = NULL;
device->input_noteoff_callback = NULL;
device->input_aftertouch_callback = NULL;
device->input_pitchbend_callback = NULL;
device->input_songposition_callback = NULL;
//two byte funcs
device->input_progchange_callback = NULL;
device->input_chanpressure_callback = NULL;
device->input_songselect_callback = NULL;
device->input_tc_quarterframe_callback = NULL;
// two byte funcs
device->input_progchange_callback = NULL;
device->input_chanpressure_callback = NULL;
device->input_songselect_callback = NULL;
device->input_tc_quarterframe_callback = NULL;
//one byte funcs
device->input_realtime_callback = NULL;
device->input_tunerequest_callback = NULL;
// one byte funcs
device->input_realtime_callback = NULL;
device->input_tunerequest_callback = NULL;
//var byte functions
device->input_sysex_callback = NULL;
device->input_fallthrough_callback = NULL;
device->input_catchall_callback = NULL;
// var byte functions
device->input_sysex_callback = NULL;
device->input_fallthrough_callback = NULL;
device->input_catchall_callback = NULL;
device->pre_input_process_callback = NULL;
device->pre_input_process_callback = NULL;
}
void midi_device_input(MidiDevice * device, uint8_t cnt, uint8_t * input) {
uint8_t i;
for (i = 0; i < cnt; i++)
bytequeue_enqueue(&device->input_queue, input[i]);
void midi_device_input(MidiDevice* device, uint8_t cnt, uint8_t* input) {
uint8_t i;
for (i = 0; i < cnt; i++) bytequeue_enqueue(&device->input_queue, input[i]);
}
void midi_device_set_send_func(MidiDevice * device, midi_var_byte_func_t send_func){
device->send_func = send_func;
}
void midi_device_set_send_func(MidiDevice* device, midi_var_byte_func_t send_func) { device->send_func = send_func; }
void midi_device_set_pre_input_process_func(MidiDevice * device, midi_no_byte_func_t pre_process_func){
device->pre_input_process_callback = pre_process_func;
}
void midi_device_set_pre_input_process_func(MidiDevice* device, midi_no_byte_func_t pre_process_func) { device->pre_input_process_callback = pre_process_func; }
void midi_device_process(MidiDevice * device) {
//call the pre_input_process_callback if there is one
if(device->pre_input_process_callback)
device->pre_input_process_callback(device);
void midi_device_process(MidiDevice* device) {
// call the pre_input_process_callback if there is one
if (device->pre_input_process_callback) device->pre_input_process_callback(device);
//pull stuff off the queue and process
byteQueueIndex_t len = bytequeue_length(&device->input_queue);
uint16_t i;
//TODO limit number of bytes processed?
for(i = 0; i < len; i++) {
uint8_t val = bytequeue_get(&device->input_queue, 0);
midi_process_byte(device, val);
bytequeue_remove(&device->input_queue, 1);
}
}
void midi_process_byte(MidiDevice * device, uint8_t input) {
if (midi_is_realtime(input)) {
//call callback, store and restore state
input_state_t state = device->input_state;
device->input_state = ONE_BYTE_MESSAGE;
midi_input_callbacks(device, 1, input, 0, 0);
device->input_state = state;
} else if (midi_is_statusbyte(input)) {
//store the byte
if (device->input_state != SYSEX_MESSAGE) {
device->input_buffer[0] = input;
device->input_count = 1;
// pull stuff off the queue and process
byteQueueIndex_t len = bytequeue_length(&device->input_queue);
uint16_t i;
// TODO limit number of bytes processed?
for (i = 0; i < len; i++) {
uint8_t val = bytequeue_get(&device->input_queue, 0);
midi_process_byte(device, val);
bytequeue_remove(&device->input_queue, 1);
}
switch (midi_packet_length(input)) {
case ONE:
device->input_state = ONE_BYTE_MESSAGE;;
}
void midi_process_byte(MidiDevice* device, uint8_t input) {
if (midi_is_realtime(input)) {
// call callback, store and restore state
input_state_t state = device->input_state;
device->input_state = ONE_BYTE_MESSAGE;
midi_input_callbacks(device, 1, input, 0, 0);
device->input_state = IDLE;
break;
case TWO:
device->input_state = TWO_BYTE_MESSAGE;
break;
case THREE:
device->input_state = THREE_BYTE_MESSAGE;
break;
case UNDEFINED:
switch(input) {
case SYSEX_BEGIN:
device->input_state = SYSEX_MESSAGE;
device->input_state = state;
} else if (midi_is_statusbyte(input)) {
// store the byte
if (device->input_state != SYSEX_MESSAGE) {
device->input_buffer[0] = input;
device->input_count = 1;
break;
case SYSEX_END:
//send what is left in the input buffer, set idle
device->input_count = 1;
}
switch (midi_packet_length(input)) {
case ONE:
device->input_state = ONE_BYTE_MESSAGE;
;
midi_input_callbacks(device, 1, input, 0, 0);
device->input_state = IDLE;
break;
case TWO:
device->input_state = TWO_BYTE_MESSAGE;
break;
case THREE:
device->input_state = THREE_BYTE_MESSAGE;
break;
case UNDEFINED:
switch (input) {
case SYSEX_BEGIN:
device->input_state = SYSEX_MESSAGE;
device->input_buffer[0] = input;
device->input_count = 1;
break;
case SYSEX_END:
// send what is left in the input buffer, set idle
device->input_buffer[device->input_count % 3] = input;
device->input_count += 1;
// call the callback
midi_input_callbacks(device, device->input_count, device->input_buffer[0], device->input_buffer[1], device->input_buffer[2]);
device->input_state = IDLE;
break;
default:
device->input_state = IDLE;
device->input_count = 0;
}
break;
default:
device->input_state = IDLE;
device->input_count = 0;
break;
}
} else {
if (device->input_state != IDLE) {
// store the byte
device->input_buffer[device->input_count % 3] = input;
// increment count
uint16_t prev = device->input_count;
device->input_count += 1;
//call the callback
midi_input_callbacks(device, device->input_count,
device->input_buffer[0], device->input_buffer[1], device->input_buffer[2]);
device->input_state = IDLE;
break;
default:
device->input_state = IDLE;
device->input_count = 0;
}
break;
default:
device->input_state = IDLE;
device->input_count = 0;
break;
}
} else {
if (device->input_state != IDLE) {
//store the byte
device->input_buffer[device->input_count % 3] = input;
//increment count
uint16_t prev = device->input_count;
device->input_count += 1;
switch(prev % 3) {
case 2:
//call callback
midi_input_callbacks(device, device->input_count,
device->input_buffer[0], device->input_buffer[1], device->input_buffer[2]);
if (device->input_state != SYSEX_MESSAGE) {
//set to 1, keeping status byte, allowing for running status
device->input_count = 1;
}
break;
case 1:
if (device->input_state == TWO_BYTE_MESSAGE) {
//call callback
midi_input_callbacks(device, device->input_count,
device->input_buffer[0], device->input_buffer[1], 0);
if (device->input_state != SYSEX_MESSAGE) {
//set to 1, keeping status byte, allowing for running status
device->input_count = 1;
switch (prev % 3) {
case 2:
// call callback
midi_input_callbacks(device, device->input_count, device->input_buffer[0], device->input_buffer[1], device->input_buffer[2]);
if (device->input_state != SYSEX_MESSAGE) {
// set to 1, keeping status byte, allowing for running status
device->input_count = 1;
}
break;
case 1:
if (device->input_state == TWO_BYTE_MESSAGE) {
// call callback
midi_input_callbacks(device, device->input_count, device->input_buffer[0], device->input_buffer[1], 0);
if (device->input_state != SYSEX_MESSAGE) {
// set to 1, keeping status byte, allowing for running status
device->input_count = 1;
}
}
break;
case 0:
default:
// one byte messages are dealt with directly
break;
}
}
break;
case 0:
default:
//one byte messages are dealt with directly
break;
}
}
}
}
}
void midi_input_callbacks(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) {
//did we end up calling a callback?
bool called = false;
if (device->input_state == SYSEX_MESSAGE) {
if (device->input_sysex_callback) {
const uint16_t start = ((cnt - 1) / 3) * 3;
const uint8_t length = (cnt - start);
uint8_t data[3];
data[0] = byte0;
data[1] = byte1;
data[2] = byte2;
device->input_sysex_callback(device, start, length, data);
called = true;
}
} else {
switch (cnt) {
case 3:
{
midi_three_byte_func_t func = NULL;
switch (byte0 & 0xF0) {
case MIDI_CC:
func = device->input_cc_callback;
break;
case MIDI_NOTEON:
func = device->input_noteon_callback;
break;
case MIDI_NOTEOFF:
func = device->input_noteoff_callback;
break;
case MIDI_AFTERTOUCH:
func = device->input_aftertouch_callback;
break;
case MIDI_PITCHBEND:
func = device->input_pitchbend_callback;
break;
case 0xF0:
if (byte0 == MIDI_SONGPOSITION)
func = device->input_songposition_callback;
break;
void midi_input_callbacks(MidiDevice* device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) {
// did we end up calling a callback?
bool called = false;
if (device->input_state == SYSEX_MESSAGE) {
if (device->input_sysex_callback) {
const uint16_t start = ((cnt - 1) / 3) * 3;
const uint8_t length = (cnt - start);
uint8_t data[3];
data[0] = byte0;
data[1] = byte1;
data[2] = byte2;
device->input_sysex_callback(device, start, length, data);
called = true;
}
} else {
switch (cnt) {
case 3: {
midi_three_byte_func_t func = NULL;
switch (byte0 & 0xF0) {
case MIDI_CC:
func = device->input_cc_callback;
break;
case MIDI_NOTEON:
func = device->input_noteon_callback;
break;
case MIDI_NOTEOFF:
func = device->input_noteoff_callback;
break;
case MIDI_AFTERTOUCH:
func = device->input_aftertouch_callback;
break;
case MIDI_PITCHBEND:
func = device->input_pitchbend_callback;
break;
case 0xF0:
if (byte0 == MIDI_SONGPOSITION) func = device->input_songposition_callback;
break;
default:
break;
}
if (func) {
// mask off the channel for non song position functions
if (byte0 == MIDI_SONGPOSITION)
func(device, byte0, byte1, byte2);
else
func(device, byte0 & 0x0F, byte1, byte2);
called = true;
}
} break;
case 2: {
midi_two_byte_func_t func = NULL;
switch (byte0 & 0xF0) {
case MIDI_PROGCHANGE:
func = device->input_progchange_callback;
break;
case MIDI_CHANPRESSURE:
func = device->input_chanpressure_callback;
break;
case 0xF0:
if (byte0 == MIDI_SONGSELECT)
func = device->input_songselect_callback;
else if (byte0 == MIDI_TC_QUARTERFRAME)
func = device->input_tc_quarterframe_callback;
break;
default:
break;
}
if (func) {
// mask off the channel
if (byte0 == MIDI_SONGSELECT || byte0 == MIDI_TC_QUARTERFRAME)
func(device, byte0, byte1);
else
func(device, byte0 & 0x0F, byte1);
called = true;
}
} break;
case 1: {
midi_one_byte_func_t func = NULL;
if (midi_is_realtime(byte0))
func = device->input_realtime_callback;
else if (byte0 == MIDI_TUNEREQUEST)
func = device->input_tunerequest_callback;
if (func) {
func(device, byte0);
called = true;
}
} break;
default:
break;
}
if(func) {
//mask off the channel for non song position functions
if (byte0 == MIDI_SONGPOSITION)
func(device, byte0, byte1, byte2);
else
func(device, byte0 & 0x0F, byte1, byte2);
called = true;
}
// just in case
if (cnt > 3) cnt = 0;
break;
}
break;
case 2:
{
midi_two_byte_func_t func = NULL;
switch (byte0 & 0xF0) {
case MIDI_PROGCHANGE:
func = device->input_progchange_callback;
break;
case MIDI_CHANPRESSURE:
func = device->input_chanpressure_callback;
break;
case 0xF0:
if (byte0 == MIDI_SONGSELECT)
func = device->input_songselect_callback;
else if (byte0 == MIDI_TC_QUARTERFRAME)
func = device->input_tc_quarterframe_callback;
break;
default:
break;
}
if(func) {
//mask off the channel
if (byte0 == MIDI_SONGSELECT || byte0 == MIDI_TC_QUARTERFRAME)
func(device, byte0, byte1);
else
func(device, byte0 & 0x0F, byte1);
called = true;
}
}
break;
case 1:
{
midi_one_byte_func_t func = NULL;
if (midi_is_realtime(byte0))
func = device->input_realtime_callback;
else if (byte0 == MIDI_TUNEREQUEST)
func = device->input_tunerequest_callback;
if (func) {
func(device, byte0);
called = true;
}
}
break;
default:
//just in case
if (cnt > 3)
cnt = 0;
break;
}
}
//if there is fallthrough default callback and we haven't called a more specific one,
//call the fallthrough
if (!called && device->input_fallthrough_callback)
device->input_fallthrough_callback(device, cnt, byte0, byte1, byte2);
//always call the catch all if it exists
if (device->input_catchall_callback)
device->input_catchall_callback(device, cnt, byte0, byte1, byte2);
// if there is fallthrough default callback and we haven't called a more specific one,
// call the fallthrough
if (!called && device->input_fallthrough_callback) device->input_fallthrough_callback(device, cnt, byte0, byte1, byte2);
// always call the catch all if it exists
if (device->input_catchall_callback) device->input_catchall_callback(device, cnt, byte0, byte1, byte2);
}

109
tmk_core/protocol/midi/midi_device.h Executable file → Normal file
View file

@ -1,20 +1,20 @@
//midi for embedded chips,
//Copyright 2010 Alex Norman
// midi for embedded chips,
// Copyright 2010 Alex Norman
//
//This file is part of avr-midi.
// This file is part of avr-midi.
//
//avr-midi is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
// avr-midi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//avr-midi is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
// avr-midi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with avr-midi. If not, see <http://www.gnu.org/licenses/>.
// You should have received a copy of the GNU General Public License
// along with avr-midi. If not, see <http://www.gnu.org/licenses/>.
/**
* @file
@ -26,7 +26,7 @@
#ifdef __cplusplus
extern "C" {
#endif
#endif
/**
* @defgroup midi_device Functions used when implementing your own midi device.
@ -51,14 +51,9 @@ extern "C" {
#include "bytequeue/bytequeue.h"
#define MIDI_INPUT_QUEUE_LENGTH 192
typedef enum {
IDLE,
ONE_BYTE_MESSAGE = 1,
TWO_BYTE_MESSAGE = 2,
THREE_BYTE_MESSAGE = 3,
SYSEX_MESSAGE} input_state_t;
typedef enum { IDLE, ONE_BYTE_MESSAGE = 1, TWO_BYTE_MESSAGE = 2, THREE_BYTE_MESSAGE = 3, SYSEX_MESSAGE } input_state_t;
typedef void (* midi_no_byte_func_t)(MidiDevice * device);
typedef void (*midi_no_byte_func_t)(MidiDevice* device);
/**
* \struct _midi_device
@ -71,45 +66,45 @@ typedef void (* midi_no_byte_func_t)(MidiDevice * device);
* You should not need to modify this structure directly.
*/
struct _midi_device {
//output send function
midi_var_byte_func_t send_func;
// output send function
midi_var_byte_func_t send_func;
//********input callbacks
//three byte funcs
midi_three_byte_func_t input_cc_callback;
midi_three_byte_func_t input_noteon_callback;
midi_three_byte_func_t input_noteoff_callback;
midi_three_byte_func_t input_aftertouch_callback;
midi_three_byte_func_t input_pitchbend_callback;
midi_three_byte_func_t input_songposition_callback;
//two byte funcs
midi_two_byte_func_t input_progchange_callback;
midi_two_byte_func_t input_chanpressure_callback;
midi_two_byte_func_t input_songselect_callback;
midi_two_byte_func_t input_tc_quarterframe_callback;
//one byte funcs
midi_one_byte_func_t input_realtime_callback;
midi_one_byte_func_t input_tunerequest_callback;
//********input callbacks
// three byte funcs
midi_three_byte_func_t input_cc_callback;
midi_three_byte_func_t input_noteon_callback;
midi_three_byte_func_t input_noteoff_callback;
midi_three_byte_func_t input_aftertouch_callback;
midi_three_byte_func_t input_pitchbend_callback;
midi_three_byte_func_t input_songposition_callback;
// two byte funcs
midi_two_byte_func_t input_progchange_callback;
midi_two_byte_func_t input_chanpressure_callback;
midi_two_byte_func_t input_songselect_callback;
midi_two_byte_func_t input_tc_quarterframe_callback;
// one byte funcs
midi_one_byte_func_t input_realtime_callback;
midi_one_byte_func_t input_tunerequest_callback;
//sysex
midi_sysex_func_t input_sysex_callback;
// sysex
midi_sysex_func_t input_sysex_callback;
//only called if more specific callback is not matched
midi_var_byte_func_t input_fallthrough_callback;
//called if registered, independent of other callbacks
midi_var_byte_func_t input_catchall_callback;
// only called if more specific callback is not matched
midi_var_byte_func_t input_fallthrough_callback;
// called if registered, independent of other callbacks
midi_var_byte_func_t input_catchall_callback;
//pre input processing function
midi_no_byte_func_t pre_input_process_callback;
// pre input processing function
midi_no_byte_func_t pre_input_process_callback;
//for internal input processing
uint8_t input_buffer[3];
input_state_t input_state;
uint16_t input_count;
// for internal input processing
uint8_t input_buffer[3];
input_state_t input_state;
uint16_t input_count;
//for queueing data between the input and the processing functions
uint8_t input_queue_data[MIDI_INPUT_QUEUE_LENGTH];
byteQueue_t input_queue;
// for queueing data between the input and the processing functions
uint8_t input_queue_data[MIDI_INPUT_QUEUE_LENGTH];
byteQueue_t input_queue;
};
/**
@ -122,7 +117,7 @@ struct _midi_device {
* @param cnt the number of bytes you are processing
* @param input the bytes to process
*/
void midi_device_input(MidiDevice * device, uint8_t cnt, uint8_t * input);
void midi_device_input(MidiDevice* device, uint8_t cnt, uint8_t* input);
/**
* @brief Set the callback function that will be used for sending output
@ -134,7 +129,7 @@ void midi_device_input(MidiDevice * device, uint8_t cnt, uint8_t * input);
* \param device the midi device to associate this callback with
* \param send_func the callback function that will do the sending
*/
void midi_device_set_send_func(MidiDevice * device, midi_var_byte_func_t send_func);
void midi_device_set_send_func(MidiDevice* device, midi_var_byte_func_t send_func);
/**
* @brief Set a callback which is called at the beginning of the
@ -145,12 +140,12 @@ void midi_device_set_send_func(MidiDevice * device, midi_var_byte_func_t send_fu
* \param device the midi device to associate this callback with
* \param midi_no_byte_func_t the actual callback function
*/
void midi_device_set_pre_input_process_func(MidiDevice * device, midi_no_byte_func_t pre_process_func);
void midi_device_set_pre_input_process_func(MidiDevice* device, midi_no_byte_func_t pre_process_func);
/**@}*/
#ifdef __cplusplus
}
#endif
#endif
#endif

44
tmk_core/protocol/midi/midi_function_types.h Executable file → Normal file
View file

@ -1,20 +1,20 @@
//midi for embedded chips,
//Copyright 2010 Alex Norman
// midi for embedded chips,
// Copyright 2010 Alex Norman
//
//This file is part of avr-midi.
// This file is part of avr-midi.
//
//avr-midi is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
// avr-midi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//avr-midi is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
// avr-midi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with avr-midi. If not, see <http://www.gnu.org/licenses/>.
// You should have received a copy of the GNU General Public License
// along with avr-midi. If not, see <http://www.gnu.org/licenses/>.
/**
* @file
@ -26,25 +26,25 @@
#ifdef __cplusplus
extern "C" {
#endif
#endif
#include <inttypes.h>
#include <stdbool.h>
//forward declaration
// forward declaration
typedef struct _midi_device MidiDevice;
typedef void (* midi_one_byte_func_t)(MidiDevice * device, uint8_t byte);
typedef void (* midi_two_byte_func_t)(MidiDevice * device, uint8_t byte0, uint8_t byte1);
typedef void (* midi_three_byte_func_t)(MidiDevice * device, uint8_t byte0, uint8_t byte1, uint8_t byte2);
//all bytes after count bytes should be ignored
typedef void (* midi_var_byte_func_t)(MidiDevice * device, uint16_t count, uint8_t byte0, uint8_t byte1, uint8_t byte2);
typedef void (*midi_one_byte_func_t)(MidiDevice *device, uint8_t byte);
typedef void (*midi_two_byte_func_t)(MidiDevice *device, uint8_t byte0, uint8_t byte1);
typedef void (*midi_three_byte_func_t)(MidiDevice *device, uint8_t byte0, uint8_t byte1, uint8_t byte2);
// all bytes after count bytes should be ignored
typedef void (*midi_var_byte_func_t)(MidiDevice *device, uint16_t count, uint8_t byte0, uint8_t byte1, uint8_t byte2);
//the start byte tells you how far into the sysex message you are, the data_length tells you how many bytes data is
typedef void (* midi_sysex_func_t)(MidiDevice * device, uint16_t start_byte, uint8_t data_length, uint8_t *data);
// the start byte tells you how far into the sysex message you are, the data_length tells you how many bytes data is
typedef void (*midi_sysex_func_t)(MidiDevice *device, uint16_t start_byte, uint8_t data_length, uint8_t *data);
#ifdef __cplusplus
}
#endif
#endif
#endif

View file

@ -5,7 +5,7 @@
#include "usb_descriptor.h"
#include "process_midi.h"
#if API_SYSEX_ENABLE
#include "api.h"
# include "api.h"
#endif
/*******************************************************************************
@ -23,162 +23,154 @@ MidiDevice midi_device;
#define SYS_COMMON_2 0x20
#define SYS_COMMON_3 0x30
static void usb_send_func(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) {
MIDI_EventPacket_t event;
event.Data1 = byte0;
event.Data2 = byte1;
event.Data3 = byte2;
static void usb_send_func(MidiDevice* device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) {
MIDI_EventPacket_t event;
event.Data1 = byte0;
event.Data2 = byte1;
event.Data3 = byte2;
uint8_t cable = 0;
uint8_t cable = 0;
//if the length is undefined we assume it is a SYSEX message
if (midi_packet_length(byte0) == UNDEFINED) {
switch(cnt) {
case 3:
if (byte2 == SYSEX_END)
event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_3);
else
event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT);
break;
case 2:
if (byte1 == SYSEX_END)
event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_2);
else
event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT);
break;
case 1:
if (byte0 == SYSEX_END)
event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_1);
else
event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT);
break;
default:
return; //invalid cnt
// if the length is undefined we assume it is a SYSEX message
if (midi_packet_length(byte0) == UNDEFINED) {
switch (cnt) {
case 3:
if (byte2 == SYSEX_END)
event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_3);
else
event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT);
break;
case 2:
if (byte1 == SYSEX_END)
event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_2);
else
event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT);
break;
case 1:
if (byte0 == SYSEX_END)
event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_1);
else
event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT);
break;
default:
return; // invalid cnt
}
} else {
// deal with 'system common' messages
// TODO are there any more?
switch (byte0 & 0xF0) {
case MIDI_SONGPOSITION:
event.Event = MIDI_EVENT(cable, SYS_COMMON_3);
break;
case MIDI_SONGSELECT:
case MIDI_TC_QUARTERFRAME:
event.Event = MIDI_EVENT(cable, SYS_COMMON_2);
break;
default:
event.Event = MIDI_EVENT(cable, byte0);
break;
}
}
} else {
//deal with 'system common' messages
//TODO are there any more?
switch(byte0 & 0xF0){
case MIDI_SONGPOSITION:
event.Event = MIDI_EVENT(cable, SYS_COMMON_3);
break;
case MIDI_SONGSELECT:
case MIDI_TC_QUARTERFRAME:
event.Event = MIDI_EVENT(cable, SYS_COMMON_2);
break;
default:
event.Event = MIDI_EVENT(cable, byte0);
break;
}
}
send_midi_packet(&event);
send_midi_packet(&event);
}
static void usb_get_midi(MidiDevice * device) {
MIDI_EventPacket_t event;
while (recv_midi_packet(&event)) {
static void usb_get_midi(MidiDevice* device) {
MIDI_EventPacket_t event;
while (recv_midi_packet(&event)) {
midi_packet_length_t length = midi_packet_length(event.Data1);
uint8_t input[3];
input[0] = event.Data1;
input[1] = event.Data2;
input[2] = event.Data3;
if (length == UNDEFINED) {
// sysex
if (event.Event == MIDI_EVENT(0, SYSEX_START_OR_CONT) || event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_3)) {
length = 3;
} else if (event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_2)) {
length = 2;
} else if (event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_1)) {
length = 1;
} else {
// XXX what to do?
}
}
midi_packet_length_t length = midi_packet_length(event.Data1);
uint8_t input[3];
input[0] = event.Data1;
input[1] = event.Data2;
input[2] = event.Data3;
if (length == UNDEFINED) {
//sysex
if (event.Event == MIDI_EVENT(0, SYSEX_START_OR_CONT) || event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_3)) {
length = 3;
} else if (event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_2)) {
length = 2;
} else if(event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_1)) {
length = 1;
} else {
//XXX what to do?
}
// pass the data to the device input function
if (length != UNDEFINED) midi_device_input(device, length, input);
}
//pass the data to the device input function
if (length != UNDEFINED)
midi_device_input(device, length, input);
}
}
static void fallthrough_callback(MidiDevice * device,
uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2){
static void fallthrough_callback(MidiDevice* device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) {
#ifdef AUDIO_ENABLE
if (cnt == 3) {
switch (byte0 & 0xF0) {
case MIDI_NOTEON:
play_note(((double)261.6)*pow(2.0, -4.0)*pow(2.0,(byte1 & 0x7F)/12.0), (byte2 & 0x7F) / 8);
break;
case MIDI_NOTEOFF:
stop_note(((double)261.6)*pow(2.0, -4.0)*pow(2.0,(byte1 & 0x7F)/12.0));
break;
if (cnt == 3) {
switch (byte0 & 0xF0) {
case MIDI_NOTEON:
play_note(((double)261.6) * pow(2.0, -4.0) * pow(2.0, (byte1 & 0x7F) / 12.0), (byte2 & 0x7F) / 8);
break;
case MIDI_NOTEOFF:
stop_note(((double)261.6) * pow(2.0, -4.0) * pow(2.0, (byte1 & 0x7F) / 12.0));
break;
}
}
if (byte0 == MIDI_STOP) {
stop_all_notes();
}
}
if (byte0 == MIDI_STOP) {
stop_all_notes();
}
#endif
}
static void cc_callback(MidiDevice * device,
uint8_t chan, uint8_t num, uint8_t val) {
//sending it back on the next channel
// midi_send_cc(device, (chan + 1) % 16, num, val);
static void cc_callback(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t val) {
// sending it back on the next channel
// midi_send_cc(device, (chan + 1) % 16, num, val);
}
#ifdef API_SYSEX_ENABLE
uint8_t midi_buffer[MIDI_SYSEX_BUFFER] = {0};
static void sysex_callback(MidiDevice * device, uint16_t start, uint8_t length, uint8_t * data) {
// SEND_STRING("\n");
// send_word(start);
// SEND_STRING(": ");
// Don't store the header
int16_t pos = start - 4;
for (uint8_t place = 0; place < length; place++) {
// send_byte(*data);
if (pos >= 0) {
if (*data == 0xF7) {
// SEND_STRING("\nRD: ");
// for (uint8_t i = 0; i < start + place + 1; i++){
// send_byte(midi_buffer[i]);
// SEND_STRING(" ");
// }
const unsigned decoded_length = sysex_decoded_length(pos);
uint8_t decoded[API_SYSEX_MAX_SIZE];
sysex_decode(decoded, midi_buffer, pos);
process_api(decoded_length, decoded);
return;
}
else if (pos >= MIDI_SYSEX_BUFFER) {
return;
}
midi_buffer[pos] = *data;
}
// SEND_STRING(" ");
data++;
pos++;
}
static void sysex_callback(MidiDevice* device, uint16_t start, uint8_t length, uint8_t* data) {
// SEND_STRING("\n");
// send_word(start);
// SEND_STRING(": ");
// Don't store the header
int16_t pos = start - 4;
for (uint8_t place = 0; place < length; place++) {
// send_byte(*data);
if (pos >= 0) {
if (*data == 0xF7) {
// SEND_STRING("\nRD: ");
// for (uint8_t i = 0; i < start + place + 1; i++){
// send_byte(midi_buffer[i]);
// SEND_STRING(" ");
// }
const unsigned decoded_length = sysex_decoded_length(pos);
uint8_t decoded[API_SYSEX_MAX_SIZE];
sysex_decode(decoded, midi_buffer, pos);
process_api(decoded_length, decoded);
return;
} else if (pos >= MIDI_SYSEX_BUFFER) {
return;
}
midi_buffer[pos] = *data;
}
// SEND_STRING(" ");
data++;
pos++;
}
}
#endif
void midi_init(void);
void setup_midi(void)
{
void setup_midi(void) {
#ifdef MIDI_ADVANCED
midi_init();
midi_init();
#endif
midi_device_init(&midi_device);
midi_device_set_send_func(&midi_device, usb_send_func);
midi_device_set_pre_input_process_func(&midi_device, usb_get_midi);
midi_register_fallthrough_callback(&midi_device, fallthrough_callback);
midi_register_cc_callback(&midi_device, cc_callback);
midi_device_init(&midi_device);
midi_device_set_send_func(&midi_device, usb_send_func);
midi_device_set_pre_input_process_func(&midi_device, usb_get_midi);
midi_register_fallthrough_callback(&midi_device, fallthrough_callback);
midi_register_cc_callback(&midi_device, cc_callback);
#ifdef API_SYSEX_ENABLE
midi_register_sysex_callback(&midi_device, sysex_callback);
midi_register_sysex_callback(&midi_device, sysex_callback);
#endif
}

View file

@ -1,9 +1,9 @@
#pragma once
#ifdef MIDI_ENABLE
#include "midi.h"
extern MidiDevice midi_device;
void setup_midi(void);
void send_midi_packet(MIDI_EventPacket_t* event);
bool recv_midi_packet(MIDI_EventPacket_t* const event);
# include "midi.h"
extern MidiDevice midi_device;
void setup_midi(void);
void send_midi_packet(MIDI_EventPacket_t* event);
bool recv_midi_packet(MIDI_EventPacket_t* const event);
#endif

158
tmk_core/protocol/midi/sysex_tools.c Executable file → Normal file
View file

@ -1,99 +1,97 @@
//midi for embedded chips,
//Copyright 2010 Alex Norman
// midi for embedded chips,
// Copyright 2010 Alex Norman
//
//This file is part of avr-midi.
// This file is part of avr-midi.
//
//avr-midi is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
// avr-midi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//avr-midi is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
// avr-midi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with avr-midi. If not, see <http://www.gnu.org/licenses/>.
// You should have received a copy of the GNU General Public License
// along with avr-midi. If not, see <http://www.gnu.org/licenses/>.
#include "sysex_tools.h"
uint16_t sysex_encoded_length(uint16_t decoded_length){
uint8_t remainder = decoded_length % 7;
if (remainder)
return (decoded_length / 7) * 8 + remainder + 1;
else
return (decoded_length / 7) * 8;
uint16_t sysex_encoded_length(uint16_t decoded_length) {
uint8_t remainder = decoded_length % 7;
if (remainder)
return (decoded_length / 7) * 8 + remainder + 1;
else
return (decoded_length / 7) * 8;
}
uint16_t sysex_decoded_length(uint16_t encoded_length){
uint8_t remainder = encoded_length % 8;
if (remainder)
return (encoded_length / 8) * 7 + remainder - 1;
else
return (encoded_length / 8) * 7;
uint16_t sysex_decoded_length(uint16_t encoded_length) {
uint8_t remainder = encoded_length % 8;
if (remainder)
return (encoded_length / 8) * 7 + remainder - 1;
else
return (encoded_length / 8) * 7;
}
uint16_t sysex_encode(uint8_t *encoded, const uint8_t *source, const uint16_t length){
uint16_t encoded_full = length / 7; //number of full 8 byte sections from 7 bytes of input
uint16_t i,j;
uint16_t sysex_encode(uint8_t *encoded, const uint8_t *source, const uint16_t length) {
uint16_t encoded_full = length / 7; // number of full 8 byte sections from 7 bytes of input
uint16_t i, j;
//fill out the fully encoded sections
for(i = 0; i < encoded_full; i++) {
uint16_t encoded_msb_idx = i * 8;
uint16_t input_start_idx = i * 7;
encoded[encoded_msb_idx] = 0;
for(j = 0; j < 7; j++){
uint8_t current = source[input_start_idx + j];
encoded[encoded_msb_idx] |= (0x80 & current) >> (1 + j);
encoded[encoded_msb_idx + 1 + j] = 0x7F & current;
}
}
// fill out the fully encoded sections
for (i = 0; i < encoded_full; i++) {
uint16_t encoded_msb_idx = i * 8;
uint16_t input_start_idx = i * 7;
encoded[encoded_msb_idx] = 0;
for (j = 0; j < 7; j++) {
uint8_t current = source[input_start_idx + j];
encoded[encoded_msb_idx] |= (0x80 & current) >> (1 + j);
encoded[encoded_msb_idx + 1 + j] = 0x7F & current;
}
}
//fill out the rest if there is any more
uint8_t remainder = length % 7;
if (remainder) {
uint16_t encoded_msb_idx = encoded_full * 8;
uint16_t input_start_idx = encoded_full * 7;
encoded[encoded_msb_idx] = 0;
for(j = 0; j < remainder; j++){
uint8_t current = source[input_start_idx + j];
encoded[encoded_msb_idx] |= (0x80 & current) >> (1 + j);
encoded[encoded_msb_idx + 1 + j] = 0x7F & current;
}
return encoded_msb_idx + remainder + 1;
} else {
return encoded_full * 8;
}
// fill out the rest if there is any more
uint8_t remainder = length % 7;
if (remainder) {
uint16_t encoded_msb_idx = encoded_full * 8;
uint16_t input_start_idx = encoded_full * 7;
encoded[encoded_msb_idx] = 0;
for (j = 0; j < remainder; j++) {
uint8_t current = source[input_start_idx + j];
encoded[encoded_msb_idx] |= (0x80 & current) >> (1 + j);
encoded[encoded_msb_idx + 1 + j] = 0x7F & current;
}
return encoded_msb_idx + remainder + 1;
} else {
return encoded_full * 8;
}
}
uint16_t sysex_decode(uint8_t *decoded, const uint8_t *source, const uint16_t length){
uint16_t decoded_full = length / 8;
uint16_t i,j;
uint16_t sysex_decode(uint8_t *decoded, const uint8_t *source, const uint16_t length) {
uint16_t decoded_full = length / 8;
uint16_t i, j;
if (length < 2)
return 0;
if (length < 2) return 0;
//fill out the fully encoded sections
for(i = 0; i < decoded_full; i++) {
uint16_t encoded_msb_idx = i * 8;
uint16_t output_start_index = i * 7;
for(j = 0; j < 7; j++){
decoded[output_start_index + j] = 0x7F & source[encoded_msb_idx + j + 1];
decoded[output_start_index + j] |= (0x80 & (source[encoded_msb_idx] << (1 + j)));
}
}
uint8_t remainder = length % 8;
if (remainder) {
uint16_t encoded_msb_idx = decoded_full * 8;
uint16_t output_start_index = decoded_full * 7;
for(j = 0; j < (remainder - 1); j++) {
decoded[output_start_index + j] = 0x7F & source[encoded_msb_idx + j + 1];
decoded[output_start_index + j] |= (0x80 & (source[encoded_msb_idx] << (1 + j)));
}
return decoded_full * 7 + remainder - 1;
} else {
return decoded_full * 7;
}
// fill out the fully encoded sections
for (i = 0; i < decoded_full; i++) {
uint16_t encoded_msb_idx = i * 8;
uint16_t output_start_index = i * 7;
for (j = 0; j < 7; j++) {
decoded[output_start_index + j] = 0x7F & source[encoded_msb_idx + j + 1];
decoded[output_start_index + j] |= (0x80 & (source[encoded_msb_idx] << (1 + j)));
}
}
uint8_t remainder = length % 8;
if (remainder) {
uint16_t encoded_msb_idx = decoded_full * 8;
uint16_t output_start_index = decoded_full * 7;
for (j = 0; j < (remainder - 1); j++) {
decoded[output_start_index + j] = 0x7F & source[encoded_msb_idx + j + 1];
decoded[output_start_index + j] |= (0x80 & (source[encoded_msb_idx] << (1 + j)));
}
return decoded_full * 7 + remainder - 1;
} else {
return decoded_full * 7;
}
}

34
tmk_core/protocol/midi/sysex_tools.h Executable file → Normal file
View file

@ -1,27 +1,27 @@
//midi for embedded chips,
//Copyright 2010 Alex Norman
// midi for embedded chips,
// Copyright 2010 Alex Norman
//
//This file is part of avr-midi.
// This file is part of avr-midi.
//
//avr-midi is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
// avr-midi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//avr-midi is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
// avr-midi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with avr-midi. If not, see <http://www.gnu.org/licenses/>.
// You should have received a copy of the GNU General Public License
// along with avr-midi. If not, see <http://www.gnu.org/licenses/>.
#ifndef SYSEX_TOOLS_H
#define SYSEX_TOOLS_H
#ifdef __cplusplus
extern "C" {
#endif
#endif
#include <inttypes.h>
@ -31,7 +31,7 @@ extern "C" {
*
* These functions are for converting data to and from a "midi-safe" format,
* which can be use to send data with sysex messages. Sysex messages may only
* contain data where the to bit is not set.
* contain data where the to bit is not set.
*
* An "encoded" midi message is one that contains all of the data from its
* original state, but does not have any of the top bits set.
@ -70,7 +70,7 @@ uint16_t sysex_decoded_length(uint16_t encoded_length);
* @param encoded The output data buffer, must be at least sysex_encoded_length(length) bytes long.
* @param source The input buffer of data to be encoded.
* @param length The number of bytes from the input buffer to encode.
*
*
* @return number of bytes encoded.
*/
uint16_t sysex_encode(uint8_t *encoded, const uint8_t *source, uint16_t length);
@ -81,7 +81,7 @@ uint16_t sysex_encode(uint8_t *encoded, const uint8_t *source, uint16_t length);
* @param decoded The output data buffer, must be at least sysex_decoded_length(length) bytes long.
* @param source The input buffer of data to be decoded.
* @param length The number of bytes from the input buffer to decode.
*
*
* @return number of bytes decoded.
*/
uint16_t sysex_decode(uint8_t *decoded, const uint8_t *source, uint16_t length);
@ -90,6 +90,6 @@ uint16_t sysex_decode(uint8_t *decoded, const uint8_t *source, uint16_t length);
#ifdef __cplusplus
}
#endif
#endif
#endif