PlatformIO Community

Working on arduino libraries


#1

hello,

I am new to platformio, I need to use it for an arduino library I am working on. I was wondering which is the best way to work on an arduino project/library because the platformio structure differ a lot to the arduino library
example of an arduino lib:
├───examples
│ ├───advanced_working
│ └───basic_working
├───extras
│ └───…
└───src

Where the source code are in ‘src’ and in examples there are some .ino sketches

What do you suggest me to have a clean as possible folder structure as the arduinio one? i need it because I am pubblishing it to github and to the arduino library manager


#2

After a lot of googling i found how to procede, i will write a short guide in the case someone will have the same problem:

  1. go to the root folder (where there is the library.properties file) of the library which you want to develop and run pio init
  2. Open Project with your favourite ide
  3. in platformio.ini write:
    [platformio]
    src_dir = examples/your_sketch_name
    lib_dir = src ;yes it appear to be wrong but is right
  4. enjoy platformio!

I also opened a issue on the docs repo https://github.com/platformio/platformio-docs/issues/46 hoping @ivankravets or @valeros would see it


#3

Hi @aster94,

It does not work for me…
This is my platformio.ini file:

[platformio]
src_dir = examples/my_example
lib_dir = src

[env:xmc1100_xmc2go]
platform = infineonxmc
board = xmc1100_xmc2go
framework = arduino

Also would it be possible to have several targets for several “examples” in the platformio.ini file? To compile several examples with several platforms-boards?

Thanks!


#4

The idea is basically to do as the “platformio ci …” command for different SRC, but included already in the platformio.ini file.
That would be great for Arduino development with VSCode or Atom.

@ivankravets is there any example of such platformio.ini configuration with src/ and exapmples/ dir in the arduino lib folder structure as platformio project?

Thanks a lot in advance :slight_smile:

BR,

jaenrig-ifx


#5

It looks correct

First of all try to compile the sketch in the arduino ide just to be sure everything is all right

If it work it is possible that the problem is due to the included libraries, at this time pio has some problems with it https://github.com/platformio/platformio-core/issues/1696


#6

Thanks @aster94. There is no other library dependency in my code… and the example is working after installing the Arduino library in the Arduino IDE.

I am using visual studio code and this PlatformIO Versions:

Home 2.0.2 + Core 4.0.0a13

In the command line, using platformio ci it works perfectly

platformio ci --lib="." --board=xmc1100_xmc2go examples/my_example


#7

Okay. Now it works :slight_smile:

[platformio]
src_dir = examples/my_example
lib_dir = .

[env:xmc1100_xmc2go]
platform = infineonxmc
board = xmc1100_xmc2go
framework = arduino

The idea is now to be able to compile diffrent “examples” for each environment in the platformio.ini file…


#8

I guess your header and cpp file where in the root folder :grin:
You may want to move them to src even if not needed (https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5:-Library-specification#source-code)

About the different examples there is workaround: you can define a flag for each board and then with some #ifdef#endif you could choose which part of the code to upload to which target


#9

Actually I have the lib in src:
src/
|_ class.h
|_ class.cpp
|_ dir1/

I guess I preferably use ci for compiling multiple src and environments. During development it is enough to be able to change the src example folder.

Thanks for your support!


#10

There are a lot of ways how to organize examples and test them with PlatformIO.
Yes, the easiest way is to use http://docs.platformio.org/en/latest/userguide/cmd_ci.html

Also, other option is to place platformio.ini into the each arduino example folder and override src_dir as

[platformio]
src_dir = .

[env:uno]
....
board = uno
lib_deps = ../../relative path to lib

Later, you can process these examples with pio run command.


#11

That looks good as well :slight_smile:

Thanks for the hint!