Arduino - pulse an LED with a cosine wave function
Posted on January 21, 2017 by Paul
In this short article, I will show you how to pulse an LED with Arduino using a cosine function. You can see the final result in the next short video:
C99/C11 dynamic array that mimics C++'s std::vector - API improvements
Posted on January 8, 2017 by Paul
In my last article I wrote a portable C dynamic array, auto-resizable and theoretically capable to store any C representable data type. While the original implementation works, it is a bit cumbersome to use in practice. A better approach is to create a dynamic array that looks like a normal C array, but it is capable to automatically adjust its size as needed. I took from Sean Barrett’s stb libraries the idea that you can store the size information of an array at the beginning of the array, while publicly exposing a pointer to where the array data resides in memory. The advantage of this approach is that from a user point of view a dynamic array can be used like a C array, except for the creation and destruction phases.
It goes without saying that this article is written for C programmers or people that use a C compiler. If you are using C++, you usually don’t need to implement a vector type, you already have one in the STL, use it.
It will be easier to understand how the general case works if we start with an explicit implementation for a dynamic array of integers. Conceptually, our code has three phases
C99/C11 dynamic array that mimics C++'s std::vector
Posted on January 6, 2017 by Paul
Recently, I’ve worked on a large C library used to store items from a database. The idea was to get all records that matched a particular criteria and store these in a C data structure for further processing. The original author of the library used a linked list to store the records which probably was a sensible idea 20 years ago. However, today’s computers perform better if you use a cache friendly data structure like an array. Basically, I wanted a dynamic array that automatically increases his size if needed, something like C++'s std::vector.
UPDATE 8 January 2017
Part 2 of this article presents a better, more flexible, implementation for a C dynamic array. Feel free to skip the remaining of this article and read the second part.
C++ - Passing a C-style array with size information to a function
Posted on November 28, 2016 by Paul
When you pass a C-style array to a function it will decay to a pointer to the first element of the array, basically losing the size information.
As a side note, in a pure C++ code, one will prefer to use std::vector or std::array instead of C-style arrays. However, there are cases like in embedded systems in which using the Standard Template Library is forbidden by your company guide rules or simply not available on the platform. This is why I think the approach presented in this article could be of some interest to C++ programmers.
In C, you will need to pass the size of the array as an extra parameter to the function or use a global variable. A potential problem with this approach is that it is easy to screw up by forgetting the array to pointer decay rule or by passing the wrong size information.
Here is an example of a classical bug when one forgets about the array to pointer decay rule:
Swift 3 and OpenGL on Linux and macOS with GLFW
Posted on November 19, 2016 by Paul
This is a short article about how to get started with Swift 3 and OpenGL on Linux and macOS. In order to keep the code portable on both operating systems, we’ll use GLFW to create a window with an OpenGL context. Keep in mind that, at time of this writing, Swift can call directly any C library. However, if you need to use a C++ library you will need to write a C wrapper around it, because Swift 3 is not interoperable with C++.
As a practical example, we’ll use OpenGL to draw a yellow rectangle on a red background. In order to keep the code short and as simple as possible, we’ll use the fixed OpenGL functionality. If you want to use modern OpenGL, please check my other OpenGL articles. Replacing the fixed OpenGL functionality from the article example is left as an exercise for the reader. Basically, once you know how to call OpenGL functions from Swift and compile the code, it is just a question of writing supporting code for dealing with shaders, buffers and so on.
Getting started with Swift 3 on Linux
Posted on November 8, 2016 by Paul
In this article, I will show you how to install and use Swift 3 on Linux. Swift is officially tested and supported on Ubuntu Linux. I will assume that you have Ubuntu 16.04 installed on your server or on your local computer.
First, make sure that you have an up to date system:
Next, install Clang which is required if you want to be able to use the Swift compiler:
Building GCC 10 on Ubuntu Linux
Posted on October 7, 2016 by Paul
Updated 8 May 2020
This is a short article about compiling GCC 10.1 from sources on Ubuntu 20.04 64 bits. The default version of GCC on Ubuntu 20.04 is 9.3 which is not bad, however version 10 has complete C++11, C++14, C++17 support and partial C++20 support. GCC 10 has C11 and C++14 support enabled by default, no need to add -std=c11 or -std=c++14.
First, let’s make sure that we have an up to date system:
Linux - Optimize your SSH login workflow
Posted on October 6, 2016 by Paul
This is a short article, triggered by an email question, on how to optimize your SSH login on Linux, specifically on a Raspberry Pi machine.
Many people use Linux as a headless server (no display and keyboard attached). However, from time to you need to connect to the server and do some changes or update the system. Typically, you will use SSH for this. If your local machine runs macOS or Linux this is done by writing in a Terminal:
If you are on Windows, you can get a complete SSH client and the Bash shell by installing Git.
Writing ssh user_name@your_remote_ip_address and your password doesn’t seem like a big deal when you have a single remote server. However, once you have more than one server this becomes a tedious process.
A recommended practice is to create a SSH key pair, basically two text files, one public and one private. The private key stays on your local computer, while the public key needs to be copied on the remote machine. The advantage is that you could use the same public key on more than one remote machine.
Install Python with NumPy, SciPy and Matplotlib on macOS Big Sur (Intel version)
Posted on October 4, 2016 by Paul
Updated 15 June 2021
In this article, I will show you how to install Python with NumPy, SciPy and Matplotlib on macOS Big Sur.
I assume you are on an Intel based Mac. If you have an arm64 Mac, also called Apple Silicon, please check my other article.
MacOS Big Sur comes by default with Python 2.7 which, at this point, receives only bug fixes and is EOL since 2020. Python 3 is the future and it is supported by all major Python libraries. In this tutorial, we’ll use Python 3.9 which is the latest stable release of Python at the time of this writing.
Start by installing the Command Line Tools for macOS. Please note, that you will need the Command Line Tools even if you’ve already installed Xcode. Open a Terminal and write:
Once the Command Line Tools are installed, we can install Python.
Raspberry Pi Raspbian - Building and Installing Vim 8.0
Posted on September 24, 2016 by Paul
About two weeks ago Vim 8 was released with some notable changes. While I’m not a Vim fanatic, I tend to use it for quick edits on my Raspberry Pi over the network. Currently, Raspbian Jessie comes with the venerable Vim 7.4 and it will be a while until this will be replaced with version 8.
Fortunately, compiling Vim from sources on Raspbian is pretty straightforward.
Install OpenCV 4 with Python 3 on Windows
Posted on September 17, 2016 by Paul
Updated 26 January 2020
If you need a short tutorial about how to get started with OpenCV 4 programming in Python 3.8 on Windows, you are in the right place. Most articles I found online, including the OpenCV documentation, seem concerned only with Python 2.7.
We’ll start by installing the latest stable version of Python 3, which at the time of this writing is 3.8. Head over to https://www.python.org/downloads/ and download the installer. The default Python Windows installer is 32 bits and this is what I will use in this article. If you need the 64 bits version of Python, check the Looking for a specific release? section from the above page.
How to share a compiled iOS app ipa with your users for testing using Dropbox or your website
Posted on May 14, 2016 by Paul
In this post I will show you a simple way in which you can share your iOS applications wirelessly with your friends or users for testing purposes. The procedure requires that you have an iOS device and an Apple developer account. You will also need a secure (https) personal website or a Dropbox account. If you can test your app on your device, you can share it with other people.
Please note that this approach does not require any kind of jailbreaking and as far as I can tell, it is accepted by Apple. Alternatively, you can use TestFlight to invite people to test your beta application. Personally, I found the approach presented in this article easier to setup and use.
How to upgrade Node.js on Mac OS X
Posted on April 29, 2016 by Paul
If you already have Node.js installed on your Mac you will need to upgrade it when a new version comes up. There are basically two ways to achieve this goal download the installer from Node.js or use the command line to upgrade Node.js.
A simple way to upgrade Node.js from the Terminal is to use the n version manager:
OpenCV video editing tutorial
Posted on June 4, 2015 by Paul
The code for this post is on GitHub: https://github.com/sol-prog/opencv-video-editing.
I’ve always knew that OpenCV can be used to do some video editing, however when I’ve actually tried to use it to open a video file, a few weeks ago, I was amazed at the quantity of misleading or incomplete tutorials you find on the web. I wrote this tutorial to save some time for others like me that need to do some quick and dirty video editing with C++ and OpenCV.
Simplest thing that you could try is to read frames from your computer’s webcam. OpenCV uses the same function, VideoCapture, for opening a video file or a camera attached to your computer, the only difference is that for a camera you will feed the function a number, while for an actual video file you will use the video path.
OpenMP on OS X
Posted on May 14, 2015 by Paul
At the time of this writing Clang, the default C++ compiler on OS X, doesn’t have an OpenMP implementation. GCC however supports OpenMP 4, but you need to install it yourself if you want to use it. GCC 5 can be built from sources for OS X if you want to have the latest and greatest or, you can install it with Homebrew.
OpenMP ads threading support to a C or C++ code through pragmas, this has the advantage that you can take a serial code and parallelize it with minimal code modifications.
As a side node, starting with C++11, you can directly use the thread header if you want to explicitly parallelize your code and this approach is supported by Clang.