RSSAmplifier

Blog

BadproG

Recent content on BadproG

badprog.comRSS feed ↗327 posts

Latest posts

Rust - Multithreading - ABBA deadlock with Arc, Mutex and OS Thread

Your program compiles, runs, prints nothing, and simply sits there forever. Welcome to the deadlock club, the most silent bug family in concurrent programming. In this tutorial we build the most classic deadlock of all, the lock ordering deadlock, also known as the ABBA deadlock. Our playground: two embedded bus devices, SPI and I2C , each protected by its own Mutex . Two threads transfer data…

Rust - CLI - Validate program arguments and handle errors with an enum and test suite

A command line program always starts with the same question: what did the user actually pass in, and can I work with that? In this tutorial we’re going to see how to handle user arguments and how to test them through unit tests and integration tests. What we need GitHub for this tutorial:…

GNU/Linux - Raspberry Pi - Configure Buildroot menuconfig for RPI 1B Rev2 and Linux kernel 6.6

So, you have a classic Raspberry Pi 1B (Rev 2) laying around, and baking a standard Raspberry Pi OS image feels too bloated? Welcome to the ultimate minimalist kitchen. Today, we are compiling a custom, lightweight embedded Linux system from scratch using Buildroot. We will configure the essentials: the Toolchain, Bootloader, Linux Kernel (6.6), a custom Device Tree, and the Root Filesystem…

GNU/Linux - Raspberry Pi - Buildroot 2024.02 minimal system on RPi 1B Rev2 with Linux kernel 6.6

Most people boot a Raspberry Pi with a pre-built image. You download it, flash it, done. But what if you want to understand what is actually running on your hardware? What if you need to control every single component of your system? This tutorial walks through building a minimal Linux system for the Raspberry Pi 1B Rev 2 from scratch using Buildroot 2024.02 LTS . By the end, you will have a…

Electronics - FreeRTOS - Scheduler, task and queue on STM32F3Discovery

When it comes to dividing the time to share tasks, a simple microcontroller sometimes needs a friend. This friend is called FreeRTOS and will help our dear STM32 to dispatch tasks as if each one was written for the CPU entirely. Let’s see this. Shopping list The Github for the current tutorial with full files and code:…

Electronics - FreeRTOS - Semaphore and mutex on STM32F3Discovery with I2C and UART

Tasks are great, queues as well. But as soon as several tasks want to use the same resource at the same time, things get messy fast. This tutorial introduces 2 synchronisation mechanisms that keep everything in order: The mutex and the binary semaphore . Let’s get started. Shopping list The Github for the current tutorial with full files and code:…

Electronics - FreeRTOS - SPI, I2C, UART with stack watermark and IWDG watchdog on STM32F3Discovery

Two sensors , two buses , six tasks , one watchdog . This tutorial makes a firmware production-ready: Strict periodic scheduling, stack size validation, and hardware-level crash recovery. Ready? Let’s go. Shopping list The Github for the current tutorial with full files and code:…

Electronics - FreeRTOS - Timer, task notification and event group on STM32F3Discovery with I2C and UART

Mutexes protect, semaphores signal. But in some situations you need a bit more: A callback that fires on its own schedule, a direct line between an ISR and a task , and a way to say “do not start until everything is ready.” This tutorial introduces 3 FreeRTOS mechanisms that round out the toolkit: software timers, task notifications , and event groups . Let’s get started.…

Electronics - STM32 - I2C communication between STM32F3Discovery and LSM303DLHC accelerometer

Every smartphone is nowadays provided with an accelerometer. In this tutorial we are going to see how to retrieve X, Y and Z values from the LSM303DLHC accelerometer on the STM32F3Discovery board. And to achieve this goal we’ll use I2C bus communication. Let’s get started. Shopping list The Github for the current tutorial with full files and code:…

Electronics - STM32 - Reading gyroscope data with SPI on STM32F3Discovery

So far we have blinked LEDs , rotated them with a timer , caught a button interrupt , and sent text over UART . Time to talk to an external chip. In this tutorial we are going to read angular velocity on X, Y, Z axes from the L3GD20 gyroscope soldered on the STM32F3Discovery board. We’ll use the SPI1 peripheral in bare metal C in order to decode a live transaction on a logic analyzer but…

Electronics - STM32 - UART printf debugging with FTDI on STM32F3Discovery

At some point, blinking LEDs are not enough to understand what your code is actually doing. You need to print things. This tutorial shows how to redirect printf() to a serial port on the STM32F3Discovery with a FTDI TTL adapter. Shopping list The Github for the current tutorial with full files and code:…

Electronics - STM32 - STM32F3Discovery LED ring rotation with TIM2 and NVIC

Our Christmas tree is almost ready. In our previous tutorial we saw how to blink all LEDs at the same time. The lights are blinking but Santa thinks it’s too static. Let’s make them rotate. Shopping list The previous tutorial for this series: electronics-stm32-blinking-all-f3discovery-leds-at-the-same-time The Github for the current tutorial with full files and code:…

Electronics - STM32 - User button as EXTI with the STM32F3Discovery board

Santa is impressed by the rotating LEDs but now he wants to control them. Let’s add a push button with the EXTI (External Interrupt). Shopping list The previous tutorials for this series: electronics-stm32-blinking-all-f3discovery-leds-at-the-same-time electronics-stm32-stm32f3discovery-led-ring-rotation-with-tim2-and-nvic The Github for the current tutorial with full files and code:…

Electronics - STM32 - Blinking all F3Discovery LEDs at the same time

Christmas is near (only 6 months), so we have to prepare our xmas tree. What a better option than using our dear STM32F3Discovery board as a Christmas lights? Let’s see this in this tutorial. (And yes, Santa Claus is watching you, so be kind.) Shopping list The Github for this tutorial with full files and code:…

ROS2 - Humble - Action in action with Python

Ever wanted to control a satellite without leaving your comfortable Linux terminal? Today is your lucky day. Welcome to this cosmic guide where we are going to build a fully automated ROS2 Action server and client in Python . Grab your coffee, open your IDE, and let’s launch this code into the stratosphere. Shopping list The Github with the full code for this tutorial:…

ROS2 - Humble - Basics with Node, Topic and Service in Python

We are going to walk through the essential building blocks of ROS2 . Rather than building a complex industrial system, we are using a battery simulator as a clear and practical example to master the three fundamental elements of robotic communication: Nodes , Topics , and Services . Let’s get started. Shopping list The GitHub of this tutorial:…

ROS2 - Humble - Starter kit

Every project has to start from somewhere. In this tutorial, we are going to see the minimal template to start any ROS 2 Humble projects with style and stability. For that goal we’re going to use GitHub Actions to automatically stress-test the Dockerfile and devcontainer.json files. Basically, we’re making sure your dev environment is always ready to roll before you even type your first node.…

ROS2 - micro-ROS - Publisher, subscriber, node, topic with STM32 and Python

While receiving data from your microcontroller is great, what about sending data in the opposite direction? First, we need to understand the difference between a publisher and a subscriber . In order to achieve this goal, we’ll need to use ROS2 topics . Each node has to send and receive data to and from a topic, that is publishing and subscribing to a topic. Shopping list Github for the…

ROS2 - micro-ROS - Using STM32 USB serial port to communicate with a Linux host via WSL 2

Connecting a microcontroller to a modern robotics ecosystem can be a daunting task, especially when working across different operating systems. In this tutorial, we will explore how to transform an STM32F3 Discovery board into a fully functional ROS 2 node with the help of micro-ROS library. More specifically, we’ll use the STM32 native USB serial port to establish real-time communication with a…

Electronics - STM32 - Setting up PlatformIO in ROS2 DevContainers

