You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
6.1 KiB

uint8_t currentSetting = 0;
#define SETTINGS_COUNT 8
#define pageDisplay 0
#define limitMode 1
#define fanOveride 2
#define wheelDiameter 3 // Speed factor
#define lightMode 4
#define minHallValue 5
#define centerHallValue 6
#define maxHallValue 7
int settings[SETTINGS_COUNT];
uint8_t boardOptions = 0b00000000;
// Map:(LightActive, Fan, LightMode) XXXAFFLL
/*
float gearRatio;
float ratioRpmSpeed;
float ratioPulseDistance;
*/
// Defining variables for Settings menu
bool changeSelectedSetting = false;
bool settingsLoopFlag = false;
bool settingsChangeFlag = false;
bool settingsChangeValueFlag = false;
const char *settingPages[SETTINGS_COUNT] = {
"Page Display",
"LimitMode",
"FanOveride",
"Wheel diameter",
"Light Mode",
"Throttle Min",
"Throttle Center",
"Throttle Max"
};
// Setting rules format: default, min, max.
int settingRules[SETTINGS_COUNT][3] {
{0, 0, 3}, //page: 0 cycle, 1 speed, 2 volt, 3 temp
{0, 0, 1}, //limit default is no limit
{0, 0, 2}, //fan 0 no, 1 on, 2 off
{83, 0, 250}, // wheel mm
{0, 0, 3}, // LightMode
{HAL_MIN, 1000, 3000},
{HAL_CENTER, 1000, 3000},
{HAL_MAX, 1000, 3000}
};
/*
const char *settingAliases[SETTINGS_COUNT] = {
"Off", "ON"
"Auto", "Off", "On",
//"Wheel diameter",
//"Throttle Min",
//"Throttle Center",
//"Throttle Max"
};
*/
// Check if an integer is within a min and max value
bool inRange(int val, int minimum, int maximum) {
return ((minimum <= val) && (val <= maximum));
}
void updateBoardOptions() {
boardOptions = 0;
boardOptions |= (uint8_t) settings[fanOveride] << 2;
boardOptions |= boardOptions | (uint8_t) lightActive << 3;
boardOptions |= boardOptions | (uint8_t) settings[lightMode];
DEBUG_PRINT("board: "); DEBUG_PRINTLN(boardOptions);
}
void updateSettings() {
preferences.begin("remote-set", false);
for (int i = 0; i < SETTINGS_COUNT; i++) {
preferences.putInt(settingPages[i], settings[i]);
}
preferences.end();
DEBUG_PRINTLN("UPDATED");
preferences.begin("remote-set", false);
DEBUG_PRINTLN(preferences.getInt(settingPages[0], 0));
preferences.end();
//calculateRatios();
updateBoardOptions();
}
void setDefaultSettings() {
for (int i = 0; i < SETTINGS_COUNT; i++) {
settings[i] = settingRules[i][0];
}
updateSettings();
}
void loadSettings() {
preferences.begin("remote-set", false);
for (int i = 0; i < SETTINGS_COUNT; i++) {
settings[i] = preferences.getInt(settingPages[i], settingRules[i][0]); // retrieve default value
}
preferences.end();
bool rewriteSettings = false;
// Loop through all settings to check if everything is fine
for (int i = 0; i < SETTINGS_COUNT; i++) {
int val = settings[i];
if (!inRange(val, settingRules[i][1], settingRules[i][2])) {
// Setting is damaged or never written. Rewrite default.
rewriteSettings = true;
settings[i] = settingRules[i][0];
}
}
if (rewriteSettings == true) {
updateSettings();
} else {
// Calculate constants
//calculateRatios();
updateBoardOptions();
}
}
void drawSettingsMenu() {
// Position on OLED
int x = 0; int y = 10;
// Draw setting title
displayString = settingPages[currentSetting];
displayString.toCharArray(displayBuffer, displayString.length() + 1);
u8g2.setFont(u8g2_font_profont12_tr);
u8g2.drawStr(x, y, displayBuffer);
int val = settings[currentSetting];
displayString = (String)val + ""; //+ settingPages[currentSetting][1];
displayString.toCharArray(displayBuffer, displayString.length() + 1);
u8g2.setFont(u8g2_font_10x20_tr );
if (changeSelectedSetting == true) {
u8g2.drawStr(x + 10, y + 20, displayBuffer);
} else {
u8g2.drawStr(x, y + 20, displayBuffer);
}
}
void drawSettingNumber() {
// Position on OLED
int x = 2; int y = 10;
// Draw current setting number box
u8g2.drawRFrame(x + 102, y - 10, 22, 32, 4);
// Draw current setting number
displayString = (String)(currentSetting + 1);
displayString.toCharArray(displayBuffer, displayString.length() + 1);
u8g2.setFont(u8g2_font_profont22_tn);
u8g2.drawStr(x + 108, 22, displayBuffer);
}
/*
// Update values used to calculate speed and distance travelled.
void calculateRatios() {
// Defining variables for speed and distance calculation
gearRatio = (float)remoteSettings.motorPulley / (float)remoteSettings.wheelPulley;
ratioRpmSpeed = (gearRatio * 60 * (float)remoteSettings.wheelDiameter * 3.14156) / (((float)remoteSettings.motorPoles / 2) * 1000000); // ERPM to Km/h
ratioPulseDistance = (gearRatio * (float)remoteSettings.wheelDiameter * 3.14156) / (((float)remoteSettings.motorPoles * 3) * 1000000); // Pulses to km travelled
}
*/
void controlSettingsMenu() {
if (triggerActive()) {
if (settingsChangeFlag == false) {
// Save settings
if (changeSelectedSetting == true) {
updateSettings();
}
changeSelectedSetting = !changeSelectedSetting;
settingsChangeFlag = true;
}
} else {
settingsChangeFlag = false;
}
if (hallMeasurement >= (settings[maxHallValue] - 250) && settingsLoopFlag == false) {
// Up
if (changeSelectedSetting == true) {
int val = settings[currentSetting] - 1;
if (inRange(val, settingRules[currentSetting][1], settingRules[currentSetting][2])) {
settings[currentSetting] = val;
settingsLoopFlag = true;
}
} else {
if (currentSetting != 0) {
currentSetting--;
settingsLoopFlag = true;
}
}
} else if (hallMeasurement <= (settings[minHallValue] + 250) && settingsLoopFlag == false) {
// Down
if (changeSelectedSetting == true) {
int val = settings[currentSetting] + 1;
if (inRange(val, settingRules[currentSetting][1], settingRules[currentSetting][2])) {
settings[currentSetting] = val;
settingsLoopFlag = true;
}
} else {
if (currentSetting < (SETTINGS_COUNT - 1)) {
currentSetting++;
settingsLoopFlag = true;
}
}
} else if (inRange(hallMeasurement, settings[centerHallValue] - 50, settings[centerHallValue] + 50)) {
settingsLoopFlag = false;
}
}