1
0
forked from me/IronOS

Merge pull request #1117 from Ralim/fix/1116

Address settings bounds checks #1116
Pull in fix for PD PPS
This commit is contained in:
Ben V. Brown
2021-12-19 19:17:24 +11:00
committed by GitHub
5 changed files with 145 additions and 123 deletions

View File

@@ -95,8 +95,9 @@
#define DETAILED_SOLDERING 0 // 0: Disable 1: Enable - Default 0 #define DETAILED_SOLDERING 0 // 0: Disable 1: Enable - Default 0
#define DETAILED_IDLE 0 // 0: Disable 1: Enable - Default 0 #define DETAILED_IDLE 0 // 0: Disable 1: Enable - Default 0
#define THERMAL_RUNAWAY_TIME_SEC 20 // Due to large thermal mass of the PCB being heated we need to pull this back a bit
#define THERMAL_RUNAWAY_TEMP_C 20 #define THERMAL_RUNAWAY_TIME_SEC 45
#define THERMAL_RUNAWAY_TEMP_C 3
#define CUT_OUT_SETTING 0 // default to no cut-off voltage #define CUT_OUT_SETTING 0 // default to no cut-off voltage
#define RECOM_VOL_CELL 33 // Minimum voltage per cell (Recommended 3.3V (33)) #define RECOM_VOL_CELL 33 // Minimum voltage per cell (Recommended 3.3V (33))
@@ -118,25 +119,26 @@
#endif #endif
#ifdef MODEL_MHP30 #ifdef MODEL_MHP30
#define SOLDERING_TEMP 200 // Default soldering temp is 200.0 °C #define SOLDERING_TEMP 200 // Default soldering temp is 200.0 °C
#define VOLTAGE_DIV 360 // Default for MHP30 #define VOLTAGE_DIV 360 // Default for MHP30
#define PID_POWER_LIMIT 65 // Sets the max pwm power limit #define PID_POWER_LIMIT 65 // Sets the max pwm power limit
#define CALIBRATION_OFFSET 0 // the adc offset in uV - MHP compensates automagically #define CALIBRATION_OFFSET 0 // the adc offset in uV - MHP compensates automagically
#define POWER_LIMIT 65 // 65 watts default power limit #define MIN_CALIBRATION_OFFSET 0 // Min value for calibration
#define MAX_POWER_LIMIT 65 // #define POWER_LIMIT 65 // 65 watts default power limit
#define POWER_LIMIT_STEPS 1 // #define MAX_POWER_LIMIT 65 //
#define OP_AMP_GAIN_STAGE OP_AMP_GAIN_STAGE_MHP30 // #define POWER_LIMIT_STEPS 1 //
#define USB_PD_VMAX 20 // Maximum voltage for PD to negotiate #define OP_AMP_GAIN_STAGE OP_AMP_GAIN_STAGE_MHP30 //
#define MODEL_HAS_DCDC // Has inductor to current filter #define USB_PD_VMAX 20 // Maximum voltage for PD to negotiate
#define PID_TIM_HZ (16) // #define MODEL_HAS_DCDC // Has inductor to current filter
#define MAX_TEMP_C 300 // Max soldering temp selectable °C #define PID_TIM_HZ (16) //
#define MAX_TEMP_F 570 // Max soldering temp selectable °F #define MAX_TEMP_C 350 // Max soldering temp selectable °C
#define MIN_TEMP_C 10 // Min soldering temp selectable °C #define MAX_TEMP_F 660 // Max soldering temp selectable °F
#define MIN_TEMP_F 60 // Min soldering temp selectable °F #define MIN_TEMP_C 10 // Min soldering temp selectable °C
#define MIN_BOOST_TEMP_C 150 // The min settable temp for boost mode °C #define MIN_TEMP_F 60 // Min soldering temp selectable °F
#define MIN_BOOST_TEMP_F 300 // The min settable temp for boost mode °F #define MIN_BOOST_TEMP_C 150 // The min settable temp for boost mode °C
#define NO_DISPLAY_ROTATE // Disable OLED rotation by accel #define MIN_BOOST_TEMP_F 300 // The min settable temp for boost mode °F
#define SLEW_LIMIT 50 // Limit to 3.0 Watts per 64ms pid loop update rate slew rate #define NO_DISPLAY_ROTATE // Disable OLED rotation by accel
#define SLEW_LIMIT 50 // Limit to 3.0 Watts per 64ms pid loop update rate slew rate
#define ACCEL_MSA #define ACCEL_MSA
#define POW_PD 1 #define POW_PD 1
#define TEMP_NTC #define TEMP_NTC

