Creating Arduino Child Libraries From Multiple Parents


Have you created various libraries for the Arduino to interface with different pieces of hardware?  Have you ever bought an Arduino shield that had multiple hardware interfaces but it didn’t come with a proper library, and instead maybe a jumbled mess of code in one big sketch file?  Writing a self-standing library (see also base class or parent class) for the Arduino isn’t the hardest thing, but when you progress into multi-level class structures with class dependency and inheritance, things get a little more complicated, but the end result hopefully is a well organized library with structured data encapsulation and a more simple model for future enhancements and maintenance.  As most of my embedded experience lies within the traditional bounds of the ANSI C language however, I find myself having to do a fair amount of searching from time to time on how to implement the features I want using the C++ language.

My latest task found me wanting to create a library that encapsulated the functionality of multiple libraries that already existed into one library interface.  This is referred to as a parent/child relationship, or a base class and a derived class.  The parent is the base class and it’s features can be inherited by the child class.  The child class is also called the derived class, as it’s functionality and features are derived from the base class.  I’m not going to get into access modifiers, such as public, protected, private, etc.  In my case, however, I was wanting to have one child class, a library interface to a common Arduino shield, which was derived from multiple parents, or base classes.  

Most of my base classes were already written, and one had many methods.  I wanted to keep things very simple to start out and sort of test the waters with the architecture I had in mind.  So instead of fiddling around with my good base classes, I set out to create a shell of 3 parent classes that each had very few, but essential and mostly identical methods, ParentA, ParentB and ParentC.  I also created a shell of the child class also containing very few, but essential methods.  In each of the constructors, destructors and methods, I placed a simple call to the Arduino’s Serial.println() function to print a statement to the serial terminal indicating when that x-tor or method was called.  

I fumbled a little figuring out how to access the initial instantiated objects of the parent classes from within the child class without dynamically allocating a new object and assigning a pointer during run time.  The simple answer, which I found via an educated guess based on a compiler error, was to use the “::”, or double colon, operator.  

Now, I am able write a wrapper that incorporates the functionality of existing libraries all through one library with one instantiation in my Arduino sketch.  The shell libraries I created have comments throughout to explain and can be downloaded from my GitHub repository here. I hope these will provide a good starting point for building a child class library from multiple parent class libraries, in case your C++ skills are a tad rusty.  I also hope to finalize and release the actual Arduino library that inspired this effort in the near future.  Until then, stay tuned and thanks for reading.

Leave a comment

Your email address will not be published. Required fields are marked *