Reflection in C++ to Generate Serializable Structs Using libclang and Python

Written by Chris Woodall on 24 Feb 2018

I have recently found myself writing a bunch of serialization (and deserialization) functions in C++ for some very basic structs, and classes. Each of these serialization functions has the same basic form which is basically a series of function calls to convert some data of some type into it’s msgpack representation. As the number of structs grows this becomes tedious and hard to upgrade. Also, there are a few points where programmer error can result in major hard to detect problems, if the ordering is wrong, or the number of fields is wrong, then all bets are off. I will be the first to admit that Google Protobufs basically solves this problem entirely by describing the transfer messages, and datatypes in its own language and then compiling the target representation.

Inspired by my recent usage of rust I decided to see if I could create the equivalent of rust’s #[derive()] syntax for applying procedural macros to a struct. You can see my experiment on github.

How I Manage My Reading List With Trello

Written by Chris Woodall on 16 Oct 2017

How I Manage My Reading List With Trello

In college I frequently would go on book reading binges during breaks, but I could not keep myself to a consistent schedule to read for pleasure during classes. This “not enough time” syndrome carried over to after I left college, even though I was not actually particularly busy outside of work. Missing reading I started to gather books I wanted to read, but was having trouble keeping track of all of them, and sorting through what to read next. So I gave some reason to the madness by using Trello. You can view my Trello Reading List and see what I have on there.

Fundamentally by maintaining a list which is so flexible and easy to manipulate I have added some tangibility to the abstract concept of “books I want to read”. I can also track and set goals related to reading rate and diversity of books since I maintain all of that data. Another plus is that it makes it easy to track what I need to write blurbs for my website. Some of my friends use other systems, including goodreads and just maintaining a literal (and literary) stack of books. I like the Trello system because it felt easier to abandon my reading past, than it did on goodreads, which made it feel like I should log every book I have read since birth. That said the friends I have who use goodreads seem to like it.

Read on for more information on how I organize my Trello reading list.

Making GIFs From Videos in Linux

Written by Chris Woodall on 23 Dec 2016

Making GIFs From Videos in Linux

I started making some .gifs to embed in documentation and my notes as I develop Rusty Nail. I always have to lookup how to convert the images to gifs. Every time I am also concerned with how big the .gifs are so after doing some looking around I made a little script which uses ffmpeg, convert, and gifsicle (with giflossy) to take an input mp4, webm, or any video that ffmpeg can decode.

The process is this:

  1. ffmpeg splits the video into a directory of gifs
  2. convert those images and stitches them together into one gif.
  3. gifsicle optimizes the gif and allow a stage to resize it and also change the color. The giflossy plug-in also can apply lossy compression to the gif video during the optimization step.

Read on for the script.

My New Bash Prompt: Exit Codes, Unicode and Git Branches

Written by Chris Woodall on 19 Dec 2016

My New Bash Prompt: Exit Codes, Unicode and Git Branches

I decided to customize my bash prompt. I initially started off with the following requirements:

  1. It should be two lines! I have found that having the input and my prompt on one line is not enough space most of the time.
  2. I want to see my current git branch, and also if there are any uncommitted changes in the current branch.
  3. I want to see my username, host name and current directory.
  4. The current directory should be truncated so that it shows the 4 closest directories in the path.
  5. The status if the previous command should be show in some way.

After some work I came up with the prompt shown above. I am excited to keep using it and customizing it, I am also happy that I decided to use unicode in the prompt. I have been avoiding unicode in general, but Rust has convinced me that I should try to accept unicode into my life more readily!

After going through the process, here are sometake aways:

  1. printf supports unicode more reliably than echo. I had some trouble getting echo to do what I want to do
  2. I always forget this one. Bash functions are never what I think they are. They are scripts you can define inside of a script. I always seem to find this causing problems for me.

I hope you enjoy! Below is the code for my prompt! Put it into you .profile, .bash_profile or .bashrc file depending on your preference. If you want to use it.

Thoughts On the Coursera Machine Learning Course

Written by Chris Woodall on 18 Dec 2016

Thoughts On the Coursera Machine Learning Course

