I've been working on a game engine for a console I've built with a microcontroller for a little while now. However, I'm curious about how Arduino libraries treat multiple classes within a single library, and if that is even possible. So far I've not been able to find any information on this topic and was wondering if anyone might know where I could find any information on it. Even an example of a library that has multiple classes would be helpful. Thanks in advance!
The hd44780 library for character LCDs like the 1602 and 2004 has multiple classes to cover the use of different interface techniques. The library is available via the IDE library manager.
Why do you think that the number of classes might be restricted somehow? C++ makes no such restrictions nor does Arduino.
There are many ways to do things from multiple classes, classes that inherit other classes, virtual functions, or templated classes that that can automatically morph a compile time depending on parameters.
If you provide more information about what you want to do, people could better help you.
If you want to see a library that provides multiple classes within a single Arduino library Adafruit does this in several of their libraries.
You can look at their LED backpack library for one example.
It has multiple classes including inheritance from other classes and even inheritance from classes in other libraries like their GFX library.
--- bill
Composition vs inheritance will give you an overview on two possibilities.
it depends what you want achieve and if and how your classes are interacting with each other.
For example I take my LED/Button/Timer Toolkit
the zip contains ONE library.
there are several classes in the three .h files
Noiasca_button.h contains one class to debounce a button. Nothing special. Just a simple button.
Noiasca_timer.h contains two classes for timers. These are two different variants of timers with different API and different implemtation and share no implementation. Just two separate classes.
Noiasca_led.h has several classes to drive LEDs with different effects.
To interact with hardware a class DiscretePin for Arduino Pins was written.
The effects (i.e. BlinkLed) inherit from a base class LedBase as most of them share common functions (i.e. each effect can be switched on or off).
The effects not only inherit from the base class but also use a hardware implementation, therefore you handover the hardware level to the constructor.
The wrappers at the end of the file just make an easier interface for the user in a more Arduino like style hiding the templates.
Noiasca_neopixel.h in the folder utility is a hardware implementation where you handover a Neopixel strip and a particular pixel on that strip to be used "like an Arduino Pin" for the LED effects. This class takes usage of the external Adafruit_Neopixel. If one would write a class for a portexpander, the LED effects could be imidiatly be reused for this hardware also.
Hope this gives you some first ideas what you can do.
Thanks for the example! This was what I was trying to find.
Well, that's the point. I couldn't find any information on whether it was restricted or not. I know that C++ doesn't have any restrictions, but I also couldn't find any examples of libraries working like that.
Thanks for the tip! Yes, I want to include multiple different classes within a single library. The LED backpack library was a really good example.
Thanks for the example! It's pretty much what I was planning to do. I have multiple different header files, and while some of them use the other classes, I don't plan on using inheritance.
do you plan to publish your library?
if yes I would like to point out:
https://docs.arduino.cc/hacking/software/LibraryTutorial
https://docs.arduino.cc/hacking/software/ArduinoStyleGuide
and a Tutorial from Sparkfun:
https://www.sparkfun.com/news/3245
I do plan on publishing it, hence why I was I wanted to be sure my plan was possible in the first place. Thanks for the resources about that.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.