Better Example Code Support


I am trying to work on a relatively complex personal project. I switched from the Arduino IDE because I wanted to easily modify and share libraries within a project. PlatformIO works great in that regard, but I miss the “examples” feature of the Arduino IDE.

It is a relatively simple concept: add an “examples” folder to your project directory that contains many code snippets that are set up like a “main” file. When one is selected, it can be built and uploaded as if it is the project’s “main” file.

It is super convenient for demonstrating the functionality and intended use cases of code in your project, but PlatformIO does not seem to have an easy way to upload example code.

I understand that one could copy and paste the code into the main file, but that is a nightmare for sharing and version control.

I have found some old threads that discuss this, but it is unresolved as far as I am aware.

Easily building code from examples dir - General Discussion - PlatformIO Community
Common sample code, like the Arduino IDE? - PlatformIO IDE - PlatformIO Community

Conceptually, this does not seem super hard to implement. Though I am woefully incompetent when it comes to this sort of thing, could we not just temporarily trick PlatformIO into thinking that “src/main.cpp” is somewhere else?? Something local, that will not affect the GitHub repository that manages the project…

I have been trying to pull some kinda trick with environments, where I set the src_filter property of an example-specific environment to remove the original main.cpp and point to a different one, depending on the example. I have not gotten this to work.

Is there something I am missing??? Some existing feature that does this??

1 Like