mirror of
https://github.com/justcallmekoko/ESP32Marauder.git
synced 2025-12-12 15:50:36 -08:00
Actually made settings work
This commit is contained in:
@@ -48,7 +48,7 @@
|
||||
|
||||
//#define TFT_SHIELD
|
||||
#define TFT_DIY
|
||||
//#define KIT
|
||||
#define KIT
|
||||
|
||||
#define SCREEN_WIDTH 240
|
||||
#define SCREEN_HEIGHT 320
|
||||
@@ -131,7 +131,7 @@ class Display
|
||||
TFT_eSPI tft = TFT_eSPI();
|
||||
TFT_eSprite img = TFT_eSprite(&tft);
|
||||
TFT_eSPI_Button key[BUTTON_ARRAY_LEN];
|
||||
const String PROGMEM version_number = "v0.9.3";
|
||||
const String PROGMEM version_number = "v0.9.4";
|
||||
|
||||
bool printing = false;
|
||||
bool loading = false;
|
||||
|
||||
@@ -1202,16 +1202,37 @@ void MenuFunctions::runBoolSetting(String key) {
|
||||
//display_obj.tftDrawGreenOnOffButton();
|
||||
}
|
||||
|
||||
void MenuFunctions::callSetting(String key) {
|
||||
String MenuFunctions::callSetting(String key) {
|
||||
specSettingMenu.name = key;
|
||||
|
||||
String setting_type = settings_obj.getSettingType(key);
|
||||
|
||||
if (setting_type == "bool") {
|
||||
this->runBoolSetting(key);
|
||||
return "bool";
|
||||
}
|
||||
}
|
||||
|
||||
void MenuFunctions::displaySetting(String key) {
|
||||
specSettingMenu.name = key;
|
||||
|
||||
bool setting_value = settings_obj.loadSetting<bool>(key);
|
||||
|
||||
display_obj.tft.setTextWrap(false);
|
||||
display_obj.tft.setFreeFont(NULL);
|
||||
display_obj.tft.setCursor(0, 100);
|
||||
display_obj.tft.setTextSize(1);
|
||||
|
||||
if (!setting_value) {
|
||||
display_obj.tft.setTextColor(TFT_RED);
|
||||
display_obj.tft.println(F("Setting disabled"));
|
||||
}
|
||||
else {
|
||||
display_obj.tft.setTextColor(TFT_GREEN);
|
||||
display_obj.tft.println(F("Setting on"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Function to build the menus
|
||||
void MenuFunctions::RunSetup()
|
||||
@@ -1572,10 +1593,13 @@ void MenuFunctions::RunSetup()
|
||||
changeMenu(settingsMenu.parentMenu);
|
||||
});
|
||||
for (int i = 0; i < settings_obj.getNumberSettings(); i++) {
|
||||
if (this->callSetting(settings_obj.setting_index_to_name(i)) == "bool")
|
||||
addNodes(&settingsMenu, settings_obj.setting_index_to_name(i), TFT_LIGHTGREY, NULL, 0, [this, i]() {
|
||||
settings_obj.toggleSetting(settings_obj.setting_index_to_name(i));
|
||||
changeMenu(&specSettingMenu);
|
||||
this->callSetting(settings_obj.setting_index_to_name(i));
|
||||
});
|
||||
//this->callSetting(settings_obj.setting_index_to_name(i));
|
||||
this->displaySetting(settings_obj.setting_index_to_name(i));
|
||||
}, settings_obj.loadSetting<bool>(settings_obj.setting_index_to_name(i)));
|
||||
}
|
||||
|
||||
// Specific setting menu
|
||||
@@ -1688,11 +1712,11 @@ void MenuFunctions::showMenuList(Menu * menu, int layer)
|
||||
|
||||
|
||||
// Function to add MenuNodes to a menu
|
||||
void MenuFunctions::addNodes(Menu * menu, String name, uint16_t color, Menu * child, int place, std::function<void()> callable)
|
||||
void MenuFunctions::addNodes(Menu * menu, String name, uint16_t color, Menu * child, int place, std::function<void()> callable, bool selected)
|
||||
{
|
||||
TFT_eSPI_Button new_button;
|
||||
menu->list->add(MenuNode{name, color, place, &new_button, callable});
|
||||
//strcpy(menu->list->get(-1).icon, bluetooth_icon);
|
||||
menu->list->add(MenuNode{name, color, place, &new_button, selected, callable});
|
||||
//menu->list->add(MenuNode{name, color, place, callable});
|
||||
}
|
||||
|
||||
void MenuFunctions::buildButtons(Menu * menu)
|
||||
@@ -1747,7 +1771,10 @@ void MenuFunctions::displayCurrentMenu()
|
||||
//display_obj.key[i].drawButton2(current_menu->list->get(i).name);
|
||||
//display_obj.key[i].drawButton(ML_DATUM, BUTTON_PADDING, current_menu->list->get(i).name);
|
||||
//display_obj.key[i].drawButton(true);
|
||||
//if (!current_menu->list->get(i).selected)
|
||||
display_obj.key[i].drawButton(false, current_menu->list->get(i).name);
|
||||
//else
|
||||
// display_obj.key[i].drawButton(true, current_menu->list->get(i).name);
|
||||
|
||||
if (current_menu->list->get(i).name != "Back")
|
||||
display_obj.tft.drawXBitmap(0,
|
||||
|
||||
@@ -109,6 +109,7 @@ struct MenuNode {
|
||||
uint16_t color;
|
||||
int icon;
|
||||
TFT_eSPI_Button* button;
|
||||
bool selected;
|
||||
std::function<void()> callable;
|
||||
};
|
||||
|
||||
@@ -173,14 +174,15 @@ class MenuFunctions
|
||||
|
||||
//TFT_eSPI_Button key[BUTTON_ARRAY_LEN];
|
||||
|
||||
void addNodes(Menu* menu, String name, uint16_t color, Menu* child, int place, std::function<void()> callable);
|
||||
void addNodes(Menu* menu, String name, uint16_t color, Menu* child, int place, std::function<void()> callable, bool selected = false);
|
||||
void drawStatusBar();
|
||||
void updateStatusBar();
|
||||
void battery(bool initial = false);
|
||||
void battery2(bool initial = false);
|
||||
void showMenuList(Menu* menu, int layer);
|
||||
void callSetting(String key);
|
||||
String callSetting(String key);
|
||||
void runBoolSetting(String ley);
|
||||
void displaySetting(String key);
|
||||
|
||||
public:
|
||||
MenuFunctions();
|
||||
|
||||
@@ -119,6 +119,76 @@ uint8_t Settings::loadSetting<uint8_t>(String key) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T Settings::saveSetting(String key, bool value) {}
|
||||
|
||||
template<>
|
||||
bool Settings::saveSetting<bool>(String key, bool value) {
|
||||
DynamicJsonDocument json(1024); // ArduinoJson v6
|
||||
|
||||
if (deserializeJson(json, this->json_settings_string)) {
|
||||
Serial.println("\nCould not parse json");
|
||||
}
|
||||
|
||||
String settings_string;
|
||||
|
||||
for (int i = 0; i < json["Settings"].size(); i++) {
|
||||
if (json["Settings"][i]["name"].as<String>() == key) {
|
||||
json["Settings"][i]["value"] = value;
|
||||
|
||||
Serial.println("Saving setting...");
|
||||
|
||||
File settingsFile = SPIFFS.open("/settings.json", FILE_WRITE);
|
||||
|
||||
if (!settingsFile) {
|
||||
Serial.println(F("Failed to create settings file"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (serializeJson(json, settingsFile) == 0) {
|
||||
Serial.println(F("Failed to write to file"));
|
||||
}
|
||||
if (serializeJson(json, settings_string) == 0) {
|
||||
Serial.println(F("Failed to write to string"));
|
||||
}
|
||||
|
||||
// Close the file
|
||||
settingsFile.close();
|
||||
|
||||
this->json_settings_string = settings_string;
|
||||
|
||||
this->printJsonSettings(settings_string);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Settings::toggleSetting(String key) {
|
||||
DynamicJsonDocument json(1024); // ArduinoJson v6
|
||||
|
||||
if (deserializeJson(json, this->json_settings_string)) {
|
||||
Serial.println("\nCould not parse json");
|
||||
}
|
||||
|
||||
for (int i = 0; i < json["Settings"].size(); i++) {
|
||||
if (json["Settings"][i]["name"].as<String>() == key) {
|
||||
if (json["Settings"][i]["value"]) {
|
||||
saveSetting<bool>(key, false);
|
||||
Serial.println("Setting value to false");
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
saveSetting<bool>(key, true);
|
||||
Serial.println("Setting value to true");
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String Settings::setting_index_to_name(int i) {
|
||||
DynamicJsonDocument json(1024); // ArduinoJson v6
|
||||
|
||||
@@ -181,36 +251,36 @@ bool Settings::createDefaultSettings(fs::FS &fs) {
|
||||
DynamicJsonDocument jsonBuffer(1024);
|
||||
String settings_string;
|
||||
|
||||
jsonBuffer["Settings"][0]["name"] = "Channel";
|
||||
jsonBuffer["Settings"][0]["type"] = "uint8_t";
|
||||
jsonBuffer["Settings"][0]["value"] = 11;
|
||||
jsonBuffer["Settings"][0]["range"]["min"] = 1;
|
||||
jsonBuffer["Settings"][0]["range"]["max"] = 14;
|
||||
//jsonBuffer["Settings"][0]["name"] = "Channel";
|
||||
//jsonBuffer["Settings"][0]["type"] = "uint8_t";
|
||||
//jsonBuffer["Settings"][0]["value"] = 11;
|
||||
//jsonBuffer["Settings"][0]["range"]["min"] = 1;
|
||||
//jsonBuffer["Settings"][0]["range"]["max"] = 14;
|
||||
|
||||
jsonBuffer["Settings"][1]["name"] = "Channel Hop Delay";
|
||||
jsonBuffer["Settings"][1]["type"] = "int";
|
||||
jsonBuffer["Settings"][1]["value"] = 1;
|
||||
jsonBuffer["Settings"][1]["range"]["min"] = 1;
|
||||
jsonBuffer["Settings"][1]["range"]["max"] = 10;
|
||||
//jsonBuffer["Settings"][1]["name"] = "Channel Hop Delay";
|
||||
//jsonBuffer["Settings"][1]["type"] = "int";
|
||||
//jsonBuffer["Settings"][1]["value"] = 1;
|
||||
//jsonBuffer["Settings"][1]["range"]["min"] = 1;
|
||||
//jsonBuffer["Settings"][1]["range"]["max"] = 10;
|
||||
|
||||
jsonBuffer["Settings"][2]["name"] = "Force PMKID";
|
||||
jsonBuffer["Settings"][0]["name"] = "Force PMKID";
|
||||
jsonBuffer["Settings"][0]["type"] = "bool";
|
||||
jsonBuffer["Settings"][0]["value"] = true;
|
||||
jsonBuffer["Settings"][0]["range"]["min"] = false;
|
||||
jsonBuffer["Settings"][0]["range"]["max"] = true;
|
||||
|
||||
jsonBuffer["Settings"][1]["name"] = "Force Probe";
|
||||
jsonBuffer["Settings"][1]["type"] = "bool";
|
||||
jsonBuffer["Settings"][1]["value"] = true;
|
||||
jsonBuffer["Settings"][1]["range"]["min"] = false;
|
||||
jsonBuffer["Settings"][1]["range"]["max"] = true;
|
||||
|
||||
jsonBuffer["Settings"][2]["name"] = "Save PCAP";
|
||||
jsonBuffer["Settings"][2]["type"] = "bool";
|
||||
jsonBuffer["Settings"][2]["value"] = true;
|
||||
jsonBuffer["Settings"][2]["range"]["min"] = false;
|
||||
jsonBuffer["Settings"][2]["range"]["max"] = true;
|
||||
|
||||
jsonBuffer["Settings"][3]["name"] = "Force Probe";
|
||||
jsonBuffer["Settings"][3]["type"] = "bool";
|
||||
jsonBuffer["Settings"][3]["value"] = true;
|
||||
jsonBuffer["Settings"][3]["range"]["min"] = false;
|
||||
jsonBuffer["Settings"][3]["range"]["max"] = true;
|
||||
|
||||
jsonBuffer["Settings"][4]["name"] = "Save PCAP";
|
||||
jsonBuffer["Settings"][4]["type"] = "bool";
|
||||
jsonBuffer["Settings"][4]["value"] = true;
|
||||
jsonBuffer["Settings"][4]["range"]["min"] = false;
|
||||
jsonBuffer["Settings"][4]["range"]["max"] = true;
|
||||
|
||||
//jsonBuffer.printTo(settingsFile);
|
||||
if (serializeJson(jsonBuffer, settingsFile) == 0) {
|
||||
Serial.println(F("Failed to write to file"));
|
||||
|
||||
@@ -25,6 +25,10 @@ class Settings {
|
||||
template <typename T>
|
||||
T loadSetting(String name);
|
||||
|
||||
template <typename T>
|
||||
T saveSetting(String key, bool value);
|
||||
|
||||
bool toggleSetting(String key);
|
||||
String getSettingType(String key);
|
||||
String setting_index_to_name(int i);
|
||||
int getNumberSettings();
|
||||
|
||||
Reference in New Issue
Block a user