This guide provides a step-by-step workflow to bridge physical STM32 hardware into a virtualized ROS2 DevContainer . It leverages PlatformIO and usbipd to enable firmware flashing from a Docker environment. First of all Github project for this tutorial: https://github.com/badprog/badprog-electronics-stm32-setting-up-platformio-in-ros2-devcontainers Shopping list: An electronic board like the…

Rust - Crate - Grcov demo

When your projects grow complex, ensuring that every single line of code is covered by your tests becomes critical. But how do you verify such a thing efficiently? Fortunately, the Rust ecosystem offers powerful crates for this purpose. This tutorial will guide you through setting up grcov for reliable code coverage. First of all The complete source code for this tutorial is available on GitHub:…

Rust - Python binding - Setting up PyO3 crate with Maturin

When you want to make large computations, using Python may not be the best solution. By building a Python module from Rust , we get the best of both worlds: Python productivity and Rust speed. In this tutorial we’ll cover the basic integration process using PyO3 , a Rust crate, for the bindings and Maturin , a Python tool,for managing the dynamic library. First of all Github of this…

Rust - Exploration - Adding memory and performance mode to the Vec struct

The original Rust Vec struct is already a fantastic growable array, easy to use. But what about adding a mode enabling to choose between memory and performance? Furthermore it’s a good way to understand why capacity and len aren’t the same thing. Let’s see that in this tutorial. First of all You can find the source code on my Github: https://github.com/badprog/badprog_vec_plus…

PostgreSQL - Setting up - Linux Ubuntu

PostgreSQL is one of the most known and used SQL server. We’re going to see how to install it on a Linux opearting system, to be precise on Linux Ubuntu . First of all For this tutorial, our Linux distribution will be Ubuntu 22.04 : https://ubuntu.com/ Then PostgreSQL 14.5 : https://www.postgresql.org/ As a bonus we’ll use pgAdmin 4 v6.15 to manage PostgreSQL as a GUI:…

C++ - NVIDIA CUDA - Using the compute_xy and sm_xy compile options to generate an executable

To use an NVIDIA GPU (the graphic card processor) you need a CUDA driver . And if you want to compile a program for a GPU you’ll have to use the CUDA toolkit . In this tutorial we’re going to see how to compile your project in order to generate an executable able to function on all available NVIDIA GPU (past, present and future). By saying “all” we say not deprecated yet of…

C++ - Qt Framework - Mixing native OpenGL functions with Qt ones

Having the Qt Framework as GUI for OpenGL can be helpful of course but sometimes it seems easier to use the native OpenGL functions directly. This is exactly what we are going to see in this tutorial by mixing Qt and native OpenGL functions. First to all For this tutorial recipe we are going to use: 2 different pairs of shaders (vertex and fragment) 2 VAOs 2 VBOs 2 EBOs Shaders will be placed in…

C++ - Qt Framework - Using QOpenGLWidget to display a shape with lighting and normal vectors

Lighting is certainly one of the most interesting effect that we can make with OpenGL. Combined with colors and normal vectors, lighting becomes visually realistic. In this tutorial we are going to see how to draw a main shape (kind of a rectangular cube), another one inside the first one and a third representing our lighting source. We are also going to add normal vectors to these two shapes but…

C++ - OpenCV - Hello world

In a previous tutorial we set up OpenCV with Visual Studio . This time we are going to make our first Hello world OpenCV project. Let’s see that. Firs​t of all In this tutorial we’ll change the color model of our image. Choose an image with colors because we are going to change them into a grayscale model. We’ll be able to come back to the original image colors. To achieve this 2…

C++ - OpenCV - Setting up with Visual Studio

OpenCV is an interesting library aimed to computer vision. But before playing with it, it’s necessary to set up your IDE. This IDE will be Visual Studio for this tutorial. So let’s get started. First of all Let’s start by downloading the OpenCV library from the official GitHub . For this tutorial we’ll use the 4.5.3 version:…

C++ - Qt Framework - Using QOpenGLWidget to display a window for moving shapes with keyboard and mouse

