Skip to content

Commit

Permalink
Configure Wokwi CI (#222)
Browse files Browse the repository at this point in the history
Added CI config for
- [x] Basic Navigation
- [x] Callbacks
- [x] IntFloatValues
- [x] Sub Menu
- [x] Item Back
- [x] List
- [x] CharsetInput
- [x] Menu Timeout
- [x] ~~_SimpleInput (ignored, largely covered by CharsetInput)_~~

### Additionally:
- BugFix for ItemSubMenu (ENTER action is ignored)
- Added more logs
  • Loading branch information
forntoh authored Sep 22, 2024
1 parent 3f6a298 commit 6eb6b5f
Show file tree
Hide file tree
Showing 17 changed files with 516 additions and 4 deletions.
82 changes: 82 additions & 0 deletions .github/workflows/wokwi_ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Wokwi CI

on: [pull_request]

jobs:
provide-examples:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- id: set-matrix
run: |
TMP="["
for FILE in examples/*/*.ino; do
EXAMPLE_NAME=$(basename $FILE .ino)
TEST_FILE="test/$EXAMPLE_NAME.test.yml"
if [ -f "$TEST_FILE" ]; then
TMP="$TMP\"$FILE\","
fi
done
TMP="${TMP::-1}]"
echo "::set-output name=matrix::$TMP"
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}

run-ci:
needs: provide-examples
strategy:
matrix:
example: ${{ fromJson(needs.provide-examples.outputs.matrix) }}

runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup cache
uses: actions/cache@v4
with:
path: |
~/.cache/pip
~/.platformio/.cache
key: ${{ runner.os }}-pio

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.x"

- name: Prepare Example File
run: |
EXAMPLE_FILE=$(python .scripts/prepare_example.py ${{ matrix.example }})
EXAMPLE_NAME=$(basename $EXAMPLE_FILE .cpp)
cp $EXAMPLE_FILE src/main.cpp
if [ -f "test/$EXAMPLE_NAME.test.yml" ]; then
echo "TEST_FILE=test/$EXAMPLE_NAME.test.yml" >> $GITHUB_ENV
fi
- name: Prepare Test Script
run: |
RUN_DURATION=$(python .scripts/prepare_workflow.py ${{ env.TEST_FILE }} ${{ vars.WOKWI_CI_TIMINGS }})
echo "RUN_DURATION=$((RUN_DURATION + ${{ vars.WOKWI_TIMEOUT_BUFFER }}))" >> $GITHUB_ENV
- name: Install PlatformIO
run: |
python -m pip install --upgrade pip
pip install --upgrade platformio
- name: Install library dependencies
run: pio lib install

- name: Build PlatformIO
run: pio run
env:
LOCAL_BUILD: 1

- name: Running ${{ env.TEST_FILE }} on Wokwi
uses: wokwi/wokwi-ci-action@v1
with:
token: ${{ secrets.WOKWI_CLI_TOKEN }}
scenario: ${{ env.TEST_FILE }}
timeout: ${{ env.RUN_DURATION }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.env
.vars
.secrets
.pio
.vscode/
src/main.cpp
Expand Down
83 changes: 83 additions & 0 deletions .scripts/prepare_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import os
import re
import sys


def replace_lines(file_path, replacements):
with open(file_path, 'r') as file:
lines = file.readlines()

with open(file_path, 'w') as file:
for line in lines:
for pattern, replacement in replacements.items():
if re.search(pattern, line):
line = replacement + '\n'
file.write(line)

if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python prepare_example.py <file_path>")
sys.exit(1)

file_path = sys.argv[1]

# Change the file extension to .cpp
base = os.path.splitext(file_path)[0]
new_file_path = base + '.cpp'
os.rename(file_path, new_file_path)

replacement1 = """
#include <Button.h>
#include <input/ButtonAdapter.h>"""

replacement2 = """
Button upBtn(5);
ButtonAdapter upBtnA(&menu, &upBtn, UP);
Button downBtn(6);
ButtonAdapter downBtnA(&menu, &downBtn, DOWN);
Button enterBtn(7);
ButtonAdapter enterBtnA(&menu, &enterBtn, ENTER);
Button backBtn(8);
ButtonAdapter backBtnA(&menu, &backBtn, BACK);
Button leftBtn(9);
ButtonAdapter leftBtnA(&menu, &leftBtn, LEFT);
Button rightBtn(10);
ButtonAdapter rightBtnA(&menu, &rightBtn, RIGHT);
Button backspaceBtn(11);
ButtonAdapter backspaceBtnA(&menu, &backspaceBtn, BACKSPACE);"""

replacement3 = """
upBtnA.observe();
downBtnA.observe();
enterBtnA.observe();
backBtnA.observe();
leftBtnA.observe();
rightBtnA.observe();
backspaceBtnA.observe();"""

replacement4 = """
upBtn.begin();
downBtn.begin();
enterBtn.begin();
backBtn.begin();
leftBtn.begin();
rightBtn.begin();
backspaceBtn.begin();
Serial.begin(9600);"""

replacement5 = """
#define DISPLAY_TIMEOUT 2000
#include <LcdMenu.h>"""

replacements = {
r"#include <input/.*Adapter.h>": replacement1,
r".*Adapter .*\(&menu, .*\);": replacement2,
r".*\.observe\(\);": replacement3,
r".*Serial.begin\(.*\);": replacement4,
r"#include <LcdMenu.h>": replacement5
}

replace_lines(new_file_path, replacements)

# Print the new path
print(new_file_path)
57 changes: 57 additions & 0 deletions .scripts/prepare_workflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os
import re
import sys


def button_press_template(button_name):
return f"""
- set-control:
part-id: {button_name}
control: pressed
value: 1
- delay: {press_holding_time}ms
- set-control:
part-id: {button_name}
control: pressed
value: 0
- delay: {wait_time_after_release}ms
"""

def replace_lines(file_path, replacements):
total_wait_time = 0
with open(file_path, 'r') as file:
lines = file.readlines()

with open(file_path, 'w') as file:
for line in lines:
for pattern, replacement in replacements.items():
if re.search(pattern, line):
line = replacement + '\n'
total_wait_time += press_holding_time + wait_time_after_release + serial_wait_time
file.write(line)

return total_wait_time

if __name__ == "__main__":
if len(sys.argv) != 5:
print("Usage: python prepare_workflow.py <file_path> <serial_wait_time> <wait_time_after_release> <press_holding_time>")
sys.exit(1)

file_path = sys.argv[1]
serial_wait_time = int(sys.argv[2])
press_holding_time = int(sys.argv[3])
wait_time_after_release = int(sys.argv[4])

replacements = {
r".*- simulate: upButton-press": button_press_template("btn1"),
r".*- simulate: downButton-press": button_press_template("btn2"),
r".*- simulate: enterButton-press": button_press_template("btn3"),
r".*- simulate: backButton-press": button_press_template("btn4"),
r".*- simulate: leftButton-press": button_press_template("btn5"),
r".*- simulate: rightButton-press": button_press_template("btn6"),
r".*- simulate: backspaceButton-press": button_press_template("btn7"),
}

total_wait_time = replace_lines(file_path, replacements)

print(total_wait_time)
62 changes: 58 additions & 4 deletions diagram.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": 1,
"author": "Uri Shaked",
"author": "Thomas Forntoh",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 200, "left": 20, "attrs": {} },
Expand All @@ -16,9 +16,51 @@
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": 207.8,
"left": 393.6,
"top": 198.2,
"left": 489.6,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-pushbutton",
"id": "btn2",
"top": 294.2,
"left": 489.6,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-pushbutton",
"id": "btn3",
"top": 246.2,
"left": 489.6,
"attrs": { "color": "green" }
},
{
"type": "wokwi-pushbutton",
"id": "btn4",
"top": 380.6,
"left": 393.6,
"attrs": { "color": "red" }
},
{
"type": "wokwi-pushbutton",
"id": "btn5",
"top": 246.2,
"left": 422.4,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-pushbutton",
"id": "btn6",
"top": 246.2,
"left": 556.8,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-pushbutton-6mm",
"id": "btn7",
"top": 353,
"left": 412.8,
"attrs": { "color": "yellow" }
}
],
"connections": [
Expand All @@ -32,7 +74,19 @@
[ "encoder1:GND", "uno:GND.1", "black", [ "h-0.8", "v38.4", "h-229.3" ] ],
[ "encoder1:VCC", "uno:5V", "red", [ "h-0.4", "v288", "h-194.4" ] ],
[ "btn1:1.l", "uno:5", "green", [ "h-163.2", "v-11.8" ] ],
[ "btn1:2.l", "uno:GND.1", "black", [ "h0" ] ]
[ "btn1:2.l", "uno:GND.1", "black", [ "h0" ] ],
[ "btn2:1.l", "uno:6", "green", [ "h0" ] ],
[ "btn3:1.l", "uno:7", "green", [ "v-28.8", "h-280.6" ] ],
[ "btn4:1.l", "uno:8", "green", [ "h0" ] ],
[ "btn2:2.l", "uno:GND.1", "black", [ "h0" ] ],
[ "btn3:2.l", "uno:GND.1", "black", [ "v29", "h-354.1" ] ],
[ "btn4:2.l", "uno:GND.1", "black", [ "h0" ] ],
[ "btn5:1.l", "uno:9", "green", [ "h0" ] ],
[ "btn6:1.l", "uno:10", "green", [ "v-19.2", "h-392.9" ] ],
[ "btn5:2.l", "uno:GND.1", "black", [ "h0" ] ],
[ "btn6:2.l", "uno:GND.1", "black", [ "v19.4", "h-430.9" ] ],
[ "btn7:1.l", "uno:11", "green", [ "h0" ] ],
[ "btn7:2.l", "uno:GND.2", "black", [ "h0" ] ]
],
"dependencies": {}
}
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ lib_deps =
feilipu/FreeRTOS@^11.0.1-5
mprograms/SimpleRotary@^1.1.3
madleech/Button@^1.0.0
Wire
2 changes: 2 additions & 0 deletions src/ItemInputCharset.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class ItemInputCharset : public ItemInput {
} else {
display->drawChar(' ');
}
printLog(F("ItemInputCharset::abortCharEdit"));
}
void commitCharEdit(DisplayInterface* display) {
uint8_t length = strlen(value);
Expand All @@ -118,6 +119,7 @@ class ItemInputCharset : public ItemInput {
value = buf;
}
abortCharEdit(display);
printLog(F("ItemInputCharset::commitCharEdit"), charset[charsetPosition]);
ItemInput::right(display);
}
void showNextChar(DisplayInterface* display) {
Expand Down
1 change: 1 addition & 0 deletions src/ItemSubMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ItemSubMenu : public MenuItem {
bool process(LcdMenu* menu, const unsigned char command) {
switch (command) {
case ENTER:
changeScreen(menu);
return true;
default:
return false;
Expand Down
1 change: 1 addition & 0 deletions src/LcdMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ MenuScreen* LcdMenu::getScreen() {
}

void LcdMenu::setScreen(MenuScreen* screen) {
printLog(F("LcdMenu::setScreen"));
this->screen = screen;
display.clear();
this->screen->draw(&display);
Expand Down
25 changes: 25 additions & 0 deletions test/Basic.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Basic Navigation Test
version: 1
author: Thomas Forntoh
steps:
- wait-serial: "#LOG# LcdMenu::setScreen"
- simulate: downButton-press
- wait-serial: "#LOG# MenuScreen:down=1"
- simulate: downButton-press
- wait-serial: "#LOG# MenuScreen:down=2"
- simulate: downButton-press
- wait-serial: "#LOG# MenuScreen:down=3"
- simulate: downButton-press
- wait-serial: "#LOG# MenuScreen:down=4"
- simulate: downButton-press
- wait-serial: "#LOG# MenuScreen:down=4"
- simulate: upButton-press
- wait-serial: "#LOG# MenuScreen:up=3"
- simulate: upButton-press
- wait-serial: "#LOG# MenuScreen:up=2"
- simulate: upButton-press
- wait-serial: "#LOG# MenuScreen:up=1"
- simulate: upButton-press
- wait-serial: "#LOG# MenuScreen:up=0"
- simulate: upButton-press
- wait-serial: "#LOG# MenuScreen:up=0"
15 changes: 15 additions & 0 deletions test/Callbacks.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Callbacks Test
version: 1
author: Thomas Forntoh
steps:
- wait-serial: "#LOG# LcdMenu::setScreen"
- simulate: downButton-press
- wait-serial: "#LOG# MenuScreen:down=1"
- simulate: downButton-press
- wait-serial: "#LOG# MenuScreen:down=2"
- simulate: enterButton-press
- wait-serial: "#LOG# ItemToggle::toggle=ON"
- simulate: enterButton-press
- wait-serial: "#LOG# ItemToggle::toggle=OFF"
- simulate: downButton-press
- wait-serial: "#LOG# MenuScreen:down=3"
Loading

0 comments on commit 6eb6b5f

Please sign in to comment.