Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
madskjeldgaard committed Jul 16, 2024
0 parents commit 09944cf
Show file tree
Hide file tree
Showing 12 changed files with 888 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: PlatformIO CI

on: [push]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
path: |
~/.cache/pip
~/.platformio/.cache
key: ${{ runner.os }}-pio
- uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install PlatformIO Core
run: pip install --upgrade platformio

- name: Build PlatformIO Project
run: pio run
35 changes: 35 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

.pio
.cache
207 changes: 207 additions & 0 deletions .scripts/commands.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
#! /bin/bash

# A helper script to help build and upload firmware projects based on PlatformIO

function list_environments(){
pio project config|grep ^env:|sed "s,env:,,"
}



function generate_vscode_tasks() {
# Lists all environments, and creates a vscode build and upload tasks for each environment
# This is useful for creating a vscode task.json file

for env in $(list_environments); do
echo "
// Upload ${env}
{
\"label\": \"upload ${env}\",
\"type\": \"shell\",
\"command\": \"pio run -t upload -e ${env}\",
},
// Build ${env}
{
\"label\": \"build ${env}\",
\"type\": \"shell\",
\"command\": \"pio run -e ${env}\",
},"
done
}

function update_vscode_tasks_json () {
# Updates the vscode tasks.json file with the tasks generated by generate_vscode_tasks

if [ ! -f .vscode/tasks.json ]; then
echo "tasks.json file not found. Please run this command from the root of the project"
exit 1
fi

# Backup the tasks.json file
cp .vscode/tasks.json .vscode/tasks.json.bak

# Create the top part
echo ' {
// This file is auto generated by the commands.sh script. Any edits will be overwritten automatically.
"version": "2.0.0",
"tasks": [
// Build all
{
"label": "Build all",
"type": "shell",
"command": "pio run",
"group": {
"kind": "build",
"isDefault": true
}
},
// Make compiledb to allow LSP to work
{
"label": "Build compiledb database for LSP / clangd",
"type": "shell",
"command": "pio run -t compiledb",
},
' > .vscode/tasks.json



# Generate the tasks
generate_vscode_tasks >> .vscode/tasks.json

# Close the tasks.json file
echo "] }" >> .vscode/tasks.json

echo "tasks.json updated successfully"
}

function print_help() {
echo "Usage: $0 <command>"
echo "Commands:"
echo " list"
echo " show"
echo " build-all"
echo " build <environment>"
echo " upload <environment>"
echo " add_board"
echo " fzf_boards"
echo " update_vscode_tasks"
exit 1
}

function build() {
if [ $# -ne 1 ]; then
echo "Usage: $0 build <environment>"
echo "See list of available environments by running $0 list"
exit 1
fi

pio run -e $1
}

function build_all() {
pio run
}

function upload() {
if [ $# -ne 1 ]; then
echo "Usage: $0 upload <environment>"
echo "See list of available environments by running $0 list"
exit 1
fi

pio run -e $1 --target upload
}

function show() {
pio project --list-targets
}

function fzf_boards() {
# Fuzzy search boards and return the board id

# Check if fzf installed
if ! command -v fzf &> /dev/null; then
echo "fzf is not installed. Please install fzf to use this feature."
exit 1
fi

pio boards --json-output | jq -r '.[] | "id:" + .id + ", name:" + .name + ", platform:" + .platform' | fzf

}

# Gets new environemnt using fzf and adds it to the platformio.ini file
function add_new_board() {
new_env=$(fzf_boards)
if [ -z "$new_env" ]; then
echo "No board selected. Exiting"
exit 1
fi
echo "Adding new environment $new_env"

# Get the board id
board_id=$(echo $new_env | awk -F',' '{print $1}' | awk -F':' '{print $2}' | xargs)
board_name=$(echo $new_env | awk -F',' '{print $2}' | awk -F':' '{print $2}' | xargs)
board_platform=$(echo $new_env | awk -F',' '{print $3}' | awk -F':' '{print $2}' | xargs)

# Add the new environment to the platformio.ini file
echo "Adding new environment to platformio.ini"
echo "
; $board_name
[env:$board_id]
platform = $board_platform
board = $board_id
; Additional build flags for $board_name here
build_flags =
\${env.build_flags}
; Additional libraries for $board_name here
lib_deps =
\${env.lib_deps}" >> platformio.ini

update_vscode_tasks_json
}

function main() {
# parse the command line arguments
if [ $# -eq 0 ]; then
print_help
exit 1
fi

case $1 in
list)
list_environments
;;
show)
show
;;
upload)
shift
upload $@
;;
build-all)
build_all
;;
build)
shift
build $@
;;

add_board)
add_new_board
;;
fzf_boards)
fzf_boards
;;
update_vscode_tasks)
update_vscode_tasks_json
;;
*)
print_help
exit 1
;;
esac
}

main $@
Loading

0 comments on commit 09944cf

Please sign in to comment.