In the past tutorials about Qt and OpenGL we saw how to deal with some basic tasks. Let’s see this time something a bit more advanced with the QOpenGLWidget class in order to display a scene from a window with a shape. This shape (a cube) can be rotated , translated , zoomed with the keyboard and the mouse . I added some options to change this cube into a colored or texured shape directly…

C++ - Qt Framework - Using OpenGL texture with index array

Playing with colors can be fun but what about textures ? What about something really realistic? In this OpenGL tutorial for Qt we are going to apply a texture on our dear triangles . Yes in OpenGL there are only triangles, no other kinds of shapes available but with some variations though (the famous primitives ). First of all We are going to see how to create a struct named StructVertex to handle…

C++ - Qt Framework - Using 1 OpenGL VAO and 2 VBOs with only 1 array for both position and color of each vertex

As we saw, in the last tutorial about using a VAO and VBO to handle 2 different objects on the scene with OpenGL , it’s possible to have 2 arrays of vertex: one to manage the position and one to deal with the color. This time we are going to see how to handle it with only one array. So let’s get started. First of all It’s an easy example with only 3 points on the screen and each…

C++ - Qt Framework - Using OpenGL VAO and VBO to handle 2 different objects on the scene

VAO (Vertex Array Object) and VBO (Vertex Buffer Object) were introduced to help programmers since OpenGL 3.0 . So it’s not a recent features but if you just started learning OpenGL it could be very complex to understand. Furthermore if you are learning OpenGL and want to use it with Qt together, it becomes a real nightmare. So what are these vertex objects and how to use them to manage two…

Android - API - Creating a Spinner with colors as choices

