midi
This commit is contained in:
93
keyboard/planck/Config/LUFAConfig.h
Executable file
93
keyboard/planck/Config/LUFAConfig.h
Executable file
@ -0,0 +1,93 @@
|
||||
/*
|
||||
LUFA Library
|
||||
Copyright (C) Dean Camera, 2012.
|
||||
|
||||
dean [at] fourwalledcubicle [dot] com
|
||||
www.lufa-lib.org
|
||||
*/
|
||||
|
||||
/*
|
||||
Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
|
||||
|
||||
Permission to use, copy, modify, distribute, and sell this
|
||||
software and its documentation for any purpose is hereby granted
|
||||
without fee, provided that the above copyright notice appear in
|
||||
all copies and that both that the copyright notice and this
|
||||
permission notice and warranty disclaimer appear in supporting
|
||||
documentation, and that the name of the author not be used in
|
||||
advertising or publicity pertaining to distribution of the
|
||||
software without specific, written prior permission.
|
||||
|
||||
The author disclaim all warranties with regard to this
|
||||
software, including all implied warranties of merchantability
|
||||
and fitness. In no event shall the author be liable for any
|
||||
special, indirect or consequential damages or any damages
|
||||
whatsoever resulting from loss of use, data or profits, whether
|
||||
in an action of contract, negligence or other tortious action,
|
||||
arising out of or in connection with the use or performance of
|
||||
this software.
|
||||
*/
|
||||
|
||||
/** \file
|
||||
* \brief LUFA Library Configuration Header File
|
||||
*
|
||||
* This header file is used to configure LUFA's compile time options,
|
||||
* as an alternative to the compile time constants supplied through
|
||||
* a makefile.
|
||||
*
|
||||
* For information on what each token does, refer to the LUFA
|
||||
* manual section "Summary of Compile Tokens".
|
||||
*/
|
||||
|
||||
#ifndef _LUFA_CONFIG_H_
|
||||
#define _LUFA_CONFIG_H_
|
||||
|
||||
#if (ARCH == ARCH_AVR8)
|
||||
|
||||
/* Non-USB Related Configuration Tokens: */
|
||||
// #define DISABLE_TERMINAL_CODES
|
||||
|
||||
/* 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}
|
||||
// #define HID_MAX_COLLECTIONS {Insert Value Here}
|
||||
// #define HID_MAX_REPORTITEMS {Insert Value Here}
|
||||
// #define HID_MAX_REPORT_IDS {Insert Value Here}
|
||||
// #define NO_CLASS_DRIVER_AUTOFLUSH
|
||||
|
||||
/* 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 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: */
|
||||
// #define USE_RAM_DESCRIPTORS
|
||||
#define USE_FLASH_DESCRIPTORS
|
||||
// #define USE_EEPROM_DESCRIPTORS
|
||||
// #define NO_INTERNAL_SERIAL
|
||||
#define FIXED_CONTROL_ENDPOINT_SIZE 8
|
||||
// #define DEVICE_STATE_AS_GPIOR {Insert Value Here}
|
||||
#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: */
|
||||
// #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
|
||||
|
||||
#error Unsupported architecture for this LUFA configuration file.
|
||||
|
||||
#endif
|
||||
#endif
|
674
protocol/lufa/midi/bytequeue/COPYING
Executable file
674
protocol/lufa/midi/bytequeue/COPYING
Executable file
File diff suppressed because it is too large
Load Diff
65
protocol/lufa/midi/bytequeue/bytequeue.c
Executable file
65
protocol/lufa/midi/bytequeue/bytequeue.c
Executable file
@ -0,0 +1,65 @@
|
||||
//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.
|
||||
//
|
||||
//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.
|
||||
//
|
||||
//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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
//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);
|
||||
}
|
||||
|
59
protocol/lufa/midi/bytequeue/bytequeue.h
Executable file
59
protocol/lufa/midi/bytequeue/bytequeue.h
Executable file
@ -0,0 +1,59 @@
|
||||
//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.
|
||||
//
|
||||
//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.
|
||||
//
|
||||
//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
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef uint8_t byteQueueIndex_t;
|
||||
|
||||
typedef struct {
|
||||
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);
|
||||
|
||||
//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);
|
||||
|
||||
//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);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
36
protocol/lufa/midi/bytequeue/interrupt_setting.c
Executable file
36
protocol/lufa/midi/bytequeue/interrupt_setting.c
Executable file
@ -0,0 +1,36 @@
|
||||
//Copyright 20010 Alex Norman
|
||||
//writen by Alex Norman
|
||||
//
|
||||
//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
|
||||
//(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.
|
||||
//
|
||||
//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
|
||||
|
||||
#include "interrupt_setting.h"
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
interrupt_setting_t store_and_clear_interrupt(void) {
|
||||
uint8_t sreg = SREG;
|
||||
cli();
|
||||
return sreg;
|
||||
}
|
||||
|
||||
void restore_interrupt_setting(interrupt_setting_t setting) {
|
||||
SREG = setting;
|
||||
}
|
||||
|
39
protocol/lufa/midi/bytequeue/interrupt_setting.h
Executable file
39
protocol/lufa/midi/bytequeue/interrupt_setting.h
Executable file
@ -0,0 +1,39 @@
|
||||
//Copyright 20010 Alex Norman
|
||||
//writen by Alex Norman
|
||||
//
|
||||
//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
|
||||
//(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.
|
||||
//
|
||||
//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
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
//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);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
277
protocol/lufa/midi/midi.c
Executable file
277
protocol/lufa/midi/midi.c
Executable file
@ -0,0 +1,277 @@
|
||||
//midi for embedded chips,
|
||||
//Copyright 2010 Alex Norman
|
||||
//
|
||||
//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
|
||||
//(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.
|
||||
//
|
||||
//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
|
||||
|
||||
#define MIN(x,y) (((x) < (y)) ? (x) : (y))
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL 0
|
||||
#endif
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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_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_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_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_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_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;
|
||||
}
|
||||
|
498
protocol/lufa/midi/midi.h
Executable file
498
protocol/lufa/midi/midi.h
Executable file
File diff suppressed because it is too large
Load Diff
291
protocol/lufa/midi/midi_device.c
Executable file
291
protocol/lufa/midi/midi_device.c
Executable file
@ -0,0 +1,291 @@
|
||||
//midi for embedded chips,
|
||||
//Copyright 2010 Alex Norman
|
||||
//
|
||||
//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
|
||||
//(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.
|
||||
//
|
||||
//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
|
||||
#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);
|
||||
|
||||
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;
|
||||
|
||||
//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;
|
||||
|
||||
//var byte functions
|
||||
device->input_sysex_callback = NULL;
|
||||
device->input_fallthrough_callback = NULL;
|
||||
device->input_catchall_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_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_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;
|
||||
}
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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:
|
||||
//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);
|
||||
}
|
||||
|
156
protocol/lufa/midi/midi_device.h
Executable file
156
protocol/lufa/midi/midi_device.h
Executable file
@ -0,0 +1,156 @@
|
||||
//midi for embedded chips,
|
||||
//Copyright 2010 Alex Norman
|
||||
//
|
||||
//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
|
||||
//(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.
|
||||
//
|
||||
//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
|
||||
* @brief Device implementation functions
|
||||
*/
|
||||
|
||||
#ifndef MIDI_DEVICE_H
|
||||
#define MIDI_DEVICE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @defgroup midi_device Functions used when implementing your own midi device.
|
||||
*
|
||||
* You use the functions when you are implementing your own midi device.
|
||||
*
|
||||
* You set a send function to actually send bytes via your device, this method
|
||||
* is called when you call a send function with this device, for instance
|
||||
* midi_send_cc
|
||||
*
|
||||
* You use the midi_device_input to process input data from the device and pass
|
||||
* it through the device's associated callbacks.
|
||||
*
|
||||
* You use the midi_device_set_pre_input_process_func if you want to have a
|
||||
* function called at the beginning of the device's process function, generally
|
||||
* to poll for input and pass that into midi_device_input
|
||||
*
|
||||
* @{
|
||||
*/
|
||||
|
||||
#include "midi_function_types.h"
|
||||
#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 void (* midi_no_byte_func_t)(MidiDevice * device);
|
||||
|
||||
/**
|
||||
* \struct _midi_device
|
||||
*
|
||||
* @brief This structure represents the input and output functions and
|
||||
* processing data for a midi device.
|
||||
*
|
||||
* A device can represent an actual physical device [serial port, usb port] or
|
||||
* something virtual.
|
||||
* You should not need to modify this structure directly.
|
||||
*/
|
||||
struct _midi_device {
|
||||
//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;
|
||||
|
||||
//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;
|
||||
|
||||
//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 queueing data between the input and the processing functions
|
||||
uint8_t input_queue_data[MIDI_INPUT_QUEUE_LENGTH];
|
||||
byteQueue_t input_queue;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Process input bytes. This function parses bytes and calls the
|
||||
* appropriate callbacks associated with the given device. You use this
|
||||
* function if you are creating a custom device and you want to have midi
|
||||
* input.
|
||||
*
|
||||
* @param device the midi device to associate the input with
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @brief Set the callback function that will be used for sending output
|
||||
* data bytes. This is only used if you're creating a custom device.
|
||||
* You'll most likely want the callback function to disable interrupts so
|
||||
* that you can call the various midi send functions without worrying about
|
||||
* locking.
|
||||
*
|
||||
* \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);
|
||||
|
||||
/**
|
||||
* @brief Set a callback which is called at the beginning of the
|
||||
* midi_device_process call. This can be used to poll for input
|
||||
* data and send the data through the midi_device_input function.
|
||||
* You'll probably only use this if you're creating a custom device.
|
||||
*
|
||||
* \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);
|
||||
|
||||
/**@}*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
50
protocol/lufa/midi/midi_function_types.h
Executable file
50
protocol/lufa/midi/midi_function_types.h
Executable file
@ -0,0 +1,50 @@
|
||||
//midi for embedded chips,
|
||||
//Copyright 2010 Alex Norman
|
||||
//
|
||||
//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
|
||||
//(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.
|
||||
//
|
||||
//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
|
||||
* @brief Function signature definitions
|
||||
*/
|
||||
|
||||
#ifndef MIDI_FUNCTION_TYPES_H
|
||||
#define MIDI_FUNCTION_TYPES_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
//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);
|
||||
|
||||
//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
|
99
protocol/lufa/midi/sysex_tools.c
Executable file
99
protocol/lufa/midi/sysex_tools.c
Executable file
@ -0,0 +1,99 @@
|
||||
//midi for embedded chips,
|
||||
//Copyright 2010 Alex Norman
|
||||
//
|
||||
//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
|
||||
//(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.
|
||||
//
|
||||
//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_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;
|
||||
|
||||
//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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
//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;
|
||||
}
|
||||
}
|
||||
|
95
protocol/lufa/midi/sysex_tools.h
Executable file
95
protocol/lufa/midi/sysex_tools.h
Executable file
@ -0,0 +1,95 @@
|
||||
//midi for embedded chips,
|
||||
//Copyright 2010 Alex Norman
|
||||
//
|
||||
//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
|
||||
//(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.
|
||||
//
|
||||
//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
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Sysex utility functions
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Every 7 bytes of decoded data is converted into 8 bytes of encoded data and
|
||||
* visa-versa. If you'd like to operate on small segments, make sure that you
|
||||
* encode in 7 byte increments and decode in 8 byte increments.
|
||||
*
|
||||
*/
|
||||
|
||||
/** @defgroup sysex_tools Sysex utility functions
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Compute the length of a message after it is encoded.
|
||||
*
|
||||
* @param decoded_length The length, in bytes, of the message to encode.
|
||||
*
|
||||
* @return The length, in bytes, of the message after encodeing.
|
||||
*/
|
||||
uint16_t sysex_encoded_length(uint16_t decoded_length);
|
||||
|
||||
/**
|
||||
* @brief Compute the length of a message after it is decoded.
|
||||
*
|
||||
* @param encoded_length The length, in bytes, of the encoded message.
|
||||
*
|
||||
* @return The length, in bytes, of the message after it is decoded.
|
||||
*/
|
||||
uint16_t sysex_decoded_length(uint16_t encoded_length);
|
||||
|
||||
/**
|
||||
* @brief Encode data so that it can be transmitted safely in a sysex message.
|
||||
*
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @brief Decode encoded data.
|
||||
*
|
||||
* @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);
|
||||
|
||||
/**@}*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user