Handle receiving of sysex conig data
This commit is contained in:
parent
72b305830f
commit
1cdface168
5 changed files with 124 additions and 24 deletions
|
@ -184,16 +184,24 @@ void sendWLChannel(const uint8_t channel) {
|
|||
//Only 14 LSB of int value are used (2MSB are discarded), so only works for unsigned data 0-16383
|
||||
|
||||
//NOTE: This assumes code is running on a little-endian CPU, both for real device (Teensy) and simulator.
|
||||
uint16_t midi16to14(uint16_t realdata) {
|
||||
uint16_t midi16to14(const uint16_t realdata) {
|
||||
return (realdata & 0x3F80) >>7 | (realdata & 0x007F) <<8;
|
||||
}
|
||||
|
||||
uint16_t midi14to16(uint16_t mididata) {
|
||||
uint16_t midi14to16(const uint16_t mididata) {
|
||||
return (mididata & 0x7F00) >> 8 | (mididata & 0x007F) <<7 ;
|
||||
}
|
||||
|
||||
//Read from a memory location, such as MIDI receive buffer
|
||||
uint16_t midi14to16(const uint8_t* mididata) {
|
||||
uint8_t msb = *mididata;
|
||||
uint8_t lsb = *(mididata+1);
|
||||
|
||||
return (msb & 0x007F) <<7 | (lsb & 0x007F);
|
||||
}
|
||||
|
||||
//This is a bit different. MSB of each byte is just discarded (instead of discarding MSB for whole value). Just used for CRC (easier to compare)
|
||||
uint32_t midi32to28(uint32_t realdata) {
|
||||
uint32_t midi32to28(const uint32_t realdata) {
|
||||
uint8_t* p = (uint8_t*)&realdata;
|
||||
|
||||
uint32_t r=0;
|
||||
|
|
|
@ -38,8 +38,9 @@ void sendWLChannel(const uint8_t channel);
|
|||
|
||||
|
||||
//Convert things between "regular data" and MIDI data (byte order and 7-bits-per-byte)
|
||||
uint16_t midi16to14(uint16_t realdata);
|
||||
uint16_t midi14to16(uint16_t mididata);
|
||||
uint32_t midi32to28(uint32_t realdata);
|
||||
uint16_t midi16to14(const uint16_t realdata);
|
||||
uint16_t midi14to16(const uint16_t mididata);
|
||||
uint16_t midi14to16(const uint8_t* mididata);
|
||||
uint32_t midi32to28(const uint32_t realdata);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -305,6 +305,7 @@ bool receiveSysexSettings(const uint8_t* data, const uint16_t length) {
|
|||
//Make sure length of receive buffer is enough to read all we need to. We can accept extra junk at the end though.
|
||||
if(length<expected_size) {
|
||||
configShowMessage("Invalid config format");
|
||||
printf("Invalid config format %d %d\n", length, expected_size);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -317,18 +318,20 @@ bool receiveSysexSettings(const uint8_t* data, const uint16_t length) {
|
|||
memcpy(&crc_rcv, data+checksum_pos, 4);
|
||||
if(crc != crc_rcv) {
|
||||
configShowMessage("Invalid checksum");
|
||||
printf("[crc] expected: %x got: %x\n", crc, crc_rcv);
|
||||
return false;
|
||||
}
|
||||
|
||||
//Verify that payload size matches the size of our EEPROM config
|
||||
uint16_t payload_size = midi14to16(data[payload_pos]);
|
||||
uint16_t payload_size = midi14to16(data+size_pos);
|
||||
if(payload_size != EEPROM_SIZE) {
|
||||
configShowMessage("Invalid config size");
|
||||
printf("[size] expected: %d got: %d (at %d)\n", EEPROM_SIZE, payload_size, size_pos);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
uint16_t eeprom_version_rcv = midi14to16(data[payload_pos+VERSION_ADDR]);
|
||||
uint16_t eeprom_version_rcv = midi14to16(data+(payload_pos+VERSION_ADDR));
|
||||
if(eeprom_version_rcv != EEPROM_VERSION) {
|
||||
configShowMessage("Invalid config version");
|
||||
return false;
|
||||
|
@ -338,8 +341,7 @@ bool receiveSysexSettings(const uint8_t* data, const uint16_t length) {
|
|||
for(uint16_t i=0; i<payload_size/2; i++) {
|
||||
uint16_t addr = i*2;
|
||||
uint16_t val;
|
||||
memcpy(&val, data+payload_pos+addr, 2);
|
||||
val = midi14to16(val);
|
||||
val = midi14to16(data+(payload_pos+addr));
|
||||
|
||||
//Skip sensor calibration values if they are "out of bounds". This makes it possible to send a config that does
|
||||
//not overwrite sensor calibration.
|
||||
|
@ -363,7 +365,7 @@ bool receiveSysexSettings(const uint8_t* data, const uint16_t length) {
|
|||
if(val<ctouchLoLimit || val>ctouchHiLimit) continue;
|
||||
}
|
||||
|
||||
writeSetting(i, val);
|
||||
writeSetting(addr, val);
|
||||
}
|
||||
|
||||
//All went well
|
||||
|
@ -397,17 +399,21 @@ void configShowMessage(const char* message) {
|
|||
}
|
||||
|
||||
uint8_t* sysex_rcv_buffer = NULL;
|
||||
uint16_t sysex_buf_size = 0;
|
||||
|
||||
void handleSysexChunk(const uint8_t *data, const uint16_t length, const bool last) {
|
||||
size_t pos = 0;
|
||||
void handleSysexChunk(const uint8_t *data, const uint16_t length, const uint8_t last) {
|
||||
uint16_t pos;
|
||||
|
||||
if(!sysex_rcv_buffer) {
|
||||
//Start out with an empty buffer
|
||||
sysex_rcv_buffer = (uint8_t *)malloc(length);
|
||||
pos = 0;
|
||||
sysex_buf_size = length;
|
||||
sysex_rcv_buffer = (uint8_t *)malloc(sysex_buf_size);
|
||||
} else {
|
||||
//Increase size of current buffer
|
||||
size_t pos = sizeof(sysex_rcv_buffer);
|
||||
sysex_rcv_buffer = (uint8_t *)realloc(sysex_rcv_buffer, pos + length);
|
||||
pos = sysex_buf_size;
|
||||
sysex_buf_size += length;
|
||||
sysex_rcv_buffer = (uint8_t *)realloc(sysex_rcv_buffer, sysex_buf_size);
|
||||
}
|
||||
|
||||
//Append this chunk to buffer
|
||||
|
@ -415,9 +421,12 @@ void handleSysexChunk(const uint8_t *data, const uint16_t length, const bool las
|
|||
|
||||
//If it's the last one, call the regular handler to process it
|
||||
if(last) {
|
||||
handleSysex(sysex_rcv_buffer, pos+length);
|
||||
handleSysex(sysex_rcv_buffer, sysex_buf_size);
|
||||
|
||||
//Discard the buffer
|
||||
free(sysex_rcv_buffer);
|
||||
sysex_rcv_buffer = NULL;
|
||||
sysex_buf_size = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue