Make (almost) all EEPROM settings read be bounded, and rewritten if "out of bounds"
This commit is contained in:
parent
c8e96343d0
commit
25d22fc42f
2 changed files with 80 additions and 74 deletions
|
@ -5,11 +5,11 @@
|
||||||
#include "globals.h"
|
#include "globals.h"
|
||||||
#include "menu.h"
|
#include "menu.h"
|
||||||
#include "hardware.h"
|
#include "hardware.h"
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
//Read settings from eeprom. Returns wether or not anything was written (due to factory reset or upgrade)
|
//Read settings from eeprom. Returns wether or not anything was written (due to factory reset or upgrade)
|
||||||
bool readEEPROM() {
|
void readEEPROM() {
|
||||||
bool factoryReset = !digitalRead(ePin) && !digitalRead(mPin);
|
bool factoryReset = !digitalRead(ePin) && !digitalRead(mPin);
|
||||||
bool hasWritten = false;
|
|
||||||
|
|
||||||
// if stored settings are not for current version, or Enter+Menu are pressed at startup, they are replaced by factory settings
|
// if stored settings are not for current version, or Enter+Menu are pressed at startup, they are replaced by factory settings
|
||||||
uint16_t settingsVersion = readSetting(VERSION_ADDR);
|
uint16_t settingsVersion = readSetting(VERSION_ADDR);
|
||||||
|
@ -97,63 +97,59 @@ bool readEEPROM() {
|
||||||
}
|
}
|
||||||
|
|
||||||
writeSetting(VERSION_ADDR, EEPROM_VERSION);
|
writeSetting(VERSION_ADDR, EEPROM_VERSION);
|
||||||
hasWritten = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// read all settings from EEPROM
|
// read all settings from EEPROM
|
||||||
breathThrVal = readSetting(BREATH_THR_ADDR);
|
breathThrVal = readSettingBounded(BREATH_THR_ADDR, breathLoLimit, breathHiLimit, BREATH_THR_FACTORY);
|
||||||
breathMaxVal = readSetting(BREATH_MAX_ADDR);
|
breathMaxVal = readSettingBounded(BREATH_MAX_ADDR, breathLoLimit, breathHiLimit, BREATH_MAX_FACTORY);
|
||||||
portamThrVal = readSetting(PORTAM_THR_ADDR);
|
portamThrVal = readSettingBounded(PORTAM_THR_ADDR, portamLoLimit, portamHiLimit, PORTAM_THR_FACTORY);
|
||||||
portamMaxVal = readSetting(PORTAM_MAX_ADDR);
|
portamMaxVal = readSettingBounded(PORTAM_MAX_ADDR, portamLoLimit, portamHiLimit, PORTAM_MAX_FACTORY);
|
||||||
pitchbThrVal = readSetting(PITCHB_THR_ADDR);
|
pitchbThrVal = readSettingBounded(PITCHB_THR_ADDR, pitchbLoLimit, pitchbHiLimit, PITCHB_THR_FACTORY);
|
||||||
pitchbMaxVal = readSetting(PITCHB_MAX_ADDR);
|
pitchbMaxVal = readSettingBounded(PITCHB_MAX_ADDR, pitchbLoLimit, pitchbHiLimit, PITCHB_MAX_FACTORY);
|
||||||
transpose = readSetting(TRANSP_ADDR);
|
transpose = readSettingBounded(TRANSP_ADDR, 0, 24, TRANSP_FACTORY);
|
||||||
MIDIchannel = readSetting(MIDI_ADDR);
|
MIDIchannel = readSettingBounded(MIDI_ADDR, 1, 16, MIDI_FACTORY);
|
||||||
breathCC = readSetting(BREATH_CC_ADDR);
|
breathCC = readSettingBounded(BREATH_CC_ADDR, 0, 127, BREATH_CC_FACTORY);
|
||||||
breathAT = readSetting(BREATH_AT_ADDR);
|
breathAT = readSettingBounded(BREATH_AT_ADDR, 0, 1, BREATH_AT_FACTORY);
|
||||||
velocity = readSetting(VELOCITY_ADDR);
|
velocity = readSettingBounded(VELOCITY_ADDR, 0, 127, VELOCITY_FACTORY);
|
||||||
portamento = readSetting(PORTAM_ADDR);
|
portamento = readSettingBounded(PORTAM_ADDR, 0, 2, PORTAM_FACTORY);
|
||||||
PBdepth = readSetting(PB_ADDR);
|
PBdepth = readSettingBounded(PB_ADDR, 0, 12, PB_FACTORY);
|
||||||
extraCT = readSetting(EXTRA_ADDR);
|
extraCT = readSettingBounded(EXTRA_ADDR, 0, 4, EXTRA_FACTORY);
|
||||||
vibrato = readSetting(VIBRATO_ADDR);
|
vibrato = readSettingBounded(VIBRATO_ADDR, 0, 9, VIBRATO_FACTORY);
|
||||||
deglitch = readSetting(DEGLITCH_ADDR);
|
deglitch = readSettingBounded(DEGLITCH_ADDR, 0, 70, DEGLITCH_FACTORY);
|
||||||
extracThrVal = readSetting(EXTRAC_THR_ADDR);
|
extracThrVal = readSettingBounded(EXTRAC_THR_ADDR, extracLoLimit, extracHiLimit, EXTRAC_THR_FACTORY);
|
||||||
extracMaxVal = readSetting(EXTRAC_MAX_ADDR);
|
extracMaxVal = readSettingBounded(EXTRAC_MAX_ADDR, extracLoLimit, extracHiLimit, EXTRAC_MAX_FACTORY);
|
||||||
patch = readSetting(PATCH_ADDR);
|
patch = readSettingBounded(PATCH_ADDR, 0, 127, PATCH_FACTORY);
|
||||||
octave = readSetting(OCTAVE_ADDR);
|
octave = readSettingBounded(OCTAVE_ADDR, 0, 6, OCTAVE_FACTORY);
|
||||||
ctouchThrVal = readSetting(CTOUCH_THR_ADDR);
|
ctouchThrVal = readSettingBounded(CTOUCH_THR_ADDR, ctouchLoLimit, ctouchHiLimit, CTOUCH_THR_FACTORY);
|
||||||
curve = readSetting(BREATHCURVE_ADDR);
|
curve = readSettingBounded(BREATHCURVE_ADDR, 0, 12, BREATHCURVE_FACTORY);
|
||||||
velSmpDl = readSetting(VEL_SMP_DL_ADDR);
|
velSmpDl = readSettingBounded(VEL_SMP_DL_ADDR, 0, 30, VEL_SMP_DL_FACTORY);
|
||||||
velBias = readSetting(VEL_BIAS_ADDR);
|
velBias = readSettingBounded(VEL_BIAS_ADDR, 0, 9, VEL_BIAS_FACTORY);
|
||||||
pinkySetting = readSetting(PINKY_KEY_ADDR);
|
pinkySetting = readSettingBounded(PINKY_KEY_ADDR, 0, 24, PINKY_KEY_FACTORY);
|
||||||
fastPatch[0] = readSetting(FP1_ADDR);
|
fastPatch[0] = readSettingBounded(FP1_ADDR, 0, 127, 0);
|
||||||
fastPatch[1] = readSetting(FP2_ADDR);
|
fastPatch[1] = readSettingBounded(FP2_ADDR, 0, 127, 0);
|
||||||
fastPatch[2] = readSetting(FP3_ADDR);
|
fastPatch[2] = readSettingBounded(FP3_ADDR, 0, 127, 0);
|
||||||
fastPatch[3] = readSetting(FP4_ADDR);
|
fastPatch[3] = readSettingBounded(FP4_ADDR, 0, 127, 0);
|
||||||
fastPatch[4] = readSetting(FP5_ADDR);
|
fastPatch[4] = readSettingBounded(FP5_ADDR, 0, 127, 0);
|
||||||
fastPatch[5] = readSetting(FP6_ADDR);
|
fastPatch[5] = readSettingBounded(FP6_ADDR, 0, 127, 0);
|
||||||
fastPatch[6] = readSetting(FP7_ADDR);
|
fastPatch[6] = readSettingBounded(FP7_ADDR, 0, 127, 0);
|
||||||
dipSwBits = readSetting(DIPSW_BITS_ADDR);
|
dipSwBits = readSetting(DIPSW_BITS_ADDR);
|
||||||
parallel = readSetting(PARAL_ADDR);
|
parallel = readSettingBounded(PARAL_ADDR, 0, 48, PARAL_FACTORY);
|
||||||
rotations[0] = readSetting(ROTN1_ADDR);
|
rotations[0] = readSettingBounded(ROTN1_ADDR, 0, 48, ROTN1_FACTORY);
|
||||||
rotations[1] = readSetting(ROTN2_ADDR);
|
rotations[1] = readSettingBounded(ROTN2_ADDR, 0, 48, ROTN2_FACTORY);
|
||||||
rotations[2] = readSetting(ROTN3_ADDR);
|
rotations[2] = readSettingBounded(ROTN3_ADDR, 0, 48, ROTN3_FACTORY);
|
||||||
rotations[3] = readSetting(ROTN4_ADDR);
|
rotations[3] = readSettingBounded(ROTN4_ADDR, 0, 48, ROTN4_FACTORY);
|
||||||
priority = readSetting(PRIO_ADDR);
|
priority = readSettingBounded(PRIO_ADDR, 0, 1, PRIO_FACTORY);
|
||||||
vibSens = readSetting(VIB_SENS_ADDR);
|
vibSens = readSettingBounded(VIB_SENS_ADDR, 1, 12, VIB_SENS_FACTORY);
|
||||||
vibRetn = readSetting(VIB_RETN_ADDR);
|
vibRetn = readSettingBounded(VIB_RETN_ADDR, 0, 4, VIB_RETN_FACTORY);
|
||||||
vibSquelch = readSetting(VIB_SQUELCH_ADDR);
|
vibSquelch = readSettingBounded(VIB_SQUELCH_ADDR, 1, 30, VIB_SQUELCH_FACTORY);
|
||||||
vibDirection = readSetting(VIB_DIRECTION_ADDR);
|
vibDirection = readSettingBounded(VIB_DIRECTION_ADDR, 0, 1, VIB_DIRECTION_FACTORY);
|
||||||
breathCC2 = readSetting(BREATH_CC2_ADDR);
|
breathCC2 = readSettingBounded(BREATH_CC2_ADDR, 0, 127, BREATH_CC2_FACTORY);
|
||||||
breathCC2Rise = readSetting(BREATH_CC2_RISE_ADDR);
|
breathCC2Rise = readSettingBounded(BREATH_CC2_RISE_ADDR, 1, 10, BREATH_CC2_RISE_FACTORY);
|
||||||
vibSensBite = readSetting(VIB_SENS_BITE_ADDR);
|
vibSensBite = readSettingBounded(VIB_SENS_BITE_ADDR, 1, 12, VIB_SENS_BITE_FACTORY);
|
||||||
vibSquelchBite = readSetting(VIB_SQUELCH_BITE_ADDR);
|
vibSquelchBite = readSettingBounded(VIB_SQUELCH_BITE_ADDR, 1, 30, VIB_SQUELCH_BITE_FACTORY);
|
||||||
vibControl = readSetting(VIB_CONTROL_ADDR);
|
vibControl = readSettingBounded(VIB_CONTROL_ADDR, 0, 1, VIB_CONTROL_FACTORY);
|
||||||
dacMode = readSetting(DAC_MODE_ADDR);
|
dacMode = readSettingBounded(DAC_MODE_ADDR, DAC_MODE_BREATH, DAC_MODE_PITCH, DAC_MODE_FACTORY);
|
||||||
trill3_interval = readSetting(TRILL3_INTERVAL_ADDR);
|
trill3_interval = readSettingBounded(TRILL3_INTERVAL_ADDR, 3, 4, TRILL3_INTERVAL_FACTORY);
|
||||||
|
|
||||||
if(trill3_interval<3 || trill3_interval > 4) trill3_interval = TRILL3_INTERVAL_FACTORY; //Deal with possible zero/bad values
|
|
||||||
|
|
||||||
//Flags stored in bit field
|
//Flags stored in bit field
|
||||||
fastBoot = (dipSwBits & (1<<DIPSW_FASTBOOT))?1:0;
|
fastBoot = (dipSwBits & (1<<DIPSW_FASTBOOT))?1:0;
|
||||||
|
@ -164,7 +160,6 @@ bool readEEPROM() {
|
||||||
specialKeyEnable = (dipSwBits & (1<<DIPSW_SPKEYENABLE))?1:0;
|
specialKeyEnable = (dipSwBits & (1<<DIPSW_SPKEYENABLE))?1:0;
|
||||||
bcasMode = (dipSwBits & (1<<DIPSW_BCASMODE))?1:0;
|
bcasMode = (dipSwBits & (1<<DIPSW_BCASMODE))?1:0;
|
||||||
|
|
||||||
return hasWritten;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -176,22 +171,31 @@ void setBit(uint16_t &bitfield, const uint8_t pos, const uint16_t value) {
|
||||||
|
|
||||||
|
|
||||||
//Read and write EEPROM data
|
//Read and write EEPROM data
|
||||||
void writeSetting(uint16_t address, uint16_t value){
|
void writeSetting(uint16_t address, uint16_t value) {
|
||||||
union {
|
union {
|
||||||
uint8_t v[2];
|
uint8_t v[2];
|
||||||
uint16_t val;
|
uint16_t val;
|
||||||
} data;
|
} data;
|
||||||
data.val = value;
|
data.val = value;
|
||||||
EEPROM.update(address, data.v[0]);
|
EEPROM.update(address, data.v[0]);
|
||||||
EEPROM.update(address+1, data.v[1]);
|
EEPROM.update(address+1, data.v[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t readSetting(uint16_t address){
|
uint16_t readSetting(uint16_t address) {
|
||||||
union {
|
union {
|
||||||
uint8_t v[2];
|
uint8_t v[2];
|
||||||
uint16_t val;
|
uint16_t val;
|
||||||
} data;
|
} data;
|
||||||
data.v[0] = EEPROM.read(address);
|
data.v[0] = EEPROM.read(address);
|
||||||
data.v[1] = EEPROM.read(address+1);
|
data.v[1] = EEPROM.read(address+1);
|
||||||
return data.val;
|
return data.val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint16_t readSettingBounded(uint16_t address, uint16_t min, uint16_t max, uint16_t defaultValue) {
|
||||||
|
uint16_t val = readSetting(address);
|
||||||
|
if(val < min || val > max) {
|
||||||
|
val = defaultValue;
|
||||||
|
writeSetting(address, val);
|
||||||
|
}
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
|
@ -121,9 +121,11 @@
|
||||||
#define DAC_MODE_FACTORY DAC_MODE_BREATH
|
#define DAC_MODE_FACTORY DAC_MODE_BREATH
|
||||||
|
|
||||||
|
|
||||||
bool readEEPROM();
|
void readEEPROM();
|
||||||
void setBit(uint16_t &bitfield, const uint8_t pos, const uint16_t value);
|
void setBit(uint16_t &bitfield, const uint8_t pos, const uint16_t value);
|
||||||
uint16_t readSetting(uint16_t address);
|
uint16_t readSetting(uint16_t address);
|
||||||
void writeSetting(uint16_t address, uint16_t value);
|
void writeSetting(uint16_t address, uint16_t value);
|
||||||
|
uint16_t readSettingBounded(uint16_t address, uint16_t min, uint16_t max, uint16_t defaultValue);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue