Class for general use - has to be a library?

I've written a fairly complex class that I want to use for multiple sketches. I don't want to make multiple copies of the source files for each sketch for version control reasons. Is the only way to do this to make my class into a library? I was under the impression that libraries are usually small and serve some simple function, so it didn't seem appropriate.

That is a mistaken impression. It sounds like a library is exactly what you need.

Best way is to place your .h and .cpp in the sketch folder. Oh wait, you said multiple sketches... then in a library folder.

lol ok great! Thanks.

Yes, but as I said, I want to use the class in multiple sketches and don't want to maintain multiple copies of the code. Version control and whatnot.

Oops yeah. Missed that. But sometimes it's handy to keep it with the sketch for development and testing... after that it's super easy to move it to a library folder.

1 Like

Ya, that's probably what I'll do. I usually have an .ino file in the library folder that I can use to test and develop the class code.

Hmmm, never tried that. Amazed that it works... when I open an example sketch and try to save it, I get a message telling me it has to create a new project folder in the main sketch folder.

1 Like

That is diametrically opposite to what I have usually done. I put the library files under development/test in the same folder as the test sketch and copy it to its library folder when I am happy with it

1 Like

it is not true... look to FastLed library :slight_smile:

1 Like

I'm not sure what you mean by an example sketch. As long as sketch has the same filename as the folder, the IDE seems to like it. So, for instance, if I have a library called KnobbyThing, it's under /libraries/KnobbyThing/, and I have KnobbyThing.cpp, KnobbyThing.h and KnobbyThing.ino. I can use the .ino to test the class, and I can also use the class in other sketches.

1 Like

I shall, thank you.

Almost all libraries ship with examples, in the /examples subdirectory. Sometimes they are not much more than test programs for the library, but usually they are more diverse and full featured to demonstrate the usage and capabilities of the library. Also templates for user experimentation.

The IDE also ships with built in examples.

1 Like

I see.
Re: your last comment, if you just copy/paste an example sketch to a new folder and rename it to the same name as the folder, it should work.

If I have a library named Goopy and want a testbed ino to refine it, I just mage Goopy.ini and place it in the same folder as the library. knowatimean?

That's not really diametrically opposed. I do the same thing - get it substantially working with a sketch, and then move it to a library folder. However, there often arises the need to make tweaks. I'll often have an ino file in the library folder to repeat basic testing to make sure I haven't accidentally broken anything.

What I meant, it forces you to do that if you attempt to compile, because in that process, it tries to save the file:

https://arduino.github.io/arduino-cli/0.31/library-specification/#development-flag-file

Development flag file
Normally the Arduino IDE treats the contents of the library folder as read-only. This is to prevent users from accidentally modifying example sketches. During the library development process you may want to edit example sketches in place using the Arduino IDE. With Arduino IDE 1.6.6 and newer, the read-only behavior can be disabled by adding a file named .development to the root of the library folder. A library.properties file must also be present. The Library Manager indexer will not pick up releases that contain a .development file so be sure not to push this file to your remote repository.

I must be missing something. I've never seen that behavior. Maybe because I'm not working from existing example sketched?

Strange, I've never seen this problem before.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.