View File

@@ -125,23 +125,24 @@
#endif #endif
#ifdef MODEL_TS100 #ifdef MODEL_TS100
#define SOLDERING_TEMP 320 // Default soldering temp is 320.0 °C #define SOLDERING_TEMP 320 // Default soldering temp is 320.0 °C
#define VOLTAGE_DIV 467 // 467 - Default divider from schematic #define VOLTAGE_DIV 467 // 467 - Default divider from schematic
#define CALIBRATION_OFFSET 900 // 900 - Default adc offset in uV #define CALIBRATION_OFFSET 900 // 900 - Default adc offset in uV
#define PID_POWER_LIMIT 70 // Sets the max pwm power limit #define MIN_CALIBRATION_OFFSET 100 // Min value for calibration
#define POWER_LIMIT 0 // 0 watts default limit #define PID_POWER_LIMIT 70 // Sets the max pwm power limit
#define MAX_POWER_LIMIT 70 // #define POWER_LIMIT 0 // 0 watts default limit
#define POWER_LIMIT_STEPS 5 // #define MAX_POWER_LIMIT 70 //
#define OP_AMP_GAIN_STAGE OP_AMP_GAIN_STAGE_TS100 // #define POWER_LIMIT_STEPS 5 //
#define TEMP_uV_LOOKUP_HAKKO // #define OP_AMP_GAIN_STAGE OP_AMP_GAIN_STAGE_TS100 //
#define USB_PD_VMAX 20 // Maximum voltage for PD to negotiate #define TEMP_uV_LOOKUP_HAKKO //
#define PID_TIM_HZ (8) // Tick rate of the PID loop #define USB_PD_VMAX 20 // Maximum voltage for PD to negotiate
#define MAX_TEMP_C 450 // Max soldering temp selectable °C #define PID_TIM_HZ (8) // Tick rate of the PID loop
#define MAX_TEMP_F 850 // Max soldering temp selectable °F #define MAX_TEMP_C 450 // Max soldering temp selectable °C
#define MIN_TEMP_C 10 // Min soldering temp selectable °C #define MAX_TEMP_F 850 // Max soldering temp selectable °F
#define MIN_TEMP_F 60 // Min soldering temp selectable °F #define MIN_TEMP_C 10 // Min soldering temp selectable °C
#define MIN_BOOST_TEMP_C 250 // The min settable temp for boost mode °C #define MIN_TEMP_F 60 // Min soldering temp selectable °F
#define MIN_BOOST_TEMP_F 480 // The min settable temp for boost mode °F #define MIN_BOOST_TEMP_C 250 // The min settable temp for boost mode °C
#define MIN_BOOST_TEMP_F 480 // The min settable temp for boost mode °F
#define POW_DC #define POW_DC
#define ACCEL_MMA #define ACCEL_MMA
#define POW_PD 0 #define POW_PD 0
@@ -150,23 +151,24 @@
#endif #endif
#ifdef MODEL_TS80 #ifdef MODEL_TS80
#define SOLDERING_TEMP 320 // Default soldering temp is 320.0 °C #define SOLDERING_TEMP 320 // Default soldering temp is 320.0 °C
#define VOLTAGE_DIV 780 // Default divider from schematic #define VOLTAGE_DIV 780 // Default divider from schematic
#define PID_POWER_LIMIT 24 // Sets the max pwm power limit #define PID_POWER_LIMIT 24 // Sets the max pwm power limit
#define CALIBRATION_OFFSET 900 // the adc offset in uV #define CALIBRATION_OFFSET 900 // the adc offset in uV
#define POWER_LIMIT 24 // 24 watts default power limit #define MIN_CALIBRATION_OFFSET 100 // Min value for calibration
#define MAX_POWER_LIMIT 40 // #define POWER_LIMIT 24 // 24 watts default power limit
#define POWER_LIMIT_STEPS 2 // #define MAX_POWER_LIMIT 40 //
#define OP_AMP_GAIN_STAGE OP_AMP_GAIN_STAGE_TS80 // #define POWER_LIMIT_STEPS 2 //
#define TEMP_uV_LOOKUP_TS80 // #define OP_AMP_GAIN_STAGE OP_AMP_GAIN_STAGE_TS80 //
#define USB_PD_VMAX 12 // Maximum voltage for PD to negotiate #define TEMP_uV_LOOKUP_TS80 //
#define PID_TIM_HZ (8) // Tick rate of the PID loop #define USB_PD_VMAX 12 // Maximum voltage for PD to negotiate
#define MAX_TEMP_C 450 // Max soldering temp selectable °C #define PID_TIM_HZ (8) // Tick rate of the PID loop
#define MAX_TEMP_F 850 // Max soldering temp selectable °F #define MAX_TEMP_C 450 // Max soldering temp selectable °C
#define MIN_TEMP_C 10 // Min soldering temp selectable °C #define MAX_TEMP_F 850 // Max soldering temp selectable °F
#define MIN_TEMP_F 60 // Min soldering temp selectable °F #define MIN_TEMP_C 10 // Min soldering temp selectable °C
#define MIN_BOOST_TEMP_C 250 // The min settable temp for boost mode °C #define MIN_TEMP_F 60 // Min soldering temp selectable °F
#define MIN_BOOST_TEMP_F 480 // The min settable temp for boost mode °F #define MIN_BOOST_TEMP_C 250 // The min settable temp for boost mode °C
#define MIN_BOOST_TEMP_F 480 // The min settable temp for boost mode °F
#define ACCEL_LIS #define ACCEL_LIS
#define POW_QC #define POW_QC
#define POW_PD 0 #define POW_PD 0
@@ -176,23 +178,24 @@
#endif #endif
#ifdef MODEL_TS80P #ifdef MODEL_TS80P
#define SOLDERING_TEMP 320 // Default soldering temp is 320.0 °C #define SOLDERING_TEMP 320 // Default soldering temp is 320.0 °C
#define VOLTAGE_DIV 650 // Default for TS80P with slightly different resistors #define VOLTAGE_DIV 650 // Default for TS80P with slightly different resistors
#define PID_POWER_LIMIT 35 // Sets the max pwm power limit #define PID_POWER_LIMIT 35 // Sets the max pwm power limit
#define CALIBRATION_OFFSET 1500 // the adc offset in uV #define CALIBRATION_OFFSET 1500 // the adc offset in uV
#define POWER_LIMIT 30 // 30 watts default power limit #define MIN_CALIBRATION_OFFSET 100 // Min value for calibration
#define MAX_POWER_LIMIT 40 // #define POWER_LIMIT 30 // 30 watts default power limit
#define POWER_LIMIT_STEPS 2 // #define MAX_POWER_LIMIT 40 //
#define OP_AMP_GAIN_STAGE OP_AMP_GAIN_STAGE_TS80 // #define POWER_LIMIT_STEPS 2 //
#define TEMP_uV_LOOKUP_TS80 // #define OP_AMP_GAIN_STAGE OP_AMP_GAIN_STAGE_TS80 //
#define USB_PD_VMAX 12 // Maximum voltage for PD to negotiate #define TEMP_uV_LOOKUP_TS80 //
#define PID_TIM_HZ (8) // Tick rate of the PID loop #define USB_PD_VMAX 12 // Maximum voltage for PD to negotiate
#define MAX_TEMP_C 450 // Max soldering temp selectable °C #define PID_TIM_HZ (8) // Tick rate of the PID loop
#define MAX_TEMP_F 850 // Max soldering temp selectable °F #define MAX_TEMP_C 450 // Max soldering temp selectable °C
#define MIN_TEMP_C 10 // Min soldering temp selectable °C #define MAX_TEMP_F 850 // Max soldering temp selectable °F
#define MIN_TEMP_F 60 // Min soldering temp selectable °F #define MIN_TEMP_C 10 // Min soldering temp selectable °C
#define MIN_BOOST_TEMP_C 250 // The min settable temp for boost mode °C #define MIN_TEMP_F 60 // Min soldering temp selectable °F
#define MIN_BOOST_TEMP_F 480 // The min settable temp for boost mode °F #define MIN_BOOST_TEMP_C 250 // The min settable temp for boost mode °C
#define MIN_BOOST_TEMP_F 480 // The min settable temp for boost mode °F
#define ACCEL_LIS #define ACCEL_LIS
#define ACCEL_MSA #define ACCEL_MSA
#define POW_PD 1 #define POW_PD 1

