Update code to work on fabricated PCB

This commit is contained in:
Riyyi
2025-08-30 17:22:33 +02:00
parent 830a4e2137
commit 8f4823586f
18 changed files with 175 additions and 243 deletions
+2
View File
@@ -0,0 +1,2 @@
CompileFlags:
Remove: [-fstrict-volatile-bitfields, -fno-tree-switch-conversion]
+1
View File
@@ -0,0 +1 @@
.pio/build/client/compile_commands.json
+10
View File
@@ -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"))
-37
View File
@@ -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
-46
View File
@@ -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 <Foo.h>
#include <Bar.h>
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
+10 -4
View File
@@ -8,23 +8,29 @@
; Please visit documentation for the other options and examples ; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html ; https://docs.platformio.org/page/projectconf.html
[env:esp32-c3-devkitm-1] [env:client]
platform = espressif32 platform = espressif32
board = esp32-c3-devkitm-1 board = esp32dev
board_build.mcu = esp32c3 board_build.mcu = esp32c3
board_build.f_cpu = 160000000L
framework = arduino framework = arduino
extra_scripts = pre:extra_script.py
build_unflags = -std=gnu++11 ; remove this default from compile flags
build_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_CDC_ON_BOOT=1
-DARDUINO_USB_MODE=1 -DARDUINO_USB_MODE=1
-DARDUINO_TINYUSB=1 -DARDUINO_TINYUSB=1
-DCORE_DEBUG_LEVEL=5 ; set the debug level (0-5, with 5 being the most verbose) -DCORE_DEBUG_LEVEL=5 ; set the debug level (0-5, with 5 being the most verbose)
lib_deps = lib_deps =
WiFi
monitor_filters = monitor_filters =
esp32_exception_decoder esp32_exception_decoder
time ; add timestamp with milliseconds for each new line time ; add timestamp with milliseconds for each new line
upload_port = /dev/ttyACM1 upload_port = /dev/ttyACM0
monitor_port = /dev/ttyACM1 monitor_port = /dev/ttyACM0
+68
View File
@@ -0,0 +1,68 @@
#include <cstdint>
#include <Arduino.h>
#include <IPAddress.h>
#include <WiFi.h>
#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)
+5
View File
@@ -0,0 +1,5 @@
#include "IPAddress.h"
void wifiSetup();
void wifiConnect();
IPAddress wifiHost();
+21 -22
View File
@@ -1,16 +1,14 @@
#include <Arduino.h> #include <Arduino.h>
#include <WiFi.h>
#include <WiFiClient.h> #include <WiFiClient.h>
#include "secrets.h" #include "connect.h"
// Default IP address = 192.168.4.1
#define HOST "192.168.4.1"
#define PORT 1234 #define PORT 1234
#define POWER_BUTTON_PIN 2 #define POWER_BUTTON_PIN 10
#define RESET_BUTTON_PIN 3 #define RESET_BUTTON_PIN 2
#define POWER_LED_PIN 3
#define RESET_LED_PIN 1
WiFiClient client; WiFiClient client;
@@ -27,25 +25,23 @@ void setup()
pinMode(POWER_BUTTON_PIN, INPUT_PULLUP); pinMode(POWER_BUTTON_PIN, INPUT_PULLUP);
pinMode(RESET_BUTTON_PIN, INPUT_PULLUP); pinMode(RESET_BUTTON_PIN, INPUT_PULLUP);
// Wait for a USB connection to be established pinMode(POWER_LED_PIN, OUTPUT);
while (!Serial) pinMode(RESET_LED_PIN, OUTPUT);
(void)0;
delay(1000);
Serial.println("Client booted!");
// Connect to button server wifiSetup();
WiFi.begin(SSID, PASSWORD);
Serial.print("Connecting"); delay(3000);
while (WiFi.status() != WL_CONNECTED) { Serial.println("Client booted!");
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to AP");
} }
void loop() 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 powerButtonState = digitalRead(POWER_BUTTON_PIN);
int resetButtonState = digitalRead(RESET_BUTTON_PIN); int resetButtonState = digitalRead(RESET_BUTTON_PIN);
@@ -66,11 +62,13 @@ void loop()
previousResetButtonState = resetButtonState; previousResetButtonState = resetButtonState;
if (isButtonPressed) { if (isButtonPressed) {
digitalWrite(RESET_LED_PIN, HIGH);
String button = (powerButtonState == LOW) ? "power" : "reset"; String button = (powerButtonState == LOW) ? "power" : "reset";
Serial.println("Pressed " + button + " button!"); Serial.println("Pressed " + button + " button!");
if (!client.connected() && !client.connect(HOST, PORT)) { if (!client.connected() && !client.connect(wifiHost(), PORT)) {
Serial.println("Connection failed"); Serial.println("Connection failed");
return; return;
} }
@@ -106,6 +104,7 @@ void ack()
// Wait for acknowledgment from the receiver // Wait for acknowledgment from the receiver
String response = client.readStringUntil('\n'); String response = client.readStringUntil('\n');
if (response == "ACK") { if (response == "ACK") {
digitalWrite(POWER_LED_PIN, HIGH);
Serial.println("Received ACK"); Serial.println("Received ACK");
return; return;
} }
-11
View File
@@ -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
+12 -10
View File
@@ -2,32 +2,34 @@
server: server:
@-cd server ; \ @-cd server ; \
platformio run --target upload ; \ platformio run --environment server --target compiledb ; \
platformio run --environment server ; \
cd .. cd ..
server-log: server-log:
@-cd server ; \ @-cd server ; \
platformio device monitor ; \ platformio device monitor --environment server ; \
cd .. cd ..
server-run: server-run: server
@-cd server ; \ @-cd server ; \
platformio run --target upload && \ platformio run --environment server --target upload ; \
platformio device monitor ; \ platformio device monitor --environment server ; \
cd .. cd ..
client: client:
@-cd client ; \ @-cd client ; \
platformio run --target upload ; \ platformio run --environment client --target compiledb ; \
platformio run --environment client ;
cd .. cd ..
client-log: client-log:
@-cd client ; \ @-cd client ; \
platformio device monitor ; \ platformio device monitor --environment client
cd .. cd ..
client-run: client-run: client
@-cd client ; \ @-cd client ; \
platformio run --target upload && \ platformio run --environment client --target upload ; \
platformio device monitor ; \ platformio device monitor --environment client
cd .. cd ..
+1
View File
@@ -0,0 +1 @@
.pio/build/server/compile_commands.json
+10
View File
@@ -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"))
-37
View File
@@ -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
-46
View File
@@ -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 <Foo.h>
#include <Bar.h>
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
+8 -2
View File
@@ -8,19 +8,25 @@
; Please visit documentation for the other options and examples ; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html ; https://docs.platformio.org/page/projectconf.html
[env:esp32-c3-devkitm-1] [env:server]
platform = espressif32 platform = espressif32
board = esp32-c3-devkitm-1 board = esp32dev
board_build.mcu = esp32c3 board_build.mcu = esp32c3
board_build.f_cpu = 160000000L
framework = arduino framework = arduino
extra_scripts = pre:extra_script.py
build_unflags = -std=gnu++11 ; remove this default from compile flags
build_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_CDC_ON_BOOT=1
-DARDUINO_USB_MODE=1 -DARDUINO_USB_MODE=1
-DARDUINO_TINYUSB=1 -DARDUINO_TINYUSB=1
-DCORE_DEBUG_LEVEL=5 ; set the debug level (0-5, with 5 being the most verbose) -DCORE_DEBUG_LEVEL=5 ; set the debug level (0-5, with 5 being the most verbose)
lib_deps = lib_deps =
WiFi
monitor_filters = monitor_filters =
esp32_exception_decoder esp32_exception_decoder
+21 -11
View File
@@ -5,17 +5,23 @@
#include "secrets.h" #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 PORT 1234
#define CHANNEL 11 #define CHANNEL 11
#define HIDDEN true #define HIDDEN true
#define MAX_CONNECTION 1 #define MAX_CONNECTION 1
#define POWER_PIN 2 #define POWER_BUTTON_PIN 10
#define RESET_PIN 3 #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 powerPressed = false;
bool resetPressed = false; bool resetPressed = false;
@@ -29,12 +35,11 @@ void setup()
Serial.begin(9600); Serial.begin(9600);
Serial.setDebugOutput(true); Serial.setDebugOutput(true);
pinMode(POWER_PIN, OUTPUT); pinMode(POWER_GATE_PIN, OUTPUT);
pinMode(RESET_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); delay(3000);
Serial.println("Server booted!"); Serial.println("Server booted!");
@@ -110,11 +115,16 @@ void processButtons()
void setPowerPin(bool enable) void setPowerPin(bool enable)
{ {
powerPressed = 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) void setResetPin(bool enable)
{ {
resetPressed = 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
-11
View File
@@ -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