I completed my first Coursera course, which was “Machine Learning” with Andrew Ng from Stanford University. The course was well taught and concentrates on building a solid base for applying Machine Learning to a variety of problems. Andrew Ng uses examples motivated by real-world problems to show how you might apply the algorithms taught. I highly recommend the course if you are looking to take a MOOC and have an interest in machine learning. I learnt a lot, but still need to apply the knowledge in some personal projects of mine.

Read on for more thoughts on the course and tips on how to succeed.

Installing libsvm For Use With GNU Octave In Ubuntu 14.04

Written by Chris Woodall on 19 Nov 2016

For week 7 of the Coursera Machine Learning course we learned about Support Vector Machines (SVM). SVMs are a useful and powerful tools for solving classification problems. They can be tailored to solve complicated classification boundaries, and don’t suffer from some of the down sides of optimizing neural networks. One of the example problems uses an SVM to classify spam emails. A SVM implementation written in MATLAB/Octave is used, but for further work libsvm (or another SVM library) is recommended. To get libsvm working with Octave in Ubuntu 14.04 there are a few steps that are not obvious. I did not find very many instructions so I compiled the steps I followed to get a stable Octave environment with libsvm. Read on for details.

Using Z3 to Find Clock Configurations Values for ARM Cortex Microcontrollers

Written by Chris Woodall on 06 Nov 2016

A few weeks ago I was setting up multiple embedded systems using the NXP Kinetis K64F12 family of microcontrollers. Do to an oversight a few of the different systems had different input clock frequencies, but all were intended to work at the same output clock frequency. This is not a major issue and the ARM Cortex-M4 processors are designed to solve this issue using a phase-locked loop (PLL). A PLL can take a clock, of one frequency and output a rather clean clock of another frequency; however, it must be properly configured. My problem now was a configuration, and mathematics problem. The parameters are calculated using the following equation:

, where and are the parameters needed for the configuration. and and are your input and output frequency which you know ahead of time.

A solution to this equation is straightforward but requires a little bit of guessing, and also checking your answers against a series of constraints specified in the datasheet. The details of this equation and its related constraints will be approached later. I wanted to automate this task so in the future I could just adjust these values without having to dig out the datasheet and do the math again. Also to possibly automate making the clock configuration code to prevent me from being the only person who understood how to configure the clocks.

What is someone who loves automation to do? Well, you could solve the equation directly. However, Michael Abed introduced me to a program and library called Z3 which is a constraints solver from Microsoft Research which I found interesting and wanted to do a simple project with since I thought it might be useful in the future. Also, this problem is well-suited as it can take a series of equations and constraints and search the solution space rather trivially easily. Also, it has a way of deciding if no solution is possible. So I decided to use the Z3 python bindings to create a python library, with a command line tool and a web API for solving this problem. This is most certainly overkill, but considering how easily it all went together I would do it again to solve similar problems.

You can see the code on GitHub and also play with see the resulting website and API demo. But read on for more details!

Error Handling: Implementing an Option<T> Type For Embedded C++

Written by Chris Woodall on 10 Oct 2016

I have been learning Rust lately, which has been an awesome and rewarding experience thus far. However, at work, and on many of my embedded side projects I use C/C++ quite extensively. But error handling ends up being a major pain point and can make an otherwise rather simple API into a disaster. This can be especially true if you are committed to avoiding throwing exceptions for various performance and real-time reliability reasons. This is where Rust’s model comes in as a possibly solution (or at the very least an interesting one): the use of the Result<T, E> types and the Option<T> type. For the time being I am going to concentrate on the Option<T> type and write about the Result<T, E> type in a future post.

The code for this blog post can be found on github as cpp-option!

Attaching a Rack To Your Bike with P-Clamps

Written by Chris Woodall on 07 Jul 2013

Attaching a Rack To Your Bike with P-Clamps

A nice and simple hack for attaching things onto bikes without dropping a ton of money. Before you buy your P-Clamps, or Rubber Coated Metal Clamps, make sure to measure the diameter of the tube on your bike you want to attach the clamp to. You can get them from Grainger and McMaster-Carr, but they are also easily found in your local hardware store.