Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature-request: please add dynamic variables for current section and current environment #3953

Closed
lnlp opened this issue May 12, 2021 · 1 comment
Assignees
Labels
config platformio.ini feature
Milestone

Comments

@lnlp
Copy link

lnlp commented May 12, 2021

Feature Request

Dynamic variables

Dynamic variables (interpolations) are useful when you have a custom configuration data between build environments. For examples, extra build_flags or project dependencies lib_deps.

Each variable should have a next format: ${<section>.<option>}, where <section> is a value from [<section>] group, and <option> is a first item from pair <option> = value.

What currently lacks and what I would like to see added is the following:

  1. A variable for the current section (name) e.g. ${this} where ${this} returns the name of the current section and ${this.option} returns the value of option in the current section.
  2. A variable for the current environment name in an environment section where within section [env:name] ${this.env} returns name.

The names of these vaiables are given as example but could be implemented/named differently. Essential is that they are available to the user as dynamic variables.

Having these variables available allows to create more powerful and more flexible platformio.ini files where actual values only have to be specified once ('single point of definition') without having to use more complex (and for many applications unnecessary) custom build scripts.

Note: Below example only demonstrates possible use of the suggested variables. It does not necessarily demonstrate the best use of these variables or when they are aboslutely needed. Latter would require a much more advanced/complex real life example to demonstrate the importance of the suggested features but that would only make this request more difficult to comprehend.

; platformio.ini

[platformio]
default_envs =
    ; myESP32
    myESP8266
    ; mySTM32

[common]
monitor_speed = 115200
build_flags =
    -D COMMON_SECTION_NAME = \"${this}\"

[env:myESP32]
monitor_speed = ${common.monitor_speed}
build_flags =
    ${common.build_flags}
    -D MONITOR_SPEED=${common.monitor_speed}
    -D BOARD_ID=\"${this.env}\"

[env:myESP8266]
monitor_speed = 74880
build_flags =
    ${common.build_flags}
    -D MONITOR_SPEED=${this.monitor_speed}
    -D BOARD_ID=\"${this.env}\"

[env:mySTM32]
monitor_speed = ${common.monitor_speed}
build_flags =
    ${common.build_flags}
    -D MONITOR_SPEED=${common.monitor_speed}
    -D BOARD_ID=\"${this.env}\"

Example flag values:

COMMON_SECTION_NAME  "common"
MONITOR_SPEED        74880
BOARD_ID             "myESP8266"

For a more real life example see platformio.ini of the LMIC-node application.

Below code is an extract from that platformio.ini.

I prefer to be able to write code like: -D BSFILE=\"boards/bsf_${this.env}.h\"
instead of: -D BSFILE=\"boards/bsf_disco_l072cz_lrwan1.h\"
Which prevents having to hard code environement strings and is easier to re-use and maintain.

[env:disco_l072cz_lrwan1]
; ST B-L072Z-LRWAN1 Discovery kit (STM32L072CZ)
; No display
platform = ststm32
board = disco_l072cz_lrwan1
framework = arduino
upload_protocol = stlink  
monitor_speed = ${common.monitor_speed}
lib_deps =
    ${common.lib_deps}    
    ${mcci_lmic.lib_deps}
build_flags =
    ${common.build_flags}
    ${mcci_lmic.build_flags} 
    -D BSFILE=\"boards/bsf_disco_l072cz_lrwan1.h\"
    -D MONITOR_SPEED=${common.monitor_speed}
    -D USE_SERIAL
    -D USE_LED
    ; -D USE_DISPLAY             ; Requires external I2C OLED display
@lnlp lnlp changed the title Add dynamic variables for current section and current environment Please add dynamic variables for current section and current environment Jun 4, 2021
@lnlp lnlp changed the title Please add dynamic variables for current section and current environment Feature-request: please add dynamic variables for current section and current environment Jun 4, 2021
@ivankravets ivankravets added this to the 5.3.0 milestone Sep 13, 2021
@ivankravets ivankravets added config platformio.ini feature labels Sep 13, 2021
@ivankravets ivankravets modified the milestones: 5.4, Backlog, 5.3.0, 5.2.5 Feb 9, 2022
@ivankravets ivankravets self-assigned this Apr 9, 2022
@ivankravets
Copy link
Member

Thanks for the idea! Please re-test with pio upgrade --dev. See updated docs https://docs.platformio.org/en/latest/projectconf/interpolation.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
config platformio.ini feature
Projects
None yet
Development

No branches or pull requests

2 participants