Modularizing ESP32 Software

In my ESP32 journey, I’ve come to a point, where I want to be able to split my code into libraries and consume third-party libraries. In this article, I’m going to explore how to do this.

The project directory tree

ESP32 projects follow a folder structure:

1
2
3
4
5
6
7
8
9
10
11
12
project/
├─ components/
│  ├─ component1/
│  │  ├─ CMakeLists.txt
│  │  ├─ ...
│  ├─ component2/
│     ├─ CMakeLists.txt
│     ├─ ...
├─ main/
│  ├─ CMakeLists.txt
│  ├─ ...
├─ CMakeLists.txt
Read More

Neovim as ESP32 IDE with Clangd LSP

In this article, I’m going to explain how to configure Neovim to work as an IDE for ESP32.

Before we start, we need to have ESP-IDF in our system. You can follow my Introduction to ESP32 development article for instructions on how to install it.

Lazy vim

I use lazy to manage my Neovim plugins, so let’s make sure it’s configured correctly. To do that, we need to add these lines to our init.lua (usually at ~/.config/nvim/init.lua):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
local lazypath = vim.fn.stdpath('data') .. '/lazy/lazy.nvim'
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    'git',
    'clone',
    '--filter=blob:none',
    'https://github.com/folke/lazy.nvim.git',
    '--branch=stable', -- latest stable release
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)

require('lazy').setup('plugins')
Read More

Introduction to ESP32 development

A few months ago, I started learning Arduino, and recently I finished my first small project. After finishing the project, I was wondering if I could build the same thing for cheaper, and that’s when I stumbled into ESP32.

ESP32 is an MCU (Micro Controller Unit) that got very popular because it has integrated WiFi, Bluetooth, very good documentation and is relatively cheap for what it does. Interestingly, the Arduino UNO R4 WiFi contains two MCU and one of them is an ESP32.

Getting an ESP32

The easiest way to get started with ESP32 is to buy a development board. While you can find some in Espressif’s website (The manufacturer of ESP32), you can also get clones from many places around the world.

I’m currently in Cape Town, so got mine from Communica. I ended up paying $7.50 USD for it. Depending on where you live and how long you are willing to wait to get one, you might be able to get it for considerably cheaper.

ESP32 dev board

Read More

Asynchronous Programming with Tokio

If you are interested in learning about asynchronous programming in more depth, I recommend reading Asynchronous Programming in Rust.

Asynchronous programming

When we run code that makes network requests, these request are sent through the network.

Sending the request and waiting for the response is done by the network peripheral and doesn’t require the CPU. This means, the CPU is free to do other things while it waits.

Code written synchronously will send a request and then block the thread waiting for a response. For example:

1
2
3
4
fn main() {
    let resp = reqwest::blocking::get("https://httpbin.org/ip")?.text()?;
    println!("{:#?}", resp);
}
Read More

Programming Concurrency in Rust

One of Rust’s most praised features is how it makes concurrent programming safe. In this article we are going to learn some ways to do concurrent programming and explain how Rust makes them safe compared to other programming languages.

Working with threads

We can start new threads with thread::spawn:

1
2
3
4
5
6
7
8
9
10
use std::thread;
use std::time::Duration;

fn main() {
    thread::spawn(|| {
        println!("The spawned thread");
    });

    thread::sleep(Duration::from_millis(1));
}

This will print:

1
The spawned thread
Read More

Smart Pointers in Rust

Rust is considered safe because it makes sure variable ownership is managed correctly in our code. In the most basic case, Rust enforces these rules:

  • Each value in Rust has an owner.
  • There can only be one owner at a time.
  • When the owner goes out of scope, the value will be dropped.

The problem is that there are some scenarios where we need to break these rules. This is where smart pointers help us.

What are smart pointers?

Smart pointers are structs that manage some internal data.

They are called pointers because they implement the Deref trait, so they can be used like pointers (Using the & and * syntax).

Read More

Rust References Lifetimes

Rust has a mechanism called borrow checker that makes sure references are not used when they are not valid anymore. The borrow checker uses lifetimes to do its job internally.

Let’s look at a simple example where the borrow checker detects a possibly invalid reference:

1
2
3
4
5
6
7
8
9
10
fn main() {
    let r;

    {
        let i = 1;
        r = &i;
    }

    println!("{}", r);
}

If we compile this, we’ll get the following error:

Read More

Traits, Rust's Interfaces

As the title says, traits are Rust’s alternative to Interfaces. They allow us to use polymorphism in Rust. We can create a trait like this:

1
2
3
trait Calculator {
    fn add(&self, left: i32, right: i32) -> i32;
}

To implement the trait we use the impl keyword on a struct:

1
2
3
4
5
6
7
struct GoodCalculator {}

impl Calculator for GoodCalculator {
    fn add(&self, left: i32, right: i32) -> i32 {
        left + right
    }
}
Read More

Testing in Rust

In this article, we are going to learn how to write and run tests for Rust.

Unit tests

Rust made the interesting decision that unit tests should be written in the same files as the code under test. Let’s imagine we have a module with a function named add:

1
2
3
pub fn add(left: i64, right: i64) -> i64 {
    left + right
}

If we want to test that function, we would modify the file to look like this:

Read More

Introduction to Rust

Rust is a relatively new programming language that promises to be as fast as C, but less complex and error prone.

Rust compiles directly to machine code, so it doesn’t require a virtual machine. This makes it faster than languages like Java or Python. It also doesn’t use a garbage collector, which makes it faster and more predictive than other compiled languages like Golang.

On top of speed and predictability, Rust also promises a programming model that ensures memory and thread safety, which makes it great for complex applications.

Installation

The recommended way to install rust in Linux and Mac is using this command:

1
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

We will be greeted by this prompt asking to choose an option:

1
2
3
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
Read More