Browse Source

Update code to work on fabricated PCB

master
Riyyi 2 weeks ago
parent
commit
8f4823586f
  1. 2
      .clangd
  2. 1
      client/compile_commands.json
  3. 10
      client/extra_script.py
  4. 37
      client/include/README
  5. 46
      client/lib/README
  6. 14
      client/platformio.ini
  7. 68
      client/src/connect.cpp
  8. 5
      client/src/connect.h
  9. 43
      client/src/main.cpp
  10. 11
      client/test/README
  11. 22
      makefile
  12. 1
      server/compile_commands.json
  13. 10
      server/extra_script.py
  14. 37
      server/include/README
  15. 46
      server/lib/README
  16. 10
      server/platformio.ini
  17. 32
      server/src/main.cpp
  18. 11
      server/test/README

2
.clangd

@ -0,0 +1,2 @@
CompileFlags:
Remove: [-fstrict-volatile-bitfields, -fno-tree-switch-conversion]

1
client/compile_commands.json

@ -0,0 +1 @@
.pio/build/client/compile_commands.json

10
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"))

37
client/include/README

@ -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
client/lib/README

@ -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

14
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

68
client/src/connect.cpp

@ -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
client/src/connect.h

@ -0,0 +1,5 @@
#include "IPAddress.h"
void wifiSetup();
void wifiConnect();
IPAddress wifiHost();

43
client/src/main.cpp

@ -1,16 +1,14 @@
#include <Arduino.h>
#include <WiFi.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 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;
}

11
client/test/README

@ -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

22
makefile

@ -2,32 +2,34 @@
server:
@-cd server ; \
platformio run --target upload ; \
platformio run --environment server --target compiledb ; \
platformio run --environment server ; \
cd ..
server-log:
@-cd server ; \
platformio device monitor ; \
platformio device monitor --environment server ; \
cd ..
server-run:
server-run: server
@-cd server ; \
platformio run --target upload && \
platformio device monitor ; \
platformio run --environment server --target upload ; \
platformio device monitor --environment server ; \
cd ..
client:
@-cd client ; \
platformio run --target upload ; \
platformio run --environment client --target compiledb ; \
platformio run --environment client ;
cd ..
client-log:
@-cd client ; \
platformio device monitor ; \
platformio device monitor --environment client
cd ..
client-run:
client-run: client
@-cd client ; \
platformio run --target upload && \
platformio device monitor ; \
platformio run --environment client --target upload ; \
platformio device monitor --environment client
cd ..

1
server/compile_commands.json

@ -0,0 +1 @@
.pio/build/server/compile_commands.json

10
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"))

37
server/include/README

@ -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
server/lib/README

@ -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
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

32
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

11
server/test/README

@ -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
Loading…
Cancel
Save