Skip to content

Instantly share code, notes, and snippets.

@gepatto
Last active Apr 19, 2021
Embed
What would you like to do?
Create a minimal Raspberry Pi Pico C-language project and vscode workspace and open it in vscocde
#!/bin/zsh
#
# makepico.sh
#
# Create a minimal Raspberry Pi Pico C-language project and open it in vscocde
#
# @original author Tony Smith
# @copyright 2021, Tony Smith
# @version 1.0.0
# @license MIT
#
make_project() {
# Create and copy the project files
# Args: 1 -- the project path
# Check the path and, if necessary
# create intermediate directories
# NOTE The check will exit on error
check_path "${1}"
# Get project name in lower case
project_name=${1:t:l}
project_path=${1}
# Create initial C file
make_source_file "${1}"
# Copy over the .make file from the SDK
file="pico_sdk_import.cmake"
if cp "${PICO_SDK_PATH}/external/${file}" "${project_path}/${file}"; then
# NOP
else
echo "Error - could not copy the ${file} file"
exit 1
fi
# Make the CMakeLists.txt file for this project
make_cmake_file "${1}"
make_vscode_workspace_file "${1}"
make_vscode_cpp_json "${1}"
# And done...
project_name=${1:t}
echo "Project ${project_name} created at ${project_path}"
code "${1}/pico_${1:t:l}.code-workspace"
}
make_source_file() {
# Output lines to the file
# Args: 1 -- project path
echo "Creating project files..."
project_name=${1:t}
source_file=${1:t:l}
{
echo -e "/*\n * Project ${project_name} created by makepico\n */\n\n"
echo '#include <stdio.h>'
echo '#include "pico/stdlib.h"'
echo '#include "pico/binary_info.h"'
echo '#include "hardware/gpio.h"'
echo -e "\n\nconst uint LED_PIN = 25;\n\n"
echo -e "int main() {\n\n stdio_init_all();\n\n gpio_init(LED_PIN);\n gpio_set_dir(LED_PIN, GPIO_OUT);\n"
echo -e " sleep_ms(1500);\n printf(\"Project Initialized: Hello World!\"); \n"
echo -e " while (1)\n {\n gpio_put(LED_PIN, !gpio_get(LED_PIN));\n sleep_ms(2000);\n }\n"
echo -e " return 0;\n}\n"
} >> "${1}/${source_file}.c"
}
make_vscode_workspace_file() {
# Output lines to the file
# Args: 1 -- project path
echo "Creating project files..."
project_name=${1:t}
source_file=${1:t:l}
{
echo '{'
echo ' "folders": ['
echo ' {'
echo ' "path": "."'
echo ' },'
echo ' {'
echo ' "path": "'${PICO_SDK_PATH}'"'
echo ' }'
echo ' ],'
echo ' "settings": {}'
echo '}'
} >> "${1}/pico_${source_file}.code-workspace"
}
make_vscode_cpp_json(){
mkdir -p "${1}/.vscode";
source_file=${1:t:l}
{
echo '{'
echo ' "configurations": ['
echo ' {'
echo ' "name": "Pico",'
echo ' "includePath": ['
echo -e " \"${PICO_SDK_PATH}/**\""
echo ' ],'
echo ' "browse": {'
echo ' "limitSymbolsToIncludedHeaders": true,'
echo ' "databaseFilename": "${default}",'
echo ' "path": ['
echo ' "${default}"'
echo ' ]'
echo ' },'
echo ' "forcedInclude": ['
echo ' "${default}"'
echo ' ],'
echo ' "intelliSenseMode": "${default}",'
echo ' "compilerPath": "${default}",'
echo ' "cStandard": "${default}",'
echo ' "cppStandard": "${default}",'
echo ' "configurationProvider": "ms-vscode.cmake-tools"'
echo ' }'
echo ' ],'
echo ' "version": 4'
echo '}'
} >> "${1}/.vscode/c_cpp_properties.json"
}
make_cmake_file() {
# Output lines to the file, interpolating the project name
# Args: 1 -- project path
echo "Creating CMakeLists.txt..."
project_name=${1:t}
source_file=${project_name:l}
{
echo "cmake_minimum_required(VERSION 3.12)"
echo "include(pico_sdk_import.cmake)"
echo "project(${project_name})"
echo "add_executable(${project_name} ${source_file}.c)"
echo "pico_sdk_init()"
echo "pico_enable_stdio_usb(${project_name} 1)"
echo "pico_enable_stdio_uart(${project_name} 1)"
echo "pico_add_extra_outputs(${project_name})"
echo "target_link_libraries(${project_name} pico_stdlib)"
} >> "${1}/CMakeLists.txt"
}
check_path() {
# Check the path is valid
# Args: 1 -- the project path as specified by the user
project_name={$1:t}
if [[ ! -d "${1}" ]]; then
if mkdir -p "${1}" >> /dev/null; then
echo "Creating project directory..."
else
echo "Error — could not create path for project ${project_name}"
exit 1
fi
fi
}
# Runtime start
# Get the arguments, which should be project path(s)
for arg in "$@"; do
make_project $arg
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment