Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Rust language

Learning rust

rust libraries

desktop app with rust

creating books

Using rust in Raspberry pi

rust GPIO for pi

Most promising seems to be RPPAL option.

I will try this option and do the write up on this.

Cross compiling rust on ubuntu

Compiling rust on pi will take for ever, cross compiling will save development time. We will use ubuntu for cross compile.

If we are on a windows machine, WSL2 also is a good way to develop for raspberry. check WSL 2: Getting started. Go ahead install ubuntu to run with WSL2.

Primary problem with cross compiling rust for pi zero is that zero is armv6 but other pis are armv7. At the time of this writing, gcc toolchain only has support for armv7. armv6 compile also produces armv7 image. So the toolchain needs to be installed from pi official tool repo from github which has armv6 support. See more in the following links,

Using this strategy we will go ahead and setup wsl2 linux detailed in Rust in Raspberry Pi.

QEMU for library dependencies

Linux kernel module with rust

rust-wasm

java to rust

python to rust

using dbus in rust

pi dbus
$ dpkg -l | grep dbus
ii  dbus                1.12.16-1 armhf  simple  interprocess messaging system (daemon and utilities)
ii  libdbus-1-3:armhf   1.12.16-1 armhf  simple  interprocess messaging system (library)
ii  libdbus-1-dev:armhf 1.12.16-1 armhf  simple  interprocess messaging system (development headers)
ii  python-dbus         1.2.8-3   armhf  simple  interprocess messaging system (Python interface)
ii  python3-dbus        1.2.8-3   armhf  simple  interprocess messaging system (Python 3 interface)

install dbus-codegen-rust

following will install dbus-codegen-rust CLI.

cargo install dbus-codegen

There are two possibilities

  • Write server and client.
  • Write client for an exiting installed server.

Client for an exiting server

example of generated code,

dbus-codegen-rust -s -d org.freedesktop.timedate1 -p /org/freedesktop/timedate1 -o src/timedate.rs -i org.freedesktop

which will put the code in src folder.

cross compile dbus

The following script downloads and cross-compiles D-Bus and Expat for Raspberry Pi zero:

#!/usr/bin/env bash

set -ex

# Clone the D-bus and Expat libraries
[ -d dbus ] || \
    git clone --branch dbus-1.13.18 --single-branch --depth=1 \
        https://gitlab.freedesktop.org/dbus/dbus.git

[ -d libexpat ] || \
    git clone --branch R_2_2_9 --single-branch --depth=1 \
    https://github.com/libexpat/libexpat.git

# Script for building these libraries:
cat << 'EOF' > build-script-docker.sh
#!/usr/bin/env bash

set -ex
cd "$(dirname "${BASH_SOURCE[0]}")"

# Point pkg-config to the sysroot:
. cross-pkg-config

# Directory to install the packages to:
export RPI_STAGING="$PWD/staging"
rm -rf "${RPI_STAGING}"

# libexpat
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

pushd libexpat/expat
./buildconf.sh
mkdir -p build
pushd build
../configure \
    --prefix="/usr/local" \
    --host="${HOST_TRIPLE}" \
    --with-sysroot="${RPI_SYSROOT}"
make -j$(nproc)
make install DESTDIR="${RPI_SYSROOT}"
make install DESTDIR="${RPI_STAGING}"
popd
popd

# dbus
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

pushd dbus
mkdir -p build
pushd build
cmake .. \
    -DCMAKE_TOOLCHAIN_FILE="$HOME/${HOST_TRIPLE}.cmake" \
    -DCMAKE_BUILD_TYPE="Release" \
    -DCMAKE_INSTALL_PREFIX="/usr/local"
make -j$(nproc)
make install DESTDIR="${RPI_SYSROOT}"
make install DESTDIR="${RPI_STAGING}"
popd
popd
EOF

# Start the Docker container with the toolchain and run the build script:
image="tttapa/rpi-cross:armv6-rpi-linux-gnueabihf-dev"
docker run --rm -it -v "$PWD:/tmp/workdir" $image \
    bash "/tmp/workdir/build-script-docker.sh"

You'll need to have Docker installed. When finished, the libraries will be in the staging folder in the working directory.

The Docker container with the toolchain is one I maintain (https://github.com/tttapa/RPi-Cpp-Toolchain), but the installation process should be similar with the toolchain you're using, you'll just have to install some extra dependencies such as make, autotools, and maybe cross-compile some other dependencies of Expat and D-Bus as well.
I also maintain some notes with instructions of the toolchains and cross-compilation processes, which you might find useful: https://tttapa.github.io/Pages/Raspberry-Pi/C++-Development/index.html

You might want to add some extra options to the configure and cmake steps, but that's outside of the scope of this answer, see the relevant D-Bus documentation.
Also note that installs both libraries to both the sysroot and the staging area, it'll depend on what you want to do with it. You have to install at least libexpat to the ${RPI_SYSROOT} folder, because that's the folder used as the sysroot for compiling dbus which depends on libexpat. The sysroot folder for the compilation of dbus is selected in the CMake Toolchain file, ~/${HOST_TRIPLE}.cmake, it's included with the Docker container. Its contents are:

SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_C_COMPILER armv6-rpi-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER armv6-rpi-linux-gnueabihf-g++)
SET(CMAKE_SYSTEM_PROCESSOR armv6)

set(CMAKE_SYSROOT $ENV{RPI_SYSROOT})
SET(CMAKE_FIND_ROOT_PATH ${CMAKE_SYSROOT}) 

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

You might also have to point pkg-config to the right sysroot folder. This is handled by the cross-pkg-config script:

export PKG_CONFIG_LIBDIR="${RPI_SYSROOT}/usr/local/lib:${RPI_SYSROOT}/opt/vc/lib"
export PKG_CONFIG_PATH="${RPI_SYSROOT}/usr/local/lib/pkgconfig:${RPI_SYSROOT}/usr/local/share/pkgconfig:${RPI_SYSROOT}/opt/vc/lib/pkgconfig"
export PKG_CONFIG_SYSROOT_DIR="${RPI_SYSROOT}"

Rust Qt binding

using rust with vscode in windows

If you are using powershell in vscode, the path might not pickup rust compiler. Read Powershell setup for vscode for more information.

Place .psrc.ps1 file at the root of the project folder with following, which is the default path of rust install.

$env:Path += ";$profile/.cargo/bin"

If you installed rust to a custom path, use that path instead.

Videos to watch:

debugging rust with vscode in windows

Server

creative content framework

Ide design

rust drawing app