Skip to content

Commit 9b3773f

Browse files
authored
Merge pull request #1 from Vuurvos1/feat/moduleCode
Feat/module code
2 parents 5221520 + 003cea6 commit 9b3773f

File tree

22 files changed

+452
-9
lines changed

22 files changed

+452
-9
lines changed

.github/workflows/test-shared.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Test Shared
2+
3+
on:
4+
push:
5+
branches: [main]
6+
paths:
7+
- 'code/shared/**'
8+
pull_request:
9+
branches: [main]
10+
paths:
11+
- 'code/shared/**'
12+
13+
jobs:
14+
test:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- uses: actions/cache@v4
20+
with:
21+
path: |
22+
~/.cache/pip
23+
~/.platformio/.cache
24+
key: ${{ runner.os }}-pio
25+
26+
- uses: actions/setup-python@v5
27+
with:
28+
python-version: '3.11'
29+
30+
- name: Install PlatformIO Core
31+
run: pip install --upgrade platformio
32+
33+
- name: Run Shared Tests
34+
run: |
35+
cd code
36+
pio test -e shared_test

code/.gitignore

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.pio
2-
.vscode/.browse.c_cpp.db*
3-
.vscode/c_cpp_properties.json
4-
.vscode/launch.json
5-
.vscode/ipch
2+
**/.vscode/.browse.c_cpp.db*
3+
**/.vscode/c_cpp_properties.json
4+
**/.vscode/launch.json
5+
**/.vscode/ipch
66

77
config.h
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
// See http://go.microsoft.com/fwlink/?LinkId=827846
3+
// for the documentation about the extensions.json format
4+
"recommendations": [
5+
"platformio.platformio-ide"
6+
],
7+
"unwantedRecommendations": [
8+
"ms-vscode.cpptools-extension-pack"
9+
]
10+
}
File renamed without changes.
File renamed without changes.

code/controller/platformio.ini

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[env:controller]
2+
platform = espressif32
3+
board = esp32dev
4+
framework = arduino
5+
monitor_speed = 115200
6+
lib_deps =
7+
; TODO: remove one stepper lib
8+
gin66/FastAccelStepper@^0.28.4
9+
waspinator/AccelStepper@^1.64
10+
lib_extra_dirs = ../shared
11+
12+
[env:native]
13+
platform = native
14+
build_flags = -I../shared
15+
lib_deps =
16+
gin66/FastAccelStepper@^0.28.4
17+
waspinator/AccelStepper@^1.64
18+
lib_extra_dirs = ../shared

code/src/main.cpp renamed to code/controller/src/main.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
#include <Arduino.h>
22
#include <config.h>
3+
#include <HardwareSerial.h>
34

45
#include <WiFi.h>
56
#include <WebServer.h>
67
#include <ESPmDNS.h>
78

89
#include <SplitFlap.h>
10+
#include <utils.h>
11+
12+
#define TXD1 19
13+
#define RXD1 21
14+
15+
// Use Serial1 for UART communication
16+
HardwareSerial mySerial(1);
917

1018
WebServer server(80);
1119

@@ -54,6 +62,9 @@ void handleFlaps()
5462
void setup()
5563
{
5664
Serial.begin(115200);
65+
mySerial.begin(9600, SERIAL_8N1, RXD1, TXD1); // UART setup
66+
67+
Serial.println("ESP32 UART Transmitter");
5768

5869
while (!Serial)
5970
;
@@ -96,4 +107,32 @@ void loop()
96107
server.handleClient();
97108

98109
splitFlap.update();
110+
111+
// send a message over serial every 2 seconds
112+
static unsigned long lastSend = 0;
113+
if (millis() - lastSend > 2000)
114+
{
115+
// data
116+
std::vector<uint8_t> data = {
117+
static_cast<uint8_t>(random(65)),
118+
static_cast<uint8_t>(random(65)),
119+
static_cast<uint8_t>(random(65)),
120+
static_cast<uint8_t>(random(65)),
121+
static_cast<uint8_t>(random(65))};
122+
123+
auto packet = createPacket(data);
124+
125+
// print the packet
126+
Serial.print("Packet: ");
127+
for (uint8_t byte : packet)
128+
{
129+
Serial.print(byte);
130+
Serial.print(" ");
131+
}
132+
Serial.println();
133+
134+
mySerial.write(packet.data(), packet.size());
135+
136+
lastSend = millis();
137+
}
99138
}
File renamed without changes.

code/module/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.pio
2+
.vscode/.browse.c_cpp.db*
3+
.vscode/c_cpp_properties.json
4+
.vscode/launch.json
5+
.vscode/ipch

code/module/.vscode/extensions.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
// See http://go.microsoft.com/fwlink/?LinkId=827846
3+
// for the documentation about the extensions.json format
4+
"recommendations": [
5+
"platformio.platformio-ide"
6+
],
7+
"unwantedRecommendations": [
8+
"ms-vscode.cpptools-extension-pack"
9+
]
10+
}