View File

@@ -114,23 +114,24 @@
#endif #endif
#ifdef MODEL_Pinecil #ifdef MODEL_Pinecil
#define SOLDERING_TEMP 320 // Default soldering temp is 320.0 °C #define SOLDERING_TEMP 320 // Default soldering temp is 320.0 °C
#define VOLTAGE_DIV 467 // 467 - Default divider from schematic #define VOLTAGE_DIV 467 // 467 - Default divider from schematic
#define CALIBRATION_OFFSET 900 // 900 - Default adc offset in uV #define CALIBRATION_OFFSET 900 // 900 - Default adc offset in uV
#define PID_POWER_LIMIT 70 // Sets the max pwm power limit #define MIN_CALIBRATION_OFFSET 100 // Min value for calibration
#define POWER_LIMIT 0 // 0 watts default limit #define PID_POWER_LIMIT 70 // Sets the max pwm power limit
#define MAX_POWER_LIMIT 70 // #define POWER_LIMIT 0 // 0 watts default limit
#define POWER_LIMIT_STEPS 5 // #define MAX_POWER_LIMIT 70 //
#define OP_AMP_GAIN_STAGE OP_AMP_GAIN_STAGE_PINECIL // Uses TS100 resistors #define POWER_LIMIT_STEPS 5 //
#define TEMP_uV_LOOKUP_HAKKO // Use Hakko lookup table #define OP_AMP_GAIN_STAGE OP_AMP_GAIN_STAGE_PINECIL // Uses TS100 resistors
#define USB_PD_VMAX 20 // Maximum voltage for PD to negotiate #define TEMP_uV_LOOKUP_HAKKO // Use Hakko lookup table
#define PID_TIM_HZ (8) // Tick rate of the PID loop #define USB_PD_VMAX 20 // Maximum voltage for PD to negotiate
#define MAX_TEMP_C 450 // Max soldering temp selectable °C #define PID_TIM_HZ (8) // Tick rate of the PID loop
#define MAX_TEMP_F 850 // Max soldering temp selectable °F #define MAX_TEMP_C 450 // Max soldering temp selectable °C
#define MIN_TEMP_C 10 // Min soldering temp selectable °C #define MAX_TEMP_F 850 // Max soldering temp selectable °F
#define MIN_TEMP_F 60 // Min soldering temp selectable °F #define MIN_TEMP_C 10 // Min soldering temp selectable °C
#define MIN_BOOST_TEMP_C 250 // The min settable temp for boost mode °C #define MIN_TEMP_F 60 // Min soldering temp selectable °F
#define MIN_BOOST_TEMP_F 480 // The min settable temp for boost mode °F #define MIN_BOOST_TEMP_C 250 // The min settable temp for boost mode °C
#define MIN_BOOST_TEMP_F 480 // The min settable temp for boost mode °F
#define POW_PD 1 #define POW_PD 1
#define POW_QC 1 #define POW_QC 1

