From 8f4823586ff8cbc039aafa122be2930efaa15837 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sat, 30 Aug 2025 17:22:33 +0200 Subject: [PATCH] Update code to work on fabricated PCB --- .clangd | 2 ++ client/compile_commands.json | 1 + client/extra_script.py | 10 ++++++ client/include/README | 37 -------------------- client/lib/README | 46 ------------------------ client/platformio.ini | 14 +++++--- client/src/connect.cpp | 68 ++++++++++++++++++++++++++++++++++++ client/src/connect.h | 5 +++ client/src/main.cpp | 43 +++++++++++------------ client/test/README | 11 ------ makefile | 34 +++++++++--------- server/compile_commands.json | 1 + server/extra_script.py | 10 ++++++ server/include/README | 37 -------------------- server/lib/README | 46 ------------------------ server/platformio.ini | 10 ++++-- server/src/main.cpp | 32 +++++++++++------ server/test/README | 11 ------ 18 files changed, 175 insertions(+), 243 deletions(-) create mode 100644 .clangd create mode 120000 client/compile_commands.json create mode 100644 client/extra_script.py delete mode 100644 client/include/README delete mode 100644 client/lib/README create mode 100644 client/src/connect.cpp create mode 100644 client/src/connect.h delete mode 100644 client/test/README create mode 120000 server/compile_commands.json create mode 100644 server/extra_script.py delete mode 100644 server/include/README delete mode 100644 server/lib/README delete mode 100644 server/test/README diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..2ae9372 --- /dev/null +++ b/.clangd @@ -0,0 +1,2 @@ +CompileFlags: + Remove: [-fstrict-volatile-bitfields, -fno-tree-switch-conversion] diff --git a/client/compile_commands.json b/client/compile_commands.json new file mode 120000 index 0000000..53004e3 --- /dev/null +++ b/client/compile_commands.json @@ -0,0 +1 @@ +.pio/build/client/compile_commands.json \ No newline at end of file diff --git a/client/extra_script.py b/client/extra_script.py new file mode 100644 index 0000000..a0afaf0 --- /dev/null +++ b/client/extra_script.py @@ -0,0 +1,10 @@ +import os +Import("env") + +# include toolchain paths +env.Replace(COMPILATIONDB_INCLUDE_TOOLCHAIN=True) + +# override compilation DB path +env.Replace(COMPILATIONDB_PATH=os.path.join("$BUILD_DIR", "compile_commands.json")) + +env.AddPostAction("buildprog", lambda *_, **__: env.Execute("pio run -t compiledb")) diff --git a/client/include/README b/client/include/README deleted file mode 100644 index 49819c0..0000000 --- a/client/include/README +++ /dev/null @@ -1,37 +0,0 @@ - -This directory is intended for project header files. - -A header file is a file containing C declarations and macro definitions -to be shared between several project source files. You request the use of a -header file in your project source file (C, C++, etc) located in `src` folder -by including it, with the C preprocessing directive `#include'. - -```src/main.c - -#include "header.h" - -int main (void) -{ - ... -} -``` - -Including a header file produces the same results as copying the header file -into each source file that needs it. Such copying would be time-consuming -and error-prone. With a header file, the related declarations appear -in only one place. If they need to be changed, they can be changed in one -place, and programs that include the header file will automatically use the -new version when next recompiled. The header file eliminates the labor of -finding and changing all the copies as well as the risk that a failure to -find one copy will result in inconsistencies within a program. - -In C, the convention is to give header files names that end with `.h'. - -Read more about using header files in official GCC documentation: - -* Include Syntax -* Include Operation -* Once-Only Headers -* Computed Includes - -https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/client/lib/README b/client/lib/README deleted file mode 100644 index 9379397..0000000 --- a/client/lib/README +++ /dev/null @@ -1,46 +0,0 @@ - -This directory is intended for project specific (private) libraries. -PlatformIO will compile them to static libraries and link into the executable file. - -The source code of each library should be placed in a separate directory -("lib/your_library_name/[Code]"). - -For example, see the structure of the following example libraries `Foo` and `Bar`: - -|--lib -| | -| |--Bar -| | |--docs -| | |--examples -| | |--src -| | |- Bar.c -| | |- Bar.h -| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html -| | -| |--Foo -| | |- Foo.c -| | |- Foo.h -| | -| |- README --> THIS FILE -| -|- platformio.ini -|--src - |- main.c - -Example contents of `src/main.c` using Foo and Bar: -``` -#include -#include - -int main (void) -{ - ... -} - -``` - -The PlatformIO Library Dependency Finder will find automatically dependent -libraries by scanning project source files. - -More information about PlatformIO Library Dependency Finder -- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/client/platformio.ini b/client/platformio.ini index bf4a1dd..0932e7d 100644 --- a/client/platformio.ini +++ b/client/platformio.ini @@ -8,23 +8,29 @@ ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html -[env:esp32-c3-devkitm-1] +[env:client] platform = espressif32 -board = esp32-c3-devkitm-1 +board = esp32dev board_build.mcu = esp32c3 +board_build.f_cpu = 160000000L framework = arduino +extra_scripts = pre:extra_script.py +build_unflags = -std=gnu++11 ; remove this default from compile flags build_flags = + -std=c++2a + -I"${platformio.packages_dir}/framework-arduinoespressif32/libraries/WiFi/src" ; doesnt work for compile_commands.json ootb -DARDUINO_USB_CDC_ON_BOOT=1 -DARDUINO_USB_MODE=1 -DARDUINO_TINYUSB=1 -DCORE_DEBUG_LEVEL=5 ; set the debug level (0-5, with 5 being the most verbose) lib_deps = + WiFi monitor_filters = esp32_exception_decoder time ; add timestamp with milliseconds for each new line -upload_port = /dev/ttyACM1 -monitor_port = /dev/ttyACM1 +upload_port = /dev/ttyACM0 +monitor_port = /dev/ttyACM0 diff --git a/client/src/connect.cpp b/client/src/connect.cpp new file mode 100644 index 0000000..c4ae021 --- /dev/null +++ b/client/src/connect.cpp @@ -0,0 +1,68 @@ +#include + +#include +#include +#include + +#include "connect.h" +#include "esp32-hal.h" +#include "secrets.h" + +uint64_t start; +uint64_t is_connecting = 0; +IPAddress host; + +void timerStart() +{ + start = millis(); +} + +uint64_t timerElapsed() +{ + auto now = millis(); + uint64_t elapsed = now - start; + return elapsed; +} + +// ----------------------------------------- + +void wifiSetup() +{ + WiFi.mode(WIFI_STA); + WiFi.setAutoReconnect(false); + WiFi.disconnect(); + delay(100); +} + +void wifiConnect() +{ + if (WiFi.status() == WL_CONNECTED) { + if (is_connecting > 0) { + host = WiFi.gatewayIP(); + Serial.println("Connected to AP!"); + } + is_connecting = 0; + return; + } + + if (is_connecting == 0 || timerElapsed() > is_connecting * 1000) { + if (is_connecting == 0) { + is_connecting = 10; // start with a 10 second delay + } + else { + is_connecting *= 2; // exponential backoff + } + + timerStart(); + Serial.println("Connecting"); + WiFi.begin(SSID, PASSWORD); + } +} + +IPAddress wifiHost() +{ + return host; +} + +// https://docs.espressif.com/projects/arduino-esp32/en/latest/api/wifi.html +// https://docs.arduino.cc/libraries/wifi/ (old) diff --git a/client/src/connect.h b/client/src/connect.h new file mode 100644 index 0000000..1e81a54 --- /dev/null +++ b/client/src/connect.h @@ -0,0 +1,5 @@ +#include "IPAddress.h" + +void wifiSetup(); +void wifiConnect(); +IPAddress wifiHost(); diff --git a/client/src/main.cpp b/client/src/main.cpp index 85ebc06..e9d4669 100644 --- a/client/src/main.cpp +++ b/client/src/main.cpp @@ -1,16 +1,14 @@ #include -#include #include -#include "secrets.h" +#include "connect.h" -// Default IP address = 192.168.4.1 - -#define HOST "192.168.4.1" #define PORT 1234 -#define POWER_BUTTON_PIN 2 -#define RESET_BUTTON_PIN 3 +#define POWER_BUTTON_PIN 10 +#define RESET_BUTTON_PIN 2 +#define POWER_LED_PIN 3 +#define RESET_LED_PIN 1 WiFiClient client; @@ -27,25 +25,23 @@ void setup() pinMode(POWER_BUTTON_PIN, INPUT_PULLUP); pinMode(RESET_BUTTON_PIN, INPUT_PULLUP); - // Wait for a USB connection to be established - while (!Serial) - (void)0; - delay(1000); - Serial.println("Client booted!"); + pinMode(POWER_LED_PIN, OUTPUT); + pinMode(RESET_LED_PIN, OUTPUT); - // Connect to button server - WiFi.begin(SSID, PASSWORD); - Serial.print("Connecting"); - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } - Serial.println("\nConnected to AP"); + wifiSetup(); + + delay(3000); + Serial.println("Client booted!"); } void loop() { - delay(20); + delay(20); // used for button debounce + + wifiConnect(); + + digitalWrite(POWER_LED_PIN, LOW); + digitalWrite(RESET_LED_PIN, LOW); int powerButtonState = digitalRead(POWER_BUTTON_PIN); int resetButtonState = digitalRead(RESET_BUTTON_PIN); @@ -66,11 +62,13 @@ void loop() previousResetButtonState = resetButtonState; if (isButtonPressed) { + digitalWrite(RESET_LED_PIN, HIGH); + String button = (powerButtonState == LOW) ? "power" : "reset"; Serial.println("Pressed " + button + " button!"); - if (!client.connected() && !client.connect(HOST, PORT)) { + if (!client.connected() && !client.connect(wifiHost(), PORT)) { Serial.println("Connection failed"); return; } @@ -106,6 +104,7 @@ void ack() // Wait for acknowledgment from the receiver String response = client.readStringUntil('\n'); if (response == "ACK") { + digitalWrite(POWER_LED_PIN, HIGH); Serial.println("Received ACK"); return; } diff --git a/client/test/README b/client/test/README deleted file mode 100644 index 9b1e87b..0000000 --- a/client/test/README +++ /dev/null @@ -1,11 +0,0 @@ - -This directory is intended for PlatformIO Test Runner and project tests. - -Unit Testing is a software testing method by which individual units of -source code, sets of one or more MCU program modules together with associated -control data, usage procedures, and operating procedures, are tested to -determine whether they are fit for use. Unit testing finds problems early -in the development cycle. - -More information about PlatformIO Unit Testing: -- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html diff --git a/makefile b/makefile index 66482e3..ca8ed87 100644 --- a/makefile +++ b/makefile @@ -2,32 +2,34 @@ server: @-cd server ; \ - platformio run --target upload ; \ - cd .. + platformio run --environment server --target compiledb ; \ + platformio run --environment server ; \ + cd .. server-log: @-cd server ; \ - platformio device monitor ; \ - cd .. + platformio device monitor --environment server ; \ + cd .. -server-run: +server-run: server @-cd server ; \ - platformio run --target upload && \ - platformio device monitor ; \ - cd .. + platformio run --environment server --target upload ; \ + platformio device monitor --environment server ; \ + cd .. client: @-cd client ; \ - platformio run --target upload ; \ - cd .. + platformio run --environment client --target compiledb ; \ + platformio run --environment client ; + cd .. client-log: @-cd client ; \ - platformio device monitor ; \ - cd .. + platformio device monitor --environment client + cd .. -client-run: +client-run: client @-cd client ; \ - platformio run --target upload && \ - platformio device monitor ; \ - cd .. + platformio run --environment client --target upload ; \ + platformio device monitor --environment client + cd .. diff --git a/server/compile_commands.json b/server/compile_commands.json new file mode 120000 index 0000000..ea14cd1 --- /dev/null +++ b/server/compile_commands.json @@ -0,0 +1 @@ +.pio/build/server/compile_commands.json \ No newline at end of file diff --git a/server/extra_script.py b/server/extra_script.py new file mode 100644 index 0000000..a0afaf0 --- /dev/null +++ b/server/extra_script.py @@ -0,0 +1,10 @@ +import os +Import("env") + +# include toolchain paths +env.Replace(COMPILATIONDB_INCLUDE_TOOLCHAIN=True) + +# override compilation DB path +env.Replace(COMPILATIONDB_PATH=os.path.join("$BUILD_DIR", "compile_commands.json")) + +env.AddPostAction("buildprog", lambda *_, **__: env.Execute("pio run -t compiledb")) diff --git a/server/include/README b/server/include/README deleted file mode 100644 index 49819c0..0000000 --- a/server/include/README +++ /dev/null @@ -1,37 +0,0 @@ - -This directory is intended for project header files. - -A header file is a file containing C declarations and macro definitions -to be shared between several project source files. You request the use of a -header file in your project source file (C, C++, etc) located in `src` folder -by including it, with the C preprocessing directive `#include'. - -```src/main.c - -#include "header.h" - -int main (void) -{ - ... -} -``` - -Including a header file produces the same results as copying the header file -into each source file that needs it. Such copying would be time-consuming -and error-prone. With a header file, the related declarations appear -in only one place. If they need to be changed, they can be changed in one -place, and programs that include the header file will automatically use the -new version when next recompiled. The header file eliminates the labor of -finding and changing all the copies as well as the risk that a failure to -find one copy will result in inconsistencies within a program. - -In C, the convention is to give header files names that end with `.h'. - -Read more about using header files in official GCC documentation: - -* Include Syntax -* Include Operation -* Once-Only Headers -* Computed Includes - -https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/server/lib/README b/server/lib/README deleted file mode 100644 index 9379397..0000000 --- a/server/lib/README +++ /dev/null @@ -1,46 +0,0 @@ - -This directory is intended for project specific (private) libraries. -PlatformIO will compile them to static libraries and link into the executable file. - -The source code of each library should be placed in a separate directory -("lib/your_library_name/[Code]"). - -For example, see the structure of the following example libraries `Foo` and `Bar`: - -|--lib -| | -| |--Bar -| | |--docs -| | |--examples -| | |--src -| | |- Bar.c -| | |- Bar.h -| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html -| | -| |--Foo -| | |- Foo.c -| | |- Foo.h -| | -| |- README --> THIS FILE -| -|- platformio.ini -|--src - |- main.c - -Example contents of `src/main.c` using Foo and Bar: -``` -#include -#include - -int main (void) -{ - ... -} - -``` - -The PlatformIO Library Dependency Finder will find automatically dependent -libraries by scanning project source files. - -More information about PlatformIO Library Dependency Finder -- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/server/platformio.ini b/server/platformio.ini index 26c5dcb..5ddad4f 100644 --- a/server/platformio.ini +++ b/server/platformio.ini @@ -8,19 +8,25 @@ ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html -[env:esp32-c3-devkitm-1] +[env:server] platform = espressif32 -board = esp32-c3-devkitm-1 +board = esp32dev board_build.mcu = esp32c3 +board_build.f_cpu = 160000000L framework = arduino +extra_scripts = pre:extra_script.py +build_unflags = -std=gnu++11 ; remove this default from compile flags build_flags = + -std=c++2a + -I"${platformio.packages_dir}/framework-arduinoespressif32/libraries/WiFi/src" ; doesnt work for compile_commands.json ootb -DARDUINO_USB_CDC_ON_BOOT=1 -DARDUINO_USB_MODE=1 -DARDUINO_TINYUSB=1 -DCORE_DEBUG_LEVEL=5 ; set the debug level (0-5, with 5 being the most verbose) lib_deps = + WiFi monitor_filters = esp32_exception_decoder diff --git a/server/src/main.cpp b/server/src/main.cpp index d166ce4..33581ce 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -5,17 +5,23 @@ #include "secrets.h" -// Default IP address = 192.168.4.1 +// Default gateway = 192.168.4.1 +// Default IP address = 192.168.4.2 #define PORT 1234 #define CHANNEL 11 #define HIDDEN true #define MAX_CONNECTION 1 -#define POWER_PIN 2 -#define RESET_PIN 3 +#define POWER_BUTTON_PIN 10 +#define RESET_BUTTON_PIN 2 +#define POWER_LED_PIN 3 +#define RESET_LED_PIN 1 -WiFiServer server(PORT); +#define POWER_GATE_PIN 6 +#define RESET_GATE_PIN 7 + +WiFiServer server(PORT, MAX_CONNECTION); bool powerPressed = false; bool resetPressed = false; @@ -29,12 +35,11 @@ void setup() Serial.begin(9600); Serial.setDebugOutput(true); - pinMode(POWER_PIN, OUTPUT); - pinMode(RESET_PIN, OUTPUT); + pinMode(POWER_GATE_PIN, OUTPUT); + pinMode(POWER_LED_PIN, OUTPUT); + pinMode(RESET_GATE_PIN, OUTPUT); + pinMode(RESET_LED_PIN, OUTPUT); - // Wait for a USB connection to be established - while (!Serial) - (void)0; delay(3000); Serial.println("Server booted!"); @@ -110,11 +115,16 @@ void processButtons() void setPowerPin(bool enable) { powerPressed = enable; - digitalWrite(POWER_PIN, (enable) ? HIGH : LOW); + // digitalWrite(POWER_GATE_PIN, (enable) ? HIGH : LOW); + digitalWrite(POWER_LED_PIN, (enable) ? HIGH : LOW); } void setResetPin(bool enable) { resetPressed = enable; - digitalWrite(RESET_PIN, (enable) ? HIGH : LOW); + // digitalWrite(RESET_GATE_PIN, (enable) ? HIGH : LOW); + digitalWrite(RESET_LED_PIN, (enable) ? HIGH : LOW); } + +// connect + to drain +// connect - to source diff --git a/server/test/README b/server/test/README deleted file mode 100644 index 9b1e87b..0000000 --- a/server/test/README +++ /dev/null @@ -1,11 +0,0 @@ - -This directory is intended for PlatformIO Test Runner and project tests. - -Unit Testing is a software testing method by which individual units of -source code, sets of one or more MCU program modules together with associated -control data, usage procedures, and operating procedures, are tested to -determine whether they are fit for use. Unit testing finds problems early -in the development cycle. - -More information about PlatformIO Unit Testing: -- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html