2 #include "sysex_tools.h"
5 void send_bytes_sysex(uint8_t message_type, uint8_t data_type, uint8_t * bytes, uint16_t length) {
6 // SEND_STRING("\nTX: ");
7 // for (uint8_t i = 0; i < length; i++) {
8 // send_byte(bytes[i]);
11 if (length > API_SYSEX_MAX_SIZE) {
12 xprintf("Sysex msg too big %d %d %d", message_type, data_type, length);
17 // The buffer size required is calculated as the following
18 // API_SYSEX_MAX_SIZE is the maximum length
19 // In addition to that we have a two byte message header consisting of the message_type and data_type
20 // This has to be encoded with an additional overhead of one byte for every starting 7 bytes
21 // We just add one extra byte in case it's not divisible by 7
22 // Then we have an unencoded header consisting of 4 bytes
23 // Plus a one byte terminator
24 const unsigned message_header = 2;
25 const unsigned unencoded_message = API_SYSEX_MAX_SIZE + message_header;
26 const unsigned encoding_overhead = unencoded_message / 7 + 1;
27 const unsigned encoded_size = unencoded_message + encoding_overhead;
28 const unsigned unencoded_header = 4;
29 const unsigned terminator = 1;
30 const unsigned buffer_size = encoded_size + unencoded_header + terminator;
31 uint8_t buffer[encoded_size + unencoded_header + terminator];
32 // The unencoded header
38 // We copy the message to the end of the array, this way we can do an inplace encoding, using the same
39 // buffer for both input and output
40 const unsigned message_size = length + message_header;
41 uint8_t* unencoded_start = buffer + buffer_size - message_size;
42 uint8_t* ptr = unencoded_start;
43 *(ptr++) = message_type;
45 memcpy(ptr, bytes, length);
47 unsigned encoded_length = sysex_encode(buffer + unencoded_header, unencoded_start, message_size);
48 unsigned final_size = unencoded_header + encoded_length + terminator;
49 buffer[final_size - 1] = 0xF7;
50 midi_send_array(&midi_device, final_size, buffer);
52 // SEND_STRING("\nTD: ");
53 // for (uint8_t i = 0; i < encoded_length + 5; i++) {
54 // send_byte(buffer[i]);