raspi_pico

JLed for the Raspberry Pi Pico

This examples demonstrates how to use JLed on the Raspberry Pi Pico. The built-in LED (GPIO 25) and a LED on GPIO 16 will be faded.

Also a Dockerfile is provided to enable a hassle-free build.

Building the demo

You have two options to build the demo. Either use a docker image (recommended), or setup everything yourself (consult Pico Quickstart Guide).

Docker build

A Dockerfile and a build-script are provided to enable a hassle-free build of the demo. The script will first build a docker-image containing the build environment, then build the example.

Local build

You need the pico-sdk and necessary tools to compile everything installed.

The PICO_SDK_PATH environment variable must point to the installation directory of the SDK.

To compile the demo sketch, run cmake . && make. The resulting pico_demo.uf2 file to be uploaded will be found in this directory afterwards.

Deploy the sketch to the Raspberry Pi Pico

To deploy the demo sketch pico_demo.uf2, press and hold BOOTSEL on the Pico and connect the Pico to your PC. The Pico will now be mounted as an external drive. Copy the file pico_demo.uf2 to the mount point. The sketch should now start automatically.

Author

(c) Copyright 2021 by Jan Delgado, License: MIT

Files

pico_demo.cpp
// JLed demo for the Raspberry Pi Pico
// Fade the built-in LED (GPIO 25) and a LED on GPIO 16.
//
// Copyright 2021 by Jan Delgado. All rights reserved.
// https://github.com/jandelgado/jled
//
#include "pico/stdlib.h"    // NOLINT
#include "jled.h"           // NOLINT

int main() {
    auto led1 = JLed(25).FadeOff(2000).DelayAfter(1000).Forever();
    auto led2 = JLed(16).FadeOn(2000).DelayAfter(1000).Forever();

    while (true) {
        led1.Update();
        led2.Update();
    }
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.13)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

# Pull in Pico SDK (must be before project) - set PICO_SDK_PATH env var
include(pico_sdk_import.cmake)

project(pico_demo C CXX ASM)

# Initialise the Pico SDK
pico_sdk_init()

# Add executable. Default name is the project name, version 0.1
add_executable(pico_demo pico_demo.cpp )
pico_set_program_name(pico_demo "pico_demo")
pico_set_program_version(pico_demo "0.1")

pico_enable_stdio_uart(pico_demo 1)
pico_enable_stdio_usb(pico_demo 0)

# Add the standard library to the build
target_link_libraries(pico_demo pico_stdlib hardware_pwm JLed)

pico_add_extra_outputs(pico_demo)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../..src)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../.. build)
Dockerfile
# A minimal dockerfile to provide a build environment to compile the JLed
# raspberry pi pico JLed in a docker container. 
FROM ubuntu:20.04

LABEL MAINTAINER "Jan Delgado <jdelgado@gmx.net>"

ARG TZ=Europe/Berlin
ENV DEBIAN_FRONTEND=noninteractive

RUN    echo ${TZ} > /etc/timezone && rm -f /etc/localtime \
    && cat /etc/timezone\
    && apt-get update \
    && apt-get install -y git cmake gcc-arm-none-eabi libnewlib-arm-none-eabi \
                          build-essential vim python3 python3-pip

RUN mkdir /pico
WORKDIR /pico

# install SDK
RUN    git clone --depth=1 -b master https://github.com/raspberrypi/pico-sdk.git \
    && cd pico-sdk && git submodule update --init 

ENV PICO_SDK_PATH=/pico/pico-sdk
build.sh
#!/bin/bash
# build the raspberry pi pico JLed example using a docker container.
# Run "./build.sh docker-image" first to build the docker-image,
# then run "./build.sh compile" to compile the example.
#
# Jan Delgado 02/2021
set -eou pipefail

usage() {
    echo "$0: <docker-image|compile|shell|clean>"
}

build_image() {
    docker build \
        --build-arg="TZ=$(timedatectl show -p Timezone --value)" \
        -t picosdk:latest .
}

run_cmd() {
    docker run -ti --rm \
        --user="$(id -u):$(id -g)" \
        -v "$(pwd)/../..:/src:z" \
        picosdk:latest "$@"
}

main() {
    case $action in
        docker-image) build_image ;;
        compile)
            run_cmd sh -c "cd /src/examples/raspi_pico && cmake . && make"
            local -r line=$(printf '=%.0s' {1..75})
            echo "$line"
            echo "BUILD SUCCESSFUL."
            echo "Now upload the file pico_demo.uf2 to your Pico manually."
            echo "$line"
            ;;
        shell) run_cmd bash ;;
        clean) git clean -d -x -f ;;
        *) usage ;;
    esac
}

action=${1:-""}
main action
pico_sdk_import.cmake
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake

# This can be dropped into an external project to help locate this SDK
# It should be include()ed prior to project()

if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
    set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
    message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
endif ()

if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
    set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
    message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
endif ()

if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
    set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
    message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
endif ()

set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")

if (NOT PICO_SDK_PATH)
    if (PICO_SDK_FETCH_FROM_GIT)
        include(FetchContent)
        set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
        if (PICO_SDK_FETCH_FROM_GIT_PATH)
            get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
        endif ()
        FetchContent_Declare(
                pico_sdk
                GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
                GIT_TAG master
        )
        if (NOT pico_sdk)
            message("Downloading Raspberry Pi Pico SDK")
            FetchContent_Populate(pico_sdk)
            set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
        endif ()
        set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
    else ()
        message(FATAL_ERROR
                "SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
                )
    endif ()
endif ()

get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
if (NOT EXISTS ${PICO_SDK_PATH})
    message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
endif ()

set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
    message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
endif ()

set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)

include(${PICO_SDK_INIT_CMAKE_FILE})