Skip to content

Commit 4a60ba5

Browse files
Add VS Code Wokwi files and Wokwi.com projects (#214)
* feat: Add Wokwi files * ci: Simulate projects * feat: Update default SSID * ci: Add wokwi.com checks * ci: Update conditions * ci: Update name * ci: Fix image * ci: Remove source commadn * ci: Update path * build: Update deps and rust components * chore: Update SSID for Wokwi simulation * chore: Debug wokwi.toml * ci: Update conditions * feat: Add wokwi vscode extension * feat: Update Wokwi files * docs: Add simulation documentation * ci: Update path * feat: Add button pressed message
1 parent b1704e6 commit 4a60ba5

27 files changed

+453
-13
lines changed

.devcontainer/devcontainer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"vadimcn.vscode-lldb",
3232
"serayuzgur.crates",
3333
"mutantdino.resourcemonitor",
34-
"yzhang.markdown-all-in-one"
34+
"yzhang.markdown-all-in-one",
35+
"Wokwi.wokwi-vscode"
3536
]
3637
}
3738
},

.devcontainer/test.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
set -ef
44

5-
WORK_DIR=/home/esp/workspace/$1
6-
75
echo "Compiling $1"
86

97
cd /home/esp/workspace/$1
@@ -12,8 +10,8 @@ if [ -f cfg.toml.example ]; then
1210
# Rename file to cfg.toml
1311
cp cfg.toml.example cfg.toml
1412
# Replace defaults
15-
sed -i 's/wifi_ssid = "FBI Surveillance Van"/wifi_ssid = "ssid"/g' cfg.toml
16-
sed -i 's/wifi_psk = "hunter2"/wifi_psk = "pass"/g' cfg.toml
13+
sed -i 's/wifi_ssid = "FBI Surveillance Van"/wifi_ssid = "Wokwi-GUEST"/g' cfg.toml
14+
sed -i 's/wifi_psk = "hunter2"/wifi_psk = ""/g' cfg.toml
1715
sed -i 's/mqtt_user = "horse"/mqtt_user = "user"/g' cfg.toml
1816
sed -i 's/mqtt_pass = "CorrectHorseBatteryStaple"/mqtt_pass = "pass"/g' cfg.toml
1917
sed -i 's/mqtt_host = "yourpc.local"/mqtt_host = "host"/g' cfg.toml
@@ -26,6 +24,8 @@ $HOME/.cargo/bin/cargo build
2624
if [[ "$1" == advanced/button-interrupt ]]; then
2725
$HOME/.cargo/bin/cargo build --example solution
2826
$HOME/.cargo/bin/cargo build --example solution_led
27+
# Simulate with Wokwi
28+
sed -i 's/^[[:space:]]*firmware[[:space:]]*=[[:space:]]*["'"'"']\([^"'"'"']*\)["'"'"']\([[:space:]]*\)$/\nfirmware = "target\/riscv32imc-esp-espidf\/debug\/examples\/solution"/' wokwi.toml
2929
fi
3030

3131
if [[ "$1" == advanced/i2c-sensor-reading ]]; then
@@ -36,6 +36,8 @@ fi
3636
if [[ "$1" == intro/http-client ]]; then
3737
$HOME/.cargo/bin/cargo build --example http_client
3838
$HOME/.cargo/bin/cargo build --example https_client
39+
# Simulate with Wokwi
40+
sed -i 's/^[[:space:]]*firmware[[:space:]]*=[[:space:]]*["'"'"']\([^"'"'"']*\)["'"'"']\([[:space:]]*\)$/\nfirmware = "target\/riscv32imc-esp-espidf\/debug\/examples\/http_client"/' wokwi.toml
3941
fi
4042

4143
if [[ "$1" == intro/http-server ]]; then

.github/button-interrupt.test.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: button-interrupt test
2+
version: 1
3+
author: Sergio Gasquez Arcos
4+
5+
steps:
6+
- wait-serial: "GPIO[9]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:1"
7+
# Press once
8+
- set-control:
9+
part-id: btn1
10+
control: pressed
11+
value: 1
12+
- delay: 100ms
13+
- set-control:
14+
part-id: btn1
15+
control: pressed
16+
value: 0
17+
- wait-serial: "Button pressed!"

.github/hardware-check.test.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: hardware-check test
2+
version: 1
3+
author: Sergio Gasquez Arcos
4+
5+
steps:
6+
- delay: 5000ms
7+
- wait-serial: "Hello, world!"

.github/http-client.test.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: http-client test
2+
version: 1
3+
author: Sergio Gasquez Arcos
4+
5+
steps:
6+
- delay: 5000ms
7+
- wait-serial: "Response code: 200"

.github/workflows/ci.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ on:
77
- "book/"
88
- "book/**"
99
- "**/README.md"
10+
- ".github/workflows/wokwi_projects.yml"
1011
pull_request:
11-
branches: [main]
1212
paths-ignore:
1313
- "book/"
1414
- "book/**"
1515
- "**/README.md"
16+
- ".github/workflows/wokwi_projects.yml"
1617
schedule:
1718
- cron: "50 7 * * *"
1819

@@ -67,3 +68,13 @@ jobs:
6768
image: espressif/rust-std-training:latest
6869
options: -u esp -v ${{ github.workspace }}:/home/esp/workspace
6970
run: /bin/bash /home/esp/workspace/.devcontainer/test.sh ${{ matrix.project.path }}
71+
72+
- name: Wokwi CI check
73+
if: matrix.project.name == 'button-interrupt' || matrix.project.name == 'http-client' || matrix.project.name == 'hardware-check'
74+
uses: wokwi/wokwi-ci-action@v1
75+
with:
76+
token: ${{ secrets.WOKWI_CLI_TOKEN }}
77+
path: ${{ matrix.project.path }}
78+
timeout: 30000
79+
scenario: ${{ github.workspace }}/.github/${{ matrix.project.name }}.test.yaml
80+
fail_text: 'Error'
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
name: Wokwi.com CI
3+
4+
on:
5+
push:
6+
paths:
7+
- '.github/workflows/wokwi_projects.yml'
8+
pull_request:
9+
paths:
10+
- '.github/workflows/wokwi_projects.yml'
11+
schedule:
12+
- cron: "50 7 * * *"
13+
workflow_dispatch:
14+
15+
jobs:
16+
wokwi-check:
17+
name: ${{ matrix.project.name }}
18+
runs-on: ubuntu-latest
19+
strategy:
20+
fail-fast: false
21+
matrix:
22+
project:
23+
- name: "hardware-check"
24+
id: "360342886675214337"
25+
path: "intro/hardware-check"
26+
- name: http-client
27+
id: "333372159510446675"
28+
path: "intro/http-client"
29+
- name: button-interrupt
30+
id: "333374799393849940"
31+
path: "advanced/button-interrupt"
32+
33+
steps:
34+
- name: Checkout the repository
35+
uses: actions/checkout@v4
36+
37+
- name: Download project code
38+
run: |
39+
wget -q -O ${{ matrix.project.name }}.zip https://wokwi.com/api/projects/${{ matrix.project.id }}/zip
40+
unzip ${{ matrix.project.name }}.zip -d ${{ matrix.project.name }}
41+
mkdir -p ${{ matrix.project.name }}/src
42+
rm -rf ${{ matrix.project.path }}/src/*.rs
43+
cp ${{ matrix.project.name }}/*.rs ${{ matrix.project.path }}/src/
44+
rm -rf ${{ matrix.project.path }}/Cargo.toml
45+
cp ${{ matrix.project.name }}/Cargo.toml ${{ matrix.project.path }}/Cargo.toml
46+
47+
- name: Create wokwi.toml
48+
run: echo -e "[wokwi]\nversion = 1\nfirmware = '${{ matrix.project.name }}/${{ matrix.project.name }}'\nelf = '${{ matrix.project.name }}/${{ matrix.project.name }}'" > ${{ matrix.project.path }}/wokwi.toml
49+
50+
- name: Update ownership
51+
run: |
52+
sudo chown 1000:1000 -R ${{ matrix.project.path }}
53+
54+
- name: Pull Docker image
55+
run: docker image pull espressif/rust-std-training
56+
57+
- name: Test code example in Docker image
58+
uses: addnab/docker-run-action@v3
59+
with:
60+
image: espressif/rust-std-training:latest
61+
options: -u esp -v ${{ github.workspace }}:/home/esp/workspace
62+
run: |
63+
cd /home/esp/workspace/${{ matrix.project.path }}
64+
if [ -f cfg.toml.example ]; then
65+
# Rename file to cfg.toml
66+
cp cfg.toml.example cfg.toml
67+
# Replace defaults
68+
sed -i 's/wifi_ssid = "FBI Surveillance Van"/wifi_ssid = "Wokwi-GUEST"/g' cfg.toml
69+
sed -i 's/wifi_psk = "hunter2"/wifi_psk = ""/g' cfg.toml
70+
fi
71+
/home/esp/.cargo/bin/cargo build --release --out-dir /home/esp/workspace/${{ matrix.project.path }}/${{ matrix.project.name }} -Z unstable-options
72+
rm -rf /home/esp/workspace/${{ matrix.project.path }}/target
73+
74+
- run: cat ${{ matrix.project.path }}/wokwi.toml
75+
76+
- name: Wokwi CI check
77+
uses: wokwi/wokwi-ci-action@v1
78+
with:
79+
token: ${{ secrets.WOKWI_CLI_TOKEN }}
80+
path: ${{ matrix.project.path }}
81+
timeout: 30000
82+
scenario: ${{ github.workspace }}/.github/${{ matrix.project.name }}.test.yaml
83+
fail_text: 'Error'
84+
85+
- name: Upload source code
86+
if: success() || failure()
87+
uses: actions/upload-artifact@v3
88+
with:
89+
name: ${{ matrix.project.name }}_${{ matrix.project.id }}
90+
path: ${{ matrix.project.path }}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"version": 1,
3+
"author": "Sergio Gasquez Arcos",
4+
"editor": "wokwi",
5+
"parts": [
6+
{
7+
"type": "board-esp32-c3-rust-1",
8+
"id": "esp",
9+
"top": -99.32,
10+
"left": 34.67,
11+
"attrs": {
12+
"builder": "rust-training-esp32c3"
13+
}
14+
},
15+
{
16+
"type": "wokwi-pushbutton",
17+
"id": "btn1",
18+
"top": 2.81,
19+
"left": -49.66,
20+
"rotate": 90,
21+
"attrs": {
22+
"color": "green",
23+
"bounce": "0"
24+
}
25+
}
26+
],
27+
"connections": [
28+
[
29+
"esp:21",
30+
"$serialMonitor:RX",
31+
"",
32+
[]
33+
],
34+
[
35+
"esp:20",
36+
"$serialMonitor:TX",
37+
"",
38+
[]
39+
],
40+
[
41+
"esp:9",
42+
"btn1:1.r",
43+
"green",
44+
[
45+
"h0"
46+
]
47+
],
48+
[
49+
"esp:GND",
50+
"btn1:2.r",
51+
"black",
52+
[
53+
"h-97.82",
54+
"v114.6",
55+
"h26"
56+
]
57+
]
58+
],
59+
"serialMonitor": {
60+
"display": "auto"
61+
}
62+
}

advanced/button-interrupt/examples/solution_led.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ fn main() -> Result<()> {
7171

7272
match res {
7373
1 => {
74+
println!("Button pressed!");
7475
// Generates random rgb values and sets them in the led.
7576
random_light(&mut led);
7677
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[wokwi]
2+
version = 1
3+
# Exercise
4+
firmware = "target/riscv32imc-esp-espidf/debug/examples/solution"
5+
elf = 'target/riscv32imc-esp-espidf/debug/button-interrupt'
6+
7+
# Solution
8+
# firmware = 'target/riscv32imc-esp-espidf/debug/examples/solution'
9+
# elf = 'target/riscv32imc-esp-espidf/debug/examples/solution'
10+
11+
# Solution - LED
12+
# firmware = 'target/riscv32imc-esp-espidf/debug/examples/solution_led'
13+
# elf = 'target/riscv32imc-esp-espidf/debug/examples/solution_led'

0 commit comments

Comments
 (0)