aboutsummaryrefslogtreecommitdiff
path: root/protocol/lufa/midi/sysex_tools.c
diff options
context:
space:
mode:
Diffstat (limited to 'protocol/lufa/midi/sysex_tools.c')
-rwxr-xr-xprotocol/lufa/midi/sysex_tools.c99
1 files changed, 99 insertions, 0 deletions
diff --git a/protocol/lufa/midi/sysex_tools.c b/protocol/lufa/midi/sysex_tools.c
new file mode 100755
index 000000000..7563a3e2a
--- /dev/null
+++ b/protocol/lufa/midi/sysex_tools.c
@@ -0,0 +1,99 @@
1//midi for embedded chips,
2//Copyright 2010 Alex Norman
3//
4//This file is part of avr-midi.
5//
6//avr-midi is free software: you can redistribute it and/or modify
7//it under the terms of the GNU General Public License as published by
8//the Free Software Foundation, either version 3 of the License, or
9//(at your option) any later version.
10//
11//avr-midi is distributed in the hope that it will be useful,
12//but WITHOUT ANY WARRANTY; without even the implied warranty of
13//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14//GNU General Public License for more details.
15//
16//You should have received a copy of the GNU General Public License
17//along with avr-midi. If not, see <http://www.gnu.org/licenses/>.
18
19#include "sysex_tools.h"
20
21uint16_t sysex_encoded_length(uint16_t decoded_length){
22 uint8_t remainder = decoded_length % 7;
23 if (remainder)
24 return (decoded_length / 7) * 8 + remainder + 1;
25 else
26 return (decoded_length / 7) * 8;
27}
28
29uint16_t sysex_decoded_length(uint16_t encoded_length){
30 uint8_t remainder = encoded_length % 8;
31 if (remainder)
32 return (encoded_length / 8) * 7 + remainder - 1;
33 else
34 return (encoded_length / 8) * 7;
35}
36
37uint16_t sysex_encode(uint8_t *encoded, const uint8_t *source, const uint16_t length){
38 uint16_t encoded_full = length / 7; //number of full 8 byte sections from 7 bytes of input
39 uint16_t i,j;
40
41 //fill out the fully encoded sections
42 for(i = 0; i < encoded_full; i++) {
43 uint16_t encoded_msb_idx = i * 8;
44 uint16_t input_start_idx = i * 7;
45 encoded[encoded_msb_idx] = 0;
46 for(j = 0; j < 7; j++){
47 uint8_t current = source[input_start_idx + j];
48 encoded[encoded_msb_idx] |= (0x80 & current) >> (1 + j);
49 encoded[encoded_msb_idx + 1 + j] = 0x7F & current;
50 }
51 }
52
53 //fill out the rest if there is any more
54 uint8_t remainder = length % 7;
55 if (remainder) {
56 uint16_t encoded_msb_idx = encoded_full * 8;
57 uint16_t input_start_idx = encoded_full * 7;
58 encoded[encoded_msb_idx] = 0;
59 for(j = 0; j < remainder; j++){
60 uint8_t current = source[input_start_idx + j];
61 encoded[encoded_msb_idx] |= (0x80 & current) >> (1 + j);
62 encoded[encoded_msb_idx + 1 + j] = 0x7F & current;
63 }
64 return encoded_msb_idx + remainder + 1;
65 } else {
66 return encoded_full * 8;
67 }
68}
69
70uint16_t sysex_decode(uint8_t *decoded, const uint8_t *source, const uint16_t length){
71 uint16_t decoded_full = length / 8;
72 uint16_t i,j;
73
74 if (length < 2)
75 return 0;
76
77 //fill out the fully encoded sections
78 for(i = 0; i < decoded_full; i++) {
79 uint16_t encoded_msb_idx = i * 8;
80 uint16_t output_start_index = i * 7;
81 for(j = 0; j < 7; j++){
82 decoded[output_start_index + j] = 0x7F & source[encoded_msb_idx + j + 1];
83 decoded[output_start_index + j] |= (0x80 & (source[encoded_msb_idx] << (1 + j)));
84 }
85 }
86 uint8_t remainder = length % 8;
87 if (remainder) {
88 uint16_t encoded_msb_idx = decoded_full * 8;
89 uint16_t output_start_index = decoded_full * 7;
90 for(j = 0; j < (remainder - 1); j++) {
91 decoded[output_start_index + j] = 0x7F & source[encoded_msb_idx + j + 1];
92 decoded[output_start_index + j] |= (0x80 & (source[encoded_msb_idx] << (1 + j)));
93 }
94 return decoded_full * 7 + remainder - 1;
95 } else {
96 return decoded_full * 7;
97 }
98}
99