code/module/include/README

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the convention is to give header files names that end with `.h'.
29+
30+
Read more about using header files in official GCC documentation:
31+
32+
* Include Syntax
33+
* Include Operation
34+
* Once-Only Headers
35+
* Computed Includes
36+
37+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

code/module/lib/README

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
This directory is intended for project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link into the executable file.
4+
5+
The source code of each library should be placed in a separate directory
6+
("lib/your_library_name/[Code]").
7+
8+
For example, see the structure of the following example libraries `Foo` and `Bar`:
9+
10+
|--lib
11+
| |
12+
| |--Bar
13+
| | |--docs
14+
| | |--examples
15+
| | |--src
16+
| | |- Bar.c
17+
| | |- Bar.h
18+
| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
19+
| |
20+
| |--Foo
21+
| | |- Foo.c
22+
| | |- Foo.h
23+
| |
24+
| |- README --> THIS FILE
25+
|
26+
|- platformio.ini
27+
|--src
28+
|- main.c
29+
30+
Example contents of `src/main.c` using Foo and Bar:
31+
```
32+
#include <Foo.h>
33+
#include <Bar.h>
34+
35+
int main (void)
36+
{
37+
...
38+
}
39+
40+
```
41+
42+
The PlatformIO Library Dependency Finder will find automatically dependent
43+
libraries by scanning project source files.
44+
45+
More information about PlatformIO Library Dependency Finder
46+
- https://docs.platformio.org/page/librarymanager/ldf.html

code/module/platformio.ini

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[env:module]
12+
platform = espressif32
13+
board = esp32dev
14+
framework = arduino
15+
monitor_speed = 115200
16+
lib_deps =
17+
; TODO: remove one stepper lib
18+
gin66/FastAccelStepper@^0.28.4
19+
waspinator/AccelStepper@^1.64
20+
lib_extra_dirs = ../shared

code/module/src/main.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <Arduino.h>
2+
#include "utils.h"
3+
#include <vector>
4+
#include "SplitFlap.h"
5+
6+
#define TXD1 19
7+
#define RXD1 21
8+
9+
// defines pins
10+
#define STEP_PIN 13
11+
#define DIR_PIN 12
12+
13+
SplitFlap splitFlap(STEP_PIN, DIR_PIN);
14+
15+
// Use Serial1 for UART communication
16+
HardwareSerial mySerial(2);
17+
18+
SplitFlap splitFlap(13, 12);
19+
20+
bool readingPacket = false;
21+
22+
void handleMyByte(uint8_t byte)
23+
{
24+
Serial.println("Received byte: " + String(byte));
25+
splitFlap.setFlap(byte);
26+
}
27+
28+
void setup()
29+
{
30+
Serial.begin(115200);
31+
mySerial.begin(9600, SERIAL_8N1, RXD1, TXD1); // UART setup
32+
33+
splitFlap.init();
34+
35+
Serial.println("Module started");
36+
}
37+
38+
void loop()
39+
{
40+
splitFlap.update();
41+
42+
// Check if data is available to read
43+
if (mySerial.available())
44+
{
45+
uint8_t incoming = mySerial.read();
46+
47+
if (!readingPacket && incoming != END_BYTE)
48+
{
49+
readingPacket = true;
50+
handleMyByte(incoming);
51+
}
52+
else
53+
{
54+
// forward the data
55+
Serial.print(String(incoming) + " ");
56+
mySerial.write(incoming);
57+
}
58+
59+
if (incoming == END_BYTE)
60+
{
61+
Serial.println("End of packet");
62+
readingPacket = false;
63+
}
64+
}
65+
}

code/module/test/README

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
This directory is intended for PlatformIO Test Runner and project tests.
3+
4+
Unit Testing is a software testing method by which individual units of
5+
source code, sets of one or more MCU program modules together with associated
6+
control data, usage procedures, and operating procedures, are tested to
7+
determine whether they are fit for use. Unit testing finds problems early
8+
in the development cycle.
9+
10+
More information about PlatformIO Unit Testing:
11+
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html

code/platformio.ini

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,25 @@
88
; Please visit documentation for the other options and examples
99
; https://docs.platformio.org/page/projectconf.html
1010

11-
[env:esp32dev]
11+
; [env:esp32dev]
12+
[env:module]
1213
platform = espressif32
1314
board = esp32dev
1415
framework = arduino
16+
build_src_filter = module/src shared/src
17+
include_filter = module/include shared/include
1518
monitor_speed = 115200
16-
lib_deps =
17-
gin66/FastAccelStepper@^0.28.4
18-
waspinator/AccelStepper@^1.64
19+
20+
[env:controller]
21+
platform = espressif32
22+
board = esp32dev
23+
framework = arduino
24+
build_src_filter = controller/src shared/src
25+
include_filter = controller/include shared/include
26+
monitor_speed = 115200
27+
28+
[env:shared_test]
29+
platform = native
30+
build_type = debug
31+
build_src_filter = shared/src
32+
test_filter = shared/test/*

code/src/SplitFlap.h renamed to code/shared/include/SplitFlap.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ class SplitFlap
3030
pinMode(dirPin, OUTPUT);
3131

3232
digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction
33+
34+
this->home();
35+
}
36+
37+
void home()
38+
{
39+
// TODO: implement homing sequence
3340
}
3441

3542
void update()

0 commit comments

Comments
 (0)