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

Sending HTTP requests with Arduino

To make HTTP and HTTPS requests from an Arduino board, we need to first install the ArduinoHttpClient library:

1
arduino-cli lib install ArduinoHttpClient

Once we have the library, we can use it to make requests:

Read More

Using Arduino Language Server With Neovim

In this post we’re going to configure neovim to work with Arduino Language Server.

Nvim Lsp Config

Neovim comes with an LSP client included, nvim-lspconfig is a plugin that helps us configure the client so it can talk to LSP servers.

This configuration should be enough to get started with Arduino:

1
2
3
4
5
6
return {
  "neovim/nvim-lspconfig",
  config = function()
    require('lspconfig').arduino_language_server.setup {}
  end
}
Read More

Using Arduino Serial Monitor From Linux

Arduino Serial Monitor is a tool that can be used for debugging or interacting with our Arduino board. More specifically, it allows us to read and write data to a serial port.

For our sketch to be able to use the serial monitor, we need to use Serial.begin and specify a baud rate. For example:

1
Serial.begin(9600);

The valid baud rates vary depending on the board we are using. 9600 is a safe value that works on most boards.

Reading

The first thing we want to do is print to the serial port. For example:

1
Serial.println("Hello");
Read More

Introduction to Arduino CLI

In my previous post, Getting Started With Arduino UNO R4, I showed how we can upload a sketch into an Arduino board. In this article, we are going to do the same, but this time using the Arduino CLI.

Why Arduino CLI?

I personally, use neovim for coding, which makes it a necessity for me to be able to compile and upload my code from my terminal.

If you prefer the IDE, this article might not be for you, but, understanding the CLI could be useful in the future to automate repetitive tasks or run things in a CI environment.

Installation

We can install the Arduino CLI on our system with these command:

1
2
3
4
5
6
7
8
# Create a folder to install the CLI
mkdir ~/bin/arduino-cli

# Move to the folder we created
cd ~/bin/arduino-cli

# Install
curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh
Read More

Getting Started With Arduino UNO R4

In this article, I’m going to show how to write a simple program for Arduino UNO R4. I expect most of the steps I follow here can be used for other models of Arduino, but I’m going to be using the LED Matrix that come in the board and will only test on this model.

Installing the IDE

In order to compile and install our programs into our Arduino, we need to download the Arduino IDE. We can get it from the Arduino Software Page.

The installation instructions might vary depending on your OS. I use Ubuntu, so I downloaded the AppImage file.

In order to run AppImage files we need FUSE:

1
2
sudo add-apt-repository universe
sudo apt install libfuse2

Then we can just run the AppImage file:

1
2
chmod +x arduino-ide_2.2.1_Linux_64bit.AppImage
./arduino-ide_2.2.1_Linux_64bit.AppImage
Read More

Introduction to KiCad for Circuit Design

In a previous article I introduced Ngspice as a popular open source circuit simulation tool. After some more time playing with circuits, I stumbled into KiCad, a popular open source tool for designing circuits.

KiCad focuses on design of schematics (diagrams that show components and how they are connected) and PCBs. On top of this, KiCad can integrate with Ngspice to run simulations on the schematics we design, making it an all-in-one tool for circuit design.

Installation

We can get the latest version of KiCad from their downloads page. Installing it in Ubuntu, is as easy as running these commands:

1
2
3
sudo add-apt-repository ppa:kicad/kicad-7.0-releases
sudo apt update
sudo apt install kicad
Read More