diff --git a/data/config.default.json b/data/config.default.json new file mode 100644 index 0000000..204d4be --- /dev/null +++ b/data/config.default.json @@ -0,0 +1,4 @@ +{ + "ssid": "NONE", + "password": "12345678" +} diff --git a/data/keyconfig.default.json b/data/keyconfig.default.json new file mode 100644 index 0000000..19108a9 --- /dev/null +++ b/data/keyconfig.default.json @@ -0,0 +1,36 @@ +{ + "keyConfig": [ + { + "title": "Default", + "keymap": [ + [177, 49, 50, 51, 52, 53, 0], + [179, 113, 119, 101, 114, 116, 8], + [193, 97, 115, 100, 102, 103, 0], + [129, 122, 120, 99, 118, 98, 176], + [128, 0, 130, 131, 32, 131, 131] + ], + "keyInfo": [ + ["Escape", "1", "2", "3", "4", "5", "NULL"], + ["Tab", "Q", "W", "E", "R", "T", "Delete"], + ["Caps Lock", "A", "S", "D", "F", "G", "NULL"], + ["Shift", "Z", "X", "C", "V", "B", "Enter"], + ["Ctrl", "FN", "Option", "Command", "Space", "Command", "Command"] + ] + } + ], + "macros": [], + "onBoardRotaryEncoder": [ + { + "rotaryMap": [0, 216, 215], + "rotaryInfo": ["NULL", "ArrowLeft", "ArrowRight"] + } + ], + "rotaryExtension": [ + { + "keymap": [97, 98, 99], + "keyInfo": ["A", "B", "C"], + "rotaryMap": [32, 91, 93], + "rotaryInfo": ["Space", "BracketLeft", "BracketRight"] + } + ] +} diff --git a/src/main.cpp b/src/main.cpp index 21552d3..af1651a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -894,45 +894,7 @@ void loop() { } } - // Read CFG Buttons - if (digitalRead(CFG_BTN_PIN_1) == ACTIVE) { - resetIdle(); - int longPressCounter = 0; - while (digitalRead(CFG_BTN_PIN_1) == ACTIVE) { - delay(10); - if (longPressCounter > 100) { - goSleeping(); - } - longPressCounter++; - } - isOutputLocked = !isOutputLocked; - } else if (digitalRead(CFG_BTN_PIN_2) == ACTIVE) { - resetIdle(); - int longPressCounter = 0; - while (digitalRead(CFG_BTN_PIN_2) == ACTIVE) { - delay(10); - if (longPressCounter > 100) { - switchBootMode(); - } - longPressCounter++; - } - isUsbMode = !isUsbMode; - usbKeyboard.releaseAll(); - bleKeyboard.releaseAll(); - } else if (digitalRead(CFG_BTN_PIN_0) == ACTIVE) { - resetIdle(); - int longPressCounter = 0; - while (digitalRead(CFG_BTN_PIN_0) == ACTIVE) { - delay(10); - if (longPressCounter > 100) { - switchBootMode(); - } - longPressCounter++; - } - isUsbMode = !isUsbMode; - usbKeyboard.releaseAll(); - bleKeyboard.releaseAll(); - } + readConfigButtons(); } /** @@ -1099,6 +1061,97 @@ void updateKeymaps() { configUpdated = true; } +void readConfigButtons() { + int longPressCounter = 0; + if (digitalRead(CFG_BTN_PIN_1) == ACTIVE) { + resetIdle(); + while (digitalRead(CFG_BTN_PIN_1) == ACTIVE) { + if (digitalRead(CFG_BTN_PIN_2) == ACTIVE) { + resetConfigFiles(); + return; + } + delay(10); + if (longPressCounter > 100) { + goSleeping(); + } + longPressCounter++; + } + isOutputLocked = !isOutputLocked; + } else if (digitalRead(CFG_BTN_PIN_2) == ACTIVE) { + resetIdle(); + while (digitalRead(CFG_BTN_PIN_2) == ACTIVE) { + if (digitalRead(CFG_BTN_PIN_1) == ACTIVE) { + resetConfigFiles(); + return; + } + delay(10); + if (longPressCounter > 100) { + switchBootMode(); + } + longPressCounter++; + } + isUsbMode = !isUsbMode; + usbKeyboard.releaseAll(); + bleKeyboard.releaseAll(); + } else if (digitalRead(CFG_BTN_PIN_0) == ACTIVE) { + resetIdle(); + while (digitalRead(CFG_BTN_PIN_0) == ACTIVE) { + delay(10); + if (longPressCounter > 100) { + switchBootMode(); + } + longPressCounter++; + } + isUsbMode = !isUsbMode; + usbKeyboard.releaseAll(); + bleKeyboard.releaseAll(); + } +} + +void resetConfigFiles() { + byte countDown = 3; + resetIdle(); + for (byte countDown = 3; 0 < countDown; countDown--) { + updateKeyInfo = true; + currentKeyInfo = "Reset config in " + (String)countDown; + + if (digitalRead(CFG_BTN_PIN_1) != ACTIVE || + digitalRead(CFG_BTN_PIN_2) != ACTIVE) { + updateKeyInfo = true; + currentKeyInfo = "Reset canceled"; + delay(1000); + return; + } + delay(1000); + } + updateKeyInfo = true; + currentKeyInfo = "Resetting config..."; + delay(500); + if (SPIFFS.begin()) { + File configFile = SPIFFS.open("/config.default.json", "r"); + File keyConfigFile = SPIFFS.open("/keyconfig.default.json", "r"); + File configFileWrite = SPIFFS.open("/config.json", "w"); + File keyConfigFileWrite = SPIFFS.open("/keyconfig.json", "w"); + if (configFile && configFileWrite) { + while (configFile.available()) { + configFileWrite.write(configFile.read()); + } + configFile.close(); + configFileWrite.close(); + } + if (keyConfigFile && keyConfigFileWrite) { + while (keyConfigFile.available()) { + keyConfigFileWrite.write(keyConfigFile.read()); + } + keyConfigFile.close(); + keyConfigFileWrite.close(); + } + currentLayoutIndex = 0; + keymapsNeedsUpdate = true; + } + return; +} + /** * Press USB HID keys. (Fix for modifier keys) * diff --git a/src/main.hpp b/src/main.hpp index 4eddebb..705196b 100644 --- a/src/main.hpp +++ b/src/main.hpp @@ -8,11 +8,6 @@ #include #include #include - -#include "USB.h" -#include "USBHIDKeyboard.h" -#include "driver/adc.h" -#include "esp_adc_cal.h" #include #include #include @@ -27,6 +22,11 @@ #include #include +#include "USB.h" +#include "USBHIDKeyboard.h" +#include "driver/adc.h" +#include "esp_adc_cal.h" + using namespace std; #define BAUD_RATE 115200 @@ -128,6 +128,7 @@ void switchLayout(); void switchLayout(int layoutIndex); int findLayoutIndex(String layoutName); void switchDevice(); +void readConfigButtons(); // OLED Control void renderScreen(); @@ -145,6 +146,7 @@ bool getUSBPowerState(); // File Management String loadJSONFileAsString(String filename); +void resetConfigFiles(); // Web Server void initWebServer();