PlatformIO Community

Here is a simple way to have many small "main" apps

Here is a very simple way to reuse code placed in lib/ from different single-file main apps. My file tree looks as follows:

$ tree
.
├── lib
│   └── ...
├── platformio.ini
└── src
    ├── board.h
    ├── main-blink.cpp
    ├── main-getiq.cpp
    ├── main-lcd.cpp
    ├── main-psram.cpp
    ├── main-qspi.cpp
    ├── main-wifi.cpp
    └── memtest.h

And the platformio.ini has something like this:

[env]
platform = ststm32
framework = cmsis
board = disco_f723ie
src_filter = +<*.h> +<main-${PIOENV}.cpp>
...

[env:blink]
[env:getiq]
[env:lcd]
[env:psram]
[env:qspi]
[env:wifi]

Note that the [env:xyz] sections can be left empty.

It’s by far the simplest way I’ve found so far to keep a collection of little test & demo apps in a single place, all compiled in the same way and using the same library code. A quick check that all the apps compile is a matter of typing pio run.

To build and upload say the blink app, type: pio run -t upload -e blink - easy!

(and if you define alias prue='pio run -t upload -e', then this becomes prue blink)

6 Likes

Nicely done!

Cheers,
Norm.

Hi there!

Where are you typing the commands at in VSCode? For me, typing pio into the terminal, set to a PlatformIO CLI, I get the standard list of commands help menu printed. Typing in pio env results in a Error: No such command "env"

Looking for a way to mimic Arduino library folder, allowing me to work on code in ‘examples’ folder while still being able to work on the library itself.

Thanks,
Randy

pio env -t upload -e blink - easy!

I suspect this should be pio run -t upload -e blink

The “env” is supplied by the -e option.

HTH

Cheers,
Norm.

Awww … good catch, fixed, thx.

1 Like

Thanks, that’s great! Just to check, should the source filter include lib, i.e. +<*.h> +<lib/*> +<main-${PIOENV}.cpp>

No, src_filter is for files in src/, the include/ and lib/ areas are always used.

Lovely, thanks! Last question - is there a way to use other variables in +<main-${PIOENV}.cpp>? Could I set e.g. SRCNAME=default in the default env, use +<main-${SRCNAME}.cpp> in the src_filter, and then have some environments that default to main-default.cpp, but allow others to set a new value, e.g.

[env]
src_filter = +<*.h> +<main-${SRCNAME}.cpp>
SRCNAME="default"

[env:usesdefault]
[env:special]
SRCNAME="special"

See https://docs.platformio.org/en/latest/projectconf/index.html
There’s no single answer, best option is to just try it and see what works best for you.