1 //midi for embedded chips,
2 //Copyright 2010 Alex Norman
4 //This file is part of avr-midi.
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.
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.
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/>.
19 #include "sysex_tools.h"
21 uint16_t sysex_encoded_length(uint16_t decoded_length){
22 uint8_t remainder = decoded_length % 7;
24 return (decoded_length / 7) * 8 + remainder + 1;
26 return (decoded_length / 7) * 8;
29 uint16_t sysex_decoded_length(uint16_t encoded_length){
30 uint8_t remainder = encoded_length % 8;
32 return (encoded_length / 8) * 7 + remainder - 1;
34 return (encoded_length / 8) * 7;
37 uint16_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
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;
53 //fill out the rest if there is any more
54 uint8_t remainder = length % 7;
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;
64 return encoded_msb_idx + remainder + 1;
66 return encoded_full * 8;
70 uint16_t sysex_decode(uint8_t *decoded, const uint8_t *source, const uint16_t length){
71 uint16_t decoded_full = length / 8;
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)));
86 uint8_t remainder = length % 8;
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)));
94 return decoded_full * 7 + remainder - 1;
96 return decoded_full * 7;