C++17 - Draw a Valentine's Day heart shape
Posted on February 14, 2019 by Paul
What could be more romantic than to generate a heart shape with C++17 on Valentine’s Day ?
In order to generate the above shape we can use the parametric form of the outer curve:
C++17 Filesystem - Writing a simple file watcher
Posted on January 13, 2019 by Paul
Updated 24 September 2019
In this article I will show you how to use the C++17 std::filesystem library to write a simple file watcher or file monitor. The advantage of using the C++17 std::filesystem library is that your code will be portable on all operating systems for which a C++17 compiler is available.
We are going to implement a C++17 file watcher that will monitor a given folder for file changes. For our limited purposes, we’ll monitor only the creation, modification and deletion of all files from the watched directory. The base folder will be checked for changes at regular time intervals and, in case of changes, we’ll run a user defined function.
Raspberry Pi - Using C++ to control a group of LEDs
Posted on December 23, 2018 by Paul
In this article, I will show you how to use C++ to control a group of LEDs connected to a Raspberry Pi. You can see the final results in this short video:
Cross compiling OpenCV 4 for Raspberry Pi and BeagleBone Black
Posted on December 18, 2018 by Paul
Updated 7 August 2019
If you want to cross compile OpenCV for Raspberry Pi Zero please check this article.
In this article, I will show you how to cross compile the latest version of OpenCV for Raspberry Pi and BeagleBone Black. At the time of this writing, OpenCV is at version 4.1.0 while the version available with the default Debian based OS on Raspberry Pi and BeagleBone Black is 3.2.0. Building OpenCV 4 directly on a Raspberry Pi or BeagleBone Black is doable, but it could take a few hours. Cross compiling OpenCV for armhf is a 20 - 30 minutes process, depending on the speed of your computer it could be even shorter.
I recommend that you do the build in a Debian Buster virtual machine or a Docker container in order to not mess your system. If you decide to install Buster in a virtual machine, make sure to use the minimal netinst system. It is really important that you start with a bare bone system, because we need to install armhf executables and libraries. By using a minimal system we avoid potential conflicts with the native x86-64 versions.
For the remaining of this article, I will mention only Raspbian and Raspberry Pi, if you are using a BeagleBone Black everything should work the same.
I’ll assume that you have a clean Raspbian install. I’ve used the latest available desktop image. In principle you should be able to follow the article using Raspbian Lite, but I did all my tests using the full desktop version of Rasbian.
Raspberry Pi Zero Internet connection through USB
Posted on December 7, 2018 by Paul
This is a short article about sharing your Internet connection through USB with a Raspberry Pi Zero. The procedure presented here will work with all versions of Raspberry Pi Zero (with or without onboard WiFi). You will need a Raspberry Pi Zero, a micro SD card and a USB to micro USB data cable.
Start by downloading the latest Raspbian image, I’ve used Raspbian Stretch Lite. Once the download is finished, extract the archive and flash it to your micro SD card. The Etcher flasher app works on macOS, Windows and Linux.
BeagleBone Black sharing your macOS Internet connection through USB
Posted on December 4, 2018 by Paul
This is a short article about sharing your Internet connection with a BeagleBone Black through USB on macOS Mojave. I assume that you’ve gone through the Getting Started setup and that you’ve installed the latest Debian image on your BBB.
Connect the BBB to your Mac through USB and start the device. Once the device is on, open a Terminal and use ssh to connect to your BBB:
C++ reading and writing BMP images
Posted on November 19, 2018 by Paul
In this article, I will show you how to implement a BMP image loader from scratch in C++. BMP is one of the oldest image formats on the Windows platform and it is supported on most other operating systems. BMP can store two-dimensional raster images with optional compression and transparency. In this article, we will implement a simplified version of the BMP format specification that will support only 24 and 32 bits depth images in the BGR and BGRA color spaces. To make our life simpler we can also ignore the, optional, compression component.
Even if you don’t plan to use BMP images, it is still a useful programming exercise to write a BMP reader/writer in C++.
From a programming point of view, a BMP file is a binary file in the little-endian format. For our purposes, we can divide a BMP image in four regions:
- file header - all BMP images starts with a five elements file header. This has information about the file type, file size and location of the pixel data.
- bitmap header - also named the info header. This has information about the width/height of the image, bits depth and so on.
- color header - contains informations about the color space and bit masks
- pixel data.
Installing Java OpenJDK on macOS Catalina
Posted on September 28, 2018 by Paul
Updated 15 October 2019
This is a short note about getting started with Java 11 LTS or Java 13 on macOS Catalina. As you probably know, starting with Java 11 there was a big change in the license under which the official Oracle JDK is provided. In short, you need to buy a license from Oracle if you want to use the official JDK in a commercial setting. As far as I know, using Oracle’s JDK on your private computer for testing and learning purposes is allowed.
That being said, for most users OpenJDK is the new JDK of choice, it is provided under an open source license and you don’t need to pay for using it.
There is also a video version of this tutorial:
C++17 constexpr generation of a FizzBuzz solution
Posted on September 24, 2018 by Paul
The famous FizzBuzz interview question asks to print all numbers from 1 to 100. If a number is divisible with 3 print fizz, if the number is divisible with 5 print buzz and if the number is divisible with both 3 and 5 print fizzbuzz. Writing a program for the above problem is trivial. A more interesting problem is to generate the solution for the FizzBuzz problem at compile time.
In C++ we use the constexpr specifier for a variable or a function that needs to be evaluated at compile time.
Install Ruby 2.5 on macOS, Windows 10 and Ubuntu 18.04
Posted on September 22, 2018 by Paul
In this article I will show you how to install the latest stable version of Ruby, which is 2.5.1 at the time of this writing, on macOS, Windows 10 and Ubuntu 18.04.
- Ruby 2.5 on macOS
- Ruby 2.4 on Windows 10 native install
- Ruby 2.5 on Windows 10 with WSL
- Ruby 2.5 on Ubuntu 18.04
How to create a .NET Core F# application and generate executables for multiple operating systems
Posted on August 13, 2018 by Paul
In this article I will show you how to create a simple F# console application that runs on .NET Core and how to generate executables for various operating systems.
Start by downloading the .NET Core SDK for your operating system from https://www.microsoft.com/net/download. Run the installer and accept the default settings.
Compiling Boost with GCC or Clang on macOS
Posted on August 7, 2018 by Paul
In this article I will show you how to build the Boost libraries under macOS with GCC 8 or Clang. Once the libraries are installed, we’ll test the build with a short demo of using Boost Filesystem.
First, you will need to download the latest stable version of Boost, I will use version 1.68.0. Extract the archive and open a Terminal in the Boost folder.
Building GCC as a cross compiler for Raspberry Pi
Posted on May 6, 2018 by Paul
Updated 10 May 2020
In this article, I will show you how to build GCC 10 as a cross compiler for Raspberry Pi. A cross compiler is a compiler that runs on an operating system and produces executables for another. This is really useful when you want to use your beefy computer to build a library or other large piece of code for Raspberry Pi. As a practical example, at the end of the article, I will show you how to use the cross compiler to build GCC itself as a native Raspberry Pi application.
An advantage of the approach presented in this article is that you can use the cross compiler to build RPi Zero and up executables. With the official crossbuild-essential-armhf from Debian you can build only Raspberry Pi 2 and up binaries.
Part of this article is a compilation of what I’ve learned reading other people posts. Here is a list of the sources I’ve used:
- http://preshing.com/20141119/how-to-build-a-gcc-cross-compiler/
- https://www.raspberrypi.org/documentation/linux/kernel/building.md
- https://wiki.osdev.org/Why_do_I_need_a_Cross_Compiler%3F
- https://wiki.osdev.org/GCC_Cross-Compiler
- https://wiki.osdev.org/Building_GCC
- http://www.ifp.illinois.edu/~nakazato/tips/xgcc.html
From the above list, the first article is the one that is the most complete and, if you follow it, you end up with a cross compiler that partially works. To be fair, the article wasn’t written for Raspberry Pi. I recommend that you read it if you want to see a more in depth explanation of certain steps of the process.
Install NumPy, SciPy, Matplotlib and OpenCV for Python 3 on Ubuntu 18.04
Posted on April 26, 2018 by Paul
Updated 26 June 2019
This is a short article about installing NumPy, SciPy, Matplotlib and OpenCV on the latest Ubuntu LTS, which at the time of this writing is 18.04. Ubuntu 18.04 comes with Python 3.6.8, but I will show you how to install Python 3.7.3, which at this time it is the latest stable version of Python.
Let’s start by making sure we have an updated system:
In the first part of this article, I will show you how to install Python 3.7 and how to create a virtual environment in which we’ll install NumPy, SciPy and Matplotlib. In the second part of the article, I will show you how to, optionally, install OpenCV 4.
There is also a video version for the first part of the tutorial:
Raspberry Pi - Install Clang 9 and compile C++17 and C++20 programs
Posted on April 22, 2018 by Paul
Updated 24 September 2019
In this article I will show you how to install Clang 9 on your Raspberry Pi system and how to compile C++17 programs. At the time of this writing Raspbian is based on Debian Buster, which comes with the stable but slightly outdated GCC 8.3 as the default C and C++ compiler. If you prefer to use GCC 9 I wrote an article about installing GCC 9 on Raspberry Pi.
There is also a video version of this tutorial:
Let’s start the installation process. Open a Terminal and download the official binary of Clang 9 for Raspberry Pi (works only for RPi2 and up, doesn’t work for RPi Zero):