Skip to content

Commit

Permalink
feat: add reset default config
Browse files Browse the repository at this point in the history
  • Loading branch information
DriftKingTW committed Feb 5, 2024
1 parent e52a28e commit 782565c
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 44 deletions.
4 changes: 4 additions & 0 deletions data/config.default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"ssid": "NONE",
"password": "12345678"
}
36 changes: 36 additions & 0 deletions data/keyconfig.default.json
Original file line number Diff line number Diff line change
@@ -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"]
}
]
}
131 changes: 92 additions & 39 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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)
*
Expand Down
12 changes: 7 additions & 5 deletions src/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
#include <ImprovWiFiLibrary.h>
#include <PCF8574.h>
#include <SPIFFS.h>

#include "USB.h"
#include "USBHIDKeyboard.h"
#include "driver/adc.h"
#include "esp_adc_cal.h"
#include <U8g2lib.h>
#include <WebServer.h>
#include <WiFi.h>
Expand All @@ -27,6 +22,11 @@
#include <iterator>
#include <string>

#include "USB.h"
#include "USBHIDKeyboard.h"
#include "driver/adc.h"
#include "esp_adc_cal.h"

using namespace std;

#define BAUD_RATE 115200
Expand Down Expand Up @@ -128,6 +128,7 @@ void switchLayout();
void switchLayout(int layoutIndex);
int findLayoutIndex(String layoutName);
void switchDevice();
void readConfigButtons();

// OLED Control
void renderScreen();
Expand All @@ -145,6 +146,7 @@ bool getUSBPowerState();

// File Management
String loadJSONFileAsString(String filename);
void resetConfigFiles();

// Web Server
void initWebServer();
Expand Down

0 comments on commit 782565c

Please sign in to comment.