Creating a library: Using classes or not?


I’m currently writing a library for a device I designed, running the Arduino Micro bootloader. The device is stand-alone (so not intended to connect other hardware to), and intended for use in classroom environments to help students learn how to program.

The device contains some advanced hardware, for which I’m writing the library. Although it is my first time writing a library, most things are clear to me and everything is working so far.

One thing however I’m not sure about. Should I write the library using classes or not? As far as I understand, classes are useful when you might have, for example, two servo motors connected. This way you can create an object for each servo. But in my case, all hardware for which I want to create functions is present only one time and the device is not designed to be expanded with extra hardware.

I want the library to be as user friendly as possible as it is intended for students with little to none programming experience, and it looks weird to me that you would have to create an object if all the code in the sketch is only intended for that object. I think it’s easier for the student to use this

function(param1, param2);

than this

mydevice.function(param1, param2);

Or am I misunderstanding some things here? Does using classes has other advantages that I’m not aware of?

All feedback is welcome!

No problem, hopefully someone does :slight_smile:

I hope so to.:slight_smile:

I will try tomorrow.

That is obviously subjective. Do you have evidence to back your opinion?

Namespace. A class keeps all of your library’s stuff out of the global namespace. A duplicate symbol error during linking will definitely be a burden to inexperienced programmers.

No real evidence, but that’s my feeling as a teacher.

My function naming is quite specific, but that could be a useful advantage indeed.

How many Arduino users have struggled with Serial.available when Serial_available is a reasonable alternative?

Hint: Even a five year old can be taught the purpose of the shift key.

True, you convinced me of using classes :slight_smile:
But I now have a question about the constructor. I understand this is a little like the “setup” section when creating an instance of the class and can be used to specify pin connections, but I was wondering if I should use it for other initialization as well.
See my most recent post about this here

Thanks for the info!

If I were in a class learning C++, I would want to learn about using classes.
Sure you can make a function into a library file with practically no new code, but is this something a programmer would see in the real world?

Learn to do it the hard way today. Everything else will be easy. Prepare the student for the real world so that when they need to write a library for hardware they will have some experience.

Since 99% of embedded development is in C… Yes.

@UKHeliBob and @pert nailed it. Include an initialization method. begin is a great choice for the name. Document that begin has to be called; ideally in setup.

You can also look at this thread for creating a library. Post #5 from @gfvalvo describes the standard model for composing the .cpp and .h files.

Thanks for your responses everyone! @6v6gt, I’ll check that thread out tomorrow. I just read it very quickly but I might have some questions about it if you don’t mind :slight_smile:

Check out my tutorial on How to Write Your Own Arduino Libraries which goes through step by step, splitting out your methods into separate files and then creating a class if you need to.
You really don’t need a class unless
a) you are going to have more than one instance of the methods
b) you what a general re-usable library for other project.

1 Like

Hi @drmpf, thanks for the link, I’ll look into it for sure!

That’s what I thought. For my project it’s useless to create more than one instance of the class, because that one instance will cover all present hardware. And the library is intended specifically for this project alone, to make controlling some of the more advanced hardware a little easier for the students.

That said, the “namespace” thing that Coding_Badly mentioned is an advantage, and it might be good practice for the students to learn early on how to use component libraries using classes. That way they will recognize the structure when they use, for example, the “servo” library for the first time.

Feel free to tell your view on this!

While I can see the necessity of namespaces in some instances (like the clash between mbed / Arduino Stream), as an Arduino library writer I find their use in some board packages a nuisance as I have to add extra ifdefs to include/exclude the namespace in my libraries to suit the various different usages in different board packages.

Most third party libraries, including mine, don’t use them.

I’m not going to get too deeply involved in a debate about the relative merits of classes and namespaces but I do tend to use namespaces. In coding terms, and when anyway only one instance of a class would exist, the difference is minimal. However, the end user of a namespace is exposed to the scope resolution operator ‘::’ which may take some explaining.

I’d simply say this. If you can reduce your code to a single function, with its global footprint reduced to the minimum (make use of static state variables to help achieve this), then design it a single function and let the students simply copy it into their own programs.

If if is more complicated and requires multiple functions (methods), then use a class model.
Since only one instance of the class will be used in this case, you can instantiate it already in the .cpp file. A class can also exist in the same directory as the sketch. No special loading into the Arduino library directory is necessary.

I could imagine that the students will, anyway, have to get used to handling classes if they want to start using peripherals or modules, even say an LCD display.

My suggestion is not to use C++ namespaces but to use a C++ class to provide the same features afforded to using a namespace.

I apologize for the confusion.

I am approaching my answer above as a student. I’ve been writing code for my Arduinos for three years, and learning C++ the hard way. In all honesty, I don’t know the difference from C++ namespaces and C++ classes. As a student, I would like to be introduced to these concepts early and as simply as possible.

So, if you have a function to turn on and off an LED, teach the different ways it can be represented. A simple function or as a library. This way when the student encounters a more complex library it won’t be complete gibberish to them. As it is to me.

1 Like

I would like to add a link from Sparkfun:

additionally: I find it very common to make a project with one LCD , one temperature sensor, one pressure sensor … all single objects of separate classes. So why not to go OOP and use classes to encapsulate data and methods even if you think that only one instance will be needed in future?