#include #include #include #include "settings.h" #include "globals.h" #include "menu.h" #include "hardware.h" #include "config.h" #include "midi.h" #include "led.h" //Read and write EEPROM data void writeInt(const uint16_t address, const uint16_t value) { union { uint8_t v[2]; uint16_t val; } data; data.val = value; EEPROM.update(address, data.v[0]); EEPROM.update(address+1, data.v[1]); } uint16_t readInt(uint16_t address) { union { uint8_t v[2]; uint16_t val; } data; data.v[0] = EEPROM.read(address); data.v[1] = EEPROM.read(address+1); return data.val; } void writeCalibration(calibration_t &calibration) { EEPROM.put(SETTINGS_OFFSET, calibration); } void readCalibration(calibration_t &calibration) { EEPROM.get(SETTINGS_OFFSET, calibration); } void writePreset(uint8_t preset) { EEPROM.put(SETTINGS_OFFSET + CALIBRATION_MAX_SIZE + preset * sizeof(preset_t), presets[preset]); } void writePresets() { EEPROM.put(SETTINGS_OFFSET + CALIBRATION_MAX_SIZE, presets); } void readPresets() { EEPROM.get(SETTINGS_OFFSET + CALIBRATION_MAX_SIZE, presets); } //Functions to send and receive config (and other things) via USB MIDI SysEx messages uint32_t crc32(const uint8_t *message, const size_t length) { size_t pos=0; uint32_t crc=0xFFFFFFFF; while (pos> 1) ^ (0xEDB88320 & -(crc & 1)); } } return ~crc; } /* Send EEPROM config dump as sysex message. Message format is structured like this: +------------------------------------------------------------------------------------+ | vendor(3) | "NuEVIc01" (8) | Payload size (2) | EEPROM data (variable) | crc32 (4) | +------------------------------------------------------------------------------------+ Payload size is for the EEPROM data chunk (not including anything else before or after CRC32 covers the entire buffer up to and including the eeprom data (but not the checksum itself) This currently operates under the assumption that the whole EEPROM chunk only consists of unsigned 16 bit ints, only using the range 0-16383 */ void sendSysexSettings() { const char *header = "NuEVIc01"; //NuEVI config dump 01 //Build a send buffer of all the things size_t sysex_size = 3 + strlen(header) + 2 + EEPROM_SIZE + 4; uint8_t *sysex_data = (uint8_t*)malloc(sysex_size); //Positions (offsets) of parts in send buffer int header_pos = 3; int size_pos = header_pos + strlen(header); int payload_pos = size_pos + 2; int checksum_pos = payload_pos + EEPROM_SIZE; //SysEX manufacturer ID memcpy(sysex_data, sysex_id, 3); //Header with command code memcpy(sysex_data+header_pos, header, strlen(header)); //Payload length *(uint16_t*)(sysex_data+size_pos) = convertToMidiValue(EEPROM_SIZE); //Config data uint16_t* config_buffer_start = (uint16_t*)(sysex_data+payload_pos); //Read one settings item at a time, change data format, and put in send buffer readPresets(); uint16_t *preset_buffer = (uint16_t*)presets; for(uint16_t idx=0; idx