PlatformIO Community

Create a "OS-Library"

Hello there,

i want to create a small Library which is used for my projects which require reoccurent tasks, like connecting to a WiFi, registering to a MQTT Broker etc. Therefore i wanted to create a Library which wraps around Arduino and provides some basic tasks. Additional code should be embedded via special functions. An example code could look like this:
#include “TinyOS.h”

void os_setup() {
   // Additional setup code

void os_loop() {
  // Additional loop code

Like Arduino uses the main() function and calls own setup() and loop() functions i want my “OS Lib” to call os_setup() and os_loop() functions.

My lib is included to an example project via lib_deps=TinyOS and lib_extra_dirs=C:\path\to\lib

The TinyOS.h contains following code:

#ifndef TinyOS_h
void os_setup(void);
void os_loop(void);

The code TinyOS.cpp contains following code:

#include "TinyOS.h"
void setup() {
  // TBD...
void loop() {
  // TBD...

How can I add the dependency to Arduino in my lib?
Is my approach correct or am I totally wrong?

Any kind of help is appreciated :slight_smile:

How exactly do you mean ‘dependency’. If you want to use Arduino functions, #include <Arduino.h> will suffice. When your library code is compiled which contains the setup() and loop() functions (while the user’s code don’t implement these funcitions), they will be linked and called by Arduino itself. That already looks okay.

If you are talking about creating a library.json description file for the library, then the field frameworks should be set to arduino to indicate that this library is only compatible with the Arduino framework. See documentation.

Thank you for your answer!

I wasn’t sure if my concept works, because I got some compile errors. But their origin was different, with your help I got it working now!! :slight_smile:

When I included <Arduino.h> in my lib, VisualStudio Code didn’t find the source file, so I thought it would be a dependency problem. But it compiles without errors and works now! Thanks!