Skip to content

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

@lnlp

Description

@lnlp

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions