#include /* Standard Arduino library */ #include /* Filesystem */ #include #include /***************************************************************** FALLS EINE LED ANGESCHLOSSSEN IST, BITTE HIER DEN PIN ANGEBEN FALLS EINE LED ANGESCHLOSSSEN IST, BITTE HIER DEN PIN ANGEBEN FALLS EINE LED ANGESCHLOSSSEN IST, BITTE HIER DEN PIN ANGEBEN *****************************************************************/ #define LED_PIN D5 class PublicKeyValuePairs { public: // Constructor initializes default keys and reads custom key-value pairs from file PublicKeyValuePairs() { /* Initialization of DEFAULT key-value pairs */ keyValueList["DEFAULT_basicButtonBounceDelay"] = "15"; /* Initialization of 'custom' key-value pairs */ keyValueList["basicButtonBounceDelay"] = "15"; /* Read custom key-value pairs from file */ readKeyValuePairsFromFile(); /* Remove duplicates from custom-keys if any */ removeDuplicates(); /* If file does not exist, write the initial custom key-value pairs to file */ if (!LittleFS.exists("/customKeyValues.txt")) { writeInitialKeyValuePairsToFile(); } } /* Get the value associated with a key */ String getKeyValue(const String& key) { if (keyValueList.find(key) != keyValueList.end()) { return keyValueList[key]; } else if (keyValueList.find("DEFAULT_" + key) != keyValueList.end()) { return keyValueList["DEFAULT_" + key]; } else { while (true) {} // Error: Key not found } } /* Set value associated with a key */ void setCustomKeyValue(const String& key, const String& value) { if (!defaultNameIsExisting(key)) { while (true) {} // Error: Key not found in DEFAULT_ keys } if (keyValueList.find(key) == keyValueList.end()) { keyValueList[key] = value; appendKeyValueToFile(key, value); } else { keyValueList[key] = value; overwriteKeyValueInFile(key, value); } } private: bool defaultNameIsExisting(const String& key) { return keyValueList.find("DEFAULT_" + key) != keyValueList.end(); } void readKeyValuePairsFromFile() { if (!LittleFS.begin()) { while (true) {} // Error: Failed to mount the file system } if (LittleFS.exists("/customKeyValues.txt")) { File file = LittleFS.open("/customKeyValues.txt", "r"); if (!file) { while (true) {} // Error: Failed to open the file } while (file.available()) { String line = file.readStringUntil('\n'); int separatorIndex = line.indexOf("=="); if (separatorIndex != -1) { String key = line.substring(0, separatorIndex); String value = line.substring(separatorIndex + 2); key.trim(); value.trim(); keyValueList[key] = value; } else { while (true) {} // Error } } file.close(); } } void writeInitialKeyValuePairsToFile() { File file = LittleFS.open("/customKeyValues.txt", "w"); if (!file) { while (true) {} // Error: Failed to create the file } for (const auto& pair : keyValueList) { if (!pair.first.startsWith("DEFAULT_")) { file.println(pair.first + "==" + pair.second); } } file.close(); } void appendKeyValueToFile(const String& key, const String& value) { File file = LittleFS.open("/customKeyValues.txt", "a"); if (!file) { while (true) {} // Error: Failed to open the file for appending } file.println(key + "==" + value); file.close(); } void overwriteKeyValueInFile(const String& key, const String& value) { File file = LittleFS.open("/customKeyValues.txt", "r"); if (!file) { while (true) {} // Error: Failed to open the file for reading } String fileContent; bool keyFound = false; while (file.available()) { String line = file.readStringUntil('\n'); if (line.startsWith(key + "==")) { if (keyFound) { while (true) {} // Error: Duplicate key found } keyFound = true; } else { fileContent += line + "\n"; } } file.close(); file = LittleFS.open("/customKeyValues.txt", "w"); if (!file) { while (true) {} // Error: Failed to open the file for writing } file.print(fileContent); file.println(key + "==" + value); file.close(); } void removeDuplicates() { std::map uniquekeyValueList; std::set processedKeys; for (const auto& pair : keyValueList) { if (processedKeys.find(pair.first) != processedKeys.end()) { if (pair.first.startsWith("DEFAULT_")) { while (true) {} // Error: Duplicate DEFAULT_ key found } else { continue; // Non-DEFAULT_ key duplicate; we skip this key } } processedKeys.insert(pair.first); uniquekeyValueList[pair.first] = pair.second; } keyValueList = uniquekeyValueList; } std::map keyValueList; }; PublicKeyValuePairs keyValuePairs; void setup() { // Initialize pin D5 as an output pinMode(LED_PIN, OUTPUT); Serial.begin(9600); delay(100); // Delay to ensure the serial port is initialized // Example usage Serial.println("Hallo!"); /* Hole den Wert der generellen Verzögerung für das Entprellen von angeschlossenen Tastern (in Millisekunden) */ Serial.println(keyValuePairs.getKeyValue("basicButtonBounceDelay")); /* Definiere bzw. setze einen eigenen Wert für das generelle Entprellen */ keyValuePairs.setCustomKeyValue("basicButtonBounceDelay", "20"); /* Hole den neu gesetzten Wert */ Serial.println(keyValuePairs.getKeyValue("basicButtonBounceDelay")); } void loop() { /* LED sollte blinken, falls der upload etc. funktioniert hat */ /* LED sollte blinken, falls der upload etc. funktioniert hat */ /* LED sollte blinken, falls der upload etc. funktioniert hat */ // Turn the LED on (HIGH is the voltage level) digitalWrite(D5, HIGH); // Wait for 1 second delay(1000); // Turn the LED off by making the voltage LOW digitalWrite(D5, LOW); // Wait for 1 second delay(1000); }