1
0
forked from me/IronOS

Pass one settings refactor

This commit is contained in:
Ben V. Brown
2021-09-12 13:31:03 +10:00
parent fc47c71ec3
commit eb2a748e3f
10 changed files with 452 additions and 496 deletions

View File

@@ -12,85 +12,147 @@
#include "BSP.h"
#include "Setup.h"
#include "configuration.h"
#include <string.h> // for memset
bool sanitiseSettings();
#include "string.h"
/*
* This struct must be a multiple of 2 bytes as it is saved / restored from
* flash in uint16_t chunks
*/
typedef struct {
uint16_t length; // Length of valid bytes following
uint16_t settingsValues[SettingsOptionsLength];
// used to make this nicely "good enough" aligned to 32 butes to make driver code trivial
uint32_t padding;
} systemSettingsType;
//~1024 is common programming size, setting threshold to be lower so we have warning
static_assert(sizeof(systemSettingsType) < 512);
// char (*__kaboom)[sizeof(systemSettingsType)] = 1; // Uncomment to print size at compile time
volatile systemSettingsType systemSettings;
void saveSettings() {
// First we erase the flash
flash_save_buffer((uint8_t *)&systemSettings, sizeof(systemSettingsType));
}
// For every setting we need to store the min/max/increment values
typedef struct {
const uint16_t min; // Inclusive minimum value
const uint16_t max; // Exclusive maximum value
const uint16_t increment; // Standard increment
const uint16_t defaultValue; // Default vaue after reset
} SettingConstants;
static const SettingConstants settingsConstants[(int)SettingsOptions::SettingsOptionsLength] = {
//{min,max,increment,default}
{0, 0, 0, 0}, // SolderingTemp
{10, 580, 0, 0}, // SleepTemp
{0, 16, 1, 0}, // SleepTime
{0, 5, 1, 0}, // MinDCVoltageCells
{24, 38, 1, 31}, // MinVoltageCells
{0, 0, 0, 0}, // QCIdealVoltage
{0, 0, 0, 0}, // OrientationMode
{0, 10, 0, 0}, // Sensitivity
{0, 1, 1, 1}, // AnimationLoop
{0, settingOffSpeed_t::MAX_VALUE, 1, settingOffSpeed_t::MEDIUM}, // AnimationSpeed
{0, 4, 1, 0}, // AutoStartMode
{0, 60, 1, 0}, // ShutdownTime
{0, 1, 1, 0}, // CoolingTempBlink
{0, 1, 1, 0}, // DetailedIDLE
{0, 1, 1, 0}, // DetailedSoldering
{0, 1, 1, 0}, // TemperatureInF
{0, 0, 0, 0}, // DescriptionScrollSpeed
{0, 3, 1, 0}, // LockingMode
{0, 100, 1, 0}, // KeepAwakePulse
{1, POWER_PULSE_WAIT_MAX, 1, 0}, // KeepAwakePulseWait
{1, POWER_PULSE_DURATION_MAX, 1, 0}, // KeepAwakePulseDuration
{360, 900, 1, 0}, // VoltageDiv
{0, 0, 0, 0}, // BoostTemp
{0, 0, 0, 0}, // CalibrationOffset
{0, MAX_POWER_LIMIT, POWER_LIMIT_STEPS, 0}, // PowerLimit
{0, 1, 1, 0}, // ReverseButtonTempChangeEnabled
{5, 90, 5, 10}, // TempChangeLongStep
{1, TEMP_CHANGE_SHORT_STEP_MAX, 1, 1}, // TempChangeShortStep
{0, 4, 1, 0}, // HallEffectSensitivity
{0, 10, 1, 0}, // AccelMissingWarningCounter
{0, 10, 1, 0}, // PDMissingWarningCounter
{0, 0, 0, 0}, // UILanguage
{0, 50, 1, 0}, // PDNegTimeout
bool restoreSettings() {
};
void saveSettings() { flash_save_buffer((uint8_t *)&systemSettings, sizeof(systemSettingsType)); }
bool loadSettings() {
// We read the flash
flash_read_buffer((uint8_t *)&systemSettings, sizeof(systemSettingsType));
// if the version is correct were done
// if not we reset and save
if (systemSettings.version != SETTINGSVERSION) {
// probably not setup
resetSettings();
return true;
}
return false;
// Then ensure all values are valid
return sanitiseSettings();
}
// Lookup function for cutoff setting -> X10 voltage
/*
* 0=DC
* 1=3S
* 2=4S
* 3=5S
* 4=6S
*/
uint8_t lookupVoltageLevel() {
if (systemSettings.minDCVoltageCells == 0)
return 90; // 9V since iron does not function effectively below this
else
return (systemSettings.minDCVoltageCells * systemSettings.minVoltageCells) + (systemSettings.minVoltageCells * 2);
bool sanitiseSettings() {
// 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
bool dirty = false;
if (systemSettings.length < (int)SettingsOptions::SettingsOptionsLength) {
dirty = true;
for (int i = systemSettings.length; i < (int)SettingsOptions::SettingsOptionsLength; i++) {
systemSettings.settingsValues[i] = 0xFFFF; // Ensure its as if it was erased
}
systemSettings.length = (int)SettingsOptions::SettingsOptionsLength;
}
for (int i = 0; i < (int)SettingsOptions::SettingsOptionsLength; i++) {
// 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) {
systemSettings.settingsValues[i] = settingsConstants[i].defaultValue;
dirty = true;
}
}
if (dirty) {
saveSettings();
}
return dirty;
}
void resetSettings() {
memset((void *)&systemSettings, 0, sizeof(systemSettingsType));
systemSettings.SleepTemp = SLEEP_TEMP; // Temperature the iron sleeps at - default 150.0 C
systemSettings.SleepTime = SLEEP_TIME; // How many seconds/minutes we wait until going
// to sleep - default 1 min
systemSettings.SolderingTemp = SOLDERING_TEMP; // Default soldering temp is 320.0 C
systemSettings.minDCVoltageCells = CUT_OUT_SETTING; // default to no cut-off voltage
systemSettings.minVoltageCells = RECOM_VOL_CELL; // Minimum voltage per cell (Recommended 3.3V (33))
systemSettings.QCIdealVoltage = 0; // Default to 9V for QC3.0 Voltage
systemSettings.PDNegTimeout = 0; // Default for PD timout to 0
systemSettings.version = SETTINGSVERSION; // Store the version number to allow for easier upgrades
systemSettings.detailedSoldering = DETAILED_SOLDERING; // Detailed soldering screen
systemSettings.detailedIDLE = DETAILED_IDLE; // Detailed idle screen (off for first time users)
systemSettings.OrientationMode = ORIENTATION_MODE; // Default to automatic
systemSettings.sensitivity = SENSITIVITY; // Default high sensitivity
systemSettings.voltageDiv = VOLTAGE_DIV; // Default divider from schematic
systemSettings.ShutdownTime = SHUTDOWN_TIME; // How many minutes until the unit turns itself off
systemSettings.BoostTemp = BOOST_TEMP; // default to 400C
systemSettings.autoStartMode = AUTO_START_MODE; // Auto start off for safety
systemSettings.lockingMode = LOCKING_MODE; // Disable locking for safety
systemSettings.coolingTempBlink = COOLING_TEMP_BLINK; // Blink the temperature on the cooling screen when its > 50C
systemSettings.temperatureInF = TEMPERATURE_INF; // default to 0
systemSettings.descriptionScrollSpeed = DESCRIPTION_SCROLL_SPEED; // default to slow
systemSettings.animationLoop = ANIMATION_LOOP; // Default false
systemSettings.animationSpeed = ANIMATION_SPEED; // Default 400 ms (Medium)
systemSettings.CalibrationOffset = CALIBRATION_OFFSET; // the adc offset in uV
systemSettings.powerLimit = POWER_LIMIT; // 30 watts default limit
systemSettings.ReverseButtonTempChangeEnabled = REVERSE_BUTTON_TEMP_CHANGE; //
systemSettings.TempChangeShortStep = TEMP_CHANGE_SHORT_STEP; //
systemSettings.TempChangeLongStep = TEMP_CHANGE_LONG_STEP; //
systemSettings.KeepAwakePulse = POWER_PULSE_DEFAULT; // Power of the power pulse
systemSettings.KeepAwakePulseWait = POWER_PULSE_WAIT_DEFAULT; // Time between Keep Awake pulses in 2.5 second increments
systemSettings.KeepAwakePulseDuration = POWER_PULSE_DURATION_DEFAULT; // Duration of the Keep Awake pusle in 250ms increments
systemSettings.hallEffectSensitivity = 1;
systemSettings.accelMissingWarningCounter = 0;
systemSettings.pdMissingWarningCounter = 0;
memset((void *)&systemSettings, 0xFF, sizeof(systemSettingsType));
sanitiseSettings();
saveSettings(); // Save defaults
}
void setSettingValue(const enum SettingsOptions option, const uint16_t newValue) {
const auto constants = settingsConstants[(int)option];
systemSettings.settingsValues[(int)option] = newValue;
if (systemSettings.settingsValues[(int)option] < constants.min) {
systemSettings.settingsValues[(int)option] = constants.min;
}
// If hit max, constrain
if (systemSettings.settingsValues[(int)option] >= constants.max) {
systemSettings.settingsValues[(int)option] = constants.max - 1;
}
}
uint16_t getSettingValue(const enum SettingsOptions option) { return systemSettings.settingsValues[(int)option]; }
bool nextSettingValue(const enum SettingsOptions option) {
const auto constants = settingsConstants[(int)option];
if (systemSettings.settingsValues[(int)option] == (constants.max - 1)) {
systemSettings.settingsValues[(int)option] = constants.min;
} else {
systemSettings.settingsValues[(int)option] += constants.increment;
}
return systemSettings.settingsValues[(int)option] == constants.max - 1;
}
bool prevSettingValue(const enum SettingsOptions option) {
const auto constants = settingsConstants[(int)option];
int value = systemSettings.settingsValues[(int)option];
if (value == constants.min) {
value = constants.max;
} else {
value -= constants.increment;
}
systemSettings.settingsValues[(int)option] = value;
return systemSettings.settingsValues[(int)option] == constants.min;
}
uint16_t lookupHallEffectThreshold() {
// Return the threshold above which the hall effect sensor is "activated"
switch (systemSettings.hallEffectSensitivity) {
switch (getSettingValue(SettingsOptions::HallEffectSensitivity)) {
case 0:
return 0;
case 1: // Low
@@ -103,3 +165,19 @@ uint16_t lookupHallEffectThreshold() {
return 0; // Off
}
}
// Lookup function for cutoff setting -> X10 voltage
/*
* 0=DC
* 1=3S
* 2=4S
* 3=5S
* 4=6S
*/
uint8_t lookupVoltageLevel() {
auto minVoltageOnCell = getSettingValue(SettingsOptions::MinDCVoltageCells);
auto minVoltageCellCount = getSettingValue(SettingsOptions::MinVoltageCells);
if (minVoltageOnCell == 0)
return 90; // 9V since iron does not function effectively below this
else
return (minVoltageOnCell * minVoltageCellCount) + (minVoltageCellCount * 2);
}