I see lots of examples where you can use variable FROM platformio.ini into the program file (main.cpp)
But I would like to use opposite: use the variable FROM main.cpp in platformio.ini
Use case: main.cpp is common for many boards and I simply use the macro in main.cpp like this:
; platformio.ini
[my_vars]
; https://docs.platformio.org/en/latest/projectconf/interpolation.html
board_name="board1" ; or "board2"
[env]
; https://docs.platformio.org/en/latest/projectconf/section_env_build.html#build-flags
build_flags =
; this will do equivalent of `#define THIS_BOARD "board1"`
'-D THIS_BOARD="${my_vars.board_name}"'
; use ${my_vars.board_name} to set the output bin file, idk how
thank you brother
but I am not sure you understood me (or I still don’t understand you):
in my config.h file I have defined which board I want to compile, i.e.:
depending which line is uncommented, the binary is for this board
and now, depending also on this uncommented line, I would like my binary file to be renamed accordingly
today, every binary file has the same name, so I need to manually rename it to (or to move to another folder) to make sure that the next compiled file does not overwrite the previous one
If I can make that after compilation for DEVICE_ID=1 my binary is “binary1.bin” then my life would be easier
So I don’t want to define which binary I am compiling in platformio.ini but in my config.h as there are many things depending on the DEVICE_ID (i.e. MQTT sensor name etc.)
I did. I don’t know if you can configure pio from code. But you definitely can configure code from pio.
So, instead of defining your board/device_id/whatever in CODE, do that in CONFIG.
Get rid of config.h and define those constants in platformio.ini. If you need those values in the code, pass them via -D build option.
For that I would make an env for each config: [env:device26], [env:device27] or [env:garage], [env:printer_room], etc.
This way each env gets its own build, and your binaries don’t get overwritten. On the minus side you need to have like 20 builds in your case, but who cares in 21. century with gigahertzs of processors and terrabites of disks
thank you @dvdnwk
I shall probably rethink this way as you say said - at the end I don’t care where it gets “scripted” as long as I don’t need to do too much of manual job
yes, I need separate binaries as each board (so MCU) uses its own config that I am not sure can be common i.e. different GPIOs, even different usage (i.e. some boards are temperature/humidity only while others are also motion sensors and/or light) so based on the DEVICE_ID I have further code in use - not everything is common amongst the boards
then going forward I could have binaries posted also to the final folders or even ftp so that sensors could upgrade firmware automatically from there
But I really thought I could send variable from CODE to PIO
one super good outcome of your proposal would be: no more manual commenting out the #define section - all have its own [env:xxx] so 1 time build and voila - all are compiled at once
maybe your solution is even simpler
thank you