With Android we haven’t a classic ComboBox like in other frameworks but we have instead a Spinner . Actually it’s exactly the same and only the name differs. In this Android Spinner tutorial we’re going to create our own cutom Spinner by replacing classic texts with squares of color. First of all The minimum SDK for this tutorial is the API 14 (Android 4.0 or also known as…

Python 3 - PySide2 - Setting up and using Qt Designer

PySide2 is a Python API for the Qt framework . This API is made with Shiboken2 , the Python binding generator. It means that you can write your code in Python and use the Qt framework as you’d do with C++. Even building a GUI with the Qt Designer is possible. So what do we wait to start using it? First of all We need to install Python 3 on your computer. So in order to have the exact same…

C++ - Boost - Converting std::vector to Boost.Python Numpy ndarray

You have a C++ std::vector and you want to convert it to a Boost.Python Numpy ndarray . But, once the ndarray got, you want to get back to the C++ array. How to do that? Let’s see that in this Boost.Python tutorial. First of all We need to install Python 3 and Boost on your computer. So in order to have the exact same software and libraries installed in the exact same locations, I suggest to…

C++ - Boost - Using Boost.Python Numpy from_data()

If you are using Python then NumPy is quite interesting for manipulating arrays . But how do we do that with C++ and Boost.Python NumPy extension? That’s what we are going to see in this Windows tutorial with an easy example of the from_data() method. First of all We need to install Python 3 and Boost on your computer. So in order to have the exact same software and libraries installed in…

C++ - Boost - Building the Boost.Python NumPy extension as a library

If you are a scientist and interested in Python , you certainly already know the NumPy package. In this tutorial I’ll propose to explain how to install it on Windows in order to be used with the Boost.Python library. We’ll also make an Hello world example. First of all We need to install Python 3 and Boost on your computer. So in order to have the exact same software and libraries…

C++ - Boost - Using Boost.Python library on Windows to create a DLL

Communication between 2 different language isn’t so easy. It’s often possible to find libraries to help us achieve this behaviour. That’s what we are going to see in this Boost.Python tutorial for Windows . Let’s get started. First of all We need to install Python 3 and Boost on your computer. So in order to have the exact same software and libraries installed in the exact…

C++ - Boost - Setting up on Windows 10

Boost libraries are some of the famous ones in the C++ world. They contain tons of functionalities but aren’t so easy to use or even understand. First things first, let’s try to install it in on our dear Windows 10 operating system (it should work as well on Windows 11 ). And test it with Visual Studio 2017 and Visual Studio 2019 . Ready? Let’s do it in this Boost installation…

Python 3 - Installing - On Windows 10

Python 3 has become an important language nowadays. So if you haven’t installed it yet on your Windows 10 operating system, it’s time (it should work as well on Windows 11 ). We are going to setup a complete installation in order to be ready using it later. Let’s see that in this Python 3 setting up tutorial. First of all We are going to set up our computer in order to use the…

C++ - Qt Framework - Using QStyledItemDelegate for displaying a QCheckBox in a QTableView

Adding text and numbers in an QAbstractItemModel was the easy part of the Qt Model/View design. In this tutorial we are going a bit further by adding a QCheckBox directly on each item of a column. But instead of using the createEditor() method we’re going to use only the paint() one. Let’s painting. First of all For this example adding a visual QObject is generally made with the…

C++ - Windows - Creating a dynamic-link library (DLL)

Creating a .DLL is an interesting process that allows a better comprehension how a dynamic-link library works on Windows. No need to say that we’ll use Visual Studio for that. Ready to dynamic linking? Let’s link. First of all We need of course Visual Studio 2017 but feel free to use the version of your choice. We’are going to create a dynamic-link library, at first, then create…

C++ - Qt Framework - Using QAbstractItemModel with QTableView and QListView

The QAbstractItemModel is usually used with views like QTableView , QTreeView or QListView . We’re going to see how to use this class in a very easy example. To be honest nothing is easy but we’ll use the minimum elements in order to have something useful for every new project. Let’s see that in this QAbstractItemModel tutorial. First of all The Model/View design is quite different from the…

Python 3 - Generating get and set for C++

Creating get and set can be a tough task. Indeed if you have like dozen of new properties to add to new classes, it could take hours. So what about generating them in few minutes with a script? Let’s see that in this Python scripting tutorial for the C++ language. First of all Fell free to adapt the code for your own language such as Java, PHP or C# for example.

C++ - Qt Framework - Using QDataStream

The QDataStream class allows to stock data from an input and give back this data to an output. Quite interesting when you want, for example, to transfer data from a computer to another. Let’s see that in this QDataStream tutorial. First of all In this tutorial we’re going to create a custom object (BadprogDS) and transfer it to another one. It’ll involve overload stream operators such as…

UNIX & GNU/Linux - SSH - Communication between client and server

In this tutorial we are going to see how two computers can communicate with each other through the SSH protocol. Why do we need SSH keys and how to use them? We will use one computer as Server and another one as Client. First of all Ubuntu 18.04.01 (LTS): https://www.ubuntu.com/ openssh-server: directly from Ubuntu (more details in the tutorial) Before starting we need some useful tools that we…

C++ - Visual Studio IDE - Sharing project property sheets on many projects

What is really annoying is to do again and again the same task without having something new to do. Set a Visual Studio project is one of this thing that you would like to automatize. The property manager allows us to do this kind of mechanism. We, indeed, are going to see how to share configurations from many C++ projects. Let’s get started. First of all Visual Studio IDE:…

C++ - Design pattern - Command

The Command design pattern creates a list of objects on which we’re going to apply a generic method. It’s a bit unusal because an object will request a method as parameter. This makes the Command design pattern a versatile one but maybe not so easy to handle at first. Once again an interesting study case. Let’s see that in this Command design pattern tutorial. First of all GitHub…

C++ - Design pattern - Memento

The Memento design pattern allows an object to retrieve last states of another object. This is used for example in the undo mechanism. So it’s a very interesting pattern that doesn’t use any interface. But instead it uses an opaque pointer in order to store old states of the main object. Let’s see that in this Memento design pattern tutorial. First of all GitHub :…