View File

@@ -26,6 +26,7 @@ bool sanitiseSettings();
* flash in uint16_t chunks * flash in uint16_t chunks
*/ */
typedef struct { typedef struct {
uint16_t versionMarker;
uint16_t length; // Length of valid bytes following uint16_t length; // Length of valid bytes following
uint16_t settingsValues[SettingsOptionsLength]; uint16_t settingsValues[SettingsOptionsLength];
// used to make this nicely "good enough" aligned to 32 butes to make driver code trivial // used to make this nicely "good enough" aligned to 32 butes to make driver code trivial
@@ -41,48 +42,48 @@ volatile systemSettingsType systemSettings;
// For every setting we need to store the min/max/increment values // For every setting we need to store the min/max/increment values
typedef struct { typedef struct {
const uint16_t min; // Inclusive minimum value const uint16_t min; // Inclusive minimum value
const uint16_t max; // Exclusive maximum value const uint16_t max; // Inclusive maximum value
const uint16_t increment; // Standard increment const uint16_t increment; // Standard increment
const uint16_t defaultValue; // Default vaue after reset const uint16_t defaultValue; // Default vaue after reset
} SettingConstants; } SettingConstants;
static const SettingConstants settingsConstants[(int)SettingsOptions::SettingsOptionsLength] = { static const SettingConstants settingsConstants[(int)SettingsOptions::SettingsOptionsLength] = {
//{min,max,increment,default} //{min,max,increment,default}
{10, MAX_TEMP_F, 5, 320}, // SolderingTemp {MIN_TEMP_C, MAX_TEMP_F, 5, 320}, // SolderingTemp
{10, MAX_TEMP_F, 5, 150}, // SleepTemp {MIN_TEMP_C, MAX_TEMP_F, 5, 150}, // SleepTemp
{0, 16, 1, SLEEP_TIME}, // SleepTime {0, 15, 1, SLEEP_TIME}, // SleepTime
{0, 5, 1, CUT_OUT_SETTING}, // MinDCVoltageCells {0, 5, 1, CUT_OUT_SETTING}, // MinDCVoltageCells
{24, 38, 1, RECOM_VOL_CELL}, // MinVoltageCells {24, 38, 1, RECOM_VOL_CELL}, // MinVoltageCells
{90, QC_VOLTAGE_MAX, 2, 90}, // QCIdealVoltage {90, QC_VOLTAGE_MAX, 2, 90}, // QCIdealVoltage
{0, 3, 1, ORIENTATION_MODE}, // OrientationMode {0, 2, 1, ORIENTATION_MODE}, // OrientationMode
{0, 10, 1, SENSITIVITY}, // Sensitivity {0, 9, 1, SENSITIVITY}, // Sensitivity
{0, 2, 1, ANIMATION_LOOP}, // AnimationLoop {0, 1, 1, ANIMATION_LOOP}, // AnimationLoop
{0, settingOffSpeed_t::MAX_VALUE, 1, ANIMATION_SPEED}, // AnimationSpeed {0, settingOffSpeed_t::MAX_VALUE - 1, 1, ANIMATION_SPEED}, // AnimationSpeed
{0, 4, 1, AUTO_START_MODE}, // AutoStartMode {0, 3, 1, AUTO_START_MODE}, // AutoStartMode
{0, 61, 1, SHUTDOWN_TIME}, // ShutdownTime {0, 60, 1, SHUTDOWN_TIME}, // ShutdownTime
{0, 2, 1, COOLING_TEMP_BLINK}, // CoolingTempBlink {0, 1, 1, COOLING_TEMP_BLINK}, // CoolingTempBlink
{0, 2, 1, DETAILED_IDLE}, // DetailedIDLE {0, 1, 1, DETAILED_IDLE}, // DetailedIDLE
{0, 2, 1, DETAILED_SOLDERING}, // DetailedSoldering {0, 1, 1, DETAILED_SOLDERING}, // DetailedSoldering
{0, 2, 1, TEMPERATURE_INF}, // TemperatureInF {0, 1, 1, TEMPERATURE_INF}, // TemperatureInF
{0, 2, 1, DESCRIPTION_SCROLL_SPEED}, // DescriptionScrollSpeed {0, 1, 1, DESCRIPTION_SCROLL_SPEED}, // DescriptionScrollSpeed
{0, 3, 1, LOCKING_MODE}, // LockingMode {0, 2, 1, LOCKING_MODE}, // LockingMode
{0, 100, 1, POWER_PULSE_DEFAULT}, // KeepAwakePulse {0, 100, 1, POWER_PULSE_DEFAULT}, // KeepAwakePulse
{1, POWER_PULSE_WAIT_MAX, 1, POWER_PULSE_WAIT_DEFAULT}, // KeepAwakePulseWait {1, POWER_PULSE_WAIT_MAX, 1, POWER_PULSE_WAIT_DEFAULT}, // KeepAwakePulseWait
{1, POWER_PULSE_DURATION_MAX, 1, POWER_PULSE_DURATION_DEFAULT}, // KeepAwakePulseDuration {1, POWER_PULSE_DURATION_MAX, 1, POWER_PULSE_DURATION_DEFAULT}, // KeepAwakePulseDuration
{360, 900, 1, VOLTAGE_DIV}, // VoltageDiv {360, 900, 1, VOLTAGE_DIV}, // VoltageDiv
{0, MAX_TEMP_F, 10, BOOST_TEMP}, // BoostTemp {MIN_TEMP_C, MAX_TEMP_F, 10, BOOST_TEMP}, // BoostTemp
{100, 2500, 1, CALIBRATION_OFFSET}, // CalibrationOffset {MIN_CALIBRATION_OFFSET, 2500, 1, CALIBRATION_OFFSET}, // CalibrationOffset
{0, MAX_POWER_LIMIT, POWER_LIMIT_STEPS, POWER_LIMIT}, // PowerLimit {0, MAX_POWER_LIMIT, POWER_LIMIT_STEPS, POWER_LIMIT}, // PowerLimit
{0, 2, 1, REVERSE_BUTTON_TEMP_CHANGE}, // ReverseButtonTempChangeEnabled {0, 1, 1, REVERSE_BUTTON_TEMP_CHANGE}, // ReverseButtonTempChangeEnabled
{5, TEMP_CHANGE_LONG_STEP_MAX, 5, TEMP_CHANGE_LONG_STEP}, // TempChangeLongStep {5, TEMP_CHANGE_LONG_STEP_MAX, 5, TEMP_CHANGE_LONG_STEP}, // TempChangeLongStep
{1, TEMP_CHANGE_SHORT_STEP_MAX, 1, TEMP_CHANGE_SHORT_STEP}, // TempChangeShortStep {1, TEMP_CHANGE_SHORT_STEP_MAX, 1, TEMP_CHANGE_SHORT_STEP}, // TempChangeShortStep
{0, 4, 1, 1}, // HallEffectSensitivity {0, 3, 1, 1}, // HallEffectSensitivity
{0, 10, 1, 0}, // AccelMissingWarningCounter {0, 9, 1, 0}, // AccelMissingWarningCounter
{0, 10, 1, 0}, // PDMissingWarningCounter {0, 9, 1, 0}, // PDMissingWarningCounter
{0, 0xFFFF, 0, 41431 /*EN*/}, // UILanguage {0, 0xFFFF, 0, 41431 /*EN*/}, // UILanguage
{0, 51, 1, 0}, // PDNegTimeout {0, 50, 1, 0}, // PDNegTimeout
{0, 2, 1, 0}, // OLEDInversion {0, 1, 1, 0}, // OLEDInversion
{0, 100, 11, 33}, // OLEDBrightness {0, 99, 11, 33}, // OLEDBrightness
}; };
static_assert((sizeof(settingsConstants) / sizeof(SettingConstants)) == ((int)SettingsOptions::SettingsOptionsLength)); static_assert((sizeof(settingsConstants) / sizeof(SettingConstants)) == ((int)SettingsOptions::SettingsOptionsLength));
@@ -100,6 +101,11 @@ bool sanitiseSettings() {
// For all settings, need to ensure settings are in a valid range // For all settings, need to ensure settings are in a valid range
// First for any not know about due to array growth, reset them and update the length value // First for any not know about due to array growth, reset them and update the length value
bool dirty = false; bool dirty = false;
if (systemSettings.versionMarker != 0x55AA) {
memset((void *)&systemSettings, 0xFF, sizeof(systemSettings));
systemSettings.versionMarker = 0x55AA;
dirty = true;
}
if (systemSettings.padding != 0xFFFFFFFF) { if (systemSettings.padding != 0xFFFFFFFF) {
systemSettings.padding = 0xFFFFFFFF; // Force padding to 0xFFFFFFFF so that rolling forwards / back should be easier systemSettings.padding = 0xFFFFFFFF; // Force padding to 0xFFFFFFFF so that rolling forwards / back should be easier
dirty = true; dirty = true;
@@ -115,8 +121,7 @@ bool sanitiseSettings() {
// Check min max for all settings, if outside the range, move to default // Check min max for all settings, if outside the range, move to default
if (systemSettings.settingsValues[i] < settingsConstants[i].min || systemSettings.settingsValues[i] > settingsConstants[i].max) { if (systemSettings.settingsValues[i] < settingsConstants[i].min || systemSettings.settingsValues[i] > settingsConstants[i].max) {
systemSettings.settingsValues[i] = settingsConstants[i].defaultValue; systemSettings.settingsValues[i] = settingsConstants[i].defaultValue;
dirty = true;
dirty = true;
} }
} }
if (dirty) { if (dirty) {
@@ -133,34 +138,45 @@ void resetSettings() {
void setSettingValue(const enum SettingsOptions option, const uint16_t newValue) { void setSettingValue(const enum SettingsOptions option, const uint16_t newValue) {
const auto constants = settingsConstants[(int)option]; const auto constants = settingsConstants[(int)option];
systemSettings.settingsValues[(int)option] = newValue; systemSettings.settingsValues[(int)option] = newValue;
// If less than min, constrain
if (systemSettings.settingsValues[(int)option] < constants.min) { if (systemSettings.settingsValues[(int)option] < constants.min) {
systemSettings.settingsValues[(int)option] = constants.min; systemSettings.settingsValues[(int)option] = constants.min;
} }
// If hit max, constrain // If hit max, constrain
if (systemSettings.settingsValues[(int)option] >= constants.max) { if (systemSettings.settingsValues[(int)option] > constants.max) {
systemSettings.settingsValues[(int)option] = constants.max - 1; systemSettings.settingsValues[(int)option] = constants.max;
} }
} }
// Lookup wrapper for ease of use (with typing)
uint16_t getSettingValue(const enum SettingsOptions option) { return systemSettings.settingsValues[(int)option]; } uint16_t getSettingValue(const enum SettingsOptions option) { return systemSettings.settingsValues[(int)option]; }
// Increment by the step size to the next value. If past the end wrap to the minimum
// Returns true if we are on the _last_ value
bool nextSettingValue(const enum SettingsOptions option) { bool nextSettingValue(const enum SettingsOptions option) {
const auto constants = settingsConstants[(int)option]; const auto constants = settingsConstants[(int)option];
if (systemSettings.settingsValues[(int)option] >= (constants.max - constants.increment)) { if (systemSettings.settingsValues[(int)option] == (constants.max)) {
// Already at max, wrap to the start
systemSettings.settingsValues[(int)option] = constants.min; systemSettings.settingsValues[(int)option] = constants.min;
} else if (systemSettings.settingsValues[(int)option] >= (constants.max - constants.increment)) {
// If within one increment of the end, constrain to the end
systemSettings.settingsValues[(int)option] = constants.max;
} else { } else {
// Otherwise increment
systemSettings.settingsValues[(int)option] += constants.increment; systemSettings.settingsValues[(int)option] += constants.increment;
} }
return (constants.max - systemSettings.settingsValues[(int)option]) <= constants.increment; // Return if we are at the max
return constants.max == systemSettings.settingsValues[(int)option];
} }
// Step backwards on the settings item
// Return true if we are at the end (min)
bool prevSettingValue(const enum SettingsOptions option) { bool prevSettingValue(const enum SettingsOptions option) {
const auto constants = settingsConstants[(int)option]; const auto constants = settingsConstants[(int)option];
int value = systemSettings.settingsValues[(int)option]; int value = systemSettings.settingsValues[(int)option];
if (value <= constants.min) { if (value <= constants.min) {
value = constants.max; value = constants.max;
} else {
value -= constants.increment;
} }
value -= constants.increment;
systemSettings.settingsValues[(int)option] = value; systemSettings.settingsValues[(int)option] = value;
return systemSettings.settingsValues[(int)option] == constants.min; return systemSettings.settingsValues[(int)option] == constants.min;
} }